From 7aa5dea8df4676b604dfe0cbfaf3a64012b2f946 Mon Sep 17 00:00:00 2001
From: cioccarellia
Date: Fri, 1 Sep 2023 01:05:31 +0200
Subject: [PATCH] v2.4.0: Multithreading support (fixing #40), kotlin +
dependency updates
---
.idea/misc.xml | 1 -
.idea/runConfigurations/Autorilascio.xml | 2 +-
README.md | 8 +--
dependencies.gradle | 69 -------------------
.../com/cioccarellia/ksprefs/KsPrefs.kt | 15 +++-
.../ksprefs/extensions/ExceptionExts.kt | 2 +-
library_info.gradle | 4 +-
.../activities/json/JsonActivity.kt | 27 ++++++++
.../activities/main/MainActivity.kt | 3 +
9 files changed, 52 insertions(+), 79 deletions(-)
delete mode 100755 dependencies.gradle
diff --git a/.idea/misc.xml b/.idea/misc.xml
index 4c0f619..1c5a477 100755
--- a/.idea/misc.xml
+++ b/.idea/misc.xml
@@ -1,4 +1,3 @@
-
diff --git a/.idea/runConfigurations/Autorilascio.xml b/.idea/runConfigurations/Autorilascio.xml
index 631296a..1de4737 100755
--- a/.idea/runConfigurations/Autorilascio.xml
+++ b/.idea/runConfigurations/Autorilascio.xml
@@ -2,7 +2,7 @@
-
+
KsPrefs
-SharedPreferences. 100% Kotlin.
+Secure SharedPreferences
@@ -18,7 +18,7 @@
```gradle
dependencies {
- implementation 'com.github.cioccarellia:ksprefs:2.3.2'
+ implementation 'com.github.cioccarellia:ksprefs:2.4.0'
}
```
@@ -27,7 +27,7 @@ dependencies {
```gradle
dependencies {
- implementation("com.github.cioccarellia:ksprefs:2.3.2")
+ implementation("com.github.cioccarellia:ksprefs:2.4.0")
}
```
@@ -38,7 +38,7 @@ dependencies {
com.github.cioccarellia
ksprefs
- 2.3.2
+ 2.4.0
pom
```
diff --git a/dependencies.gradle b/dependencies.gradle
deleted file mode 100755
index cca1ae8..0000000
--- a/dependencies.gradle
+++ /dev/null
@@ -1,69 +0,0 @@
-/**
- * Designed and developed by Andrea Cioccarelli (@cioccarellia)
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-ext.versions = [
- kt : "$kotlin_version"
-]
-
-ext.deps = [
- gradle_plugins: [
- android : "com.android.tools.build:gradle:7.0.2",
- versions : "com.github.ben-manes:gradle-versions-plugin:0.39.0",
- kotlin : "org.jetbrains.kotlin:kotlin-gradle-plugin:${versions.kt}",
- reflection : "org.jetbrains.kotlin:kotlin-reflect:${versions.kt}",
- serialization: "org.jetbrains.kotlin:kotlin-serialization:${versions.kt}"
- ],
-
- kotlin : [
- stdlib8 : "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${versions.kt}",
- coroutines : [
- core : "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1",
- android: "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
- ],
- test : [
- mockito: "com.nhaarman.mockitokotlin2:mockito-kotlin:2.2.0"
- ],
- serialization: "org.jetbrains.kotlinx:kotlinx-serialization-runtime:0.20.0"
- ],
-
- androidx : [
- annotations : "androidx.annotation:annotation:1.1.0",
- app_compat : "androidx.appcompat:appcompat:1.3.1",
- constraint_layout: "com.android.support.constraint:constraint-layout:2.0.4",
- material : "com.google.android.material:material:1.4.0",
- lifecycle : [
- live_data : "androidx.lifecycle:lifecycle-livedata:2.2.0",
- view_model: "androidx.lifecycle:lifecycle-viewmodel:2.2.0",
- ext : "androidx.lifecycle:lifecycle-extensions:2.2.0"
- ],
- test : [
- core: "androidx.test:core:1.1.0"
- ]
- ],
-
- debug : [
- timber: "com.jakewharton.timber:timber:4.7.1"
- ],
-
- test : [
- junit : "junit:junit:4.13.2",
- robolectric : "org.robolectric:robolectric:4.6.1",
- androidx_test_core : "androidx.test:core:1.4.0",
- androidx_test_runner_espresso: "androidx.test.espresso:espresso-core:3.2.0",
- mockito_core : "org.mockito:mockito-core:3.12.4",
- truth : "com.google.truth:truth:1.1.3"
- ]
-]
\ No newline at end of file
diff --git a/library/src/main/kotlin/com/cioccarellia/ksprefs/KsPrefs.kt b/library/src/main/kotlin/com/cioccarellia/ksprefs/KsPrefs.kt
index 0989df9..ea6149d 100755
--- a/library/src/main/kotlin/com/cioccarellia/ksprefs/KsPrefs.kt
+++ b/library/src/main/kotlin/com/cioccarellia/ksprefs/KsPrefs.kt
@@ -100,6 +100,7 @@ class KsPrefs(
* @param[value] The value to be derived and stored.
* @param[commitStrategy] The strategy defining how to finalize this operation.
* */
+ @Synchronized
fun push(
key: String,
value: T,
@@ -124,6 +125,7 @@ class KsPrefs(
* @param[key] The key for the target field.
* @param[value] The value to be derived and stored.
* */
+ @Synchronized
fun queue(
key: String,
value: T
@@ -147,6 +149,7 @@ class KsPrefs(
* @return The value KsPref got back for the matching key, or [fallback].
* */
@CheckResult
+ @Synchronized
fun pull(
key: String,
fallback: T
@@ -178,7 +181,11 @@ class KsPrefs(
@CheckResult
inline fun pull(
key: String
- ): T = dispatcher.pull(key, kclass = T::class)
+ ): T {
+ synchronized(this) {
+ return dispatcher.pull(key, kclass = T::class)
+ }
+ }
/**
* This function (unsafely) pulls a value from the [Shared Preferences][SharedPreferences] object.
@@ -204,6 +211,7 @@ class KsPrefs(
* @throws NoSuchKeyException If no value is found for the given [key].
* */
@CheckResult
+ @Synchronized
fun pull(
key: String,
kclass: KClass
@@ -233,6 +241,7 @@ class KsPrefs(
* @throws NoSuchKeyException If no value is found for the given [key].
* */
@CheckResult
+ @Synchronized
fun pull(
key: String,
jclass: Class
@@ -259,6 +268,7 @@ class KsPrefs(
* @return Whether the value exists inside the storage or not.
* */
@CheckResult
+ @Synchronized
fun exists(
key: String
): Boolean = dispatcher.exists(key)
@@ -277,6 +287,7 @@ class KsPrefs(
*
* @param[commitStrategy] Optional parameterization for the [commit strategy][KspConfig.commitStrategy].
* */
+ @Synchronized
fun save(
commitStrategy: CommitStrategy = config.commitStrategy
): Unit = dispatcher.save(commitStrategy)
@@ -290,6 +301,7 @@ class KsPrefs(
*
* @param[key] The key for the target field.
* */
+ @Synchronized
fun remove(
key: String
) {
@@ -299,6 +311,7 @@ class KsPrefs(
/**
* Calls the [clear()] method on [Shared Preferences][SharedPreferences], and removes all entries.
* */
+ @Synchronized
fun clear() {
dispatcher.clear()
}
diff --git a/library/src/main/kotlin/com/cioccarellia/ksprefs/extensions/ExceptionExts.kt b/library/src/main/kotlin/com/cioccarellia/ksprefs/extensions/ExceptionExts.kt
index 8dbee27..64f5d74 100755
--- a/library/src/main/kotlin/com/cioccarellia/ksprefs/extensions/ExceptionExts.kt
+++ b/library/src/main/kotlin/com/cioccarellia/ksprefs/extensions/ExceptionExts.kt
@@ -19,6 +19,6 @@ import com.cioccarellia.ksprefs.exceptions.EngineException
internal fun Result.getOrThrowException(
operation: String = ""
-): T = getOrElse { exception ->
+): T = this.getOrElse { exception ->
throw EngineException.convertFrom(exception, operation)
}
\ No newline at end of file
diff --git a/library_info.gradle b/library_info.gradle
index 4675bcc..03e0591 100755
--- a/library_info.gradle
+++ b/library_info.gradle
@@ -21,8 +21,8 @@ ext.library = [
artifact : "ksprefs",
publish_group : "com.github.cioccarellia",
- publish_version : "2.3.3",
- publish_version_code: 233,
+ publish_version : "2.4.0",
+ publish_version_code: 240,
description : "Kotlin SharedPreferences, Simplified",
website : "https://github.com/cioccarellia/ksprefs"
diff --git a/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/json/JsonActivity.kt b/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/json/JsonActivity.kt
index 44db3e7..c4ccb5b 100755
--- a/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/json/JsonActivity.kt
+++ b/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/json/JsonActivity.kt
@@ -16,10 +16,15 @@
package com.cioccarellia.ksprefsample.activities.json
import android.os.Bundle
+import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.cioccarellia.ksprefsample.App
import com.cioccarellia.ksprefsample.R
+import kotlinx.coroutines.CoroutineScope
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
import org.json.JSONObject
class JsonActivity : AppCompatActivity() {
@@ -51,6 +56,28 @@ class JsonActivity : AppCompatActivity() {
Toast.LENGTH_LONG
).show()
+
+/*
+ // multithreading tests
+ for (i in 1..1000){
+ CoroutineScope(Dispatchers.Default).launch() {
+ App.prefs.push("xxx", true)
+ val isEnabled = App.prefs.pull("xxx", true)
+ Log.d("Testing", isEnabled.toString())
+ }
+ }
+
+
+
+ for (i in 1..1000){
+ CoroutineScope(Dispatchers.Default).launch() {
+ App.prefs.push("xxx", false)
+ val isEnabled = App.prefs.pull("xxx", true)
+ Log.d("Testing", isEnabled.toString())
+ }
+ }
+*/
+
finish()
}
}
diff --git a/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/main/MainActivity.kt b/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/main/MainActivity.kt
index f910c58..f7d9d55 100755
--- a/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/main/MainActivity.kt
+++ b/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/main/MainActivity.kt
@@ -89,6 +89,9 @@ class MainActivity : AppCompatActivity() {
append(App.prefs.internalReport())
}
+
+
+
}
private fun KsPrefs.internalReport() = buildString {