The Android arsenal – settings


about

introduction

This library is based on Flows and Coroutines and works with the provided DataStore storage or even with a custom storage implementation. It supports LiveData by default as Flows can be easily converted LiveData. Preferences are elegantly declared via delegates. Additionally the Preference screen Module offers a DSL for easy set-up RecyclerView based preference screens. Custom extensions to custom settings screens are also supported.

The most important functions are listed below:

  • Define preferences elegantly via delegates
  • Flow and coroutine based
  • makes it possible to observe individual / some / all preferences
  • Provides the ability to interrupt updates
  • offers a DSL for a RecyclerView Settings screen

Vein

With this library you can declare settings via kotlin delegatesand watch and update them via Kotlin Flows. This works with any storage implementation. An implementation for JetPack DataStore already exists.

1/4 define settings:
object UserSettingsModel : SettingsModel(DataStoreStorage(name = "user")) { // Basic val name by stringPref("User") val alive by boolPref(true) val hairColor by intPref(Color.parseColor("#A52A2A")) val age by intPref(40) val income by floatPref(50000f) val dayOfBirth by longPref(0L) // Sets val childrenAges by intSetPref(setOf(20, 18, 16)) val childrenIncomes by floatSetPref(setOf(30000f, 10000f, 0f)) val childrenDaysOfBirth by longSetPref(setOf(0L, 0L, 0L)) // Enum val car by enumPref(Car.Tesla) // custom class - provide a custom converter (String <=> Class) val testClass by anyPref(TestClass.CONVERTER, TestClass())
}
2/4 monitor / read settings:
// 1) simply observe a settingUserSettingsModel.name.observe(lifecycleScope) { L.d { "name = $it"}
}// 2) direct read (not recommended if not necessary but may be useful in many cases => simply returns flow.first() in a blocking way)val name = UserSettingsModel.name.value// 3) observe a setting onceUserSettingsModel.name.observeOnce(lifecycleScope) { L.d { "name = $it"}
}// 4) observe ALL settingsUserSettingsModel.changes.onEach { L.d { "[ALL SETTINGS OBSERVER] Setting '${it.setting.key}' changed its value to ${it.value}" }
}.launchIn(lifecycleScope)// 5) observe SOME settingsUserSettingsModel.changes´ .filter { it.setting == UserSettingsModel.name || it.setting == UserSettingsModel.age }.onEach { // we know that either the name or the age changes L.d { "[SOME SETTINGS OBSERVER] Setting '${it.setting.key}' changed its value to ${it.value}" } }.launchIn(lifecycleScope) // 6) read multiple settings in a suspending way
lifecycleScope.launch(Dispatchers.IO) { val name = UserSettingsModel.childName1.flow.first() val alive = DemoSettingsModel.alive.flow.first() val hairColor = DemoSettingsModel.hairColor.flow.first() withContext(Dispatchers.Main) { textView.text = "Informations: $name, $alive, $hairColor" }
}
3/4 dates of life:
val lifedata = UserSettingsModel.name.flow.asLiveData()
4/4 update settings:
lifecycleScope.launch(Dispatchers.IO) { UserSettingsModel.name.update("Some new name") UserSettingsModel.age.update(30)
}

camp

The Storage is an abstraction to support any memory implementation. The datastore The module offers an implementation based on the Android JetPack DataStore.

The corresponding readme file can be found here: DataStore module README

Settings screen

The Preference Screen * With modules, you can easily create preference screens like following over DSL.

Explanations, code examples and more can be found here: Preference Screen Modules README

Gradle (via JitPack.io)

  1. Add jitpack to your project build.gradle::
repositories { maven { url "https://jitpack.io" }
}
  1. Add the compilation instruction to your module build.gradle::
dependencies { // core module implementation "com.github.MFlisar.MaterialPreferences:core:<LATEST-VERSION>" // data store module implementation "com.github.MFlisar.MaterialPreferences:datastore:<LATEST-VERSION>" // screen modules implementation "com.github.MFlisar.MaterialPreferences:screen:<LATEST-VERSION>" implementation "com.github.MFlisar.MaterialPreferences:screen-input:<LATEST-VERSION>" implementation "com.github.MFlisar.MaterialPreferences:screen-choice:<LATEST-VERSION>" implementation "com.github.MFlisar.MaterialPreferences:screen-color:<LATEST-VERSION>"
}

DO

Recent Articles

Related Stories

Leave A Reply

Please enter your comment!
Please enter your name here

Stay on op - Ge the daily news in your inbox