Skip to content

Commit

Permalink
Merge pull request #353 from TheJoeFin/Packaged-File-Issues
Browse files Browse the repository at this point in the history
Packaged file issues
  • Loading branch information
TheJoeFin authored Oct 21, 2023
2 parents bfcc9ba + 6102c9b commit f8b6ac0
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 69 deletions.
2 changes: 1 addition & 1 deletion Text-Grab-Package/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<Identity
Name="40087JoeFinApps.TextGrab"
Publisher="CN=153F3B0F-BA3D-4964-8098-71AC78A1DF6A"
Version="4.1.3.0" />
Version="4.1.4.0" />

<Properties>
<DisplayName>Text Grab</DisplayName>
Expand Down
39 changes: 38 additions & 1 deletion Text-Grab/Services/HistoryService.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using System;
using Humanizer;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using Text_Grab.Models;
using Text_Grab.Properties;
Expand Down Expand Up @@ -102,6 +105,40 @@ public async Task LoadHistories()
HistoryWithImage = await LoadHistory(nameof(HistoryWithImage));
}

public async Task PopulateMenuItemWithRecentGrabs(MenuItem recentGrabsMenuItem)
{
List<HistoryInfo> grabsHistory = GetRecentGrabs();
grabsHistory = grabsHistory.OrderByDescending(x => x.CaptureDateTime).ToList();

recentGrabsMenuItem.Items.Clear();

if (grabsHistory.Count < 1)
{
recentGrabsMenuItem.IsEnabled = false;
return;
}

string historyBasePath = await FileUtilities.GetPathToHistory();

foreach (HistoryInfo history in grabsHistory)
{
string imageFullPath = Path.Combine(historyBasePath, history.ImagePath);
if (string.IsNullOrWhiteSpace(history.ImagePath) || !File.Exists(imageFullPath))
continue;

MenuItem menuItem = new();
menuItem.Click += (object sender, RoutedEventArgs args) =>
{
GrabFrame grabFrame = new(history);
try { grabFrame.Show(); }
catch { menuItem.IsEnabled = false; }
};

menuItem.Header = $"{history.CaptureDateTime.Humanize()} | {history.TextContent.MakeStringSingleLine().Truncate(20)}";
recentGrabsMenuItem.Items.Add(menuItem);
}
}

public void SaveToHistory(GrabFrame grabFrameToSave)
{
if (!Settings.Default.UseHistory)
Expand Down
32 changes: 28 additions & 4 deletions Text-Grab/Utilities/FileUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Windows.Media.Streaming.Adaptive;
using Windows.Storage;
using Windows.Storage.Streams;

Expand Down Expand Up @@ -71,6 +72,18 @@ public static string GetPathToLocalFile(string imageRelativePath)

return Path.Combine(dirPath, imageRelativePath);
}

public async static Task<string> GetPathToHistory()
{
if (ImplementAppOptions.IsPackaged())
{
StorageFolder historyFolder = await GetStorageFolderPackaged("", FileStorageKind.WithHistory);
return historyFolder.Path;
}

return GetFolderPathUnpackaged("", FileStorageKind.WithHistory);
}

public static Task<string> GetTextFileAsync(string fileName, FileStorageKind storageKind)
{
if (ImplementAppOptions.IsPackaged())
Expand Down Expand Up @@ -122,10 +135,13 @@ public static Task<bool> SaveTextFile(string textContent, string filename, FileS
}
private async static Task<string> GetTextFilePackaged(string fileName, FileStorageKind storageKind)
{
StorageFolder folder = await GetStorageFolderPackaged(fileName, storageKind);

try
{
StorageFolder folder = await GetStorageFolderPackaged(fileName, storageKind);

if (storageKind == FileStorageKind.Absolute)
fileName = Path.GetFileName(fileName);

StorageFile file = await folder.GetFileAsync(fileName);
using Stream stream = await file.OpenStreamForReadAsync();
StreamReader streamReader = new(stream);
Expand Down Expand Up @@ -182,11 +198,15 @@ private static async Task<StorageFolder> GetStorageFolderPackaged(string fileNam
switch (storageKind)
{
case FileStorageKind.Absolute:
return await StorageFolder.GetFolderFromPathAsync(fileName);
string? dirPath = Path.GetDirectoryName(fileName);
StorageFolder absoluteFolder = await StorageFolder.GetFolderFromPathAsync(dirPath);
return absoluteFolder;
case FileStorageKind.WithExe:
return ApplicationData.Current.LocalFolder;
case FileStorageKind.WithHistory:
return await ApplicationData.Current.LocalFolder.CreateFolderAsync("history", CreationCollisionOption.OpenIfExists);
ApplicationData currentAppData = ApplicationData.Current;
StorageFolder storageFolder = await currentAppData.LocalFolder.CreateFolderAsync("history", CreationCollisionOption.OpenIfExists);
return storageFolder;
default:
break;
}
Expand Down Expand Up @@ -232,6 +252,10 @@ private static async Task<bool> SaveTextFilePackaged(string textContent, string
try
{
StorageFolder storageFolder = await GetStorageFolderPackaged(filename, storageKind);

if (storageKind == FileStorageKind.Absolute)
filename = Path.GetFileName(filename);

StorageFile textFile = await storageFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
using IRandomAccessStream randomAccessStream = await textFile.OpenAsync(FileAccessMode.ReadWrite);
DataWriter dataWriter = new(randomAccessStream);
Expand Down
2 changes: 1 addition & 1 deletion Text-Grab/Views/EditTextWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@
<MenuItem
x:Name="CloseMenuItem"
Click="CloseMenuItem_Click"
Header="_Exit"
Header="Close"
InputGestureText="Alt + F4" />
</MenuItem>
<MenuItem Header="_Edit">
Expand Down
35 changes: 2 additions & 33 deletions Text-Grab/Views/EditTextWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -970,37 +970,6 @@ private void ListFilesMenuItem_Click(object sender, RoutedEventArgs e)
}
}

private void LoadRecentGrabsHistory()
{
List<HistoryInfo> grabsHistory = Singleton<HistoryService>.Instance.GetRecentGrabs();
grabsHistory = grabsHistory.OrderByDescending(x => x.CaptureDateTime).ToList();

OpenRecentGrabsMenuItem.Items.Clear();

if (grabsHistory.Count < 1)
{
OpenRecentGrabsMenuItem.IsEnabled = false;
return;
}

foreach (HistoryInfo history in grabsHistory)
{
if (string.IsNullOrWhiteSpace(history.ImagePath) || !File.Exists(history.ImagePath))
continue;

MenuItem menuItem = new();
menuItem.Click += (object sender, RoutedEventArgs args) =>
{
GrabFrame grabFrame = new(history);
try { grabFrame.Show(); }
catch { menuItem.IsEnabled = false; }
};

menuItem.Header = $"{history.CaptureDateTime.Humanize()} | {history.TextContent.MakeStringSingleLine().Truncate(20)}";
OpenRecentGrabsMenuItem.Items.Add(menuItem);
}
}

private void LoadRecentTextHistory()
{
List<HistoryInfo> grabsHistories = Singleton<HistoryService>.Instance.GetEditWindows();
Expand Down Expand Up @@ -1066,10 +1035,10 @@ private void MakeQrCodeExecuted(object sender, ExecutedRoutedEventArgs e)
window.Show();
}

private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
private async void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
{
LoadRecentTextHistory();
LoadRecentGrabsHistory();
await Singleton<HistoryService>.Instance.PopulateMenuItemWithRecentGrabs(OpenRecentGrabsMenuItem);
}

private void MoveLineDown(object? sender, ExecutedRoutedEventArgs? e)
Expand Down
30 changes: 2 additions & 28 deletions Text-Grab/Views/GrabFrame.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1474,35 +1474,9 @@ private void LoadOcrLanguages()
isLanguageBoxLoaded = true;
}

private void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
private async void MenuItem_SubmenuOpened(object sender, RoutedEventArgs e)
{
List<HistoryInfo> grabsHistories = Singleton<HistoryService>.Instance.GetRecentGrabs();
grabsHistories = grabsHistories.OrderByDescending(x => x.CaptureDateTime).ToList();

OpenRecentGrabsMenuItem.Items.Clear();

if (grabsHistories.Count < 1)
{
OpenRecentGrabsMenuItem.IsEnabled = false;
return;
}

foreach (HistoryInfo history in grabsHistories)
{
if (string.IsNullOrWhiteSpace(history.ImagePath) || !File.Exists(history.ImagePath))
continue;

MenuItem menuItem = new();
menuItem.Click += (object sender, RoutedEventArgs args) =>
{
GrabFrame grabFrame = new(history);
try { grabFrame.Show(); }
catch { menuItem.IsEnabled = false; }
};

menuItem.Header = $"{history.CaptureDateTime.Humanize()} | {history.TextContent.MakeStringSingleLine().Truncate(20)}";
OpenRecentGrabsMenuItem.Items.Add(menuItem);
}
await Singleton<HistoryService>.Instance.PopulateMenuItemWithRecentGrabs(OpenRecentGrabsMenuItem);
}

private void MergeWordBordersExecuted(object sender, ExecutedRoutedEventArgs? e = null)
Expand Down
2 changes: 1 addition & 1 deletion Text-Grab/Views/SettingsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
<TextBlock
VerticalAlignment="Center"
Style="{StaticResource TextBodyNormal}"
Text="Version 4.1.3" />
Text="Version 4.1.4" />

<controls:CollapsibleButton
x:Name="AboutBTN"
Expand Down

0 comments on commit f8b6ac0

Please sign in to comment.