Skip to content

Commit

Permalink
Improvements to work done by @tsvietOK (#562)
Browse files Browse the repository at this point in the history
* Fixed terminals json deserialization during second app start

After first install and start app it working fine, but during second start it crashed. Reason is different models of terminals.json in Assets folder and serialized json(after checking for wt.exe in 4e5f739)

* Fix WIndows Terminal starting when press Open in Terminal in root folder

* Prevent exception from unwanted state by handling serialization exception

Co-authored-by: Vladyslav <tsvetkov_vlad@ukr.net>
  • Loading branch information
lukeblevins and tsvietOK authored Apr 10, 2020
1 parent 0ab1784 commit 7e24663
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions Files/View Models/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -307,30 +307,41 @@ private async void LoadTerminalApps()
}

var content = await FileIO.ReadTextAsync(file);
TerminalFileModel terminalsFileModel = null;
try
{
terminalsFileModel = JsonConvert.DeserializeObject<TerminalFileModel>(content);
}
catch (JsonSerializationException)
{
var defaultFile = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/terminal/terminal.json"));

var terminals = JsonConvert.DeserializeObject<TerminalFileModel>(content).Terminals;
file = await localSettingsFolder.CreateFileAsync("terminal.json", CreationCollisionOption.ReplaceExisting);
await FileIO.WriteBufferAsync(file, await FileIO.ReadBufferAsync(await defaultFile));
var defaultContent = await FileIO.ReadTextAsync(file);
terminalsFileModel = JsonConvert.DeserializeObject<TerminalFileModel>(defaultContent);

Terminals = terminals;
}

// Ensure Windows Terminal is not already in List
if (Terminals.FirstOrDefault(x => x.Path.Equals("wt.exe", StringComparison.OrdinalIgnoreCase)) == null)
if (terminalsFileModel.Terminals.FirstOrDefault(x => x.Path.Equals("wt.exe", StringComparison.OrdinalIgnoreCase)) == null)
{
PackageManager packageManager = new PackageManager();
var terminalPackage = packageManager.FindPackagesForUser(string.Empty, "Microsoft.WindowsTerminal_8wekyb3d8bbwe");
if (terminalPackage != null)
{
terminals.Add(new TerminalModel()
terminalsFileModel.Terminals.Add(new TerminalModel()
{
Id = terminals.Count + 1,
Id = terminalsFileModel.Terminals.Count + 1,
Name = "Windows Terminal",
Path = "wt.exe",
arguments = "-d \"{0}\"",
arguments = "-d {0}",
icon = ""
});
await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(terminals, Formatting.Indented));
Terminals = terminals;
await FileIO.WriteTextAsync(file, JsonConvert.SerializeObject(terminalsFileModel, Formatting.Indented));
}
}
Terminals = terminalsFileModel.Terminals;
}

private IList<TerminalModel> _Terminals = null;
Expand Down

0 comments on commit 7e24663

Please sign in to comment.