Exemplo n.º 1
0
        public SchoolsViewModel(IEdsWebApiService edsWebApiService)
        {
            _edsWebApiService = edsWebApiService;

            _edsWebApiService.SchoolsConnect()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Transform(x => new SchoolViewModel(x))
            .Bind(out _schools)
            .Subscribe();

            this.WhenAnyValue(x => x.SelectedSchool)
            .Where(x => x != null)
            .Do(x =>
            {
                this.SelectedSchoolId       = x.Id;
                this.SelectedSchoolName     = x.Name;
                this.SelectedSchoolLocation = x.Location;
                this.IsEditMode             = false;
            })
            .Subscribe();

            _addSchoolEnabled = this.WhenAnyValue(
                x => x._edsWebApiService.IsLoggedIn,
                x => x.SelectedSchoolId,
                (isLoggedIn, selectedSchoolId) => isLoggedIn && (selectedSchoolId != 0))
                                .ToProperty(this, x => x.AddSchoolEnabled);

            AddSchoolCommand = ReactiveCommand.Create(
                DoAddSchool, this.WhenAnyValue(x => x.AddSchoolEnabled));

            AddSchoolCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));

            _editSchoolEnabled = this.WhenAnyValue(
                x => x.SelectedSchool, x => x.IsEditMode,
                (schoolViewModel, isEditMode) => (schoolViewModel != null) && !isEditMode)
                                 .ToProperty(this, x => x.EditSchoolEnabled);

            EditSchoolCommand = ReactiveCommand.Create(
                DoEditSchool, this.WhenAnyValue(x => x.EditSchoolEnabled));

            EditSchoolCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));

            _saveSchoolEnabled = this.WhenAnyValue(
                x => x.SelectedSchoolName,
                x => x.SelectedSchoolLocation,
                x => x.IsEditMode,
                (name, location, isEditMode) =>
                !string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(location) && isEditMode)
                                 .ToProperty(this, x => x.SaveSchoolEnabled);

            SaveSchoolCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoSaveSchool();
            }, this.WhenAnyValue(x => x.SaveSchoolEnabled));

            SaveSchoolCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));

            _deleteSchoolEnabled = this.WhenAnyValue(
                x => x.SelectedSchoolId,
                x => x._edsWebApiService.IsAdmin,
                (id, isAdmin) =>
                (id > 0) && isAdmin)
                                   .ToProperty(this, x => x.DeleteSchoolEnabled);

            DeleteSchoolCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoDeleteSchool();
            }, this.WhenAnyValue(x => x.DeleteSchoolEnabled));

            DeleteSchoolCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));
        }
Exemplo n.º 2
0
        public StudentsViewModel(IEdsWebApiService edsWebApiService)
        {
            _edsWebApiService = edsWebApiService;

            _edsWebApiService.StudentsConnect()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Transform(x => new StudentViewModel(x))
            .Bind(out _students)
            .Subscribe();

            _edsWebApiService.SchoolsConnect()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Transform(x => new SchoolViewModel(x))
            .Bind(out _schools)
            .Subscribe();

            this.WhenAnyValue(x => x.SelectedStudent)
            .Where(x => x != null)
            .Do(x =>
            {
                this.SelectedStudentId                     = x.Id;
                this.SelectedStudentName                   = x.Name;
                this.SelectedStudentGender                 = x.Gender;
                this.SelectedStudentLocation               = x.Location;
                this.SelectedStudentBirthday               = x.Birthday.GetValueOrDefault(DateTime.Today);
                this.SelectedStudentEnrollmentDate         = x.EnrollmentDate.GetValueOrDefault(DateTime.Today);
                this.SelectedStudentEnrollmentClass        = x.EnrollmentClass;
                this.SelectedStudentEnrollmentAcademicYear = x.EnrollmentAcademicYear;
                this.SelectedStudentEnrollmentReason       = x.EnrollmentReason;
                this.SelectedStudentConfirmedDate          = x.ConfirmedDate.GetValueOrDefault(SelectedStudentEnrollmentDate);
                this.SelectedStudentConfirmedClass         = x.ConfirmedClass;
                this.SelectedStudentConfirmedAcademicYear  = x.ConfirmedAcademicYear;
                this.SelectedStudentSponsorshipStatus      = x.SponsorshipStatus;

                foreach (var schoolViewModel in _schools)
                {
                    if (schoolViewModel.Id != x.SchoolId)
                    {
                        continue;
                    }
                    this.SelectedStudentSchoolViewModel = schoolViewModel;
                    break;
                }

                this.IsEditMode = false;
            })
            .Subscribe();

            _addStudentEnabled = this.WhenAnyValue(
                x => x._edsWebApiService.IsLoggedIn,
                x => x.SelectedStudentId,
                (isLoggedIn, selectedStudentId) => isLoggedIn && (selectedStudentId != 0))
                                 .ToProperty(this, x => x.AddStudentEnabled);

            AddStudentCommand = ReactiveCommand.Create(
                DoAddStudent, this.WhenAnyValue(x => x.AddStudentEnabled));

            AddStudentCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));

            _editStudentEnabled = this.WhenAnyValue(
                x => x.SelectedStudent,
                x => x.IsEditMode,
                (studentViewModel, isEditMode) => (studentViewModel != null) && !isEditMode)
                                  .ToProperty(this, x => x.EditStudentEnabled);

            EditStudentCommand = ReactiveCommand.Create(
                DoEditStudent, this.WhenAnyValue(x => x.EditStudentEnabled));

            EditStudentCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));

            _saveStudentEnabled = this.WhenAnyValue(
                x => x.SelectedStudentName,
                x => x.SelectedStudentLocation,
                x => x.IsEditMode,
                (name, location, isEditMode) =>
                !string.IsNullOrEmpty(name) &&
                !string.IsNullOrEmpty(location) &&
                isEditMode)
                                  .ToProperty(this, x => x.SaveStudentEnabled);

            SaveStudentCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoSaveStudent();
            }, this.WhenAnyValue(x => x.SaveStudentEnabled));

            SaveStudentCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));

            _deleteStudentEnabled = this.WhenAnyValue(
                x => x.SelectedStudentId,
                x => x._edsWebApiService.IsAdmin,
                (id, isAdmin) =>
                (id > 0) && isAdmin)
                                    .ToProperty(this, x => x.DeleteStudentEnabled);

            DeleteStudentCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoDeleteStudent();
            }, this.WhenAnyValue(x => x.DeleteStudentEnabled));

            DeleteStudentCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));


            SelectPhotoCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                var photoPath = await this.selectPhoto.Handle(string.Empty);

                if (string.IsNullOrEmpty(photoPath) || !File.Exists(photoPath))
                {
                    SelectedStudentPhoto = null;
                    return;
                }

                SelectedStudentPhoto = new BitmapImage(new Uri(photoPath));

                var base64String = Base64ImageEncoderDecoder.ToBase64(SelectedStudentPhoto);
                var photo        = await _edsWebApiService.GetPhoto(SelectedStudentId);

                if (photo == null)
                {
                    photo = new Photo(0, SelectedStudentId, base64String);
                    await _edsWebApiService.AddPhoto(photo);
                }
                else
                {
                    photo.Image = base64String;
                    await _edsWebApiService.UpdatePhoto(photo.Id.GetValueOrDefault(0), photo);
                }
            }, this.WhenAnyValue(x => x.EditStudentEnabled)
                );
            SelectPhotoCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));

            LoadPhotoCommand = ReactiveCommand.CreateFromTask <int>(
                async(int studentId) =>
            {
                var photo = await _edsWebApiService.GetPhoto(studentId);

                if (photo == null)
                {
                    SelectedStudentPhoto = null;
                    return;
                }

                this.SelectedStudentPhoto = Base64ImageEncoderDecoder.FromBase64(photo.Image);
            }, this.WhenAnyValue(x => x.EditStudentEnabled)
                );

            this.WhenAnyValue(x => x.SelectedStudent)
            .Where(x => x != null)
            .Select(x => x.Id)
            .InvokeCommand(LoadPhotoCommand);

            var listAcademicYearStrings = new List <string>();

            for (var year = 2020; year < 2050; year++)
            {
                listAcademicYearStrings.Add($"{year}/{year + 1}");
            }

            AcademicYearOptions = listAcademicYearStrings;

            var listSchoolFormStrings = new List <string>();

            for (var schoolClass = 1; schoolClass <= 12; schoolClass++)
            {
                listSchoolFormStrings.Add(SchoolClassConverter.ToSchoolForm(schoolClass));
            }

            SchoolClassOptions = listSchoolFormStrings;

            this.selectPhoto = new Interaction <string, string>();
        }