Replicating Microsoft News with Xamarin Part 14 (Using Xamarin.Essentials: Preferences and SecureStorage and Wrapping Up)

Elisha Misoi
3 min readApr 27, 2020

Often times we need to store user preferences, settings and sensitive user data locally. Xamarin.Essentials has Preferences and SecureStorage that does the heavy lifting for us to help store user settings and sensitive user data.

So what’s our use case? We want to store theme preference of the user, news preferences of the user and also store the user’s uid and email which can be sensitive information.

Let’s got ahead and create a folder and name it ‘AppSettings’. Inside the folder let’s add an empty class and name it ‘Settings’. Let’s make the class static.

We want to add some enums to help us to easily and quickly identify our preference types:

Now let’s add some methods to handle the saving of Preferences, i.e user settings and preferences that don’t necessarily need to be secured e.g theme preference:

Great! Let’s write some functions for handling SecureStorage:

Below is the helper class for converting enums to strings and vise versa:

Great 😎.

WRAPPING UP:

Yup you read that right!! We are now completing the app!! 😜. It’s been a long journey and I am proud that we are here now 🙂.

Step 1 (Save user Interests and user object):

Let’s save user Interests and user object on Successful login.

Let’s add functions in the ‘DatabaseHelper’ class that will handle fetching of locally saved user interests and update user interests in the database:

Full ‘DatabaseHelper’ class code below:

Let’s save a user object and user interests in locally in ‘InterestsViewModel’:

Full ‘SettingsViewModel’ code below:

Step 2 (Change Theme Depending on User preference):

Modify your ‘ThemeHelper’ class to look like below:

Then call the function ‘GetAppTheme()’ when the app starts in ‘App.xaml.cs’:

Step 3 (Modify ‘MainFeedViewModel’ to get user’s saved Interests from ‘Settings’):

Full ‘MainFeedViewModel’ class below:

Yup!! That should be it!! 🙂🙂🙂🙂

Look at what we created 😍:

How happy are you about this? I am astounded 😁😋😍!!!

Link to full project: https://github.com/Elisha-Misoi/Nomadic

All relevant links: https://link.medium.com/fsVGOOLP15

Fork and Contribute where you can 🙂.

Till we meet again, adios amigo 🤗.

Disclaimer ☠!!

As much as consideration was taken for iOS platform-specific code, this application has not been tested on iOS. In theory it should work, but whatever bugs that you may encounter or on the event that the application does not successfully build or for whatever reason your device bursts into flames, I might not be of help. Sorry 😐.

A certain amount of code might end up missing in the blog post please refer to the branch link.

I might not be able to explain every single line of code so please refer to comments in the code for more explanation.

--

--