Exemplo n.º 1
0
        public LoginViewModel(IAppSettingsService appSettingsService, IEdsWebApiService edsWebApiService)
        {
            _appSettingsService = appSettingsService;
            _edsWebApiService   = edsWebApiService;

            Email          = _appSettingsService.GetSettings().Email;
            StorePassword  = !string.IsNullOrEmpty(_appSettingsService.GetSettings().ProtectedPassword);
            SecurePassword =
                PasswordEncryptionService.UnProtectToSecureString(_appSettingsService.GetSettings().ProtectedPassword);

            _loginEnabled = this.WhenAnyValue(
                x => x.Email,
                x => x.SecurePassword,
                x => x.IsLoggedIn,
                (email, securePassword, isLoggedIn) =>
                !isLoggedIn &&
                !string.IsNullOrEmpty(email) &&
                (securePassword.Length != 0)).ToProperty(this, x => x.LoginEnabled);

            LoginCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoLogin();
            }, this.WhenAnyValue(x => x.LoginEnabled));

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

            _edsWebApiService.UsersConnect()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Transform(x => new UserViewModel(x))
            .Bind(out _users)
            .Subscribe();

            this.WhenAnyValue(x => x.SelectedUser)
            .Where(x => x != null)
            .Do(x =>
            {
                this.SelectedUserId       = x.Id;
                this.SelectedUserName     = x.Name;
                this.SelectedUserRole     = x.Role;
                this.SelectedUserEmail    = x.Email;
                this.SelectedUserPassword = x.Password;
                this.IsEditMode           = false;
            })
            .Subscribe();

            _addUserEnabled = this.WhenAnyValue(
                x => x._edsWebApiService.IsLoggedIn,
                x => x.SelectedUserId,
                x => x._edsWebApiService.IsAdmin,
                (isLoggedIn, selectedUserId, isAdmin) => isLoggedIn && isAdmin && (selectedUserId != 0))
                              .ToProperty(this, x => x.AddUserEnabled);

            AddUserCommand = ReactiveCommand.Create(
                DoAddUser, this.WhenAnyValue(x => x.AddUserEnabled));

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

            _editUserEnabled = this.WhenAnyValue(
                x => x.SelectedUser,
                x => x.IsEditMode,
                x => x._edsWebApiService.IsAdmin,
                (userViewModel, isEditMode, isAdmin) => (userViewModel != null) && !isEditMode && isAdmin)
                               .ToProperty(this, x => x.EditUserEnabled);

            EditUserCommand = ReactiveCommand.Create(
                DoEditUser, this.WhenAnyValue(x => x.EditUserEnabled));

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

            _saveUserEnabled = this.WhenAnyValue(
                x => x.SelectedUserName,
                x => x.SelectedUserEmail,
                x => x.SelectedUserRole,
                x => x.SelectedUserPassword,
                x => x.IsEditMode,
                x => x._edsWebApiService.IsAdmin,
                (name, email, role, password, isEditMode, isAdmin) =>
                !string.IsNullOrEmpty(name) &&
                !string.IsNullOrEmpty(email) &&
                !string.IsNullOrEmpty(role) &&
                !string.IsNullOrEmpty(password) &&
                isEditMode &&
                isAdmin)
                               .ToProperty(this, x => x.SaveUserEnabled);

            SaveUserCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoSaveUser();
            }, this.WhenAnyValue(x => x.SaveUserEnabled));

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

            _deleteUserEnabled = this.WhenAnyValue(
                x => x.SelectedUserId,
                x => x._edsWebApiService.IsAdmin,
                (id, isAdmin) =>
                (id > 0) && isAdmin)
                                 .ToProperty(this, x => x.DeleteUserEnabled);

            DeleteUserCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoDeleteUser();
            }, this.WhenAnyValue(x => x.DeleteUserEnabled));

            DeleteUserCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));
        }
Exemplo n.º 3
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.º 4
0
 public MainWindowViewModel(IEdsWebApiService edsWebApiService)
 {
     _edsWebApiService = edsWebApiService;
 }
Exemplo n.º 5
0
        public SponsorsViewModel(IEdsWebApiService edsWebApiService)
        {
            _edsWebApiService = edsWebApiService;

            _edsWebApiService.SponsorsConnect()
            .ObserveOn(RxApp.MainThreadScheduler)
            .Transform(x => new SponsorViewModel(x))
            .Bind(out _sponsors)
            .Subscribe();

            this.WhenAnyValue(x => x.SelectedSponsor)
            .Where(x => x != null)
            .Do(x =>
            {
                this.SelectedSponsorId         = x.Id;
                this.SelectedSponsorName       = x.Name;
                this.SelectedSponsorSalutation = x.Salutation;
                this.SelectedSponsorStreet     = x.Street;
                this.SelectedSponsorCity       = x.City;
                this.SelectedSponsorZip        = x.Zip;
                this.SelectedSponsorEmail      = x.Email;
                this.IsEditMode = false;
            })
            .Subscribe();

            _addSponsorEnabled = this.WhenAnyValue(
                x => x._edsWebApiService.IsLoggedIn,
                x => x.SelectedSponsorId,
                (isLoggedIn, selectedSponsorId) => isLoggedIn && (selectedSponsorId != 0))
                                 .ToProperty(this, x => x.AddSponsorEnabled);

            AddSponsorCommand = ReactiveCommand.Create(
                DoAddSponsor, this.WhenAnyValue(x => x.AddSponsorEnabled));

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

            _editSponsorEnabled = this.WhenAnyValue(
                x => x.SelectedSponsor, x => x.IsEditMode,
                (sponsorViewModel, isEditMode) => (sponsorViewModel != null) && !isEditMode)
                                  .ToProperty(this, x => x.EditSponsorEnabled);

            EditSponsorCommand = ReactiveCommand.Create(
                DoEditSponsor, this.WhenAnyValue(x => x.EditSponsorEnabled));

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

            _saveSponsorEnabled = this.WhenAnyValue(
                x => x.SelectedSponsorName,
                x => x.IsEditMode,
                (name, isEditMode) =>
                !string.IsNullOrEmpty(name) && isEditMode)
                                  .ToProperty(this, x => x.SaveSponsorEnabled);

            SaveSponsorCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoSaveSponsor();
            }, this.WhenAnyValue(x => x.SaveSponsorEnabled));

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

            _deleteSponsorEnabled = this.WhenAnyValue(
                x => x.SelectedSponsorId,
                x => x._edsWebApiService.IsAdmin,
                (id, isAdmin) =>
                (id > 0) && isAdmin)
                                    .ToProperty(this, x => x.DeleteSponsorEnabled);

            DeleteSponsorCommand = ReactiveCommand.CreateFromTask(
                async() =>
            {
                await DoDeleteSponsor();
            }, this.WhenAnyValue(x => x.DeleteSponsorEnabled));

            DeleteSponsorCommand.ThrownExceptions.Subscribe(ex => MessageBus.Current.SendMessage(ex));
        }
Exemplo n.º 6
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>();
        }