Exemple #1
0
        public MainWindowViewModel()
        {
            _dialogService  = DialogServiceFactory.GetDialogServiceInstance();
            _fbTokenService = FacebookServiceFactory.GetFacebookTokenServiceInstance();
            _settingsRepos  = SettingsCreatorFactory.GetSetingsInstance();

            GetTokenCommand = new DelegateCommand(async _ =>
            {
                //Reset result
                ErrorMsg = null;
                FBToken  = null;
                //End of - Reset result

                //Open loading indicator
                IsGettingData = true;

                Debug.WriteLine($"Email: {UserEmail} - Password: {UserPassword}");

                string lastEmail = _settingsRepos.LastLoggedInEmail;
                if (UserEmail != lastEmail)
                {
                    //Lưu lại email đã dùng để lấy token mới
                    _settingsRepos.LastLoggedInEmail = UserEmail;
                }

                //Request new Facebook token
                var getTokenResult = await _fbTokenService.GetTokenInfoAsyncByLocMai(UserEmail, UserPassword);
                if (!getTokenResult.Success)
                {
                    ErrorMsg = getTokenResult.Message;
                    Debug.WriteLine(ErrorMsg);
                }
                else
                {
                    FBToken = getTokenResult.UserTokenInfo.AccessToken;
                    Debug.WriteLine($"Token: {FBToken}");
                }

                //Remove loading indicator
                IsGettingData = false;
            },
                                                  _ => !string.IsNullOrEmpty(UserEmail) && !string.IsNullOrEmpty(UserPassword) && !IsGettingData);

            SaveTokenCommand = new DelegateCommand(_ =>
            {
                //Cách lưu file text: https://stackoverflow.com/questions/45276878/creating-a-txt-file-and-write-to-it

                string filePath =
                    _dialogService.ShowSaveFileDialog();

                if (filePath == null)
                {
                    return;
                }
                using (var tw = new StreamWriter(filePath, false))
                {
                    try
                    {
                        tw.Write(FBToken); //Thực hiện ghi file
                        Debug.WriteLine($"Token was written to file: {filePath}");
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex);
                    }
                }
            });
        }