44
55using Avalonia . Threading ;
66
7+ using Nullinside . Api . Common . Twitch ;
8+
79using ReactiveUI ;
810
911using TwitchStreamingTools . Models ;
@@ -14,21 +16,21 @@ namespace TwitchStreamingTools.ViewModels.Pages;
1416/// <summary>
1517/// Handles binding your account to the application.
1618/// </summary>
17- public class AccountViewModel : PageViewModelBase {
19+ public class AccountViewModel : PageViewModelBase , IDisposable {
1820 /// <summary>
19- /// Polls the clipboard waiting for an oAuth configuration .
21+ /// The timer used to check the twitch OAuth token against the API .
2022 /// </summary>
21- private IClipboardPoller < OAuthResponse > ? _clipboardPoller ;
23+ private readonly DispatcherTimer _timer ;
2224
2325 /// <summary>
24- /// True if we have a valid OAuth token, false otherwise .
26+ /// Polls the clipboard waiting for an oAuth configuration .
2527 /// </summary>
26- private bool _hasValidOAuthToken ;
28+ private IDisposable ? _clipboardPoller ;
2729
2830 /// <summary>
29- /// The timer used to check the twitch OAuth token against the API .
31+ /// True if we have a valid OAuth token, false otherwise .
3032 /// </summary>
31- private readonly DispatcherTimer _timer ;
33+ private bool _hasValidOAuthToken ;
3234
3335 /// <summary>
3436 /// The authenticated user's twitch username.
@@ -40,7 +42,7 @@ public class AccountViewModel : PageViewModelBase {
4042 /// </summary>
4143 public AccountViewModel ( ) {
4244 OnLaunchBrowser = ReactiveCommand . Create ( LaunchBrowser ) ;
43- OnLogout = ReactiveCommand . Create ( Logout ) ;
45+ OnLogout = ReactiveCommand . Create ( ClearCredentials ) ;
4446 _timer = new DispatcherTimer {
4547 Interval = TimeSpan . FromSeconds ( 5 )
4648 } ;
@@ -79,18 +81,12 @@ public string? TwitchUsername {
7981 set => this . RaiseAndSetIfChanged ( ref _twitchUsername , value ) ;
8082 }
8183
82- /// <summary>
83- /// Handles logging out of the application.
84- /// </summary>
85- private void Logout ( ) {
86- try {
87- Configuration . Instance . OAuth = null ;
88- Configuration . Instance . WriteConfiguration ( ) ;
89- OnCheckApiStatus ( ) ;
90- }
91- catch {
92- // do nothing
93- }
84+ /// <inheritdoc />
85+ public void Dispose ( ) {
86+ _timer . Stop ( ) ;
87+ _clipboardPoller ? . Dispose ( ) ;
88+ OnLaunchBrowser . Dispose ( ) ;
89+ OnLogout . Dispose ( ) ;
9490 }
9591
9692 /// <summary>
@@ -100,14 +96,27 @@ private async void OnCheckApiStatus() {
10096 _timer . Stop ( ) ;
10197 try {
10298 if ( null == Configuration . Instance . OAuth ? . Bearer ) {
103- TwitchUsername = null ;
104- HasValidOAuthToken = false ;
99+ ClearCredentials ( ) ;
105100 return ;
106101 }
107102
108103 var api = await TwitchApiWrapper . CreateApi ( ) ;
109104 TwitchUsername = ( await api . GetUser ( ) ) . username ;
110105 HasValidOAuthToken = ! string . IsNullOrWhiteSpace ( TwitchUsername ) ;
106+
107+ if ( HasValidOAuthToken ) {
108+ if ( ! string . Equals ( api . OAuth ? . AccessToken , Configuration . Instance . OAuth . Bearer ) ||
109+ ! string . Equals ( TwitchUsername , Configuration . Instance . TwitchUsername ) ) {
110+ SetCredentials ( TwitchUsername , new OAuthResponse {
111+ Bearer = api . OAuth . AccessToken ,
112+ Refresh = api . OAuth . RefreshToken ?? string . Empty ,
113+ ExpiresUtc = api . OAuth . ExpiresUtc ?? DateTime . MinValue
114+ } ) ;
115+ }
116+ }
117+ else {
118+ ClearCredentials ( ) ;
119+ }
111120 }
112121 catch {
113122 TwitchUsername = null ;
@@ -122,8 +131,9 @@ private async void OnCheckApiStatus() {
122131 /// Launches the computer's default browser to generate an OAuth token.
123132 /// </summary>
124133 private void LaunchBrowser ( ) {
125- Configuration . Instance . OAuth = null ;
126- Configuration . Instance . WriteConfiguration ( ) ;
134+ if ( null != _clipboardPoller ) {
135+ _clipboardPoller . Dispose ( ) ;
136+ }
127137
128138 _clipboardPoller = new ClipboardPoller < OAuthResponse > ( Constants . CLIPBOARD ! , OnOAuthReceived ) ;
129139
@@ -140,12 +150,31 @@ private void LaunchBrowser() {
140150 /// <param name="oauth">The new OAuth token.</param>
141151 private void OnOAuthReceived ( OAuthResponse oauth ) {
142152 try {
143- Configuration . Instance . OAuth = oauth ;
144- Configuration . Instance . WriteConfiguration ( ) ;
153+ SetCredentials ( null , oauth ) ;
145154 OnCheckApiStatus ( ) ;
146155 }
147156 catch {
148157 // do nothing
149158 }
150159 }
160+
161+ private void ClearCredentials ( ) {
162+ TwitchUsername = null ;
163+ HasValidOAuthToken = false ;
164+ TwitchClientProxy . Instance . Dispose ( ) ;
165+
166+ if ( null != Configuration . Instance . OAuth ) {
167+ Configuration . Instance . OAuth = null ;
168+ Configuration . Instance . TwitchUsername = null ;
169+ Configuration . Instance . WriteConfiguration ( ) ;
170+ }
171+ }
172+
173+ private void SetCredentials ( string ? username , OAuthResponse oauth ) {
174+ Configuration . Instance . OAuth = oauth ;
175+ Configuration . Instance . TwitchUsername = username ;
176+ Configuration . Instance . WriteConfiguration ( ) ;
177+ TwitchClientProxy . Instance . TwitchUsername = username ;
178+ TwitchClientProxy . Instance . TwitchOAuthToken = oauth . Bearer ;
179+ }
151180}
0 commit comments