KvdLingen
KvdLingen

Reputation: 1344

PreferencesFragment and MVVM architecture

In my app I am using ViewModels to provide the view with data and Data repositories to provide the viewmodel with data and to handle the communication with the datasources, e.g. database, internet API and Preferences.

Now android uses special fragments to handle settings. These special fragments handle the settings data through direct writing of sharedPreferences. So they do not conform to the MVVM architecture. I also read data from the API, extract some initial settings data from it and save it in its own preferencefile. The user should be able to change this extracted data within certain limits. So I am using key, value pairs but just in another file. This separation gives me the possibilty to delete the extracted data without losing other data, logging the user out of the app for example because the accesstoken is deleted.

I can write a normal fragment to handle the settings but is this advised? Or are there other ways to use androids build in settings fragments and still keep data seperated according to the MVVM architecture?

Upvotes: 3

Views: 630

Answers (1)

KvdLingen
KvdLingen

Reputation: 1344

Figured most of it out. PreferenceFragmentCompat and the Preferences in fact hide the implementation of the datastorage so complying with the MVVM architecture and seperation of responsibilities is seen to. In the datarepository dealing with the preferences a OnPreferenceChangedListener can be implemented which reloads the preferences and through LiveData supplies the preferences to the viewmodels.

For the separation of my special data a new preferencedatastore probably can be written.

Upvotes: 2

Related Questions