Skip to content

Commit

Permalink
v2.4.0: Multithreading support (fixing #40), kotlin + dependency updates
Browse files Browse the repository at this point in the history
  • Loading branch information
cioccarellia committed Aug 31, 2023
1 parent 7cb0a51 commit 7aa5dea
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 79 deletions.
1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/runConfigurations/Autorilascio.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<a href="https://github.com/cioccarellia/ksprefs" target="_blank"><img width="100" src="https://raw.githubusercontent.com/cioccarellia/ksprefs/master/extras/ksprefs.png"></a>
</p>
<h1 align="center">KsPrefs</h1>
<p align="center">SharedPreferences. 100% Kotlin.</p>
<p align="center">Secure SharedPreferences</p>
<p align="center">
<a href="https://search.maven.org/artifact/com.github.cioccarellia/ksprefs"><img src="https://img.shields.io/maven-central/v/com.github.cioccarellia/ksprefs.svg?label=Maven%20Central" alt="Download from MavenCentral"></a>
<a href="https://app.circleci.com/pipelines/github/cioccarellia/ksprefs"><img src="https://circleci.com/gh/cioccarellia/ksprefs.svg?style=svg" alt="CircleCI"></a>
Expand All @@ -18,7 +18,7 @@

```gradle
dependencies {
implementation 'com.github.cioccarellia:ksprefs:2.3.2'
implementation 'com.github.cioccarellia:ksprefs:2.4.0'
}
```
</details>
Expand All @@ -27,7 +27,7 @@ dependencies {

```gradle
dependencies {
implementation("com.github.cioccarellia:ksprefs:2.3.2")
implementation("com.github.cioccarellia:ksprefs:2.4.0")
}
```
</details>
Expand All @@ -38,7 +38,7 @@ dependencies {
<dependency>
<groupId>com.github.cioccarellia</groupId>
<artifactId>ksprefs</artifactId>
<version>2.3.2</version>
<version>2.4.0</version>
<type>pom</type>
</dependency>
```
Expand Down
69 changes: 0 additions & 69 deletions dependencies.gradle

This file was deleted.

15 changes: 14 additions & 1 deletion library/src/main/kotlin/com/cioccarellia/ksprefs/KsPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 <T : Any> push(
key: String,
value: T,
Expand All @@ -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 <T : Any> queue(
key: String,
value: T
Expand All @@ -147,6 +149,7 @@ class KsPrefs(
* @return The value KsPref got back for the matching key, or [fallback].
* */
@CheckResult
@Synchronized
fun <T : Any> pull(
key: String,
fallback: T
Expand Down Expand Up @@ -178,7 +181,11 @@ class KsPrefs(
@CheckResult
inline fun <reified T : Any> 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.
Expand All @@ -204,6 +211,7 @@ class KsPrefs(
* @throws NoSuchKeyException If no value is found for the given [key].
* */
@CheckResult
@Synchronized
fun <T : Any> pull(
key: String,
kclass: KClass<T>
Expand Down Expand Up @@ -233,6 +241,7 @@ class KsPrefs(
* @throws NoSuchKeyException If no value is found for the given [key].
* */
@CheckResult
@Synchronized
fun <T : Any> pull(
key: String,
jclass: Class<T>
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -290,6 +301,7 @@ class KsPrefs(
*
* @param[key] The key for the target field.
* */
@Synchronized
fun remove(
key: String
) {
Expand All @@ -299,6 +311,7 @@ class KsPrefs(
/**
* Calls the [clear()] method on [Shared Preferences][SharedPreferences], and removes all entries.
* */
@Synchronized
fun clear() {
dispatcher.clear()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ import com.cioccarellia.ksprefs.exceptions.EngineException

internal fun <T> Result<T>.getOrThrowException(
operation: String = ""
): T = getOrElse { exception ->
): T = this.getOrElse { exception ->
throw EngineException.convertFrom(exception, operation)
}
4 changes: 2 additions & 2 deletions library_info.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ class MainActivity : AppCompatActivity() {

append(App.prefs.internalReport())
}



}

private fun KsPrefs.internalReport() = buildString {
Expand Down

0 comments on commit 7aa5dea

Please sign in to comment.