11using System ;
2- using System . IO ;
3- using System . Linq ;
42using System . Collections . Generic ;
53
64namespace InvisibleManXRay . Handlers
75{
6+ using Configs ;
87 using Models ;
98 using Values ;
9+ using Utilities ;
1010
1111 public class ConfigHandler : Handler
1212 {
13- private Dictionary < string , Config > configs ;
14- private Func < int > getCurrentConfigIndex ;
13+ private GeneralConfig generalConfig ;
14+ private SubscriptionConfig subscriptionConfig ;
15+
16+ private Func < string > getCurrentConfigPath ;
1517
1618 public ConfigHandler ( )
1719 {
18- this . configs = new Dictionary < string , Config > ( ) ;
19- LoadConfigFiles ( ) ;
20+ this . generalConfig = new GeneralConfig ( ) ;
21+ this . subscriptionConfig = new SubscriptionConfig ( ) ;
2022 }
2123
22- public void Setup ( Func < int > getCurrentConfigIndex )
24+ public void Setup ( Func < string > getCurrentConfigPath )
2325 {
24- this . getCurrentConfigIndex = getCurrentConfigIndex ;
26+ this . getCurrentConfigPath = getCurrentConfigPath ;
27+ subscriptionConfig . Setup ( getCurrentConfigPath ) ;
2528 }
2629
27- public void LoadConfigFiles ( )
30+ public void LoadFiles ( GroupType group , string path )
2831 {
29- configs . Clear ( ) ;
32+ BaseConfig config = group == GroupType . GENERAL ? generalConfig : subscriptionConfig ;
33+ config . LoadFiles ( path ) ;
34+ }
3035
31- DirectoryInfo directoryInfo = new DirectoryInfo ( Directory . CONFIGS ) ;
32- if ( ! directoryInfo . Exists )
33- return ;
36+ public void CreateConfig ( string remark , string data ) => generalConfig . CreateConfig ( remark , data ) ;
3437
35- FileInfo [ ] files = directoryInfo . GetFiles ( ) . OrderBy ( file => file . CreationTime ) . ToArray ( ) ;
36- foreach ( FileInfo file in files )
37- {
38- AddConfigToList ( CreateConfig ( file . FullName ) ) ;
39- }
40- }
38+ public void CreateSubscription ( string remark , string url , string data ) => subscriptionConfig . CreateSubscription ( remark , url , data ) ;
4139
42- public void CopyConfig ( string path )
43- {
44- string destinationPath = $ "{ Directory . CONFIGS } /{ GetFileName ( path ) } ";
45- CopyToConfigsDirectory ( ) ;
46- SetFileTime ( destinationPath ) ;
47- AddConfigToList ( CreateConfig ( destinationPath ) ) ;
40+ public void DeleteSubscription ( Subscription subscription ) => subscriptionConfig . DeleteSubscription ( subscription ) ;
4841
49- void CopyToConfigsDirectory ( )
50- {
51- System . IO . Directory . CreateDirectory ( Directory . CONFIGS ) ;
52- File . Copy ( path , destinationPath , true ) ;
53- }
54- }
42+ public void CopyConfig ( string path ) => generalConfig . CopyConfig ( path ) ;
5543
56- public void CreateConfig ( string remark , string data )
57- {
58- string destinationPath = $ "{ Directory . CONFIGS } /{ remark } .json";
59- SaveToConfigsDirectory ( ) ;
60- SetFileTime ( destinationPath ) ;
61- AddConfigToList ( CreateConfig ( destinationPath ) ) ;
44+ public Config GetCurrentConfig ( ) => CreateConfigModel ( getCurrentConfigPath . Invoke ( ) ) ;
6245
63- void SaveToConfigsDirectory ( )
64- {
65- System . IO . Directory . CreateDirectory ( Directory . CONFIGS ) ;
66- File . WriteAllText ( destinationPath , data ) ;
67- }
68- }
46+ public void RemoveConfigFromList ( string path ) => GetCurrentBaseConfig ( ) . RemoveConfigFromList ( path ) ;
6947
70- public Config GetCurrentConfig ( )
48+ public List < Config > GetAllGeneralConfigs ( )
7149 {
72- int currentConfigIndex = getCurrentConfigIndex . Invoke ( ) ;
73-
74- try
75- {
76- return configs . ElementAt ( currentConfigIndex ) . Value ;
77- }
78- catch
79- {
80- return null ;
81- }
50+ generalConfig . LoadFiles ( ) ;
51+ return generalConfig . GetAllConfigs ( ) ;
8252 }
8353
84- public List < Config > GetAllConfigs ( ) => configs . Select ( config => config . Value ) . ToList ( ) ;
85-
86- public void RemoveConfigFromList ( string path )
54+ public List < Config > GetAllSubscriptionConfigs ( string path )
8755 {
88- if ( configs . ContainsKey ( path ) )
89- configs . Remove ( path ) ;
56+ subscriptionConfig . LoadFiles ( path ) ;
57+ return subscriptionConfig . GetAllConfigs ( ) ;
9058 }
9159
92- private void AddConfigToList ( Config config )
60+ public List < Subscription > GetAllGroups ( )
9361 {
94- if ( configs . ContainsKey ( config . Path ) )
95- configs [ config . Path ] = config ;
96- else
97- configs . Add ( config . Path , config ) ;
62+ subscriptionConfig . LoadGroups ( ) ;
63+ return subscriptionConfig . GetAllGroups ( ) ;
9864 }
9965
100- private Config CreateConfig ( string path )
66+ public Config CreateConfigModel ( string path )
10167 {
102- return new Config (
103- path : $ "{ Directory . CONFIGS } /{ GetFileName ( path ) } ",
104- name : GetFileName ( path ) ,
105- type : ConfigType . FILE ,
106- updateTime : GetFileUpdateTime ( path )
107- ) ;
68+ if ( string . IsNullOrEmpty ( path ) || ! FileUtility . IsFileExists ( path ) )
69+ return null ;
70+
71+ return GetCurrentBaseConfig ( ) . CreateConfigModel ( path ) ;
10872 }
10973
110- private void SetFileTime ( string path )
74+ public bool IsCurrentPathEqualRootConfigPath ( )
11175 {
112- File . SetCreationTime ( path , DateTime . Now ) ;
113- File . SetLastWriteTime ( path , DateTime . Now ) ;
114- }
76+ return GetCurrentConfigDirectory ( ) == GetRootConfigDirectory ( ) ;
77+
78+ string GetCurrentConfigDirectory ( )
79+ {
80+ string path = getCurrentConfigPath . Invoke ( ) ;
81+ string directory = FileUtility . GetDirectory ( path ) ;
11582
116- private string GetFileName ( string path ) => System . IO . Path . GetFileName ( path ) ;
83+ if ( string . IsNullOrEmpty ( path ) || ! FileUtility . IsFileExists ( path ) )
84+ directory = Directory . CONFIGS ;
85+
86+ return FileUtility . GetFullPath ( directory ) ;
87+ }
88+
89+ string GetRootConfigDirectory ( )
90+ {
91+ return FileUtility . GetFullPath ( Directory . CONFIGS ) ;
92+ }
93+ }
11794
118- private string GetFileUpdateTime ( string path ) => System . IO . File . GetLastWriteTime ( path ) . ToShortDateString ( ) ;
95+ private BaseConfig GetCurrentBaseConfig ( )
96+ {
97+ if ( IsCurrentPathEqualRootConfigPath ( ) )
98+ return generalConfig ;
99+ else
100+ return subscriptionConfig ;
101+ }
119102 }
120103}
0 commit comments