Preference functionality for Jetpack Compose

Overview

Release

ComposePreferences

Compose Preferences is a library which makes it easy to add preference functionality to your Compose app.

It provides an easy to use PreferenceScreen Composable, which displays a list of PreferenceItems and PreferenceGroups. The Preferences are saved in a DataStore and managed by a DataStoreManager, which makes it easy to access the preferences on the preference screen or from anywhere else (other screens, viewModels, services, ...).

Supported Preference Items

  • Preference Group: A container for multiple PreferenceItems
  • TextPreference: A basic PreferenceItem that only displays text.
  • SwitchPreference: A PreferenceItem that provides a two-state toggleable option.
  • ListPreference: A PreferenceItem that displays a list of entries as a dialog. Only one entry can be selected at any given time.
  • MultiSelectListPreference: A PreferenceItem that displays a list of entries as a dialog. Multiple entries can be selected at the same time.
  • SeekBarPreference: A PreferenceItem that displays a seekBar and the currently selected value.
  • DropDownPreference: A PreferenceItem that displays a list of entries as a dropdown menu. Only one entry can be selected at any given time.

Usage

To use ComposePreferences, create a DataStore and simply add a PreferenceScreen to your App:

val Context.dataStore: DataStore<Preferences> by preferencesDataStore(name = "settings")

setContent {
  MaterialTheme {
    PreferenceScreen(
      items = listOf(
        // PreferenceItems and PreferenceGroups
      ),
      dataStore = this.dataStore,
      statusBarPadding = true // When going edgeToEdge
    )
  }
}

You can also directly supply a DataStoreManager instead of the DataStore to the PreferenceScreen Composable.

When creating the PreferenceItems you have to supply a PreferenceRequest object. A PreferenceRequest wrapps a DataStore PreferenceKey and a default value and makes it easy to querry the selection the user made outside of the PreferenceScreen.

val waterRequest = PreferenceRequest<Boolean>(
  key = booleanPreferencesKey("pref_switch"), 
  defaultValue = true
)

// PreferenceScreen
PreferenceItem.SwitchPreference(
  request = ,
  title = "Switch Preference",
  summary = "A preference with a switch.",
  singleLineTitle = true,
  icon = {
    Icon(
      imageVector = Icons.Outlined.Delete,
      contentDescription = null,
      modifier = Modifier.padding(8.dp)
    )
  },
)

// Some service
lifecycleScope.launch {
  dataStoreManager.getPreferenceFlow(waterRequest).collect { waterEnabled ->
    // Do something when the value changes
    delay(100)
    dataStoreManager.editPreference(waterRequest.key, !waterEnabled)
  }
}

Read & Edit Preferences

Reading and editing Preferences on the PreferenceScreen is managed by ComposePreferences automatically. To access Preferences outside of the PreferenceScreen, you can query the DataStore you supplied to the PreferenceScreen Composable directly or use the DataStoreManager provided by ComposePreferences:

Read a preference

You can read a preference by using DataStoreManager's getPreference() and getPreferenceFlow() methods. Both methods require a PreferenceRequest parameter and either return the value for the key defined in the request or the default value, if the key is not present.

val dataStoreManager = DataStoreManager(this.dataStore)

val waterEnabledRequest: PreferenceRequest<Boolean> = PreferenceRequest(
  key = booleanPreferencesKey("water_enabled"),
  defaultValue = true,
)

// Synchronously (suspending)
val waterEnabled: Boolean = dataStoreManager.getPreference(waterEnabledRequest)

// Asynchronously
val waterEnabledFlow: Flow<Boolean> = dataStoreManager.getPreferenceFlow(waterEnabledRequest)

Edit a preference

You can edit a preference using DataStoreManager's editPreference() method. It is suspending and takes a preferenceKey and a newValue parameter and updates the DataStore with the newValue.

dataStoreManager.editPreference(waterEnabledRequest.key, false)

Download

repositories {
     maven { url = uri("https://jitpack.io") }
}

dependencies {
    implementation("com.github.Sh4dowSoul.ComposePreferences:compose-preferences:<version>")
    
    // Optional - Easy access to preferences from modules without compose dependency
    implementation("com.github.Sh4dowSoul.ComposePreferences:datastore-manager:<version>") 
}
Comments
  • Update to Compose 1.0.0-rc01

    Update to Compose 1.0.0-rc01

    • Update AGP to 7.0.0-beta05
    • Update Gradle to v7.1.1
    • Add DropDown Menu preference item
    • Improve List and Multiselect preference Dialog UI
    • Remove unneeded @ExperimentalCoroutinesApi annotation
    • Remove star imports
    • Use Composable for icon instead of ImageVector
    opened by rk779 1
  • De-duplicated datastorepreferences, added EditTextPreference

    De-duplicated datastorepreferences, added EditTextPreference

    I've updated to the latest version of compose, and re-used all of the components from preferences in dataStorePreferences. I removed the flow-preferences dependency. I wasn't motivated to do the work to support it.

    I also added a string preference. I switched from AlertDialog to a custom implementation based on Dialog. AlertDialog has some weird behaviors. I couldn't manage to getting padding on composables inside AlertDialog to work correctly and I've run into a few other issues with it.

    I tried to match https://material.io/design/platform-guidance/android-settings.html as much as possible. I changed the buttons on the list preference dialogs to match what is there.

    opened by sproctor 1
  • Dependency Updates (Compose Beta 8)

    Dependency Updates (Compose Beta 8)

    This PR updates some dependencies, most notably Jetpack Compose to 1.0.0-Beta08

    Other updates:

    • RefreshVersions 0.10.0 -> 0.10.1
    • AGP 7.0.0-alpha15 -> 7.0.0-beta02
    • AndroidX Activity 1.3.0-alpha07 -> 1.3.0
    • Accompanist 0.9.1 -> 0.11.1
    • Kotlin 1.4.32 -> 1.5.10
    opened by Sh4dowSoul 0
  • Refactor Compose Preferences

    Refactor Compose Preferences

    This PR contains a complete refactor of Compose Preferences, which expands the old datastore implementation with features from the legacy implementation (eg. group enable/disable support) and other improvements such as statusbar padding support and preferenceRequests. PreferenceRequests are a new model introduced by ComposePreferences which allows to reuse key/default value pairs on compose (preference screen) and non compose (services, viewmodel, ...) screens to get access to user preferences. Also introduce the datastore-manager which includes apis to simply access those settings on non compose screens.

    Also includes the following changes:

    • Naming Improvements
    • Maven / Jitpack Support
    • Reworked group support incl. enabled / disabled state
    • Update Gradle & AGP
    • Update RefreshVersions
    • Update dependencies (Compose Beta 7, DataStore Beta 1 & more)
    • Add support for statusbar padding (for screens without toolbar and transparent statusbar)
    opened by Sh4dowSoul 0
  • Preference dependency & Material 3 Switch/Slider/ListItem

    Preference dependency & Material 3 Switch/Slider/ListItem

    Add dependency to PreferenceItem

    When the corresponding SwitchPreference is turned off, dependency preferences are disabled and cannot be modified.

    Material 3

    Replaced SeekbarPreferenceWidget.kt slider and SwitchPreference.kt switch with material 3 slider and switch. Replaced MaterialListItem with material3 list item

    opened by joaomanaia 0
  • Crash with Compose material 1.2.0-alpha04 or above

    Crash with Compose material 1.2.0-alpha04 or above

    When launching the demo app with version.androidx.compose.material=1.2.0-alpha04 or any newer:

    java.lang.IllegalStateException: Asking for measurement result of unmeasured layout modifier at androidx.compose.ui.node.LayoutNodeWrapper.getMeasureResult(LayoutNodeWrapper.kt:104) at androidx.compose.ui.node.LayoutNodeAlignmentLines.recalculate(LayoutNodeAlignmentLines.kt:160) at androidx.compose.ui.node.LayoutNode.layoutChildren$ui_release(LayoutNode.kt:989) at androidx.compose.ui.node.LayoutNode.onNodePlaced$ui_release(LayoutNode.kt:937) at androidx.compose.ui.node.InnerPlaceable.placeAt-f8xVGno(InnerPlaceable.kt:122) at androidx.compose.ui.layout.Placeable.access$placeAt-f8xVGno(Placeable.kt:31) ...

    I also reproduce it in my own app using ComposePreferences. I'm trying to understand what's going on.

    opened by jventrib 0
  • Optional Summary and Icon

    Optional Summary and Icon

    It would be great if the PreferenceItem supports optional summary and icon. Submitting empty String for summary and empty Composable for icon creates weird alignment. Are there any plans for this?

    opened by NoelChew 0
  • Nullable value in PreferenceRequest

    Nullable value in PreferenceRequest

    Love the concept of the library, but i have slight problem with it's usage (PreferenceRequest to be exact). I have some setting that i want to be nullable. But right now it is simply not possible, since the T have to be the same type as the preferencesKey from DataStore. Since DataStore CAN return store null with non-nullable generic argument then we have a type missmatch here. Example:

    val nullableRequest = PreferenceRequest<String?>(
        key = stringPreferencesKey("test"),
        defaultValue = null
    )
    
    opened by jakoss 0
Releases(0.1.4)
  • 0.1.4(Feb 14, 2022)

  • 0.1.2(Feb 14, 2022)

  • 0.1.1(Jul 18, 2021)

    • Update to compose 1.0.0-rc02
    • Add DropDown Preference (thanks to @rk779)
    • Improve List and Multiselect preference Dialog UI (thanks to @rk779).
    • Update Preference models to receive a composable slot instead of an ImageVector (thanks to @rk779). This makes it possible to pass any composable to use as an icon for the preference. You can now load images from the network (using coil) for example. For simple usecases a new PreferenceIcon model can be used, which takes an ImageVector like before and applies some standard spacing to it.
    Source code(tar.gz)
    Source code(zip)
  • 0.1.0(Jun 12, 2021)

Owner
Niklas Schnettler
Niklas Schnettler
Common preference/settings Composables for Jetpack Compose.

ComposePrefs ComposePrefs is a preferences library for Jetpack Compose which makes it easy to implement preferences/settings in your Compose Android a

Jamal Mulla 54 Jan 5, 2023
ComposePrefs3 is a fully featured library of preference composables for Jetpack Compose.

ComposePrefs3 This is the M3 version of ComposePrefs. The M2 version can be found here. ComposePrefs3 is a preferences library for Jetpack Compose whi

Jamal Mulla 21 Dec 2, 2022
Jetpack Compose based project, used to stress-testing compose features / integrations and explore non-trivial functionality

Project containing Jetpack Compose samples For pagination & network images it uses CATAAS. Known issues Navigation-Compose Issue with fast tapping on

Denis Rudenko 59 Dec 14, 2022
Recreated iOS Calculator UI and functionality for android with Jetpack Compose

Compose-iOS-Calculator Recreated iOS Calculator UI and functionality for android with Jetpack Compose Currently using Regex to do the math, but when I

Ikechukwu Eze 6 Oct 11, 2022
ComposeTextBug - Issue with MotionLayout+Compose in Text functionality

ComposeTextBug Issue with MotionLayout+Compose in Text functionality Demo: devic

Yahor 0 Jan 12, 2022
Jetpack Compose Boids | Flocking Insect 🐜. bird or Fish simulation using Jetpack Compose Desktop 🚀, using Canvas API 🎨

?? ?? ?? Compose flocking Ants(boids) ?? ?? ?? Jetpack compose Boids | Flocking Insect. bird or Fish simulation using Jetpack Compose Desktop ?? , usi

Chetan Gupta 38 Sep 25, 2022
A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Why Not Compose! A collection of animations, compositions, UIs using Jetpack Compose. You can say Jetpack Compose cookbook or play-ground if you want!

Md. Mahmudul Hasan Shohag 186 Jan 1, 2023
Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

Learn Jetpack Compose for Android by Examples. Learn how to use Jetpack Compose for Android App Development. Android’s modern toolkit for building native UI.

MindOrks 382 Jan 5, 2023
This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Compose.

JetBMICalculator This is a sample app(For beginners - App #2) built using Jetpack Compose. It demonstrates the concept of State Hoisting in Jetpack Co

BHAVNA THACKER 3 Dec 31, 2022
Jetpack-Compose-Demo - Instagram Profile UI using Jetpack Compose

Jetpack-Compose-Demo Instagram Profile UI using Jetpack Compose

omar 1 Aug 11, 2022
Jetpack-compose-animations-examples - Cool animations implemented with Jetpack compose

Jetpack-compose-animations-examples This repository consists of 4 animations: St

Canopas Software 180 Jan 2, 2023
Compose-navigation - Set of utils to help with integrating Jetpack Compose and Jetpack's Navigation

Jetpack Compose Navigation Set of utils to help with integrating Jetpack Compose

Adam Kobus 5 Apr 5, 2022
Jetpack-compose-uis - A collection of some UIs using Jetpack Compose. built using Katalog

Jetpack Compose UIs This is a collection of some UIs using Jetpack Compose. It i

Mori Atsushi 3 Dec 15, 2022
A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose

Authentication A simple authentication application using Jetpack compose to illustrate signin and sign up using Mvvm, Kotlin and jetpack compose Scree

Felix Kariuki 5 Dec 29, 2022
An application that i developed with a aim of learning Jetpack compose and many other jetpack libraries

An application that i developed with a aim of learning Jetpack compose and many other jetpack libraries, The application make use of jikan Api which displays a list of animations,there more details and even trailers of the animations.

Odhiambo Brandy 10 Nov 23, 2022
A Kotlin library to use Jetpack Compose in Android and iOS. Allow to write UI for both in Kotin. Still experimental as many compose features are not yet available.

Multiplatform Compose A Kotlin library to use Jetpack Compose in Android and iOS. Allow to write UI for both in Kotin. Still experimental as many comp

Clément Beffa 548 Jan 7, 2023
K5-compose is a sketchy port of p5.js for Jetpack Compose

k5-compose k5-compose is a sketchy port of P5.js for Jetpack Compose Desktop. This library provides you a playground to play with your sketches so you

Nikhil Chaudhari 176 Nov 22, 2022
Pokedex Compose is an independent re-write of a demo application by the name of Pokedex, but written in jetpack compose.

Pokedex Compose Pokedex Compose is an independent re-write of a similar project by the name of Pokedex. I am recreating the UI but I am doing it using

Jose Patino 4 May 1, 2022
Compose-Instagram-Profile-UI - Instagram profile screen UI using android jetpack compose

Compose-Intsgram-Profile-UI Instagram profile screen UI using android jetpack co

TILLERN 1 Mar 8, 2022