Skip to content

Commit

Permalink
Updated dependencies & kotlin, simplified gradle scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
cioccarellia committed Aug 31, 2023
1 parent ddcabe8 commit 7cb0a51
Show file tree
Hide file tree
Showing 18 changed files with 111 additions and 114 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

3 changes: 3 additions & 0 deletions .idea/kotlinc.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/misc.xml

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

8 changes: 7 additions & 1 deletion .idea/modules.xml

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

69 changes: 34 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
<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.5.31-orange.svg" alt="Kotlin"></a>
<a href="https://kotlinlang.org/docs/releases.html"><img src="https://img.shields.io/badge/kotlin-1.9.0-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://source.android.com/setup/start/build-numbers"><img src="https://img.shields.io/badge/compile-33-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>

Expand Down Expand Up @@ -50,7 +50,6 @@ dependencies {
- ⚙️ Fully customizable behaviour.
- 🔒 Built-in cryptography & decoding engines (PlainText, Base64, AES-CBC, AES-ECB, Android KeyStore + AES-GCM / RSA KeyPair).
- 🗂 Extensive type & enum support.
- 🧡 Kotlin powered.

```kotlin
val prefs = KsPrefs(applicationContext)
Expand Down Expand Up @@ -94,13 +93,13 @@ class App : Application() {

### Terminology

| Term | Description |
|------------------------|-----------------------------------------------------------------------------------------------------------------------|
| SP | Android Shared Preferences |
| Entry | Key-Value pair stored by _SP_. Thus the basic data entity which can be pushed and pulled |
| Term | Description |
|------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| SP | Android Shared Preferences |
| Entry | Key-Value pair stored by _SP_. Thus the basic data entity which can be pushed and pulled |
| Persistent XML Storage | _SP_ XML file containing actual entries. Stored in the application [private storage](https://developer.android.com/training/data-storage/app-specific) |
| | |
| | |
| | |
| | |


### Read <small>(Pull)</small>
Expand Down Expand Up @@ -157,14 +156,14 @@ val prefs = KsPrefs(applicationContext) {
}
```

| Field | Type | Description | Default Value |
|-----------------|----------------|--------------------------------------------------------------------------------------------|----------------------|
| encryptionType | EncryptionType | Encryption technique used to encrypt and decrypt data | PlainText() |
| commitStrategy | CommitStrategy | Strategy to use at the moment of writing preferences entries to the persistent XML storage | CommitStrategy.APPLY |
| autoSave | AutoSavePolicy | Whether after a `push()` operation changes are saved to the persistent XML storage; saving strategy depending on `commitStrategy` | AutoSavePolicy.AUTOMATIC |
| mode | Int | SharedPreferences access mode | Context.MODE_PRIVATE |
| charset | Charset | Charset used for string-to-byte and byte-to-string conversions | Charsets.UTF_8 |
| keyRegex | Regex? | Regular Expression which, if non null, every key must match. | null |
| Field | Type | Description | Default Value |
|----------------|----------------|-----------------------------------------------------------------------------------------------------------------------------------|--------------------------|
| encryptionType | EncryptionType | Encryption technique used to encrypt and decrypt data | PlainText() |
| commitStrategy | CommitStrategy | Strategy to use at the moment of writing preferences entries to the persistent XML storage | CommitStrategy.APPLY |
| autoSave | AutoSavePolicy | Whether after a `push()` operation changes are saved to the persistent XML storage; saving strategy depending on `commitStrategy` | AutoSavePolicy.AUTOMATIC |
| mode | Int | SharedPreferences access mode | Context.MODE_PRIVATE |
| charset | Charset | Charset used for string-to-byte and byte-to-string conversions | Charsets.UTF_8 |
| keyRegex | Regex? | Regular Expression which, if non null, every key must match. | null |



Expand All @@ -182,12 +181,12 @@ However, if `autoSave` is turned off (using `AutoSavePolicy.MANUAL`), `push()` w

Here is a table representing when values are saved to the storage, depending on the policy in use.

| `AutoSavePolicy` | AUTO | MANUAL |
|---------|--------------------|--------------------|
| push() | :white_check_mark: | :x: |
| queue() | :x: | :x: |
| save() | :white_check_mark: | :white_check_mark: |
| SharedPreferences.Editor.commit() | :white_check_mark: | :white_check_mark: |
| `AutoSavePolicy` | AUTO | MANUAL |
|-----------------------------------|--------------------|--------------------|
| push() | :white_check_mark: | :x: |
| queue() | :x: | :x: |
| save() | :white_check_mark: | :white_check_mark: |
| SharedPreferences.Editor.commit() | :white_check_mark: | :white_check_mark: |
| SharedPreferences.Editor.apply() | :white_check_mark: | :white_check_mark: |

*:pushpin: `AutoSavePolicy` chooses when to write changes to the persistent XML storage and when to keep them in memory.*<br>
Expand All @@ -198,13 +197,13 @@ The best (and default) practise while dealing with SharedPreferences is to use `

Here is a table representing various features of different commit strategies. Check out the official documentation [here](https://developer.android.com/reference/android/content/SharedPreferences.Editor.html) and see [this](https://stackoverflow.com/questions/5960678/whats-the-difference-between-commit-and-apply-in-sharedpreferences) post for more intel.

| `CommitStrategy` | APPLY | COMMIT | NONE |
|--------|--------------------|--------------------|------|
| in-memory | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| XML | :white_check_mark: | :white_check_mark: | :x: |
| async | :white_check_mark: | :x: | :heavy_minus_sign: |
| atomic | :white_check_mark: | :white_check_mark: | :heavy_minus_sign: |
| error report | :x: | :white_check_mark: | :heavy_minus_sign: |
| `CommitStrategy` | APPLY | COMMIT | NONE |
|------------------|--------------------|--------------------|---------------------|
| in-memory | :white_check_mark: | :white_check_mark: | :white_check_mark: |
| XML | :white_check_mark: | :white_check_mark: | :x: |
| async | :white_check_mark: | :x: | :heavy_minus_sign: |
| atomic | :white_check_mark: | :white_check_mark: | :heavy_minus_sign: |
| error report | :x: | :white_check_mark: | :heavy_minus_sign: |

*:pushpin: The `CommitStrategy` involves how to write changes to the persistent XML storage.*<br>

Expand All @@ -222,11 +221,11 @@ There are two storing scopes for SharedPreferences:
- XML (key-value pairs are kept on a file). Writing to a file is mildly expensive but it allows preferences to survive across application restarts.<br>
Here is a table explaining how different methods inside KsPrefs touch and go through those storing scopes.

| `StoringScope` | in-memory | XML |
|---------|--------------------|---------------------------------|
| push(k, v) | :white_check_mark: | :white_check_mark: (By default) |
| queue(k, v) | :white_check_mark: | :x: |
| save() | :white_check_mark: | :white_check_mark: |
| `StoringScope` | in-memory | XML |
|----------------|--------------------|---------------------------------|
| push(k, v) | :white_check_mark: | :white_check_mark: (By default) |
| queue(k, v) | :white_check_mark: | :x: |
| save() | :white_check_mark: | :white_check_mark: |

*:pushpin: The `StoringScope` determines at which level changes are propagated.*<br>

Expand Down
30 changes: 9 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

/**
* Designed and developed by Andrea Cioccarelli (@cioccarellia)
*
Expand All @@ -13,12 +15,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

apply from: rootProject.file("gradle/versions_plugin_config.gradle")
apply plugin: 'io.codearte.nexus-staging'
apply plugin: "io.codearte.nexus-staging"

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

repositories {
google()
Expand All @@ -28,10 +30,10 @@ buildscript {
}

dependencies {
classpath deps.gradle_plugins.android
classpath deps.gradle_plugins.kotlin
classpath 'io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.30.0'
classpath 'com.github.ben-manes:gradle-versions-plugin:0.39.0'
classpath "com.android.tools.build:gradle:8.1.1"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:${kotlin_version}"
classpath "io.codearte.gradle.nexus:gradle-nexus-staging-plugin:0.30.0"
classpath "com.github.ben-manes:gradle-versions-plugin:0.47.0"
}
}

Expand All @@ -42,18 +44,4 @@ allprojects {
mavenCentral()
gradlePluginPortal()
}
}

subprojects {
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8

kotlinOptions {
jvmTarget = 1.8

freeCompilerArgs += ["-module-name", project.path.replace(':', '')]
freeCompilerArgs += ["-Xopt-in=kotlin.RequiresOptIn"]
}
}
}
1 change: 0 additions & 1 deletion dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/

ext.versions = [
build_tools: "30.0.2",
kt : "$kotlin_version"
]

Expand Down
6 changes: 5 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
kotlin.code.style=official
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
org.gradle.configuration-cache=true
15 changes: 1 addition & 14 deletions gradle/android_common_config.gradle
Original file line number Diff line number Diff line change
@@ -1,24 +1,11 @@
apply plugin: "kotlin-android"

apply from: rootProject.file("dependencies.gradle")
apply from: rootProject.file("library_info.gradle")

ext.module_package_id = "${library.publish_group}.${module_name}"

android {
compileSdkVersion library.compile_sdk
buildToolsVersion versions.build_tools

compileOptions {
sourceCompatibility 1.8
targetCompatibility 1.8
}

lintOptions {

abortOnError false

}

defaultConfig {
minSdkVersion library.min_sdk
Expand All @@ -41,4 +28,4 @@ android {
java.srcDir("src/$name/kotlin")
}
}
}
}
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.3-all.zip
30 changes: 20 additions & 10 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,24 @@ ext {
apply from: rootProject.file("gradle/android_library_config.gradle")

dependencies {
compileOnly deps.androidx.annotations
implementation deps.kotlin.stdlib8
implementation deps.kotlin.coroutines.android
implementation deps.androidx.lifecycle.ext

testImplementation deps.test.junit
testImplementation deps.test.androidx_test_core
testImplementation deps.test.mockito_core
testImplementation deps.test.robolectric
testImplementation deps.test.truth
implementation "androidx.annotation:annotation:1.6.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:${kotlin_version}"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.7.3"
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"

testImplementation "junit:junit:4.13.2"
testImplementation "androidx.test:core:1.5.0"
testImplementation "org.mockito:mockito-core:5.5.0"
testImplementation "org.robolectric:robolectric:4.10.3"
testImplementation "com.google.truth:truth:1.1.5"
}

android {
compileOptions {
sourceCompatibility JavaVersion.VERSION_17
targetCompatibility JavaVersion.VERSION_17
}

namespace 'com.cioccarellia.ksprefs'
}
2 changes: 1 addition & 1 deletion library/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<manifest package="com.cioccarellia.ksprefs" />
<manifest />
6 changes: 3 additions & 3 deletions library/src/main/kotlin/com/cioccarellia/ksprefs/KsPrefs.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class KsPrefs(
fun <T : Any> queue(
key: String,
value: T
): Unit = dispatcher.push(key, value, CommitStrategy.NONE)
): Unit = dispatcher.push(key, value, strategy = CommitStrategy.NONE)

/**
* This function pulls a value from the [Shared Preferences][SharedPreferences] object.
Expand Down Expand Up @@ -178,7 +178,7 @@ class KsPrefs(
@CheckResult
inline fun <reified T : Any> pull(
key: String
): T = dispatcher.pull(key, T::class)
): T = dispatcher.pull(key, kclass = T::class)

/**
* This function (unsafely) pulls a value from the [Shared Preferences][SharedPreferences] object.
Expand Down Expand Up @@ -207,7 +207,7 @@ class KsPrefs(
fun <T : Any> pull(
key: String,
kclass: KClass<T>
): T = dispatcher.pull(key, kclass)
): T = dispatcher.pull(key, kclass = kclass)

/**
* This function (unsafely) pulls a value from the [Shared Preferences][SharedPreferences] object.
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 XML_PREFIX = "ksp_"
const 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 @@ -19,6 +19,7 @@ import android.content.SharedPreferences
import com.cioccarellia.ksprefs.KsPrefs
import com.cioccarellia.ksprefs.config.model.AutoSavePolicy
import com.cioccarellia.ksprefs.config.model.CommitStrategy
import com.cioccarellia.ksprefs.config.model.CommitStrategy.*

internal typealias Writer = SharedPreferences.Editor

Expand All @@ -39,12 +40,12 @@ internal fun Writer.finalize(
}
}

@Suppress("NON_EXHAUSTIVE_WHEN")
internal fun Writer.forceFinalization(
commitStrategy: CommitStrategy
) {
when (commitStrategy) {
CommitStrategy.APPLY -> apply()
CommitStrategy.COMMIT -> commit()
APPLY -> apply()
COMMIT -> commit()
NONE -> {}
}
}
Loading

0 comments on commit 7cb0a51

Please sign in to comment.