Skip to content

Commit

Permalink
Show alerts about errors during startup
Browse files Browse the repository at this point in the history
DisplayAlert fails showing alerts when MainPage is not ready
  • Loading branch information
topeterk committed Dec 4, 2021
1 parent 4fbf155 commit 64e4f56
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 11 deletions.
55 changes: 49 additions & 6 deletions Source/HitCounterManager/Common/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
//SOFTWARE.

using System;
using System.Collections.Generic;
using Xamarin.Forms;
using HitCounterManager.Common.OsLayer;
using HitCounterManager.Common.PlatformLayer;
Expand All @@ -39,6 +40,7 @@ public enum TimerIDs : uint { Shortcuts, ShortcutsCapture, GameTime, GameTimeGui

public partial class App : Application
{
private bool StartupDone = false;
public readonly IOsLayer OsLayer;
public readonly IPlatformLayer PlatformLayer;
private HookHandle WindowHookHandle;
Expand Down Expand Up @@ -78,10 +80,51 @@ public App(IOsLayer osLayer, IPlatformLayer platformLayer)
MainPage = new NavigationPage(main);

MainPage.Appearing += MainPage_Appearing;
PageAppearing += App_PageAppearing;

ModalPopped += App_ModalPopped;
}

private struct PostponedAlert
{
public string Title;
public string Message;
public string Cancel;

public PostponedAlert(string Title, string Message, string Cancel)
{
this.Title = Title;
this.Message = Message;
this.Cancel = Cancel;
}
}
private List<PostponedAlert> PostponedAlerts = new List<PostponedAlert>();

/// <summary>
/// Page.DisplayAlert but when no appeared yet, it will be postponed until MainPage has appeared
/// </summary>
/// <param name="Title"></param>
/// <param name="Message"></param>
/// <param name="Cancel"></param>
public void DisplayAlert(string Title, string Message, string Cancel)
{
if (StartupDone)
MainPage.DisplayAlert(Title, Message, Cancel);
else
PostponedAlerts.Add(new PostponedAlert(Title, Message, Cancel));
}

private void App_PageAppearing(object sender, Page e)
{
if (!StartupDone)
{
StartupDone = true;
foreach(var Alert in PostponedAlerts) MainPage.DisplayAlert(Alert.Title, Alert.Message, Alert.Cancel);
PostponedAlerts.Clear();
PostponedAlerts = null;
}
}

private void App_ModalPopped(object sender, ModalPoppedEventArgs e)
{
IDisposable bindingContext = e.Modal.BindingContext as IDisposable;
Expand Down Expand Up @@ -128,12 +171,7 @@ private void MainPage_Appearing(object sender, EventArgs e)
// Register the hook function and hot keys
WindowHookHandle = PlatformLayer.HookInstall(WndProc, PlatformLayer.ApplicationWindowHandle);
bool Success = LoadAllHotKeySettings();
#if TODO // Call DisplayAlert or anything like that?
if (!Success)
MessageBox.Show("Not all enabled hot keys could be registered successfully!", "Error setting up hot keys!");
#else
Success = !Success; // just to avoid compiler warning about (yet) unused variable
#endif
if (!Success) DisplayAlert("Error setting up hot keys!", "Not all enabled hot keys could be registered successfully!", "OK");
}
}

Expand Down Expand Up @@ -187,6 +225,11 @@ private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam
SaveSettings();
}

/// <summary>
/// By definition ????:
/// GTK: After MainPage_Appearing
/// WPF: Before MainPage_Appearing
/// </summary>
protected override void OnStart() { }

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Source/HitCounterManager/Models/SaveModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public T ReadXML(bool EnableBackup, string Filename = null)
catch (FileNotFoundException) { } // Exception.HResult == COR_E_FILENOTFOUND only be available since .Net 4.5, use overloading for older frameworks
catch (Exception ex)
{
Application.Current.MainPage.DisplayAlert("Error loading settings!", ex.Message + Environment.NewLine + "==> Using defaults", "OK");
App.CurrentApp.DisplayAlert("Error loading settings!", ex.Message + Environment.NewLine + "==> Using defaults", "OK");
}
finally
{
Expand All @@ -102,7 +102,7 @@ public bool WriteXML(T data)
catch (Exception ex)
{
if (null != file) file.Close();
Application.Current.MainPage.DisplayAlert("Error writing settings!", ex.Message, "OK");
App.CurrentApp.DisplayAlert("Error writing settings!", ex.Message, "OK");
}
return false;
}
Expand Down
4 changes: 1 addition & 3 deletions Source/HitCounterManager/ViewModels/SettingsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,7 @@ private void SetNextShortcutMethod(Shortcuts.SC_HotKeyMethod next)
case Shortcuts.SC_HotKeyMethod.SC_HotKeyMethod_LLKb: CallPropertyChanged(this, nameof(RadioHotKeyMethod_LLKb)); break;
default: break;
}
#if TODO // Make sure this can only happen after the OnAppearing event! Maybe DisplayAlert?
MessageBox.Show("Changes only take effect after restarting the application.", "Restart required", MessageBoxButtons.OK, MessageBoxIcon.Information);
#endif
App.CurrentApp.DisplayAlert("Restart required", "Changes only take effect after restarting the application.", "OK");
}
public bool RadioHotKeyMethod_Sync
{
Expand Down

0 comments on commit 64e4f56

Please sign in to comment.