From f39a800454afd26c3ae284dbe1d334595f6f9b47 Mon Sep 17 00:00:00 2001 From: Daniel Chalmers Date: Mon, 22 Jan 2024 21:14:59 -0600 Subject: [PATCH] Fix import confirmation dialog timing out --- JournalApp/App.xaml.cs | 2 +- .../Components/Dialogs/CustomMessageBox.razor | 1 - .../Dialogs/CustomMessageBox.razor.cs | 5 ++++- .../Components/Pages/SettingsPage.razor | 22 ++++++++++++++----- JournalApp/Data/AppDataService.cs | 16 +++++++++----- JournalApp/Platforms/Android/MainActivity.cs | 8 +++---- 6 files changed, 35 insertions(+), 19 deletions(-) diff --git a/JournalApp/App.xaml.cs b/JournalApp/App.xaml.cs index b4f3a17..e9addb3 100644 --- a/JournalApp/App.xaml.cs +++ b/JournalApp/App.xaml.cs @@ -10,7 +10,7 @@ public App() } public static Window Window { get; private set; } - + public static string ActivatedFilePath { get; set; } public static event EventHandler NewIntent; diff --git a/JournalApp/Components/Dialogs/CustomMessageBox.razor b/JournalApp/Components/Dialogs/CustomMessageBox.razor index 9347f1e..2d49197 100644 --- a/JournalApp/Components/Dialogs/CustomMessageBox.razor +++ b/JournalApp/Components/Dialogs/CustomMessageBox.razor @@ -1,6 +1,5 @@ @namespace JournalApp @inherits MudComponentBase -@inject KeyEventService KeyEventService diff --git a/JournalApp/Components/Dialogs/CustomMessageBox.razor.cs b/JournalApp/Components/Dialogs/CustomMessageBox.razor.cs index be5dff9..18f5ebf 100644 --- a/JournalApp/Components/Dialogs/CustomMessageBox.razor.cs +++ b/JournalApp/Components/Dialogs/CustomMessageBox.razor.cs @@ -11,6 +11,9 @@ public partial class CustomMessageBox : MudComponentBase private IDialogReference _reference; private ActivatableCallback _yesCallback, _cancelCallback, _noCallback; + [Inject] + private KeyEventService KeyEventService { get; set; } = null!; + [Inject] private IDialogService DialogService { get; set; } = null!; @@ -193,7 +196,7 @@ protected override void OnInitialized() { base.OnInitialized(); - KeyEventService.Entered(() => OnCancelClicked()); + KeyEventService.Entered(OnCancelClicked); if (YesButton is not null) _yesCallback = new ActivatableCallback() { ActivateCallback = OnYesActivated }; diff --git a/JournalApp/Components/Pages/SettingsPage.razor b/JournalApp/Components/Pages/SettingsPage.razor index c909c2e..828d321 100644 --- a/JournalApp/Components/Pages/SettingsPage.razor +++ b/JournalApp/Components/Pages/SettingsPage.razor @@ -70,9 +70,20 @@ else await base.OnInitializedAsync(); KeyEventService.Entered(Close); + } - if (App.ActivatedFilePath != null) - await StartImport(); + protected override async Task OnAfterRenderAsync(bool firstRender) + { + await base.OnAfterRenderAsync(firstRender); + + if (firstRender) + { + if (App.ActivatedFilePath != null) + { + await StartImport(); + StateHasChanged(); + } + } } void SetUpSafetyPlan() @@ -99,7 +110,7 @@ else else { logger.LogInformation("Explaining how to import"); - await DialogService.ShowCustomMessageBox(string.Empty, "Find the backup in your files app and either click it or choose Open With -> JournalApp. You can also directly share the file from any app.", showFeedbackLink: true); + await DialogService.ShowCustomMessageBox(string.Empty, "Find the backup in your files app and click it or choose Open With -> JournalApp. You can also share the file directly from another app.", showFeedbackLink: true); } } @@ -111,6 +122,7 @@ else StateHasChanged(); var path = App.ActivatedFilePath; + await AppDataService.StartImportWizard(DialogService, App.ActivatedFilePath); } finally @@ -137,12 +149,10 @@ else public async Task GetCreditsText() { - logger.LogDebug("Getting credits stream"); + logger.LogDebug("Reading credits file"); using var stream = await FileSystem.OpenAppPackageFileAsync("Credits.txt"); using var reader = new StreamReader(stream); - - logger.LogDebug("Reading credits as text"); return reader.ReadToEnd(); } diff --git a/JournalApp/Data/AppDataService.cs b/JournalApp/Data/AppDataService.cs index 97469ca..bd9c98b 100644 --- a/JournalApp/Data/AppDataService.cs +++ b/JournalApp/Data/AppDataService.cs @@ -16,6 +16,7 @@ public async Task StartImportWizard(IDialogService dialogService, string p } logger.LogInformation($"Reading file: {path}"); + var sw = Stopwatch.StartNew(); // Attempt to read the file and its archive. BackupFile backup; @@ -25,23 +26,24 @@ public async Task StartImportWizard(IDialogService dialogService, string p { backup = await BackupFile.ReadArchive(fs); } + + logger.LogDebug($"Archive was read successfully after {sw.ElapsedMilliseconds}"); } catch (Exception ex) { - logger.LogInformation(ex, "Failed to read archive"); + logger.LogInformation(ex, $"Failed to read archive after {sw.ElapsedMilliseconds}"); await dialogService.ShowCustomMessageBox(string.Empty, $"Nothing happened; Failed to read archive: {ex.Message}.", showFeedbackLink: true); return false; } - logger.LogDebug("Archive was read"); - // Warn the user of what's going to happen. + sw.Restart(); if (await dialogService.ShowCustomMessageBox(string.Empty, $"The selected backup contains {backup.Days.Count} days, {backup.Categories.Count} categories/medications, {backup.Points.Count} points, and {backup.PreferenceBackups.Count} preferences. " + "This will replace ALL existing data, cannot be undone, and may take a few minutes.", yesText: "Import data", cancelText: "Cancel") == null) { - logger.LogDebug("User declined to import data"); + logger.LogDebug($"User declined to import data after {sw.ElapsedMilliseconds}"); return false; } @@ -54,22 +56,24 @@ public async Task StartImportWizard(IDialogService dialogService, string p } // Apply the backup content to the database. + sw.Restart(); await using (var db = await dbFactory.CreateDbContextAsync()) { db.Days.RemoveRange(db.Days); db.Categories.RemoveRange(db.Categories); db.Points.RemoveRange(db.Points); await db.SaveChangesAsync(); - logger.LogDebug("Cleared old db sets"); + logger.LogDebug($"Cleared old db sets after {sw.ElapsedMilliseconds}"); } + sw.Restart(); await using (var db = await dbFactory.CreateDbContextAsync()) { await db.Days.AddRangeAsync(backup.Days); await db.Categories.AddRangeAsync(backup.Categories); await db.Points.AddRangeAsync(backup.Points); await db.SaveChangesAsync(); - logger.LogDebug("Added new data"); + logger.LogDebug($"Added new data after {sw.ElapsedMilliseconds}"); } logger.LogInformation("Finished import"); diff --git a/JournalApp/Platforms/Android/MainActivity.cs b/JournalApp/Platforms/Android/MainActivity.cs index 275a899..ca87f15 100644 --- a/JournalApp/Platforms/Android/MainActivity.cs +++ b/JournalApp/Platforms/Android/MainActivity.cs @@ -2,14 +2,12 @@ using Android.Content; using Android.Content.PM; using Android.OS; -using Android.Views; using AndroidX.Activity; using JournalApp.Platforms.Android; namespace JournalApp; [Activity(Theme = "@style/Maui.SplashTheme", LaunchMode = LaunchMode.SingleInstance, MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation | ConfigChanges.UiMode | ConfigChanges.ScreenLayout | ConfigChanges.SmallestScreenSize | ConfigChanges.Density)] -// TODO: Narrow the scope of these filters while being the default for .journalapp files and being a share target. [IntentFilter( [Intent.ActionSend, Intent.ActionView], Categories = [Intent.CategoryDefault], @@ -17,12 +15,14 @@ namespace JournalApp; DataPathPatterns = [ "/.*\\.journalapp", "/.*\\..*\\.journalapp", - "/.*\\..*\\..*\\.journalapp" + "/.*\\..*\\..*\\.journalapp", + "/.*\\..*\\..*\\..*\\.journalapp" ])] +// Added because the above filter doesn't work in Samsung My Files - https://stackoverflow.com/questions/50407193/open-custom-filetype-in-samsung-file-explorer. [IntentFilter( [Intent.ActionSend, Intent.ActionView], Categories = [Intent.CategoryDefault], - DataScheme = "content", + DataSchemes = ["content", "file"], DataMimeType = "*/*" )] public class MainActivity : MauiAppCompatActivity