Skip to content

Commit

Permalink
Fix import confirmation dialog timing out
Browse files Browse the repository at this point in the history
  • Loading branch information
danielchalmers committed Jan 23, 2024
1 parent f9ddc1f commit f39a800
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion JournalApp/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public App()
}

public static Window Window { get; private set; }

public static string ActivatedFilePath { get; set; }

public static event EventHandler NewIntent;
Expand Down
1 change: 0 additions & 1 deletion JournalApp/Components/Dialogs/CustomMessageBox.razor
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
@namespace JournalApp
@inherits MudComponentBase
@inject KeyEventService KeyEventService

<MudDialog @attributes="UserAttributes" OnBackdropClick="OnBackdropClick">
<TitleContent>
Expand Down
5 changes: 4 additions & 1 deletion JournalApp/Components/Dialogs/CustomMessageBox.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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!;

Expand Down Expand Up @@ -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 };
Expand Down
22 changes: 16 additions & 6 deletions JournalApp/Components/Pages/SettingsPage.razor
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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);
}
}

Expand All @@ -111,6 +122,7 @@ else
StateHasChanged();

var path = App.ActivatedFilePath;

await AppDataService.StartImportWizard(DialogService, App.ActivatedFilePath);
}
finally
Expand All @@ -137,12 +149,10 @@ else

public async Task<string> 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();
}

Expand Down
16 changes: 10 additions & 6 deletions JournalApp/Data/AppDataService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public async Task<bool> StartImportWizard(IDialogService dialogService, string p
}

logger.LogInformation($"Reading file: {path}");
var sw = Stopwatch.StartNew();

// Attempt to read the file and its archive.
BackupFile backup;
Expand All @@ -25,23 +26,24 @@ public async Task<bool> 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;
}

Expand All @@ -54,22 +56,24 @@ public async Task<bool> 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");
Expand Down
8 changes: 4 additions & 4 deletions JournalApp/Platforms/Android/MainActivity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,27 @@
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],
DataMimeType = "application/*",
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
Expand Down

0 comments on commit f39a800

Please sign in to comment.