User Preferences - ChrispyPeaches/FocusFriends GitHub Wiki

User Preferences

What are they?

User Preferences in .NET MAUI are used to store local values within an App's datafiles

FocusApp Preference Helper

The preference helper provides a way to standardize the usage of FocusApp preferences.

  • Default values are all stored in one place and don't need to be specified each time a preference is retrieved

PreferenceNames

The PreferenceNames enum details the various names of each FocusApp preference.

Methods

Get(PreferenceNames prefName)

Get is used to check if there is saved value, and if there isn't, the default value will be returned.

  • TPrefType is the datatype of the preference you're retrieving Usage example:
double ambianceVolume = PreferencesHelper.Get<double>(PreferencesHelper.PreferenceNames.ambiance_volume);

Set

When changing the preference, .Set is used

  • TPrefType is the datatype of the preference you're setting Usage Examples:
  1. With TPrefType automatically determined
    PreferencesHelper.Set(PreferencesHelper.PreferenceNames.ambiance_volume, newValue);
    
  2. With TPrefType manually specified
    PreferencesHelper.Set<bool>(PreferencesHelper.PreferenceNames.ambiance_volume, newBool);
    

Native Maui Preferences

Example

Here's an example of a Key/Value Pair for saving ambient audio:

image

.Get is used to check if there is saved value, and if there isn't, the second value is the "Default value"

When changing the preference, .Set is used:

image

For switches, the name of the switch is passed through a function where a key and value is set.

image

⚠️ **GitHub.com Fallback** ⚠️