Skip to content

Commit

Permalink
Merge pull request #2664 from PaulWoitaschek/prefs_migration
Browse files Browse the repository at this point in the history
Add the prefs library as local sources
  • Loading branch information
PaulWoitaschek authored Dec 28, 2024
2 parents d9a77ff + 75d8caa commit c5623a1
Show file tree
Hide file tree
Showing 47 changed files with 775 additions and 38 deletions.
3 changes: 1 addition & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,7 @@ dependencies {

implementation(libs.conductor)

implementation(libs.prefs.android)
testImplementation(libs.prefs.inMemory)
implementation(projects.pref)

testImplementation(libs.androidX.test.runner)
testImplementation(libs.androidX.test.junit)
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/kotlin/voice/app/injection/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.appcompat.app.AppCompatDelegate
import coil.Coil
import coil.ImageLoader
import com.google.android.material.color.DynamicColors
import de.paulwoitaschek.flowpref.Pref
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
Expand All @@ -15,6 +14,7 @@ import voice.app.scanner.MediaScanTrigger
import voice.common.DARK_THEME_SETTABLE
import voice.common.pref.PrefKeys
import voice.common.rootComponent
import voice.pref.Pref
import javax.inject.Inject
import javax.inject.Named

Expand Down
12 changes: 6 additions & 6 deletions app/src/main/kotlin/voice/app/injection/PrefsModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ import androidx.datastore.core.DataStore
import com.squareup.anvil.annotations.ContributesTo
import dagger.Module
import dagger.Provides
import de.paulwoitaschek.flowpref.Pref
import de.paulwoitaschek.flowpref.android.AndroidPreferences
import de.paulwoitaschek.flowpref.android.boolean
import de.paulwoitaschek.flowpref.android.enum
import de.paulwoitaschek.flowpref.android.int
import de.paulwoitaschek.flowpref.android.stringSet
import kotlinx.serialization.builtins.ListSerializer
import kotlinx.serialization.builtins.nullable
import kotlinx.serialization.builtins.serializer
Expand All @@ -31,6 +25,12 @@ import voice.common.pref.RootAudiobookFolders
import voice.common.pref.SingleFileAudiobookFolders
import voice.common.pref.SingleFolderAudiobookFolders
import voice.datastore.VoiceDataStoreFactory
import voice.pref.AndroidPreferences
import voice.pref.Pref
import voice.pref.boolean
import voice.pref.enum
import voice.pref.int
import voice.pref.stringSet
import javax.inject.Named
import javax.inject.Singleton

Expand Down
2 changes: 1 addition & 1 deletion bookOverview/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ dependencies {
implementation(projects.data)
implementation(projects.sleepTimer)
implementation(projects.scanner)
implementation(projects.pref)

implementation(libs.lifecycle)
implementation(libs.documentFile)
implementation(libs.dagger.core)
implementation(libs.datastore)
implementation(libs.prefs.core)

testImplementation(libs.bundles.testing.jvm)
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.core.net.toUri
import androidx.datastore.core.DataStore
import de.paulwoitaschek.flowpref.Pref
import kotlinx.collections.immutable.toImmutableMap
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
Expand All @@ -37,6 +36,7 @@ import voice.data.repo.internals.dao.LegacyBookDao
import voice.data.repo.internals.dao.RecentBookSearchDao
import voice.playback.PlayerController
import voice.playback.playstate.PlayStateManager
import voice.pref.Pref
import voice.search.BookSearch
import javax.inject.Inject
import javax.inject.Named
Expand Down
3 changes: 2 additions & 1 deletion common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ android {

dependencies {
implementation(projects.strings)
implementation(projects.pref)

implementation(libs.appCompat)
implementation(libs.dagger.core)
implementation(libs.material)
api(libs.immutable)
implementation(libs.prefs.core)
api(libs.conductor)
implementation(libs.androidxCore)
implementation(libs.viewBinding)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package voice.common.compose

import com.squareup.anvil.annotations.ContributesTo
import de.paulwoitaschek.flowpref.Pref
import voice.common.AppScope
import voice.common.pref.PrefKeys
import voice.pref.Pref
import javax.inject.Named

@ContributesTo(AppScope::class)
Expand Down
3 changes: 1 addition & 2 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies {
implementation(libs.appCompat)
implementation(libs.androidxCore)
implementation(libs.serialization.json)
implementation(libs.prefs.core)
implementation(projects.pref)

api(libs.room.runtime)
ksp(libs.room.compiler)
Expand All @@ -51,7 +51,6 @@ dependencies {
testImplementation(libs.androidX.test.runner)
testImplementation(libs.junit)
testImplementation(libs.robolectric)
testImplementation(libs.prefs.inMemory)
testImplementation(libs.koTest.assert)
testImplementation(libs.coroutines.test)
}
4 changes: 2 additions & 2 deletions data/src/test/kotlin/voice/data/TestComponent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package voice.data
import androidx.room.migration.Migration
import com.squareup.anvil.annotations.MergeComponent
import dagger.BindsInstance
import de.paulwoitaschek.flowpref.Pref
import de.paulwoitaschek.flowpref.inmemory.InMemoryPref
import voice.common.AppScope
import voice.common.pref.PrefKeys
import voice.pref.Pref
import voice.pref.inmemory.InMemoryPref
import javax.inject.Named
import javax.inject.Singleton

Expand Down
2 changes: 1 addition & 1 deletion folderPicker/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ dependencies {
implementation(projects.data)
implementation(projects.sleepTimer)
implementation(projects.documentfile)
implementation(projects.pref)

implementation(libs.datastore)
implementation(libs.coil)
implementation(libs.prefs.core)
implementation(libs.materialDialog.core)
implementation(libs.androidxCore)
implementation(libs.documentFile)
Expand Down
4 changes: 0 additions & 4 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,6 @@ androidTest-rules = "androidx.test:rules:1.6.1"

conductor = "com.bluelinelabs:conductor:4.0.0-preview-4"

prefs-core = { module = "com.github.PaulWoitaschek.FlowPref:core", version.ref = "prefs" }
prefs-android = { module = "com.github.PaulWoitaschek.FlowPref:android", version.ref = "prefs" }
prefs-inMemory = { module = "com.github.PaulWoitaschek.FlowPref:in-memory", version.ref = "prefs" }

materialDialog-core = { module = "com.afollestad.material-dialogs:core", version.ref = "materialDialog" }
materialDialog-input = { module = "com.afollestad.material-dialogs:input", version.ref = "materialDialog" }

Expand Down
2 changes: 1 addition & 1 deletion migration/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ dependencies {
implementation(projects.data)
implementation(projects.sleepTimer)
implementation(projects.scanner)
implementation(projects.pref)

implementation(libs.dagger.core)
implementation(libs.datastore)
implementation(libs.prefs.core)

testImplementation(libs.bundles.testing.jvm)
}
3 changes: 1 addition & 2 deletions playback/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ dependencies {
implementation(projects.common)
implementation(projects.strings)
implementation(projects.data)
implementation(projects.pref)

implementation(libs.androidxCore)
implementation(libs.prefs.core)
implementation(libs.datastore)
implementation(libs.coil)
implementation(libs.coroutines.guava)
Expand All @@ -31,7 +31,6 @@ dependencies {
implementation(libs.media3.session)

testImplementation(libs.bundles.testing.jvm)
testImplementation(libs.prefs.inMemory)
testImplementation(libs.media3.testUtils.core)
testImplementation(libs.media3.testUtils.robolectric)
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import androidx.media3.common.ForwardingPlayer
import androidx.media3.common.MediaItem
import androidx.media3.common.Player
import androidx.media3.exoplayer.ExoPlayer
import de.paulwoitaschek.flowpref.Pref
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
Expand All @@ -24,6 +23,7 @@ import voice.playback.misc.VolumeGain
import voice.playback.session.MediaId
import voice.playback.session.MediaItemProvider
import voice.playback.session.toMediaIdOrNull
import voice.pref.Pref
import java.time.Instant
import javax.inject.Inject
import javax.inject.Named
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.media3.test.utils.TestExoPlayerBuilder
import androidx.media3.test.utils.robolectric.TestPlayerRunHelper
import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import de.paulwoitaschek.flowpref.inmemory.InMemoryPref
import io.kotest.matchers.MatcherResult
import io.kotest.matchers.should
import io.mockk.Runs
Expand Down Expand Up @@ -37,6 +36,7 @@ import voice.playback.session.MediaId
import voice.playback.session.MediaItemProvider
import voice.playback.session.search.book
import voice.playback.session.toMediaIdOrNull
import voice.pref.inmemory.InMemoryPref
import java.time.Instant
import java.util.UUID
import java.util.concurrent.TimeUnit
Expand Down
3 changes: 1 addition & 2 deletions playbackScreen/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,16 @@ dependencies {
implementation(projects.data)
implementation(projects.datastore)
implementation(projects.sleepTimer)
implementation(projects.pref)

implementation(libs.datastore)
implementation(libs.coil)
implementation(libs.androidxCore)
implementation(libs.prefs.core)
implementation(libs.materialDialog.core)
implementation(libs.androidxCore)
implementation(libs.material)

implementation(libs.dagger.core)

testImplementation(libs.prefs.inMemory)
testImplementation(libs.turbine)
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.datastore.core.DataStore
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import de.paulwoitaschek.flowpref.Pref
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.Flow
Expand All @@ -35,6 +34,7 @@ import voice.playback.misc.Decibel
import voice.playback.misc.VolumeGain
import voice.playback.playstate.PlayStateManager
import voice.playbackScreen.batteryOptimization.BatteryOptimization
import voice.pref.Pref
import voice.sleepTimer.SleepTimer
import voice.sleepTimer.SleepTimerViewState
import javax.inject.Named
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package voice.playbackScreen

import de.paulwoitaschek.flowpref.inmemory.InMemoryPref
import io.kotest.matchers.shouldBe
import io.mockk.coEvery
import io.mockk.every
Expand All @@ -17,6 +16,7 @@ import voice.data.BookContent
import voice.data.Bookmark
import voice.data.Chapter
import voice.data.ChapterId
import voice.pref.inmemory.InMemoryPref
import voice.sleepTimer.SleepTimer
import voice.sleepTimer.SleepTimerViewState
import java.time.Instant
Expand Down
11 changes: 11 additions & 0 deletions pref/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
plugins {
id("voice.library")
}

dependencies {
implementation(libs.androidxCore)

testImplementation(libs.junit)
testImplementation(libs.robolectric)
testImplementation(libs.koTest.assert)
}
40 changes: 40 additions & 0 deletions pref/src/main/kotlin/voice/pref/AndroidPreferences.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package voice.pref

import android.content.SharedPreferences
import voice.pref.internal.AndroidPref
import voice.pref.internal.DelegatingPrefAdapter
import voice.pref.internal.InternalPrefAdapter
import java.util.concurrent.CopyOnWriteArrayList

class AndroidPreferences(private val sharedPrefs: SharedPreferences) {

private val registered = CopyOnWriteArrayList<AndroidPref<*>>()

@Suppress("unused")
fun <T> create(
key: String,
default: T,
adapter: PrefAdapter<T>,
): Pref<T> {
return create(key, default, DelegatingPrefAdapter(adapter))
}

internal fun <T> create(
key: String,
default: T,
adapter: InternalPrefAdapter<T>,
): Pref<T> {
return AndroidPref(
sharedPrefs,
adapter,
key,
default,
).also {
registered += it
}
}

fun clear(commit: Boolean = false) {
registered.forEach { it.delete(commit = commit) }
}
}
21 changes: 21 additions & 0 deletions pref/src/main/kotlin/voice/pref/Pref.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package voice.pref

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.filterNotNull
import kotlin.properties.ReadWriteProperty

abstract class Pref<T> : ReadWriteProperty<Any, T> {

@Suppress("LeakingThis")
var value: T by this

abstract fun setAndCommit(value: T)

abstract val flow: Flow<T>

abstract fun delete(commit: Boolean = false)
}

@Suppress("unused")
val <T : Any> Pref<T?>.flowNotNull: Flow<T>
get() = flow.filterNotNull()
7 changes: 7 additions & 0 deletions pref/src/main/kotlin/voice/pref/PrefAdapter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package voice.pref

interface PrefAdapter<T> {

fun toString(value: T): String
fun fromString(string: String): T
}
Loading

0 comments on commit c5623a1

Please sign in to comment.