1
1
using System . Collections . Generic ;
2
2
using ReactiveUI ;
3
+ using ReactiveUI . Fody . Helpers ;
3
4
using Splat ;
4
5
using SS14 . Launcher . Api ;
5
6
using SS14 . Launcher . Models . Data ;
6
7
using SS14 . Launcher . Models . Logins ;
7
8
using SS14 . Launcher . Utility ;
8
9
using SS14 . Launcher . ViewModels . IdentityTabs ;
9
10
using SS14 . Launcher . ViewModels . Login ;
11
+ using System ;
12
+ using System . Collections . ObjectModel ;
13
+ using System . Diagnostics . CodeAnalysis ;
14
+ using System . Reactive . Linq ;
15
+ using Avalonia . Controls ;
16
+ using Avalonia . VisualTree ;
17
+ using DynamicData ;
18
+ using JetBrains . Annotations ;
19
+ using SS14 . Launcher . Localization ;
20
+ using SS14 . Launcher . Views ;
10
21
11
22
namespace SS14 . Launcher . ViewModels ;
12
23
13
24
public class MainWindowIdentityViewModel : ViewModelBase
14
25
{
15
26
private readonly DataManager _cfg ;
27
+ private readonly LoginManager _loginManager ;
16
28
17
29
// Identity Tabs
18
30
public InformationTabViewModel InformationTab { get ; }
@@ -21,13 +33,14 @@ public class MainWindowIdentityViewModel : ViewModelBase
21
33
public GuestTabViewModel GuestTab { get ; }
22
34
public LoginTabViewModel WizardsDenLoginTab { get ; }
23
35
24
- public IReadOnlyList < IdentityTabViewModel > IdentityTabs { get ; }
36
+ public ObservableCollection < IdentityTabViewModel > IdentityTabs { get ; set ; } = new ObservableCollection < IdentityTabViewModel > ( ) ;
25
37
26
38
public LanguageDropDownViewModel LanguageDropDown { get ; }
27
39
28
40
public MainWindowIdentityViewModel ( )
29
41
{
30
42
_cfg = Locator . Current . GetRequiredService < DataManager > ( ) ;
43
+ _loginManager = Locator . Current . GetRequiredService < LoginManager > ( ) ;
31
44
32
45
// Identity Tabs
33
46
@@ -37,16 +50,59 @@ public MainWindowIdentityViewModel()
37
50
GuestTab = new GuestTabViewModel ( ) ;
38
51
WizardsDenLoginTab = new LoginTabViewModel ( ) ;
39
52
40
- IdentityTabs = new IdentityTabViewModel [ ]
41
- {
42
- InformationTab ,
43
- KeyNewTab ,
44
- KeyImportTab ,
45
- GuestTab ,
46
- WizardsDenLoginTab
47
- } ;
53
+ IdentityTabs . Add ( InformationTab ) ;
54
+ IdentityTabs . Add ( KeyNewTab ) ;
55
+ IdentityTabs . Add ( KeyImportTab ) ;
56
+ IdentityTabs . Add ( GuestTab ) ;
57
+ IdentityTabs . Add ( WizardsDenLoginTab ) ;
48
58
49
59
LanguageDropDown = new LanguageDropDownViewModel ( ) ;
60
+
61
+ _loginManager . Logins . Connect ( )
62
+ . DistinctUntilChanged ( )
63
+ . ObserveOn ( RxApp . MainThreadScheduler )
64
+ . Subscribe (
65
+ _ => // Check the data after list modification actions are completed
66
+ {
67
+ if ( ! _cfg . MultiAccountsPerProvider ) // Respect multi-account setting
68
+ {
69
+ // Replace tabs as needed
70
+ ReplaceOrUnreplaceTabInListBasedOnAccountExisting ( KeyNewTab , typeof ( LoginInfoKey ) ) ;
71
+ ReplaceOrUnreplaceTabInListBasedOnAccountExisting ( WizardsDenLoginTab , typeof ( LoginInfoAccount ) ) ; // TODO: Properly support one account PER provider, instead of globally
72
+ }
73
+ } ) ;
74
+ }
75
+
76
+ private void ReplaceOrUnreplaceTabInListBasedOnAccountExisting ( IdentityTabViewModel identityTabViewModel , Type accountType )
77
+ {
78
+ if ( _loginManager . HasAccountOfType ( accountType ) )
79
+ {
80
+ // Already has it, so remove from list if it is in there
81
+ for ( int i = 0 ; i < IdentityTabs . Count ; i ++ )
82
+ {
83
+ if ( IdentityTabs [ i ] == identityTabViewModel )
84
+ {
85
+ // IdentityTabs[i] = new AlreadyMadeTabViewModel(IdentityTabs[i].Name);
86
+ // (^ This syntax won't update UI)
87
+ IdentityTabs . Replace ( IdentityTabs [ i ] ,
88
+ new AlreadyMadeTabViewModel ( IdentityTabs [ i ] . Name , IdentityTabs [ i ] ) ) ;
89
+ return ;
90
+ }
91
+ }
92
+ } else {
93
+ // Doesn't already have, so revert the replacement if necessary
94
+ for ( int i = 0 ; i < IdentityTabs . Count ; i ++ )
95
+ {
96
+ if ( IdentityTabs [ i ] is AlreadyMadeTabViewModel alreadyMadeTabViewModel &&
97
+ alreadyMadeTabViewModel . ReplacementFor == identityTabViewModel )
98
+ {
99
+ //IdentityTabs[i] = identityTabViewModel;
100
+ // (^ This syntax won't update UI)
101
+ IdentityTabs . Replace ( IdentityTabs [ i ] , identityTabViewModel ) ;
102
+ return ;
103
+ }
104
+ }
105
+ }
50
106
}
51
107
52
108
public string Version => $ "v{ LauncherVersion . Version } ";
0 commit comments