private async void connectionSettingsControl_ConnectionComplete(object sender, EventArgs e) { //refresh remote API connection after account has been changed or connected remoteDataRepository.EmptyAllCacheItems(); initRemoteRepo(); await activitySummaryControl.Initialize(remoteDataRepository, false); }
private async void verifyConnection() { string accessToken = ""; string refreshToken = ""; using (IUnitOfWorkDataProvider dp = AppStorage.GetUnitOfWorkDataProvider()) { accessToken = dp.Settings.GetAsString(SettingKeyType.JDAccessToken); refreshToken = dp.Settings.GetAsString(SettingKeyType.JDRefreshToken); remoteDataRepository.SetAuthData(accessToken, refreshToken); } string newUserID = ""; lblLoadingMessage.Text = "Verifying connection"; await Task.Run(async() => { System.Threading.Thread.Sleep(1000); newUserID = await remoteDataRepository.TestConnection(); }); pnlLoading.Visible = false; if (!string.IsNullOrEmpty(newUserID)) { if (newUserID != _currentUserId && !_firstRun) //switched accounts { if (MessageBox.Show("You have switched to a different account. This will reset all application data. Are you sure you want to continue?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes) { pnlConnectionSettingsConnected.Visible = true; pnlConnectionSettingsLinkAccount.Visible = false; pnlConnectionSettingsVerifyCode.Visible = false; _currentUserId = newUserID; using (IUnitOfWorkDataProvider dp = AppStorage.GetUnitOfWorkDataProvider()) { //remove all organizations saved dp.Organizations.DeleteAll(); //save updated token and secret dp.Settings.UpsertSettingWithKey(SettingKeyType.JDAccessToken, accessToken); dp.Settings.UpsertSettingWithKey(SettingKeyType.JDRefreshToken, refreshToken); dp.Settings.UpsertSettingWithKey(SettingKeyType.LastUserID, _currentUserId); dp.SaveChanges(); } this.OnConnectionComplete(new EventArgs()); } else { //reset auth token back to original remoteDataRepository.SetAuthData(accessToken, refreshToken); pnlConnectionSettingsConnected.Visible = true; pnlConnectionSettingsLinkAccount.Visible = false; pnlConnectionSettingsVerifyCode.Visible = false; this.OnConnectionComplete(new EventArgs()); } } else { //handle same user just save new secret and token _currentUserId = newUserID; using (IUnitOfWorkDataProvider dp = AppStorage.GetUnitOfWorkDataProvider()) { dp.Settings.UpdateSettingWithKey(SettingKeyType.JDAccessToken, accessToken); dp.Settings.UpdateSettingWithKey(SettingKeyType.JDRefreshToken, refreshToken); dp.Settings.UpsertSettingWithKey(SettingKeyType.LastUserID, _currentUserId); dp.SaveChanges(); } pnlConnectionSettingsConnected.Visible = true; pnlConnectionSettingsLinkAccount.Visible = false; pnlConnectionSettingsVerifyCode.Visible = false; this.OnConnectionComplete(new EventArgs()); } remoteDataRepository.EmptyAllCacheItems(); } else { MessageBox.Show("Unable to establish connection. Verify you have a network connection."); pnlConnectionSettingsVerifyCode.Visible = true; } pnlLoading.Visible = false; }