Skip to content

Commit

Permalink
Version bump: updated kotlin. added clear() method
Browse files Browse the repository at this point in the history
  • Loading branch information
cioccarellia committed Apr 7, 2021
1 parent f895534 commit 39a9e42
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 22 deletions.
2 changes: 0 additions & 2 deletions .github/FUNDING.yml

This file was deleted.

9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
<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>
<a href="https://app.codacy.com/manual/cioccarellia/ksprefs/dashboard"><img src="https://api.codacy.com/project/badge/Grade/f10cdbdbe7b84d0ea7a03b755c104e03" alt="Codacy"></a>
<a href="https://kotlinlang.org/docs/releases.html"><img src="https://img.shields.io/badge/kotlin-1.4.30-orange.svg" alt="Kotlin"></a>
<a href="https://kotlinlang.org/docs/releases.html"><img src="https://img.shields.io/badge/kotlin-1.4.32-orange.svg" alt="Kotlin"></a>
<a href="https://source.android.com/setup/start/build-numbers"><img src="https://img.shields.io/badge/min-19-00e676.svg" alt="Android Min Sdk"></a>
<a href="https://source.android.com/setup/start/build-numbers"><img src="https://img.shields.io/badge/compile-30-00e676.svg" alt="Android Compile Version"></a>
<a href="https://github.com/cioccarellia/ksprefs/blob/master/LICENSE.md"><img src="https://img.shields.io/badge/license-Apache%202.0-blue.svg" alt="License"></a>
</p>


#### ⚠️ since 2.2.4 ksprefs has been moved to mavenCentral. The publishing coordinates have changed.
<details open><summary>Gradle</summary>

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

```gradle
dependencies {
implementation("com.github.cioccarellia:ksprefs:2.2.4")
implementation("com.github.cioccarellia:ksprefs:2.2.5")
}
```
</details>
Expand All @@ -39,7 +38,7 @@ dependencies {
<dependency>
<groupId>com.github.cioccarellia</groupId>
<artifactId>ksprefs</artifactId>
<version>2.2.4</version>
<version>2.2.5</version>
<type>pom</type>
</dependency>
```
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ apply from: rootProject.file("gradle/versions_plugin_config.gradle")
apply plugin: 'io.codearte.nexus-staging'

buildscript {
ext.kotlin_version = '1.4.30'
ext.kotlin_version = '1.4.32'
apply from: rootProject.file("dependencies.gradle")

repositories {
Expand Down
7 changes: 7 additions & 0 deletions library/src/main/kotlin/com/cioccarellia/ksprefs/KsPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,11 @@ class KsPrefs(
) {
dispatcher.remove(key)
}

/**
* Calls the [clear()] method on [Shared Preferences][SharedPreferences], and removes all entries.
* */
fun clear() {
dispatcher.clear()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data class KspConfig @RestrictTo(RestrictTo.Scope.LIBRARY) constructor(
@IntRange(from = 0x0000, to = 0x0010)
var mode: Int = Defaults.MODE,
var charset: Charset = Defaults.CHARSET,
var xmlPrefix: String = Defaults.XMLPREFIX,
var xmlPrefix: String = Defaults.XML_PREFIX,

var autoSave: AutoSavePolicy = Defaults.AUTO_SAVE_POLICY,
var commitStrategy: CommitStrategy = Defaults.COMMIT_STRATEGY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal object Defaults {

/** Charset */
val CHARSET = Charsets.UTF_8
val XMLPREFIX = "ksp_"
val XML_PREFIX = "ksp_"

/** Saving */
val AUTO_SAVE_POLICY = AutoSavePolicy.AUTOMATIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,7 @@ internal class KspDispatcher(

@PublishedApi
internal fun remove(key: String) = enclosure.remove(key)

@PublishedApi
internal fun clear() = enclosure.clear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ import com.cioccarellia.ksprefs.extensions.*
internal class KspEnclosure(
private val namespace: String,
context: Context,
internal var sharedReader: Reader = context.sharedPrefs(namespace),
internal var sharedReader: Reader = context.sharedprefs(namespace),
internal val engine: Engine = EnginePicker.select(context)
) {
private inline val defaultCommitStrategy
get() = KsPrefs.config.commitStrategy

private inline fun deriveVal(
private inline fun deriveValue(
value: ByteArray
): ByteArray = engine.derive(
Transmission(value)
).payload

private inline fun integrateVal(
private inline fun integrateValue(
value: ByteArray
): ByteArray = engine.integrate(
Transmission(value)
Expand All @@ -59,13 +59,13 @@ internal class KspEnclosure(
internal fun read(
key: String,
fallback: ByteArray
) = integrateVal(
sharedReader.read(deriveKey(key), deriveVal(fallback))
) = integrateValue(
sharedReader.read(deriveKey(key), deriveValue(fallback))
)

internal fun readUnsafe(
key: String
) = integrateVal(
) = integrateValue(
sharedReader.readOrThrow(deriveKey(key))
)

Expand All @@ -74,7 +74,7 @@ internal class KspEnclosure(
value: ByteArray,
strategy: CommitStrategy
) = with(sharedReader.edit()) {
write(deriveKey(key), deriveVal(value))
write(deriveKey(key), deriveValue(value))
finalize(strategy)
}

Expand All @@ -96,4 +96,9 @@ internal class KspEnclosure(
delete(deriveKey(key))
finalize(defaultCommitStrategy)
}

internal fun clear() = with(sharedReader.edit()) {
clear()
finalize(commitStrategy = CommitStrategy.APPLY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import androidx.annotation.RestrictTo
import com.cioccarellia.ksprefs.KsPrefs

@RestrictTo(RestrictTo.Scope.LIBRARY)
internal fun Context.sharedPrefs(namespace: String): Reader = try {
val actualStorageFileName = "${KsPrefs.config.xmlPrefix}$namespace"
internal fun Context.sharedprefs(namespace: String, prefix: String = KsPrefs.config.xmlPrefix): Reader = try {
val actualStorageFileName = "${prefix}$namespace"
getSharedPreferences(actualStorageFileName, KsPrefs.config.mode)!!
} catch (knpe: KotlinNullPointerException) {
throw IllegalStateException("Could not get SharedPreferences instance")
throw IllegalStateException("Could not create SharedPreferences instance")
}
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.2.4",
publish_version_code: 224,
publish_version : "2.2.5",
publish_version_code: 225,

description : "Kotlin SharedPreferences, Simplified",
website : "https://github.com/cioccarellia/ksprefs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ class BatchActivity : AppCompatActivity() {
withContext(Dispatchers.Default) {
for (i in 0..n) {
prefs.expose().edit().putString("dfu-$i", i.toString()).apply()

withContext(Dispatchers.Main) {
progressDfu.progress++
defaultTitle.text = "SharedPreferences.edit() -> $i"
Expand Down

0 comments on commit 39a9e42

Please sign in to comment.