Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
0x7c13 committed Aug 31, 2023
1 parent 5001f15 commit 6c47fee
Show file tree
Hide file tree
Showing 25 changed files with 147 additions and 161 deletions.
4 changes: 2 additions & 2 deletions src/Notepads/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,11 +313,11 @@ private static void ExtendViewIntoTitleBar()
// }
//}

//private static async Task UpdateJumpList()
//private static async Task UpdateJumpListAsync()
//{
// if (JumpListService.IsJumpListOutOfDate)
// {
// if (await JumpListService.UpdateJumpList())
// if (await JumpListService.UpdateJumpListAsync())
// {
// JumpListService.IsJumpListOutOfDate = false;
// }
Expand Down
6 changes: 2 additions & 4 deletions src/Notepads/Controls/TextEditor/ITextEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ void Init(TextFile textFile,

void ResetEditorState(TextEditorStateMetaData metadata, string newText = null);

Task ReloadFromEditingFile();

Task ReloadFromEditingFile(Encoding encoding);
Task ReloadFromEditingFileAsync(Encoding encoding = null);

LineEnding GetLineEnding();

Expand Down Expand Up @@ -116,7 +114,7 @@ void GetLineColumnSelection(

bool IsEditorEnabled();

Task SaveContentToFileAndUpdateEditorState(StorageFile file);
Task SaveContentToFileAndUpdateEditorStateAsync(StorageFile file);

string GetContentForSharing();

Expand Down
27 changes: 11 additions & 16 deletions src/Notepads/Controls/TextEditor/TextEditor.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ await Task.Run(async () =>
{
await Task.Delay(TimeSpan.FromSeconds(_fileStatusCheckerDelayInSec), cancellationToken);
LoggingService.LogInfo($"[{nameof(TextEditor)}] Checking file status for \"{EditingFile.Path}\".", consoleOnly: true);
await CheckAndUpdateFileStatus(cancellationToken);
await CheckAndUpdateFileStatusAsync(cancellationToken);
await Task.Delay(TimeSpan.FromSeconds(_fileStatusCheckerPollingRateInSec), cancellationToken);
}
}, cancellationToken);
Expand All @@ -406,7 +406,7 @@ public void StopCheckingFileStatus()
}
}

private async Task CheckAndUpdateFileStatus(CancellationToken cancellationToken)
private async Task CheckAndUpdateFileStatusAsync(CancellationToken cancellationToken)
{
if (EditingFile == null) return;

Expand All @@ -420,13 +420,13 @@ private async Task CheckAndUpdateFileStatus(CancellationToken cancellationToken)

FileModificationState? newState = null;

if (!await FileSystemUtility.FileExists(EditingFile))
if (!await FileSystemUtility.FileExistsAsync(EditingFile))
{
newState = FileModificationState.RenamedMovedOrDeleted;
}
else
{
newState = await FileSystemUtility.GetDateModified(EditingFile) != LastSavedSnapshot.DateModifiedFileTime ?
newState = await FileSystemUtility.GetDateModifiedAsync(EditingFile) != LastSavedSnapshot.DateModifiedFileTime ?
FileModificationState.Modified :
FileModificationState.Untouched;
}
Expand Down Expand Up @@ -486,16 +486,11 @@ public void Init(TextFile textFile, StorageFile file, bool resetLastSavedSnapsho
_loaded = true;
}

public async Task ReloadFromEditingFile()
{
await ReloadFromEditingFile(null);
}

public async Task ReloadFromEditingFile(Encoding encoding)
public async Task ReloadFromEditingFileAsync(Encoding encoding = null)
{
if (EditingFile != null)
{
var textFile = await FileSystemUtility.ReadFile(EditingFile, ignoreFileSizeLimit: false, encoding: encoding);
var textFile = await FileSystemUtility.ReadFileAsync(EditingFile, ignoreFileSizeLimit: false, encoding: encoding);
Init(textFile, EditingFile, clearUndoQueue: false);
LineEndingChanged?.Invoke(this, EventArgs.Empty);
EncodingChanged?.Invoke(this, EventArgs.Empty);
Expand Down Expand Up @@ -709,23 +704,23 @@ public bool IsEditorEnabled()
return TextEditorCore.IsEnabled;
}

public async Task SaveContentToFileAndUpdateEditorState(StorageFile file)
public async Task SaveContentToFileAndUpdateEditorStateAsync(StorageFile file)
{
if (Mode == TextEditorMode.DiffPreview) CloseSideBySideDiffViewer();
TextFile textFile = await SaveContentToFile(file); // Will throw if not succeeded
TextFile textFile = await SaveContentToFileAsync(file); // Will throw if not succeeded
FileModificationState = FileModificationState.Untouched;
Init(textFile, file, clearUndoQueue: false, resetText: false);
FileSaved?.Invoke(this, EventArgs.Empty);
StartCheckingFileStatusPeriodically();
}

private async Task<TextFile> SaveContentToFile(StorageFile file)
private async Task<TextFile> SaveContentToFileAsync(StorageFile file)
{
var text = TextEditorCore.GetText();
var encoding = RequestedEncoding ?? LastSavedSnapshot.Encoding;
var lineEnding = RequestedLineEnding ?? LastSavedSnapshot.LineEnding;
await FileSystemUtility.WriteToFile(LineEndingUtility.ApplyLineEnding(text, lineEnding), encoding, file); // Will throw if not succeeded
var newFileModifiedTime = await FileSystemUtility.GetDateModified(file);
await FileSystemUtility.WriteToFileAsync(LineEndingUtility.ApplyLineEnding(text, lineEnding), encoding, file); // Will throw if not succeeded
var newFileModifiedTime = await FileSystemUtility.GetDateModifiedAsync(file);
return new TextFile(text, encoding, lineEnding, newFileModifiedTime);
}

Expand Down
24 changes: 8 additions & 16 deletions src/Notepads/Controls/TextEditor/TextEditorContextFlyout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ public MenuFlyoutItem Cut
Key = VirtualKey.X,
IsEnabled = false,
});
_cut.Click += (sender, args) => { _textEditorCore.Document.Selection.Cut(); };
_cut.Click += (sender, args) => _textEditorCore.Document.Selection.Cut();
}
return _cut;
}
Expand Down Expand Up @@ -191,7 +191,7 @@ public MenuFlyoutItem Paste
Key = VirtualKey.V,
IsEnabled = false,
});
_paste.Click += async (sender, args) => { await _textEditorCore.PastePlainTextFromWindowsClipboard(null); };
_paste.Click += async (sender, args) => await _textEditorCore.PastePlainTextFromWindowsClipboardAsync(null);
}
return _paste;
}
Expand All @@ -210,7 +210,7 @@ public MenuFlyoutItem Undo
Key = VirtualKey.Z,
IsEnabled = false,
});
_undo.Click += (sender, args) => { _textEditorCore.Undo(); };
_undo.Click += (sender, args) => _textEditorCore.Undo();
}
return _undo;
}
Expand All @@ -230,7 +230,7 @@ public MenuFlyoutItem Redo
IsEnabled = false,
});
_redo.KeyboardAcceleratorTextOverride = "Ctrl+Shift+Z";
_redo.Click += (sender, args) => { _textEditorCore.Redo(); };
_redo.Click += (sender, args) => _textEditorCore.Redo();
}
return _redo;
}
Expand All @@ -249,10 +249,7 @@ public MenuFlyoutItem SelectAll
Key = VirtualKey.A,
IsEnabled = false,
});
_selectAll.Click += (sender, args) =>
{
_textEditorCore.Document.Selection.SetRange(0, Int32.MaxValue);
};
_selectAll.Click += (sender, args) => _textEditorCore.Document.Selection.SetRange(0, Int32.MaxValue);
}
return _selectAll;
}
Expand Down Expand Up @@ -307,10 +304,8 @@ public MenuFlyoutItem WebSearch
Key = VirtualKey.E,
IsEnabled = false,
});
_webSearch.Click += (sender, args) =>
{
_textEditorCore.SearchInWeb();
};
_webSearch.Click += async (sender, args) => await _textEditorCore.SearchInWebAsync();

return _webSearch;
}
}
Expand All @@ -322,10 +317,7 @@ public MenuFlyoutItem Share
if (_share == null)
{
_share = new MenuFlyoutItem { Icon = new SymbolIcon(Symbol.Share), Text = _resourceLoader.GetString("TextEditor_ContextFlyout_ShareButtonDisplayText") };
_share.Click += (sender, args) =>
{
Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();
};
_share.Click += (sender, args) => Windows.ApplicationModel.DataTransfer.DataTransferManager.ShowShareUI();
}
return _share;
}
Expand Down
3 changes: 2 additions & 1 deletion src/Notepads/Controls/TextEditor/TextEditorCore.WebSearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
using Windows.System;
using Notepads.Services;
using Notepads.Utilities;
using System.Threading.Tasks;

public partial class TextEditorCore
{
public async void SearchInWeb()
public async Task SearchInWebAsync()
{
try
{
Expand Down
6 changes: 3 additions & 3 deletions src/Notepads/Controls/TextEditor/TextEditorCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private KeyboardCommandHandler GetKeyboardCommandHandler()
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.Number0, (args) => ResetFontSizeToDefault()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.NumberPad0, (args) => ResetFontSizeToDefault()),
new KeyboardCommand<KeyRoutedEventArgs>(VirtualKey.F5, (args) => InsertDateTimeString()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.E, (args) => SearchInWeb()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.E, async (args) => await SearchInWebAsync()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.D, (args) => DuplicateText()),
new KeyboardCommand<KeyRoutedEventArgs>(true, false, false, VirtualKey.J, (args) => JoinText()),
new KeyboardCommand<KeyRoutedEventArgs>(VirtualKey.Tab, (args) => AddIndentation(AppSettingsService.EditorDefaultTabIndents)),
Expand Down Expand Up @@ -354,7 +354,7 @@ private void OnPointerWheelChanged(object sender, PointerRoutedEventArgs args)

private async void OnPaste(object sender, TextControlPasteEventArgs args)
{
await PastePlainTextFromWindowsClipboard(args);
await PastePlainTextFromWindowsClipboardAsync(args);
}

private void OnCopyingToClipboard(RichEditBox sender, TextControlCopyingToClipboardEventArgs args)
Expand Down Expand Up @@ -655,7 +655,7 @@ public void SmartlyTrimTextSelection()
Document.Selection.SetRange(startPosition + startOffset, endPosition - endOffset);
}

public async Task PastePlainTextFromWindowsClipboard(TextControlPasteEventArgs args)
public async Task PastePlainTextFromWindowsClipboardAsync(TextControlPasteEventArgs args)
{
if (args != null)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Notepads/Core/INotepadsCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface INotepadsCore
event EventHandler<IReadOnlyList<IStorageItem>> StorageItemsDropped;
event KeyEventHandler TextEditorKeyDown;

Task<ITextEditor> CreateTextEditor(
Task<ITextEditor> CreateTextEditorAsync(
Guid id,
StorageFile file,
Encoding encoding = null,
Expand All @@ -50,7 +50,7 @@ ITextEditor CreateTextEditor(

void OpenTextEditors(ITextEditor[] editors, Guid? selectedEditorId = null);

Task SaveContentToFileAndUpdateEditorState(ITextEditor textEditor, StorageFile file);
Task SaveContentToFileAndUpdateEditorStateAsync(ITextEditor textEditor, StorageFile file);

void DeleteTextEditor(ITextEditor textEditor);

Expand Down
8 changes: 4 additions & 4 deletions src/Notepads/Core/NotepadsCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ public void OpenTextEditors(ITextEditor[] editors, Guid? selectedEditorId = null
}
}

public async Task<ITextEditor> CreateTextEditor(
public async Task<ITextEditor> CreateTextEditorAsync(
Guid id,
StorageFile file,
Encoding encoding = null,
bool ignoreFileSizeLimit = false)
{
var textFile = await FileSystemUtility.ReadFile(file, ignoreFileSizeLimit, encoding);
var textFile = await FileSystemUtility.ReadFileAsync(file, ignoreFileSizeLimit, encoding);
return CreateTextEditor(id, textFile, file, file.Name);
}

Expand Down Expand Up @@ -207,9 +207,9 @@ public ITextEditor CreateTextEditor(
return textEditor;
}

public async Task SaveContentToFileAndUpdateEditorState(ITextEditor textEditor, StorageFile file)
public async Task SaveContentToFileAndUpdateEditorStateAsync(ITextEditor textEditor, StorageFile file)
{
await textEditor.SaveContentToFileAndUpdateEditorState(file); // Will throw if not succeeded
await textEditor.SaveContentToFileAndUpdateEditorStateAsync(file); // Will throw if not succeeded
MarkTextEditorSetSaved(textEditor);
TextEditorSaved?.Invoke(this, textEditor);
}
Expand Down
20 changes: 10 additions & 10 deletions src/Notepads/Core/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public async Task SaveSessionAsync(Action actionAfterSaving = null)
{
try
{
var textEditorSessionData = await GetTextEditorSessionData(textEditor);
var textEditorSessionData = await GetTextEditorSessionDataAsync(textEditor);

if (textEditorSessionData == null) continue;

Expand Down Expand Up @@ -228,7 +228,7 @@ public async Task SaveSessionAsync(Action actionAfterSaving = null)
_semaphoreSlim.Release();
}

private async Task<TextEditorSessionDataV1> GetTextEditorSessionData(ITextEditor textEditor)
private async Task<TextEditorSessionDataV1> GetTextEditorSessionDataAsync(ITextEditor textEditor)
{
if (_sessionDataCache.TryGetValue(textEditor.Id, out TextEditorSessionDataV1 textEditorSessionData))
{
Expand All @@ -238,7 +238,7 @@ private async Task<TextEditorSessionDataV1> GetTextEditorSessionData(ITextEditor
}
else // Text content has been changed or editor has not backed up yet
{
textEditorSessionData = await BuildTextEditorSessionData(textEditor);
textEditorSessionData = await BuildTextEditorSessionDataAsync(textEditor);

if (textEditorSessionData == null)
{
Expand All @@ -251,7 +251,7 @@ private async Task<TextEditorSessionDataV1> GetTextEditorSessionData(ITextEditor
return textEditorSessionData;
}

private async Task<TextEditorSessionDataV1> BuildTextEditorSessionData(ITextEditor textEditor)
private async Task<TextEditorSessionDataV1> BuildTextEditorSessionDataAsync(ITextEditor textEditor)
{
TextEditorSessionDataV1 textEditorData = new TextEditorSessionDataV1
{
Expand All @@ -262,7 +262,7 @@ private async Task<TextEditorSessionDataV1> BuildTextEditorSessionData(ITextEdit
{
// Add the opened file to FutureAccessList so we can access it next launch
var futureAccessToken = ToToken(textEditor.Id);
await FutureAccessListUtility.TryAddOrReplaceTokenInFutureAccessList(futureAccessToken, textEditor.EditingFile);
await FutureAccessListUtility.TryAddOrReplaceTokenInFutureAccessListAsync(futureAccessToken, textEditor.EditingFile);
textEditorData.EditingFileFutureAccessToken = futureAccessToken;
textEditorData.EditingFileName = textEditor.EditingFileName;
textEditorData.EditingFilePath = textEditor.EditingFilePath;
Expand Down Expand Up @@ -392,7 +392,7 @@ private async Task<ITextEditor> RecoverTextEditorAsync(TextEditorSessionDataV1 e

if (editorSessionData.EditingFileFutureAccessToken != null)
{
editingFile = await FutureAccessListUtility.GetFileFromFutureAccessList(editorSessionData.EditingFileFutureAccessToken);
editingFile = await FutureAccessListUtility.GetFileFromFutureAccessListAsync(editorSessionData.EditingFileFutureAccessToken);
}

string lastSavedFile = editorSessionData.LastSavedBackupFilePath;
Expand All @@ -407,7 +407,7 @@ private async Task<ITextEditor> RecoverTextEditorAsync(TextEditorSessionDataV1 e
else if (editingFile != null && lastSavedFile == null && pendingFile == null) // File without pending changes
{
var encoding = EncodingUtility.GetEncodingByName(editorSessionData.StateMetaData.LastSavedEncoding);
textEditor = await _notepadsCore.CreateTextEditor(editorSessionData.Id, editingFile, encoding: encoding, ignoreFileSizeLimit: true);
textEditor = await _notepadsCore.CreateTextEditorAsync(editorSessionData.Id, editingFile, encoding: encoding, ignoreFileSizeLimit: true);
textEditor.ResetEditorState(editorSessionData.StateMetaData);
}
else // File with pending changes
Expand All @@ -417,7 +417,7 @@ private async Task<ITextEditor> RecoverTextEditorAsync(TextEditorSessionDataV1 e

if (lastSavedFile != null)
{
TextFile lastSavedTextFile = await FileSystemUtility.ReadFile(lastSavedFile, ignoreFileSizeLimit: true,
TextFile lastSavedTextFile = await FileSystemUtility.ReadFileAsync(lastSavedFile, ignoreFileSizeLimit: true,
EncodingUtility.GetEncodingByName(editorSessionData.StateMetaData.LastSavedEncoding));
lastSavedText = lastSavedTextFile.Content;
}
Expand All @@ -436,7 +436,7 @@ private async Task<ITextEditor> RecoverTextEditorAsync(TextEditorSessionDataV1 e

if (pendingFile != null)
{
TextFile pendingTextFile = await FileSystemUtility.ReadFile(pendingFile,
TextFile pendingTextFile = await FileSystemUtility.ReadFileAsync(pendingFile,
ignoreFileSizeLimit: true,
EncodingUtility.GetEncodingByName(editorSessionData.StateMetaData.LastSavedEncoding));
pendingText = pendingTextFile.Content;
Expand All @@ -452,7 +452,7 @@ private static async Task<bool> BackupTextAsync(string text, Encoding encoding,
{
try
{
await FileSystemUtility.WriteToFile(LineEndingUtility.ApplyLineEnding(text, lineEnding), encoding, file);
await FileSystemUtility.WriteToFileAsync(LineEndingUtility.ApplyLineEnding(text, lineEnding), encoding, file);
return true;
}
catch (Exception ex)
Expand Down
Loading

0 comments on commit 6c47fee

Please sign in to comment.