6
6
using System . Windows ;
7
7
using CommunityToolkit . Mvvm . Input ;
8
8
using Flow . Launcher . Core ;
9
- using Flow . Launcher . Core . Resource ;
10
9
using Flow . Launcher . Infrastructure ;
11
10
using Flow . Launcher . Infrastructure . Logger ;
12
11
using Flow . Launcher . Infrastructure . UserSettings ;
@@ -16,6 +15,8 @@ namespace Flow.Launcher.SettingPages.ViewModels;
16
15
17
16
public partial class SettingsPaneAboutViewModel : BaseModel
18
17
{
18
+ private static readonly string ClassName = nameof ( SettingsPaneAboutViewModel ) ;
19
+
19
20
private readonly Settings _settings ;
20
21
private readonly Updater _updater ;
21
22
@@ -24,7 +25,16 @@ public string LogFolderSize
24
25
get
25
26
{
26
27
var size = GetLogFiles ( ) . Sum ( file => file . Length ) ;
27
- return $ "{ InternationalizationManager . Instance . GetTranslation ( "clearlogfolder" ) } ({ BytesToReadableString ( size ) } )";
28
+ return $ "{ App . API . GetTranslation ( "clearlogfolder" ) } ({ BytesToReadableString ( size ) } )";
29
+ }
30
+ }
31
+
32
+ public string CacheFolderSize
33
+ {
34
+ get
35
+ {
36
+ var size = GetCacheFiles ( ) . Sum ( file => file . Length ) ;
37
+ return $ "{ App . API . GetTranslation ( "clearcachefolder" ) } ({ BytesToReadableString ( size ) } )";
28
38
}
29
39
}
30
40
@@ -42,7 +52,7 @@ public string LogFolderSize
42
52
} ;
43
53
44
54
public string ActivatedTimes => string . Format (
45
- InternationalizationManager . Instance . GetTranslation ( "about_activate_times" ) ,
55
+ App . API . GetTranslation ( "about_activate_times" ) ,
46
56
_settings . ActivateTimes
47
57
) ;
48
58
@@ -88,14 +98,35 @@ private void OpenWelcomeWindow()
88
98
private void AskClearLogFolderConfirmation ( )
89
99
{
90
100
var confirmResult = App . API . ShowMsgBox (
91
- InternationalizationManager . Instance . GetTranslation ( "clearlogfolderMessage" ) ,
92
- InternationalizationManager . Instance . GetTranslation ( "clearlogfolder" ) ,
101
+ App . API . GetTranslation ( "clearlogfolderMessage" ) ,
102
+ App . API . GetTranslation ( "clearlogfolder" ) ,
93
103
MessageBoxButton . YesNo
94
104
) ;
95
105
96
106
if ( confirmResult == MessageBoxResult . Yes )
97
107
{
98
- ClearLogFolder ( ) ;
108
+ if ( ! ClearLogFolder ( ) )
109
+ {
110
+ App . API . ShowMsgBox ( App . API . GetTranslation ( "clearfolderfailMessage" ) ) ;
111
+ }
112
+ }
113
+ }
114
+
115
+ [ RelayCommand ]
116
+ private void AskClearCacheFolderConfirmation ( )
117
+ {
118
+ var confirmResult = App . API . ShowMsgBox (
119
+ App . API . GetTranslation ( "clearcachefolderMessage" ) ,
120
+ App . API . GetTranslation ( "clearcachefolder" ) ,
121
+ MessageBoxButton . YesNo
122
+ ) ;
123
+
124
+ if ( confirmResult == MessageBoxResult . Yes )
125
+ {
126
+ if ( ! ClearCacheFolder ( ) )
127
+ {
128
+ App . API . ShowMsgBox ( App . API . GetTranslation ( "clearfolderfailMessage" ) ) ;
129
+ }
99
130
}
100
131
}
101
132
@@ -113,29 +144,54 @@ private void OpenParentOfSettingsFolder(object parameter)
113
144
App . API . OpenDirectory ( parentFolderPath ) ;
114
145
}
115
146
116
-
117
147
[ RelayCommand ]
118
148
private void OpenLogsFolder ( )
119
149
{
120
150
App . API . OpenDirectory ( GetLogDir ( Constant . Version ) . FullName ) ;
121
151
}
122
152
123
153
[ RelayCommand ]
124
- private Task UpdateApp ( ) => _updater . UpdateAppAsync ( false ) ;
154
+ private Task UpdateAppAsync ( ) => _updater . UpdateAppAsync ( false ) ;
125
155
126
- private void ClearLogFolder ( )
156
+ private bool ClearLogFolder ( )
127
157
{
158
+ var success = true ;
128
159
var logDirectory = GetLogDir ( ) ;
129
160
var logFiles = GetLogFiles ( ) ;
130
161
131
- logFiles . ForEach ( f => f . Delete ( ) ) ;
162
+ logFiles . ForEach ( f =>
163
+ {
164
+ try
165
+ {
166
+ f . Delete ( ) ;
167
+ }
168
+ catch ( Exception e )
169
+ {
170
+ App . API . LogException ( ClassName , $ "Failed to delete log file: { f . Name } ", e ) ;
171
+ success = false ;
172
+ }
173
+ } ) ;
132
174
133
175
logDirectory . EnumerateDirectories ( "*" , SearchOption . TopDirectoryOnly )
176
+ // Do not clean log files of current version
134
177
. Where ( dir => ! Constant . Version . Equals ( dir . Name ) )
135
178
. ToList ( )
136
- . ForEach ( dir => dir . Delete ( ) ) ;
179
+ . ForEach ( dir =>
180
+ {
181
+ try
182
+ {
183
+ dir . Delete ( true ) ;
184
+ }
185
+ catch ( Exception e )
186
+ {
187
+ App . API . LogException ( ClassName , $ "Failed to delete log directory: { dir . Name } ", e ) ;
188
+ success = false ;
189
+ }
190
+ } ) ;
137
191
138
192
OnPropertyChanged ( nameof ( LogFolderSize ) ) ;
193
+
194
+ return success ;
139
195
}
140
196
141
197
private static DirectoryInfo GetLogDir ( string version = "" )
@@ -148,6 +204,55 @@ private static List<FileInfo> GetLogFiles(string version = "")
148
204
return GetLogDir ( version ) . EnumerateFiles ( "*" , SearchOption . AllDirectories ) . ToList ( ) ;
149
205
}
150
206
207
+ private bool ClearCacheFolder ( )
208
+ {
209
+ var success = true ;
210
+ var cacheDirectory = GetCacheDir ( ) ;
211
+ var cacheFiles = GetCacheFiles ( ) ;
212
+
213
+ cacheFiles . ForEach ( f =>
214
+ {
215
+ try
216
+ {
217
+ f . Delete ( ) ;
218
+ }
219
+ catch ( Exception e )
220
+ {
221
+ App . API . LogException ( ClassName , $ "Failed to delete cache file: { f . Name } ", e ) ;
222
+ success = false ;
223
+ }
224
+ } ) ;
225
+
226
+ cacheDirectory . EnumerateDirectories ( "*" , SearchOption . TopDirectoryOnly )
227
+ . ToList ( )
228
+ . ForEach ( dir =>
229
+ {
230
+ try
231
+ {
232
+ dir . Delete ( true ) ;
233
+ }
234
+ catch ( Exception e )
235
+ {
236
+ App . API . LogException ( ClassName , $ "Failed to delete cache directory: { dir . Name } ", e ) ;
237
+ success = false ;
238
+ }
239
+ } ) ;
240
+
241
+ OnPropertyChanged ( nameof ( CacheFolderSize ) ) ;
242
+
243
+ return success ;
244
+ }
245
+
246
+ private static DirectoryInfo GetCacheDir ( )
247
+ {
248
+ return new DirectoryInfo ( DataLocation . CacheDirectory ) ;
249
+ }
250
+
251
+ private static List < FileInfo > GetCacheFiles ( )
252
+ {
253
+ return GetCacheDir ( ) . EnumerateFiles ( "*" , SearchOption . AllDirectories ) . ToList ( ) ;
254
+ }
255
+
151
256
private static string BytesToReadableString ( long bytes )
152
257
{
153
258
const int scale = 1024 ;
@@ -156,8 +261,7 @@ private static string BytesToReadableString(long bytes)
156
261
157
262
foreach ( string order in orders )
158
263
{
159
- if ( bytes > max )
160
- return $ "{ decimal . Divide ( bytes , max ) : ##.##} { order } ";
264
+ if ( bytes > max ) return $ "{ decimal . Divide ( bytes , max ) : ##.##} { order } ";
161
265
162
266
max /= scale ;
163
267
}
0 commit comments