public LeaveFormRecordPageViewModel(INavigationService navigationService, IPageDialogService dialogService,
                                            LeaveFormService leaveFormService, LeaveCategoryService leaveCategoryService,
                                            MyUserService myUserService,
                                            RefreshTokenService refreshTokenService,
                                            SystemStatusService systemStatusService, AppStatus appStatus)
        {
            this.navigationService    = navigationService;
            this.dialogService        = dialogService;
            this.leaveFormService     = leaveFormService;
            this.leaveCategoryService = leaveCategoryService;
            this.myUserService        = myUserService;
            this.refreshTokenService  = refreshTokenService;
            this.systemStatusService  = systemStatusService;
            this.appStatus            = appStatus;

            #region 新增 儲存 按鈕命令
            SaveCommand = new DelegateCommand(async() =>
            {
                #region 進行資料完整性檢查
                SelectedItem.CombineDate();
                var checkResult = SelectedItem.Validation();
                if (!string.IsNullOrEmpty(checkResult))
                {
                    await dialogService.DisplayAlertAsync("錯誤", $"請檢查並且修正錯誤{Environment.NewLine}{Environment.NewLine}" +
                                                          $"{checkResult}", "確定");
                    return;
                }
                #endregion

                #region 進行記錄儲存
                APIResult apiResult = new APIResult();
                using (IProgressDialog fooIProgressDialog = UserDialogs.Instance.Loading($"請稍後,儲存資料中...",
                                                                                         null, null, true, MaskType.Black))
                {
                    await AppStatusHelper.ReadAndUpdateAppStatus(systemStatusService, appStatus);
                    #region 檢查 Access Token 是否還可以使用
                    bool refreshTokenResult = await RefreshTokenHelper
                                              .CheckAndRefreshToken(dialogService, refreshTokenService,
                                                                    systemStatusService, appStatus);
                    if (refreshTokenResult == false)
                    {
                        return;
                    }
                    #endregion

                    if (CrudAction == MagicStringHelper.CrudAddAction)
                    {
                        #region 新增 請假單
                        fooIProgressDialog.Title = "請稍後,新增 請假單";
                        SelectedItem.Id          = 0;
                        apiResult = await leaveFormService.PostAsync(SelectedItem);
                        if (apiResult.Status == true)
                        {
                            ToastHelper.ShowToast($"請假單 已經新增");

                            NavigationParameters paras = new NavigationParameters();
                            paras.Add(MagicStringHelper.CrudActionName, MagicStringHelper.CrudRefreshAction);
                            await navigationService.GoBackAsync(paras);
                        }
                        else
                        {
                            await dialogService.DisplayAlertAsync("錯誤", $"請假單 儲存失敗:{apiResult.Message}", "確定");
                        }
                        #endregion
                    }
                    else
                    {
                        #region 儲存 請假單
                        fooIProgressDialog.Title = "請稍後,儲存 請假單";
                        apiResult = await leaveFormService.PutAsync(SelectedItem);
                        if (apiResult.Status == true)
                        {
                            ToastHelper.ShowToast($"請假單 已經儲存");

                            NavigationParameters paras = new NavigationParameters();
                            paras.Add(MagicStringHelper.CrudActionName, MagicStringHelper.CrudRefreshAction);
                            await navigationService.GoBackAsync(paras);
                        }
                        else
                        {
                            await dialogService.DisplayAlertAsync("錯誤", $"請假單 儲存失敗:{apiResult.Message}", "確定");
                        }
                        #endregion
                    }

                    #region 取得 請假單
                    fooIProgressDialog.Title = "請稍後,取得 請假單";
                    apiResult = await leaveFormService.GetAsync();
                    if (apiResult.Status == true)
                    {
                        await leaveFormService.WriteToFileAsync();
                    }
                    else
                    {
                        await dialogService.DisplayAlertAsync("錯誤", $"取得 請假單 失敗:{apiResult.Message}", "確定");
                    }
                    #endregion
                }
                #endregion
            });
            #endregion

            #region 刪除 按鈕命令
            DeleteCommand = new DelegateCommand(async() =>
            {
                #region 進行記錄刪除
                var confirm = await dialogService.DisplayAlertAsync(
                    "警告", "是否要刪除這筆紀錄?", "確定", "取消");
                if (confirm == false)
                {
                    return;
                }

                APIResult apiResult = new APIResult();
                using (IProgressDialog fooIProgressDialog = UserDialogs.Instance.Loading($"請稍後,刪除資料中...",
                                                                                         null, null, true, MaskType.Black))
                {
                    await AppStatusHelper.ReadAndUpdateAppStatus(systemStatusService, appStatus);
                    #region 檢查 Access Token 是否還可以使用
                    bool refreshTokenResult = await RefreshTokenHelper
                                              .CheckAndRefreshToken(dialogService, refreshTokenService,
                                                                    systemStatusService, appStatus);
                    if (refreshTokenResult == false)
                    {
                        return;
                    }
                    #endregion

                    #region 刪除 請假單
                    fooIProgressDialog.Title = "請稍後,刪除 請假單";
                    apiResult = await leaveFormService.DeleteAsync(SelectedItem);
                    if (apiResult.Status == true)
                    {
                        ToastHelper.ShowToast($"請假單 已經刪除");

                        NavigationParameters paras = new NavigationParameters();
                        paras.Add(MagicStringHelper.CrudActionName, MagicStringHelper.CrudRefreshAction);
                        await navigationService.GoBackAsync(paras);
                    }
                    else
                    {
                        await dialogService.DisplayAlertAsync("錯誤", $"請假單 刪除失敗:{apiResult.Message}", "確定");
                    }
                    #endregion

                    #region 取得 請假單
                    fooIProgressDialog.Title = "請稍後,取得 請假單";
                    apiResult = await leaveFormService.GetAsync();
                    if (apiResult.Status == true)
                    {
                        await leaveFormService.WriteToFileAsync();
                    }
                    else
                    {
                        await dialogService.DisplayAlertAsync("錯誤", $"取得 請假單 失敗:{apiResult.Message}", "確定");
                    }
                    #endregion
                }
                #endregion
            });
            #endregion
        }
Example #2
0
        public LeaveFormPageViewModel(INavigationService navigationService, IPageDialogService dialogService,
                                      LeaveFormService leaveFormService, LeaveCategoryService leaveCategoryService,
                                      MyUserService myUserService,
                                      RefreshTokenService refreshTokenService,
                                      SystemStatusService systemStatusService, AppStatus appStatus)
        {
            this.navigationService    = navigationService;
            this.dialogService        = dialogService;
            this.leaveFormService     = leaveFormService;
            this.leaveCategoryService = leaveCategoryService;
            this.myUserService        = myUserService;
            this.refreshTokenService  = refreshTokenService;
            this.systemStatusService  = systemStatusService;
            this.appStatus            = appStatus;

            #region 新增紀錄
            AddCommand = new DelegateCommand(async() =>
            {
                NavigationParameters paras = new NavigationParameters();
                var fooObject          = new LeaveFormDto();
                fooObject.FormDate     = DateTime.Now.Date;
                fooObject.BeginDate    = DateTime.Now.AddDays(1).Date + new TimeSpan(09, 00, 00);
                fooObject.CompleteDate = DateTime.Now.AddDays(1).Date + new TimeSpan(18, 00, 00);
                fooObject.Hours        = 8.0m;

                #region 設定該使用者為預設紀錄申請者
                var myUser = myUserService.Items
                             .FirstOrDefault(x => x.Id == appStatus.SystemStatus.UserID);
                if (myUser != null)
                {
                    fooObject.MyUserId   = myUser.Id;
                    fooObject.MyUserName = myUser.Name;
                }
                #endregion

                paras.Add(MagicStringHelper.CurrentSelectdItemParameterName, fooObject);
                paras.Add(MagicStringHelper.CrudActionName, MagicStringHelper.CrudAddAction);
                await navigationService.NavigateAsync("LeaveFormRecordPage", paras);
            });
            #endregion

            #region 點選某筆紀錄觸發命令
            ItemTappedCommand = new DelegateCommand(async() =>
            {
                NavigationParameters paras = new NavigationParameters();
                var fooObject = SelectedItem.Clone();
                paras.Add(MagicStringHelper.CurrentSelectdItemParameterName, fooObject);
                paras.Add(MagicStringHelper.CrudActionName, MagicStringHelper.CrudEditAction);
                await navigationService.NavigateAsync("LeaveFormRecordPage", paras);
            });
            #endregion

            #region 更新遠端紀錄命令
            RefreshCommand = new DelegateCommand(async() =>
            {
                IsRefresh = true;
                await ReloadData();
                IsRefresh = false;
            });
            #endregion
        }