Skip to content
Permalink
Browse files

optimizations (#136)

* optimizations

* Refactor
  • Loading branch information...
chenghanzou authored and JasonStein committed Aug 12, 2019
1 parent 280f3b3 commit cd1ba1908f670e78914a7feab33bfd84df38beb3
Showing with 69 additions and 64 deletions.
  1. +4 −4 src/Notepads/Core/INotepadsCore.cs
  2. +12 −12 src/Notepads/Core/NotepadsCore.cs
  3. +53 −48 src/Notepads/Core/SessionManager.cs
@@ -1,11 +1,11 @@

namespace Notepads.Core
{
using Notepads.Controls.TextEditor;
using Notepads.Utilities;
using System;
using System.Text;
using System.Threading.Tasks;
using Notepads.Controls.TextEditor;
using Notepads.Utilities;
using Windows.Storage;
using Windows.UI.Xaml.Input;

@@ -34,9 +34,9 @@ public interface INotepadsCore

event KeyEventHandler TextEditorKeyDown;

void OpenNewTextEditor();
TextEditor OpenNewTextEditor(Guid? id = null);

Task OpenNewTextEditor(StorageFile file);
Task<TextEditor> OpenNewTextEditor(StorageFile file, Guid? id = null);

TextEditor OpenNewTextEditor(
Guid id,
@@ -1,16 +1,16 @@

namespace Notepads.Core
{
using Notepads.Controls.TextEditor;
using Notepads.Extensions;
using Notepads.Services;
using Notepads.Utilities;
using SetsView;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Notepads.Controls.TextEditor;
using Notepads.Extensions;
using Notepads.Services;
using Notepads.Utilities;
using SetsView;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI;
@@ -72,10 +72,10 @@ public class NotepadsCore : INotepadsCore
_sessionManager = SessionUtility.GetSessionManager(this);
}

public void OpenNewTextEditor()
public TextEditor OpenNewTextEditor(Guid? id = null)
{
OpenNewTextEditor(
Guid.NewGuid(),
return OpenNewTextEditor(
id ?? Guid.NewGuid(),
string.Empty,
null,
-1,
@@ -84,19 +84,19 @@ public void OpenNewTextEditor()
false);
}

public async Task OpenNewTextEditor(StorageFile file)
public async Task<TextEditor> OpenNewTextEditor(StorageFile file, Guid? id = null)
{
if (FileOpened(file))
{
SwitchTo(file);
return;
return GetSelectedTextEditor();
}

var textFile = await FileSystemUtility.ReadFile(file);
var dateModifiedFileTime = await FileSystemUtility.GetDateModified(file);

OpenNewTextEditor(
Guid.NewGuid(),
return OpenNewTextEditor(
id ?? Guid.NewGuid(),
textFile.Content,
file,
dateModifiedFileTime,
@@ -1,10 +1,6 @@

namespace Notepads.Core
{
using Newtonsoft.Json;
using Notepads.Controls.TextEditor;
using Notepads.Services;
using Notepads.Utilities;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
@@ -13,6 +9,11 @@ namespace Notepads.Core
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Notepads.Controls.TextEditor;
using Notepads.Services;
using Notepads.Utilities;
using Windows.Foundation.Collections;
using Windows.Storage;

internal class SessionManager : ISessionManager
@@ -134,8 +135,6 @@ public async Task SaveSessionAsync()

NotepadsSessionDataV1 sessionData = new NotepadsSessionDataV1();

int sessionDataUpdateCount = 0;

foreach (TextEditor textEditor in textEditors)
{
if (textEditor.EditingFile != null)
@@ -148,21 +147,21 @@ public async Task SaveSessionAsync()
{
textEditorData = new TextEditorSessionData { Id = textEditor.Id };

if (textEditor.EditingFile != null)
if (textEditor.IsModified)
{
// Persist the last save known to the app, which might not be up-to-date (if the file was modified outside the app)
BackupMetadata lastSaved = await SaveLastSavedChangesAsync(textEditor);

if (lastSaved == null)
if (textEditor.EditingFile != null)
{
continue;
}
// Persist the last save known to the app, which might not be up-to-date (if the file was modified outside the app)
BackupMetadata lastSaved = await SaveLastSavedChangesAsync(textEditor);

textEditorData.LastSaved = lastSaved;
}
if (lastSaved == null)
{
continue;
}

textEditorData.LastSaved = lastSaved;
}

if (textEditor.IsModified)
{
// Persist pending changes relative to the last save
BackupMetadata pending = await SavePendingChangesAsync(textEditor);

@@ -176,37 +175,45 @@ public async Task SaveSessionAsync()

// We will not create new backup files for this text editor unless it has changes
_sessionData.TryAdd(textEditor.Id, textEditorData);
sessionDataUpdateCount++;
}

if (textEditorData.LastSaved != null || textEditorData.Pending != null)
{
sessionData.TextEditors.Add(textEditorData);
sessionData.TextEditors.Add(textEditorData);

if (textEditor == selectedTextEditor)
{
sessionData.SelectedTextEditor = textEditor.Id;
}
if (textEditor == selectedTextEditor)
{
sessionData.SelectedTextEditor = textEditor.Id;
}
}

bool sessionDataSaved = false;

try
{
string sessionJson = JsonConvert.SerializeObject(sessionData, _encodingConverter);
ApplicationData.Current.LocalSettings.Values[SessionDataKey] = sessionJson;
IPropertySet settings = ApplicationData.Current.LocalSettings.Values;

if (!settings.TryGetValue(SessionDataKey, out object currentValue) || !string.Equals((string)currentValue, sessionJson, StringComparison.OrdinalIgnoreCase))
{
settings[SessionDataKey] = sessionJson;
sessionDataSaved = true;
}
}
catch (Exception ex)
{
LoggingService.LogError($"Failed to save session metadata: {ex.Message}");
return; // Failed to save the session - do not proceed to delete backup files
}

await DeleteOrphanedBackupFilesAsync(sessionData);
if (sessionDataSaved)
{
await DeleteOrphanedBackupFilesAsync(sessionData);
}

stopwatch.Stop();
if (sessionDataUpdateCount > 0)

if (sessionDataSaved)
{
LoggingService.LogInfo($"Successfully saved the current session with [{sessionDataUpdateCount}] new updates. Total time: {stopwatch.Elapsed.TotalMilliseconds} milliseconds.", consoleOnly: true);
LoggingService.LogInfo($"Successfully saved the current session. Total time: {stopwatch.Elapsed.TotalMilliseconds} milliseconds.", consoleOnly: true);
}

_semaphoreSlim.Release();
@@ -260,26 +267,29 @@ private async Task<TextEditor> RecoverTextEditorAsync(TextEditorSessionData text
BackupMetadata pending = textEditorData.Pending;
TextEditor textEditor;

if (sourceFile == null)
if (sourceFile == null) // Untitled.txt or file not found
{
textEditor = _notepadsCore.OpenNewTextEditor(
textEditorData.Id,
string.Empty,
null,
-1,
EditorSettingsService.EditorDefaultEncoding,
EditorSettingsService.EditorDefaultLineEnding,
false);

await ApplyChangesAsync(textEditor, pending ?? lastSaved);
if (lastSaved != null || pending != null)
{
textEditor = _notepadsCore.OpenNewTextEditor(textEditorData.Id);
await ApplyChangesAsync(textEditor, pending ?? lastSaved);
}
else
{
textEditor = null;
}
}
else
else if (lastSaved == null && pending == null) // File without pending changes
{
TextFile lastSavedContent = await FileSystemUtility.ReadFile(lastSaved.BackupFilePath);
textEditor = await _notepadsCore.OpenNewTextEditor(sourceFile, textEditorData.Id);
}
else // File with pending changes
{
TextFile textFile = await FileSystemUtility.ReadFile(lastSaved.BackupFilePath);

textEditor = _notepadsCore.OpenNewTextEditor(
textEditorData.Id,
lastSavedContent.Content,
textFile.Content,
sourceFile,
lastSaved.DateModified,
lastSaved.Encoding,
@@ -294,11 +304,6 @@ private async Task<TextEditor> RecoverTextEditorAsync(TextEditorSessionData text

private async Task ApplyChangesAsync(TextEditor textEditor, BackupMetadata backupMetadata)
{
if (backupMetadata == null)
{
return;
}

TextFile textFile = await FileSystemUtility.ReadFile(backupMetadata.BackupFilePath);
textEditor.Init(textFile, textEditor.EditingFile, resetOriginalSnapshot: false, isModified: true);
textEditor.TryChangeEncoding(backupMetadata.Encoding);

0 comments on commit cd1ba19

Please sign in to comment.
You can’t perform that action at this time.