Skip to content

Commit

Permalink
refactoring changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aayushpandya committed Apr 4, 2023
1 parent 622594c commit ff46f12
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 34 deletions.
31 changes: 31 additions & 0 deletions src/Notepads/Core/BackupSessionUtility.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace Notepads.Utilities
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Toolkit.Uwp.Helpers;
using Notepads.Core;
using Notepads.Services;
using Windows.Storage;
using Microsoft.AppCenter.Analytics;
internal static class BackupSessionUtility
{
public static async Task<StorageFolder> GetBackupFolderAsync(string backupFolderName)
{
return await FileSystemUtility.GetOrCreateAppFolder(backupFolderName);
}

public static async Task<IReadOnlyList<StorageFile>> GetAllBackupFilesAsync(string backupFolderName)
{
StorageFolder backupFolder = await GetBackupFolderAsync(backupFolderName);
return await backupFolder.GetFilesAsync();
}

public static async Task<StorageFile> CreateNewFileInBackupFolderAsync(string fileName, CreationCollisionOption collisionOption, string backupFolderName)
{
StorageFolder backupFolder = await GetBackupFolderAsync(backupFolderName);
return await backupFolder.CreateFileAsync(fileName, collisionOption);
}
}
}
6 changes: 3 additions & 3 deletions src/Notepads/Core/SessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private async Task<TextEditorSessionDataV1> BuildTextEditorSessionData(ITextEdit
if (textEditor.EditingFile != null)
{
// Persist the last save known to the app, which might not be up-to-date (if the file was modified outside the app)
var lastSavedBackupFile = await SessionUtility.CreateNewFileInBackupFolderAsync(
var lastSavedBackupFile = await BackupSessionUtility.CreateNewFileInBackupFolderAsync(
ToToken(textEditor.Id) + "-LastSaved",
CreationCollisionOption.ReplaceExisting,
_backupFolderName);
Expand All @@ -293,7 +293,7 @@ private async Task<TextEditorSessionDataV1> BuildTextEditorSessionData(ITextEdit
!string.Equals(textEditor.LastSavedSnapshot.Content, textEditor.GetText()))
{
// Persist pending changes relative to the last save
var pendingBackupFile = await SessionUtility.CreateNewFileInBackupFolderAsync(
var pendingBackupFile = await BackupSessionUtility.CreateNewFileInBackupFolderAsync(
ToToken(textEditor.Id) + "-Pending",
CreationCollisionOption.ReplaceExisting,
_backupFolderName);
Expand Down Expand Up @@ -470,7 +470,7 @@ private async Task DeleteOrphanedBackupFilesAsync(NotepadsSessionDataV1 sessionD
.Where(path => path != null)
.ToHashSet(StringComparer.OrdinalIgnoreCase);

foreach (StorageFile backupFile in await SessionUtility.GetAllBackupFilesAsync(_backupFolderName))
foreach (StorageFile backupFile in await BackupSessionUtility.GetAllBackupFilesAsync(_backupFolderName))
{
if (!backupPaths.Contains(backupFile.Path))
{
Expand Down
1 change: 1 addition & 0 deletions src/Notepads/Notepads.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
<Compile Include="Controls\TextEditor\TextEditorCore.LineHighlighter.cs" />
<Compile Include="Controls\TextEditor\TextEditorCore.LineNumbers.cs" />
<Compile Include="Controls\TextEditor\TextEditorCore.MoveText.cs" />
<Compile Include="Core\BackupSessionUtility.cs" />
<Compile Include="Extensions\DispatcherExtensions.cs" />
<Compile Include="Extensions\ScrollViewerExtensions.cs" />
<Compile Include="Utilities\FutureAccessListUtility.cs" />
Expand Down
14 changes: 0 additions & 14 deletions src/Notepads/Services/MRUService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,19 +62,5 @@ public static async Task<IList<IStorageItem>> Get(int top = 10)
return items;
}

public static void ClearAll()
{
try
{
Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Clear();
}
catch (Exception ex)
{
Analytics.TrackEvent("MRUService_FailedToClearMRU", new Dictionary<string, string>()
{
{ "Exception", ex.ToString() }
});
}
}
}
}
18 changes: 2 additions & 16 deletions src/Notepads/Utilities/SessionUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,6 @@ public static ISessionManager GetSessionManager(INotepadsCore notepadCore, strin
return sessionManager;
}

public static async Task<StorageFolder> GetBackupFolderAsync(string backupFolderName)
{
return await FileSystemUtility.GetOrCreateAppFolder(backupFolderName);
}

public static async Task<IReadOnlyList<StorageFile>> GetAllBackupFilesAsync(string backupFolderName)
{
StorageFolder backupFolder = await GetBackupFolderAsync(backupFolderName);
return await backupFolder.GetFilesAsync();
}

public static async Task<string> GetSerializedSessionMetaDataAsync(string sessionMetaDataFileName)
{
try
Expand Down Expand Up @@ -108,10 +97,7 @@ public static async Task DeleteSerializedSessionMetaDataAsync(string sessionMeta
}
}

public static async Task<StorageFile> CreateNewFileInBackupFolderAsync(string fileName, CreationCollisionOption collisionOption, string backupFolderName)
{
StorageFolder backupFolder = await GetBackupFolderAsync(backupFolderName);
return await backupFolder.CreateFileAsync(fileName, collisionOption);
}

}

}
19 changes: 18 additions & 1 deletion src/Notepads/Views/MainPage/NotepadsMainPage.MainMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Media;
using Notepads.Services;
using System;
using Microsoft.AppCenter.Analytics;

public sealed partial class NotepadsMainPage
{
Expand Down Expand Up @@ -156,7 +158,7 @@ private async Task BuildOpenRecentButtonSubItems()

clearRecentlyOpenedSubItem.Click += async (sender, args) =>
{
MRUService.ClearAll();
ClearAll();
await BuildOpenRecentButtonSubItems();
};
openRecentSubItem.Items?.Add(clearRecentlyOpenedSubItem);
Expand All @@ -169,5 +171,20 @@ private async Task BuildOpenRecentButtonSubItems()
MainMenuButtonFlyout.Items.Insert(indexToInsert, openRecentSubItem);
}
}
private static void ClearAll()
{
try
{
Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList.Clear();
}
catch (Exception ex)
{
Analytics.TrackEvent("MRUService_FailedToClearMRU", new Dictionary<string, string>()
{
{ "Exception", ex.ToString() }
});
}
}
}

}

0 comments on commit ff46f12

Please sign in to comment.