Skip to content

Instantly share code, notes, and snippets.

@dperish
Created February 27, 2023 14:15
Show Gist options
  • Save dperish/0ff4cb865362d0c1d74a48b8b6bb9147 to your computer and use it in GitHub Desktop.
Save dperish/0ff4cb865362d0c1d74a48b8b6bb9147 to your computer and use it in GitHub Desktop.
Maui Test Providers
public interface IMauiProviders
{
IDialogProvider Dialogs { get; }
IPreferencesProvider Preferences { get; }
IShellProvider Shell { get; }
}
public class MauiProviders : IMauiProviders
{
public IDialogProvider Dialogs { get; protected set; }
public IPreferencesProvider Preferences { get; protected set; }
public IShellProvider Shell { get; protected set; }
public MauiProviders(
IDialogProvider dialogs,
IPreferencesProvider preferences,
IShellProvider shell)
{
Dialogs = dialogs;
Preferences = preferences;
Shell = shell;
}
}
public interface IDialogProvider
{
Task ShowConfirmationAsync(string title, string message, string cancel = "OK");
Task ShowAlertAsync(string title, string message, string accept, string cancel);
Task<string> ShowActionsAsync(string title, string message, string destruction, params string[] buttons);
Task ToastAsync(string message, ToastDuration duration = ToastDuration.Short, double textSize = 14D);
Task SnackBarAsync(string message, Action action = null, string actionButtonText = "OK");
}
public class DialogProvider : IDialogProvider
{
protected readonly IShellProvider ShellProvider;
public DialogProvider(IShellProvider shellProvider)
{
ShellProvider = shellProvider;
}
public Task ShowConfirmationAsync(string title, string message, string cancel = "OK")
{
return ShellProvider.Current.DisplayAlert(title, message, cancel);
}
public Task ShowAlertAsync(string title, string message, string accept, string cancel)
{
return ShellProvider.Current.DisplayAlert(title, message, accept, cancel);
}
public Task<string> ShowActionsAsync(string title, string message, string destruction, params string[] buttons)
{
return ShellProvider.Current.DisplayActionSheet(title, "Resources.CloseText", destruction, buttons);
}
public Task ToastAsync(string message, ToastDuration duration = ToastDuration.Short, double textSize = 14D)
{
return Toast.Make(message, duration, textSize).Show();
}
public Task SnackBarAsync(string message, Action? action = null, string actionButtonText = "OK")
{
return ShellProvider.Current.DisplaySnackbar(message, action, actionButtonText);
}
}
public interface IPreferencesProvider
{
void Clear(string key);
bool ContainsKey(string key);
bool ContainsKey(string key, string sharedName);
DateTime Get(string key, DateTime defaultValue);
DateTime Get(string key, DateTime defaultValue, string sharedName);
string Get(string key, string defaultValue);
int Get(string key, int defaultValue);
int Get(string key, int defaultValue, string sharedName);
bool Get(string key, bool defaultValue);
long Get(string key, long defaultValue);
long Get(string key, long defaultValue, string sharedName);
void Remove(string key);
void Set(string key, string value);
void Set(string key, int value);
void Set(string key, int value, string sharedName);
void Set(string key, long value);
void Set(string key, long value, string sharedName);
void Set(string key, DateTime value);
void Set(string key, DateTime value, string sharedName);
void Set(string key, bool value);
void Set(string key, bool value, string sharedName);
}
public class PreferencesProvider : IPreferencesProvider
{
public void Clear(string key)
=> Preferences.Clear(key);
public bool ContainsKey(string key)
=> Preferences.ContainsKey(key);
public bool ContainsKey(string key, string sharedName)
=> Preferences.ContainsKey(key, sharedName);
public DateTime Get(string key, DateTime defaultValue)
=> Preferences.Get(key, defaultValue);
public DateTime Get(string key, DateTime defaultValue, string sharedName)
=> Preferences.Get(key, defaultValue, sharedName);
public string? Get(string key, string defaultValue)
=> Preferences.Get(key, defaultValue);
public int Get(string key, int defaultValue)
=> Preferences.Get(key, defaultValue);
public int Get(string key, int defaultValue, string sharedName)
=> Preferences.Get(key, defaultValue, sharedName);
public bool Get(string key, bool defaultValue)
=> Preferences.Get(key, defaultValue);
public long Get(string key, long defaultValue)
=> Preferences.Get(key, defaultValue);
public long Get(string key, long defaultValue, string sharedName)
=> Preferences.Get(key, defaultValue, sharedName);
public void Remove(string key)
=> Preferences.Remove(key);
public void Set(string key, string value)
=> Preferences.Set(key, value);
public void Set(string key, int value)
=> Preferences.Set(key, value);
public void Set(string key, int value, string sharedName)
=> Preferences.Set(key, value, sharedName);
public void Set(string key, long value)
=> Preferences.Set(key, value);
public void Set(string key, long value, string sharedName)
=> Preferences.Set(key, value, sharedName);
public void Set(string key, DateTime value)
=> Preferences.Set(key, value);
public void Set(string key, DateTime value, string sharedName)
=> Preferences.Set(key, value, sharedName);
public void Set(string key, bool value)
=> Preferences.Set(key, value);
public void Set(string key, bool value, string sharedName)
=> Preferences.Set(key, value, sharedName);
}
public interface IShellProvider
{
Shell Current { get; }
}
public class ShellProvider : IShellProvider
{
public Shell Current => Shell.Current;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment