Skip to content

Commit

Permalink
fixes for #1 and #2 + UI improvements :D
Browse files Browse the repository at this point in the history
  • Loading branch information
lochidev committed Jul 13, 2023
1 parent a80926b commit e182e64
Show file tree
Hide file tree
Showing 9 changed files with 108 additions and 62 deletions.
2 changes: 0 additions & 2 deletions MauiApp1/MauiProgram.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using CommunityToolkit.Maui.Storage;
using MauiApp1.Models;
using MauiApp1.Services;
using Microsoft.Extensions.Logging;
using MudBlazor.Services;

namespace MauiApp1;
Expand All @@ -23,7 +22,6 @@ public static MauiApp CreateMauiApp()
builder.Services.AddMudServices();
#if DEBUG
builder.Services.AddBlazorWebViewDeveloperTools();
builder.Logging.AddDebug();
#endif
#if ANDROID
builder.Services.AddSingleton<ICheckIfActivated, CheckIfActivated>();
Expand Down
10 changes: 10 additions & 0 deletions MauiApp1/Models/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MauiApp1.Models
{
internal static class AppSettings
{
public static List<string> supportedList = new() { "echo", "date", "clipboard", "random" };
public static string OldDictPath = Path.Combine(FileSystem.Current.CacheDirectory, "keywords.json");
public static string DictPath = Path.Combine(FileSystem.Current.AppDataDirectory, "keywords.json");
public static string GlobalVarsPath = Path.Combine(FileSystem.Current.AppDataDirectory, "global.json");
}
}
2 changes: 1 addition & 1 deletion MauiApp1/Models/DictWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace MauiApp1.Models
public class DictWrapper
{
[JsonPropertyName("globalVars")]
public List<Var> global_vars { get; set; }
public List<Var> global_vars { get; set; }
[JsonPropertyName("matches")]
public List<Match> matches { get; set; }
}
Expand Down
74 changes: 49 additions & 25 deletions MauiApp1/Pages/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
</div>
<MudText class="mb-3">Keywords</MudText>
<div class="d-flex">
<MudTextField class="mr-1" Placeholder="t:omw" @bind-Value="currentMatch.Trigger" Label="Key" Variant="Variant.Outlined"></MudTextField>
<MudTextField class="mr-1" Placeholder="On my way" @bind-Value="currentMatch.Replace" Label="Value" Variant="Variant.Outlined"></MudTextField>
<MudTextField Lines="2" class="mr-1" @bind-Value="currentMatch.Trigger" Label="Key" Variant="Variant.Outlined"></MudTextField>
<MudTextField Lines="2" class="mr-1" @bind-Value="currentMatch.Replace" Label="Value" Variant="Variant.Outlined"></MudTextField>
<MudButton @onclick="async () => await AddItemAsync(currentMatch)" Color="Color.Primary" Variant="Variant.Text">Add</MudButton>
</div>
<MudExpansionPanels Elevation="0" class="mt-2">
Expand All @@ -53,12 +53,22 @@
<MudDivider class="mt-2" />
@if (dict is not null)
{
@foreach (var item in dict)
@foreach (var item in dict.Take(dict.Count < lazyLoadIndex ? dict.Count : lazyLoadIndex))
{
<div class="d-flex mt-2">
<MudTextField class="mr-1" Disabled="true" Value="item.Key" Label="Key" Variant="Variant.Outlined"></MudTextField>
<MudTextField class="mr-1" Disabled="true" Value="item.Value.Replace" Variant="Variant.Outlined"></MudTextField>
<MudButton @onclick="async () => await RemoveItemAsync(item.Value)" Color="Color.Error" Variant="Variant.Text">Delete</MudButton>
<MudPaper class="border-solid border-2 mud-border-primary pa-2 mt-2">
<div class="d-flex">
<MudText class="mr-10" Typo="Typo.h6" Align="Align.Start">@item.Key</MudText>
<MudButton @onclick="() => EditItem(item)" Color="Color.Primary" Variant="Variant.Text">Edit</MudButton>
<MudButton @onclick="async () => await RemoveItemAsync(item.Value)" Color="Color.Error" Variant="Variant.Text">Delete</MudButton>
</div>
<MudText class="mr-1" Typo="Typo.h6" Align="Align.Start">@item.Value.Replace</MudText>
</MudPaper>
}
@if (dict.Count > lazyLoadIndex){
<div class="d-flex justify-center mt-3">
<MudButton
@onclick="AdjustLazyLoad" Color="Color.Primary" Variant="Variant.Filled">
Load more</MudButton>
</div>
}

Expand Down Expand Up @@ -174,24 +184,21 @@
bool canTextExpand = false;
DateTime? date;
TimeSpan? timeSpan;
Match currentMatch = new() { Replace = string.Empty, Trigger = string.Empty, Vars = new() { } };
Match currentMatch = new() { Replace = "On my way", Trigger = "t:omw", Vars = new() { } };
private Dictionary<string, Match> dict;
private static List<string> supportedList = new() { "echo", "date", "clipboard", "random" };
private string OldDictPath = Path.Combine(FileSystem.Current.CacheDirectory, "keywords.json");
private string DictPath = Path.Combine(FileSystem.Current.AppDataDirectory, "keywords.json");
private string GlobalVarsPath = Path.Combine(FileSystem.Current.AppDataDirectory, "global.json");
private List<Var> globalVars = new();
private Var currentVar = new() { };
private Params currentParam = new() { };
private int lazyLoadIndex = 100;
protected override async Task OnInitializedAsync()
{
await base.OnInitializedAsync();
textList.Add("");
textList.Add("");
canTextExpand = acService.IsActivated();
if (File.Exists(OldDictPath))
if (File.Exists(AppSettings.OldDictPath))
{
var stream = File.OpenRead(OldDictPath);
var stream = File.OpenRead(AppSettings.OldDictPath);
dict = JsonSerializer.Deserialize<Dictionary<string, Match>>(stream);
if (canTextExpand)
{
Expand All @@ -201,18 +208,18 @@
}
}
await stream.DisposeAsync();
var backupFn = DictPath + ".oldbackup";
File.Copy(OldDictPath, backupFn);
var backupFn = AppSettings.DictPath + ".oldbackup";
File.Copy(AppSettings.OldDictPath, backupFn);
await SaveDictAsync();
if (File.Exists(backupFn))
{
File.Delete(OldDictPath);
File.Delete(AppSettings.OldDictPath);
await dialogService.DisplayConfirmAsync("Migrated successfully!", "All your data in cache has been moved to app data directory instead successfully!");
}
}
if (File.Exists(DictPath))
if (File.Exists(AppSettings.DictPath))
{
using var stream = File.OpenRead(DictPath);
using var stream = File.OpenRead(AppSettings.DictPath);
dict = JsonSerializer.Deserialize<Dictionary<string, Match>>(stream);
if (canTextExpand)
{
Expand All @@ -224,9 +231,9 @@
}
else
dict = new();
if (File.Exists(GlobalVarsPath))
if (File.Exists(AppSettings.GlobalVarsPath))
{
using var stream = File.OpenRead(GlobalVarsPath);
using var stream = File.OpenRead(AppSettings.GlobalVarsPath);
globalVars = JsonSerializer.Deserialize<List<Var>>(stream);
if (canTextExpand)
{
Expand Down Expand Up @@ -273,7 +280,7 @@
{
if (x.Type is not null)
{
if (!supportedList.Contains(x.Type))
if (!AppSettings.supportedList.Contains(x.Type))
{
skips++;
notSupported = true;
Expand Down Expand Up @@ -302,7 +309,7 @@
{
globalVars.AddRange(localDict.global_vars);
var str = JsonSerializer.Serialize(globalVars);
await File.WriteAllTextAsync(GlobalVarsPath, str);
await File.WriteAllTextAsync(AppSettings.GlobalVarsPath, str);
WeakReferenceMessenger.Default.Send(new AcGlobalsMessage(globalVars));
}
await dialogService.DisplayConfirmAsync("Import successful!", $"Successfully imported keywords! Skipped: {skips} unsupported variable types. Make sure to save your work!");
Expand All @@ -326,6 +333,7 @@
};
foreach (var item in dict)
{
item.Value.Replace = item.Value.Replace.Replace("\\n", Environment.NewLine);
dictWrapper.matches.Add(item.Value);
}
if (await dialogService.DisplayConfirmAsync("Choose format!", "Select yml if this is an espanso config", "Json", "Yml"))
Expand Down Expand Up @@ -375,7 +383,8 @@
}
currentVar.Params = currentParam;
copy.Vars.Add(currentVar);
dict[copy.Trigger] = copy;
dict[copy.Trigger] = new(copy); // need another copy :(
copy.Replace = copy.Replace.Replace("\\n", Environment.NewLine);
SendMessage("Add", copy);
}
else
Expand Down Expand Up @@ -412,7 +421,7 @@
try
{
var str = JsonSerializer.Serialize(dict);
await File.WriteAllTextAsync(DictPath, str);
await File.WriteAllTextAsync(AppSettings.DictPath, str);
await dialogService.DisplayConfirmAsync("Success!", "File saved successfully to app data directory. Make sure to backup your data through the export feature!");
}
catch (Exception e)
Expand Down Expand Up @@ -552,5 +561,20 @@
format = format.Replace("%r", "hh:mm:ss tt");
return format;
}
private void EditItem(KeyValuePair<string, Match> item){
currentMatch = item.Value;
}
private void AdjustLazyLoad()
{
var diff = dict.Count - lazyLoadIndex;
if(diff > 100)
{
lazyLoadIndex += 100;
}
else
{
lazyLoadIndex += diff;
}
}
}

3 changes: 1 addition & 2 deletions MauiApp1/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using Android.App;
using Android.Content.PM;
using Android.OS;

namespace MauiApp1;

[Activity(Theme = "@style/Maui.SplashTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)]
public class MainActivity : MauiAppCompatActivity
{

}
16 changes: 8 additions & 8 deletions MauiApp1/Platforms/Android/MainApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace MauiApp1;
[Application]
public class MainApplication : MauiApplication
{
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}
public MainApplication(IntPtr handle, JniHandleOwnership ownership)
: base(handle, ownership)
{
}

protected override MauiApp CreateMauiApp()
{
return MauiProgram.CreateMauiApp();
}
protected override MauiApp CreateMauiApp()
{
return MauiProgram.CreateMauiApp();
}
}
11 changes: 4 additions & 7 deletions MauiApp1/Platforms/Android/Services/CheckIfActivated.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using Android.AccessibilityServices;
using Android;
using Android.AccessibilityServices;
using Android.Content;
using Android.Content.PM;
using Android.Views.Accessibility;
using MauiApp1.Models;
using Android.Provider;
using Microsoft.Maui.ApplicationModel;
using AndroidX.Activity.Result.Contract;
using Android.Views.Accessibility;
using AndroidX.Core.App;
using AndroidX.Core.Content;
using CommunityToolkit.Maui.Core;
using Android;
using MauiApp1.Models;

namespace MauiApp1.Services
{
Expand Down
37 changes: 28 additions & 9 deletions MauiApp1/Platforms/Android/Services/MyAccessibilityService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
using Android.Views.Accessibility;
using CommunityToolkit.Mvvm.Messaging;
using MauiApp1.Models;
using System.Collections.Concurrent;
using System.Security.Cryptography;
using System.Text.Json;

[Service(Exported = false, Label = "TextToolsPro", Permission = Manifest.Permission.BindAccessibilityService)]
[IntentFilter(new[] { "android.accessibilityservice.AccessibilityService" })]
[MetaData("android.accessibilityservice", Resource = "@xml/accessibility_service")]
public class MyAccessibilityService : AccessibilityService
{
private ConcurrentDictionary<string, Match> dict;
private Dictionary<string, Match> dict;
private List<Var> globals;

public override void OnCreate()
Expand All @@ -27,14 +27,15 @@ public override void OnCreate()
if (cmd == "Add")
{
if (!(string.IsNullOrEmpty(item.Trigger) || string.IsNullOrEmpty(item.Replace)))
{
dict.AddOrUpdate(item.Trigger, item,
(key, oldValue) => item);
{
//dict.AddOrUpdate(item.Trigger, item,
//(key, oldValue) => item);
dict[item.Trigger] = item;
}
}
else if(cmd == "Quit")
else if (cmd == "Quit")
{
DisableSelf();
DisableSelf();
}
else if (cmd is not "_")
{
Expand All @@ -45,6 +46,24 @@ public override void OnCreate()
{
globals = m.Value;
});
try
{
if (File.Exists(AppSettings.DictPath))
{
using var stream = File.OpenRead(AppSettings.DictPath);
dict = JsonSerializer.Deserialize<Dictionary<string, Match>>(stream);
}
else
dict = new();
if (File.Exists(AppSettings.GlobalVarsPath))
{
using var stream = File.OpenRead(AppSettings.GlobalVarsPath);
globals = JsonSerializer.Deserialize<List<Var>>(stream);
}
}
catch (Exception)
{
}
}

public override void OnAccessibilityEvent(AccessibilityEvent e)
Expand All @@ -59,7 +78,7 @@ public override void OnAccessibilityEvent(AccessibilityEvent e)
if (Text != null)
{
string og = Text[0].ToString();
var arr = og.Split(' ');
var arr = og.Split(new char[]{ ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
bool send = false;
foreach (var text in arr)
{
Expand All @@ -81,7 +100,7 @@ public override void OnAccessibilityEvent(AccessibilityEvent e)
replace = ParseItem(item, replace);
}
}
if(replace is not null)
if (replace is not null)
{
og = og.Replace(match.Trigger, replace);
send = true;
Expand Down
15 changes: 7 additions & 8 deletions MauiApp1/TextComparePro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<ApplicationIdGuid>A9E98E65-AE83-5428-9AB1-578DC0552CB8</ApplicationIdGuid>

<!-- Versions -->
<ApplicationDisplayVersion>6.0</ApplicationDisplayVersion>
<ApplicationVersion>11</ApplicationVersion>
<ApplicationDisplayVersion>6.1</ApplicationDisplayVersion>
<ApplicationVersion>12</ApplicationVersion>

<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'ios'">14.2</SupportedOSPlatformVersion>
<SupportedOSPlatformVersion Condition="$([MSBuild]::GetTargetPlatformIdentifier('$(TargetFramework)')) == 'maccatalyst'">14.0</SupportedOSPlatformVersion>
Expand All @@ -32,9 +32,9 @@
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|net7.0-android|AnyCPU'">
<AndroidPackageFormat>aab</AndroidPackageFormat>
<AndroidPackageFormat>apk</AndroidPackageFormat>
<AndroidUseAapt2>True</AndroidUseAapt2>
<AndroidCreatePackagePerAbi>False</AndroidCreatePackagePerAbi>
<AndroidCreatePackagePerAbi>True</AndroidCreatePackagePerAbi>
</PropertyGroup>

<ItemGroup>
Expand All @@ -60,11 +60,10 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.0" />
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="CommunityToolkit.Maui" Version="5.2.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
<PackageReference Include="MudBlazor" Version="6.2.2" />
<PackageReference Include="YamlDotNet" Version="13.1.0" />
<PackageReference Include="MudBlazor" Version="6.7.0" />
<PackageReference Include="YamlDotNet" Version="13.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e182e64

Please sign in to comment.