-
Notifications
You must be signed in to change notification settings - Fork 371
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2664 from PaulWoitaschek/prefs_migration
Add the prefs library as local sources
- Loading branch information
Showing
47 changed files
with
775 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.