11using Files . DataModels ;
2+ using Files . Filesystem ;
23using Newtonsoft . Json ;
34using System ;
45using System . IO ;
@@ -11,9 +12,7 @@ public class TerminalController : IJson
1112 {
1213 private string defaultTerminalPath = "ms-appx:///Assets/terminal/terminal.json" ;
1314
14- private StorageFile JsonFile { get ; set ; }
15-
16- private StorageFolder Folder { get ; set ; }
15+ private string folderPath => Path . Combine ( ApplicationData . Current . LocalFolder . Path , "settings" ) ;
1716
1817 public TerminalFileModel Model { get ; set ; }
1918
@@ -26,45 +25,69 @@ public TerminalController()
2625
2726 private async Task LoadAsync ( )
2827 {
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 )
3130 {
32- JsonFile = await Folder . GetFileAsync ( JsonFileName ) ;
31+ Model = await GetDefaultTerminalFileModel ( ) ;
32+ return ;
3333 }
34- catch ( FileNotFoundException )
35- {
36- var defaultFile = StorageFile . GetFileFromApplicationUriAsync ( new Uri ( defaultTerminalPath ) ) ;
3734
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+ }
4049 }
4150
42- var content = await FileIO . ReadTextAsync ( JsonFile ) ;
43-
4451 try
4552 {
53+ var content = await FileIO . ReadTextAsync ( JsonFile . Result ) ;
4654 Model = JsonConvert . DeserializeObject < TerminalFileModel > ( content ) ;
4755 if ( Model == null )
4856 {
49- Model = new TerminalFileModel ( ) ;
5057 throw new JsonParsingNullException ( JsonFileName ) ;
5158 }
5259 }
5360 catch ( JsonParsingNullException )
5461 {
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 ( ) ;
6164 }
6265 catch ( Exception )
6366 {
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 ) ) ;
6676 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 ;
6891 }
6992 }
7093
@@ -102,16 +125,17 @@ public async Task GetInstalledTerminalsAsync()
102125
103126 public void SaveModel ( )
104127 {
105- if ( JsonFile == null )
128+ try
106129 {
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+ }
108136 }
109-
110- using ( var file = File . CreateText ( Folder . Path + Path . DirectorySeparatorChar + JsonFileName ) )
137+ catch
111138 {
112- JsonSerializer serializer = new JsonSerializer ( ) ;
113- serializer . Formatting = Formatting . Indented ;
114- serializer . Serialize ( file , Model ) ;
115139 }
116140 }
117141 }
0 commit comments