1
+ using System ;
2
+ using System . Threading . Tasks ;
3
+
4
+ using Avalonia . Threading ;
5
+
6
+ using Nullinside . Api . Common . Twitch ;
7
+
8
+ using TwitchStreamingTools . Models ;
9
+
10
+ namespace TwitchStreamingTools . Services ;
11
+
12
+ /// <summary>
13
+ /// Manages the credentials in the application.
14
+ /// </summary>
15
+ public class AccountManager : IAccountManager {
16
+ /// <summary>
17
+ /// The timer used to check the twitch OAuth token against the API.
18
+ /// </summary>
19
+ private readonly DispatcherTimer _timer ;
20
+
21
+ /// <summary>
22
+ /// The twitch chat api.
23
+ /// </summary>
24
+ private readonly ITwitchApiProxy _twitchApi ;
25
+
26
+ /// <summary>
27
+ /// The twitch chat client.
28
+ /// </summary>
29
+ private readonly ITwitchClientProxy _twitchClient ;
30
+
31
+ /// <summary>
32
+ /// Initializes a new instance of the <see cref="AccountManager" /> class.
33
+ /// </summary>
34
+ /// <param name="twitchClient">The twitch chat client.</param>
35
+ /// <param name="twitchApi">The twitch chat api.</param>
36
+ public AccountManager ( ITwitchClientProxy twitchClient , ITwitchApiProxy twitchApi ) {
37
+ _twitchClient = twitchClient ;
38
+ _twitchApi = twitchApi ;
39
+ _timer = new DispatcherTimer {
40
+ Interval = TimeSpan . FromSeconds ( 5 )
41
+ } ;
42
+
43
+ _timer . Tick += async ( _ , _ ) => await OnCheckCredentials ( ) ;
44
+ _ = OnCheckCredentials ( ) ;
45
+ }
46
+
47
+ /// <inheritdoc />
48
+ public string ? TwitchUsername { get ; set ; }
49
+
50
+ /// <inheritdoc />
51
+ public bool CredentialsAreValid { get ; set ; }
52
+
53
+ /// <inheritdoc />
54
+ public Action < bool > ? OnCredentialsStatusChanged { get ; set ; }
55
+
56
+ /// <inheritdoc />
57
+ public Action < TwitchAccessToken ? > ? OnCredentialsChanged { get ; set ; }
58
+
59
+ /// <inheritdoc />
60
+ public async Task UpdateCredentials ( string bearer , string refresh , DateTime expires ) {
61
+ _twitchApi . OAuth = new TwitchAccessToken {
62
+ AccessToken = bearer ,
63
+ RefreshToken = refresh ,
64
+ ExpiresUtc = expires
65
+ } ;
66
+
67
+ ( string ? id , string ? username ) ? user = null ;
68
+ try {
69
+ user = await _twitchApi . GetUser ( ) ;
70
+ }
71
+ catch {
72
+ // Do nothing
73
+ }
74
+
75
+ Configuration . Instance . OAuth = new OAuthResponse {
76
+ Bearer = bearer ,
77
+ Refresh = refresh ,
78
+ ExpiresUtc = expires
79
+ } ;
80
+
81
+ Configuration . Instance . TwitchUsername = user ? . username ;
82
+ Configuration . Instance . WriteConfiguration ( ) ;
83
+ _twitchClient . TwitchOAuthToken = bearer ;
84
+ _twitchClient . TwitchUsername = user ? . username ;
85
+
86
+ OnCredentialsChanged ? . Invoke ( null ) ;
87
+ await OnCheckCredentials ( ) ;
88
+ }
89
+
90
+ /// <inheritdoc />
91
+ public void DeleteCredentials ( ) {
92
+ _twitchApi . OAuth = null ;
93
+ Configuration . Instance . OAuth = null ;
94
+ Configuration . Instance . TwitchUsername = null ;
95
+ _twitchClient . TwitchOAuthToken = null ;
96
+ _twitchClient . TwitchUsername = null ;
97
+ CredentialsAreValid = false ;
98
+ TwitchUsername = null ;
99
+
100
+ OnCredentialsChanged ? . Invoke ( null ) ;
101
+ OnCredentialsStatusChanged ? . Invoke ( false ) ;
102
+ }
103
+
104
+ /// <summary>
105
+ /// Checks the OAuth token against the API to verify its validity.
106
+ /// </summary>
107
+ private async Task OnCheckCredentials ( ) {
108
+ _timer . Stop ( ) ;
109
+ try {
110
+ bool previousValue = CredentialsAreValid ;
111
+ string ? username = ( await _twitchApi . GetUser ( ) ) . username ;
112
+ CredentialsAreValid = ! string . IsNullOrWhiteSpace ( username ) ;
113
+ TwitchUsername = username ;
114
+
115
+ if ( previousValue != CredentialsAreValid ) {
116
+ OnCredentialsStatusChanged ? . Invoke ( CredentialsAreValid ) ;
117
+ }
118
+ }
119
+ catch {
120
+ // Do nothing
121
+ }
122
+ finally {
123
+ _timer . Start ( ) ;
124
+ }
125
+ }
126
+ }
0 commit comments