public StudentFormModel(
            StudentFormToken token,
            PhotoService photoService,
            StudentCardService studentCardService,
            LocalDbContext context
            )
        {
            _token        = token;
            _photoService = photoService;
            _context      = context;
            this.ChosenGroupTableConfig = new TableConfig {
                Filter       = (o, s) => ((ChoseGroupModel)o).Group.Name.ToUpperInvariant().Contains(s),
                Sorts        = ChosenGroupSorts,
                ColumnWidths = new[] {
                    new GridLength(40),
                    new GridLength(1, GridUnitType.Star),
                }
            };
            this.AvailableGroupTableConfig = new TableConfig {
                Filter       = (o, s) => ((GroupEntity)o).Name.ToUpperInvariant().Contains(s),
                Sorts        = AvailableGroupSorts,
                ColumnWidths = new[] {
                    new GridLength(1, GridUnitType.Star),
                }
            };
            this.SelectGroupsHandler      = ReactiveCommand.Create(SelectGroups);
            this.DeselectGroupsHandler    = ReactiveCommand.Create(DeselectGroups);
            this.SelectStudentCardHandler = ReactiveCommand.Create(ReadCard);
            this.ValidationRule(model => model.CardUid,
                                s => { return(s == null || s.Equals(string.Empty) || StudentEntity.IsCardUidValid(s)); },
                                s => Localization["Неверный Uid"]);
            this.ValidationRule(model => model.FirstName, s => !string.IsNullOrWhiteSpace(s),
                                Localization["Имя не может быть пустым"]);
            this.ValidationRule(model => model.LastName, s => !string.IsNullOrWhiteSpace(s),
                                Localization["Имя не может быть пустым"]);
            this.SaveHandler = ReactiveCommand.Create(Save);
            this.WhenActivated(disposable => {
                this.IsValid().Subscribe(b => this.IsValid = b);
                this.WhenAnyValue(model => model.Student)
                .Where(LambdaHelper.NotNull)
                .Subscribe(entity => {
                    this.FirstName   = entity.FirstName;
                    this.SecondName  = entity.SecondName;
                    this.LastName    = entity.LastName;
                    this.CardUid     = entity.CardUid;
                    this.Email       = entity.Email;
                    this.PhoneNumber = entity.PhoneNumber;
                })
                .DisposeWith(disposable);
                studentCardService.ReadStudentCards.ToObservableChangeSet()
                .ToCollection()
                .Subscribe(onNext: items => this.ReadStudents = items.ToList())
                .DisposeWith(disposable);
                this.WhenAnyValue(model => model.FirstName).Subscribe(s => this.Student.FirstName = s)
                .DisposeWith(disposable);
                this.WhenAnyValue(model => model.LastName).Subscribe(s => this.Student.LastName = s)
                .DisposeWith(disposable);
                this.WhenAnyValue(model => model.SecondName).Subscribe(s => this.Student.SecondName = s)
                .DisposeWith(disposable);
                this.WhenAnyValue(model => model.CardUid).Subscribe(s => {
                    var isCardUidValid = StudentEntity.IsCardUidValid(s);
                    if (!isCardUidValid)
                    {
                        this.IsShowPhoto = false;
                    }
                    else
                    {
                        ShowPhotoAsync(s);
                        this.IsShowPhoto = true;
                    }

                    this.Student.CardUid = s;
                })
                .DisposeWith(disposable);
                this.WhenAnyValue(model => model.PhoneNumber).Subscribe(s => this.Student.PhoneNumber = s)
                .DisposeWith(disposable);
                this.WhenAnyValue(model => model.Email).Subscribe(s => this.Student.Email = s)
                .DisposeWith(disposable);
            });
            Initialize(token.Student);
        }
Esempio n. 2
0
        public RegistrationPageModel(
            StudentCardService studentCardService,
            PhotoService photoService,
            TabPageHost tabPageHost,
            WindowPageHost windowPageHost,
            LocalDbContext db,
            RegistrationPageToken token,
            MainReducer mainReducer,
            PageControllerReducer reducer
            )
        {
            _tabPageHost            = tabPageHost;
            _windowPageHost         = windowPageHost;
            _db                     = db;
            _token                  = token;
            _mainReducer            = mainReducer;
            this.StudentCardService = studentCardService;
            this.PhotoService       = photoService;
            this.DoRegister         = ReactiveCommand.Create(() => {
                if (this.AllStudentsMode)
                {
                    foreach (IStudentViewModel selectedStudent in this.SelectedStudents.ToList())
                    {
                        RegisterExtStudent(selectedStudent.Student);
                    }
                }
                else
                {
                    Register();
                }
            });
            this.DoUnRegister             = ReactiveCommand.Create(UnRegister);
            this.OpenStudentLessonHandler = ReactiveCommand.Create(() =>
                                                                   OpenLesson(this.SelectedStudentLessonNote?.Note?.StudentLesson?.Lesson));

            this.ShowStudent = ReactiveCommand.Create(() => {
                var selectedStudent      = _selectedStudent;
                var studentViewPageToken = new StudentViewPageToken("Студент", selectedStudent);
                tabPageHost.AddPageAsync <StudentViewPageModule, StudentViewPageToken>(studentViewPageToken);
            });
            this.AddStudentNote = ReactiveCommand.Create(() => {
                var noteFormToken = new NoteListFormToken("Заметки", () => new StudentLessonNote()
                {
                    StudentLesson = _selectedStudentLesson.StudentLesson,
                    EntityId      = _selectedStudentLesson.StudentLesson.Id
                }, _selectedStudentLesson.StudentLesson.Notes);
                windowPageHost.AddPageAsync <NoteListFormModule, NoteListFormToken>(noteFormToken);
            });
            this.ToggleAllStudentTable = new ButtonConfig {
                Command = ReactiveCommand.Create(() => this.AllStudentsMode = !this.AllStudentsMode),
                Text    = Localization["Все студенты"]
            };
            this.AllStudentsFilter = (o, s) => {
                var student      = ((IStudentViewModel)o).Student;
                var alreadyAdded =
                    IsStudentAlreadyRegistered(RegisteredStudents.Cast <StudentLessonInfoViewModel>(), student) ||
                    IsStudentAlreadyRegistered(LessonStudents.Cast <StudentLessonInfoViewModel>(), student);
                if (alreadyAdded)
                {
                    return(false);
                }
                s = s.ToLowerInvariant();
                return(student.FirstName != null &&
                       student.FirstName.ToLowerInvariant()
                       .Contains(s) ||
                       student.LastName != null &&
                       student.LastName.ToLowerInvariant()
                       .Contains(s) ||
                       student.SecondName != null &&
                       student.SecondName.ToLowerInvariant()
                       .Contains(s));
            };

            InitTableConfigs();
            Init(token.Lesson);
            this.WhenActivated(disposable => {
                this.WhenAnyValue(model => model.TimerState)
                .Where(LambdaHelper.NotNull)
                .ObserveOnDispatcher(DispatcherPriority.Background)
                .Subscribe(state => {
                    this.TimerString = $"{state.TimeLeft:hh\\:mm\\:ss}/{state.CurrentTime:HH:mm:ss}";
                }).DisposeWith(disposable);
                this.WhenAnyValue(model => model.Lesson)
                .Where(LambdaHelper.NotNull)
                .ObserveOnDispatcher(DispatcherPriority.Background)
                .Subscribe(entity => {
                    var groupName  = entity.Group?.Name ?? entity.Stream.Name;
                    var lessonInfo =
                        $"{Localization["common.lesson.type." + entity.LessonType]}: {entity.Order}/{entity.GetLessonsCount()}";
                    this.LessonInfoState = new LessonInfoState {
                        GroupName  = groupName,
                        LessonInfo = lessonInfo,
                        Date       = entity.Date?.ToString("dd.MM.yyyy"),
                        Time       =
                            $"[{entity.Schedule.OrderNumber}] {entity.Schedule.Begin:hh\\:mm} - {entity.Schedule.End:hh\\:mm}"
                    };
                }).DisposeWith(disposable);
                this.WhenAnyValue(model => model.AllStudentsMode)
                .Throttle(TimeSpan.FromMilliseconds(100))
                .ObserveOnDispatcher(DispatcherPriority.Background)
                .Subscribe(b => {
                    this.ToggleAllStudentTable.Text = b ? Localization["Занятие"] : Localization["Все студенты"];
                    if (!b || this.AllStudents.Count != 0)
                    {
                        return;
                    }
                    var studentViewModels = db.Students
                                            .Include(model => model.Groups)
                                            .ToList() // create query and load
                                            .Select(model => new StudentViewModel(model))
                                            .ToList();
                    this.AllStudents.AddRange(studentViewModels);
                }).DisposeWith(disposable);

                this.WhenAnyValue(model => model.IsLessonChecked)
                .ObserveOnDispatcher(DispatcherPriority.Background)
                .Subscribe(b => {
                    if (this.Lesson == null)
                    {
                        return;
                    }

                    this.Lesson.Checked = b;
                    this._db.ThrottleSave();
                }).DisposeWith(disposable);
                this.LessonStudentsTableConfig.SelectedItem
                .Where(LambdaHelper.NotNull)
                .Merge(this.RegisteredStudentsTableConfig.SelectedItem.AsObservable().Where(LambdaHelper.NotNull))
                .Cast <StudentLessonInfoViewModel>()
                .Do(o => this._selectedStudentLesson = o)
                .Select(o => o.StudentLesson.Student)
                .Merge(this.AllStudentsTableConfig.SelectedItem.Where(LambdaHelper.NotNull))
                .Throttle(TimeSpan.FromMilliseconds(200))
                .Subscribe
                (
                    async o => {
                    var studentEntity = o as StudentEntity;
                    _selectedStudent  = studentEntity;
                    await UpdateDescription(studentEntity);
                    await UpdateStudentLessonNotes(studentEntity);
                }
                ).DisposeWith(disposable);

                this.WhenRemoved <LessonEntity>()
                .Where(entities => entities.Any(entity => entity.Id == this.Lesson?.Id))
                .Subscribe(_ => token.Deactivate())
                .DisposeWith(disposable);
                this.WhenRemoved <StudentLessonNote>()
                .Merge(this.WhenAdded <StudentLessonNote>())
                .Merge(this.WhenUpdated <StudentLessonNote>())
                .Where(notes => this._selectedStudent != null &&
                       notes.Any(note => note.StudentLesson._StudentId == this._selectedStudent.Id))
                .ObserveOnDispatcher(DispatcherPriority.Background)
                .Subscribe(_ => UpdateStudentLessonNotes(this._selectedStudent));
                this.StudentCardService.ReadStudentCard.Subscribe(ReadStudentData);
            });
            GetControls()
            .ToObservable()
            .TakeUntil(DestroySubject)
            .Subscribe(controls =>
            {
                reducer.Dispatch(new RegisterControlsAction(token, controls));
            });
        }