1
1
using Files . DataModels ;
2
+ using Files . Filesystem ;
2
3
using Newtonsoft . Json ;
3
4
using System ;
4
5
using System . IO ;
@@ -11,9 +12,7 @@ public class TerminalController : IJson
11
12
{
12
13
private string defaultTerminalPath = "ms-appx:///Assets/terminal/terminal.json" ;
13
14
14
- private StorageFile JsonFile { get ; set ; }
15
-
16
- private StorageFolder Folder { get ; set ; }
15
+ private string folderPath => Path . Combine ( ApplicationData . Current . LocalFolder . Path , "settings" ) ;
17
16
18
17
public TerminalFileModel Model { get ; set ; }
19
18
@@ -26,45 +25,69 @@ public TerminalController()
26
25
27
26
private async Task LoadAsync ( )
28
27
{
29
- Folder = await ApplicationData . Current . LocalFolder . CreateFolderAsync ( "settings" , CreationCollisionOption . OpenIfExists ) ;
30
- try
28
+ StorageFolder Folder = await FilesystemTasks . Wrap ( ( ) => ApplicationData . Current . LocalFolder . CreateFolderAsync ( "settings" , CreationCollisionOption . OpenIfExists ) . AsTask ( ) ) ;
29
+ if ( Folder == null )
31
30
{
32
- JsonFile = await Folder . GetFileAsync ( JsonFileName ) ;
31
+ Model = await GetDefaultTerminalFileModel ( ) ;
32
+ return ;
33
33
}
34
- catch ( FileNotFoundException )
35
- {
36
- var defaultFile = StorageFile . GetFileFromApplicationUriAsync ( new Uri ( defaultTerminalPath ) ) ;
37
34
38
- JsonFile = await Folder . CreateFileAsync ( JsonFileName ) ;
39
- await FileIO . WriteBufferAsync ( JsonFile , await FileIO . ReadBufferAsync ( await defaultFile ) ) ;
35
+ var JsonFile = await FilesystemTasks . Wrap ( ( ) => Folder . GetFileAsync ( JsonFileName ) . AsTask ( ) ) ;
36
+ if ( ! JsonFile )
37
+ {
38
+ if ( JsonFile == FilesystemErrorCode . ERROR_NOTFOUND )
39
+ {
40
+ Model = await GetDefaultTerminalFileModel ( ) ;
41
+ SaveModel ( ) ;
42
+ return ;
43
+ }
44
+ else
45
+ {
46
+ Model = await GetDefaultTerminalFileModel ( ) ;
47
+ return ;
48
+ }
40
49
}
41
50
42
- var content = await FileIO . ReadTextAsync ( JsonFile ) ;
43
-
44
51
try
45
52
{
53
+ var content = await FileIO . ReadTextAsync ( JsonFile . Result ) ;
46
54
Model = JsonConvert . DeserializeObject < TerminalFileModel > ( content ) ;
47
55
if ( Model == null )
48
56
{
49
- Model = new TerminalFileModel ( ) ;
50
57
throw new JsonParsingNullException ( JsonFileName ) ;
51
58
}
52
59
}
53
60
catch ( JsonParsingNullException )
54
61
{
55
- var defaultFile = StorageFile . GetFileFromApplicationUriAsync ( new Uri ( defaultTerminalPath ) ) ;
56
-
57
- JsonFile = await Folder . CreateFileAsync ( JsonFileName , CreationCollisionOption . ReplaceExisting ) ;
58
- await FileIO . WriteBufferAsync ( JsonFile , await FileIO . ReadBufferAsync ( await defaultFile ) ) ;
59
- var defaultContent = await FileIO . ReadTextAsync ( JsonFile ) ;
60
- Model = JsonConvert . DeserializeObject < TerminalFileModel > ( defaultContent ) ;
62
+ Model = await GetDefaultTerminalFileModel ( ) ;
63
+ SaveModel ( ) ;
61
64
}
62
65
catch ( Exception )
63
66
{
64
- var defaultFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( defaultTerminalPath ) ) ;
65
- JsonFile = null ;
67
+ Model = await GetDefaultTerminalFileModel ( ) ;
68
+ }
69
+ }
70
+
71
+ private async Task < TerminalFileModel > GetDefaultTerminalFileModel ( )
72
+ {
73
+ try
74
+ {
75
+ StorageFile defaultFile = await StorageFile . GetFileFromApplicationUriAsync ( new Uri ( defaultTerminalPath ) ) ;
66
76
var defaultContent = await FileIO . ReadTextAsync ( defaultFile ) ;
67
- Model = JsonConvert . DeserializeObject < TerminalFileModel > ( defaultContent ) ;
77
+ return JsonConvert . DeserializeObject < TerminalFileModel > ( defaultContent ) ;
78
+ }
79
+ catch
80
+ {
81
+ var model = new TerminalFileModel ( ) ;
82
+ model . Terminals . Add ( new Terminal ( )
83
+ {
84
+ Name = "CMD" ,
85
+ Path = "cmd.exe" ,
86
+ Arguments = "" ,
87
+ Icon = ""
88
+ } ) ;
89
+ model . ResetToDefaultTerminal ( ) ;
90
+ return model ;
68
91
}
69
92
}
70
93
@@ -102,16 +125,17 @@ public async Task GetInstalledTerminalsAsync()
102
125
103
126
public void SaveModel ( )
104
127
{
105
- if ( JsonFile == null )
128
+ try
106
129
{
107
- return ;
130
+ using ( var file = File . CreateText ( Path . Combine ( folderPath , JsonFileName ) ) )
131
+ {
132
+ JsonSerializer serializer = new JsonSerializer ( ) ;
133
+ serializer . Formatting = Formatting . Indented ;
134
+ serializer . Serialize ( file , Model ) ;
135
+ }
108
136
}
109
-
110
- using ( var file = File . CreateText ( Folder . Path + Path . DirectorySeparatorChar + JsonFileName ) )
137
+ catch
111
138
{
112
- JsonSerializer serializer = new JsonSerializer ( ) ;
113
- serializer . Formatting = Formatting . Indented ;
114
- serializer . Serialize ( file , Model ) ;
115
139
}
116
140
}
117
141
}
0 commit comments