4
4
5
5
using Avalonia . Threading ;
6
6
7
+ using Nullinside . Api . Common . Twitch ;
8
+
7
9
using ReactiveUI ;
8
10
9
11
using TwitchStreamingTools . Models ;
@@ -14,21 +16,21 @@ namespace TwitchStreamingTools.ViewModels.Pages;
14
16
/// <summary>
15
17
/// Handles binding your account to the application.
16
18
/// </summary>
17
- public class AccountViewModel : PageViewModelBase {
19
+ public class AccountViewModel : PageViewModelBase , IDisposable {
18
20
/// <summary>
19
- /// Polls the clipboard waiting for an oAuth configuration .
21
+ /// The timer used to check the twitch OAuth token against the API .
20
22
/// </summary>
21
- private IClipboardPoller < OAuthResponse > ? _clipboardPoller ;
23
+ private readonly DispatcherTimer _timer ;
22
24
23
25
/// <summary>
24
- /// True if we have a valid OAuth token, false otherwise .
26
+ /// Polls the clipboard waiting for an oAuth configuration .
25
27
/// </summary>
26
- private bool _hasValidOAuthToken ;
28
+ private IDisposable ? _clipboardPoller ;
27
29
28
30
/// <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 .
30
32
/// </summary>
31
- private readonly DispatcherTimer _timer ;
33
+ private bool _hasValidOAuthToken ;
32
34
33
35
/// <summary>
34
36
/// The authenticated user's twitch username.
@@ -40,7 +42,7 @@ public class AccountViewModel : PageViewModelBase {
40
42
/// </summary>
41
43
public AccountViewModel ( ) {
42
44
OnLaunchBrowser = ReactiveCommand . Create ( LaunchBrowser ) ;
43
- OnLogout = ReactiveCommand . Create ( Logout ) ;
45
+ OnLogout = ReactiveCommand . Create ( ClearCredentials ) ;
44
46
_timer = new DispatcherTimer {
45
47
Interval = TimeSpan . FromSeconds ( 5 )
46
48
} ;
@@ -79,18 +81,12 @@ public string? TwitchUsername {
79
81
set => this . RaiseAndSetIfChanged ( ref _twitchUsername , value ) ;
80
82
}
81
83
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 ( ) ;
94
90
}
95
91
96
92
/// <summary>
@@ -100,14 +96,27 @@ private async void OnCheckApiStatus() {
100
96
_timer . Stop ( ) ;
101
97
try {
102
98
if ( null == Configuration . Instance . OAuth ? . Bearer ) {
103
- TwitchUsername = null ;
104
- HasValidOAuthToken = false ;
99
+ ClearCredentials ( ) ;
105
100
return ;
106
101
}
107
102
108
103
var api = await TwitchApiWrapper . CreateApi ( ) ;
109
104
TwitchUsername = ( await api . GetUser ( ) ) . username ;
110
105
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
+ }
111
120
}
112
121
catch {
113
122
TwitchUsername = null ;
@@ -122,8 +131,9 @@ private async void OnCheckApiStatus() {
122
131
/// Launches the computer's default browser to generate an OAuth token.
123
132
/// </summary>
124
133
private void LaunchBrowser ( ) {
125
- Configuration . Instance . OAuth = null ;
126
- Configuration . Instance . WriteConfiguration ( ) ;
134
+ if ( null != _clipboardPoller ) {
135
+ _clipboardPoller . Dispose ( ) ;
136
+ }
127
137
128
138
_clipboardPoller = new ClipboardPoller < OAuthResponse > ( Constants . CLIPBOARD ! , OnOAuthReceived ) ;
129
139
@@ -140,12 +150,31 @@ private void LaunchBrowser() {
140
150
/// <param name="oauth">The new OAuth token.</param>
141
151
private void OnOAuthReceived ( OAuthResponse oauth ) {
142
152
try {
143
- Configuration . Instance . OAuth = oauth ;
144
- Configuration . Instance . WriteConfiguration ( ) ;
153
+ SetCredentials ( null , oauth ) ;
145
154
OnCheckApiStatus ( ) ;
146
155
}
147
156
catch {
148
157
// do nothing
149
158
}
150
159
}
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
+ }
151
180
}
0 commit comments