From 5e0548037cf9bd5fe111ef8a48c9c1ef7dbbbc4a Mon Sep 17 00:00:00 2001 From: cioccarellia Date: Thu, 1 Oct 2020 08:39:56 +0200 Subject: [PATCH] v2.2.1: KsPrefs API: created PrefsCenter & implemented counter --- .../cioccarellia/ksprefs/api/PrefsCenter.kt | 22 ++++++++++++++++ library_info.gradle | 4 +-- .../com/cioccarellia/ksprefsample/App.kt | 3 +++ .../activities/main/MainActivity.kt | 4 +++ .../prefcenter/StartCounterPrefCenter.kt | 26 +++++++++++++++++++ 5 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 library/src/main/kotlin/com/cioccarellia/ksprefs/api/PrefsCenter.kt create mode 100644 test/src/main/kotlin/com/cioccarellia/ksprefsample/prefcenter/StartCounterPrefCenter.kt diff --git a/library/src/main/kotlin/com/cioccarellia/ksprefs/api/PrefsCenter.kt b/library/src/main/kotlin/com/cioccarellia/ksprefs/api/PrefsCenter.kt new file mode 100644 index 0000000..dfa241e --- /dev/null +++ b/library/src/main/kotlin/com/cioccarellia/ksprefs/api/PrefsCenter.kt @@ -0,0 +1,22 @@ +/** + * 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. + */ +package com.cioccarellia.ksprefs.api + +import com.cioccarellia.ksprefs.KsPrefs + +open class PrefsCenter( + val prefs: KsPrefs +) \ No newline at end of file diff --git a/library_info.gradle b/library_info.gradle index e1a36af..693f687 100755 --- a/library_info.gradle +++ b/library_info.gradle @@ -19,8 +19,8 @@ ext.library = [ compile_sdk : 30, publish_group : "com.cioccarellia", - publish_version : "2.2.0", - publish_version_code: 220, + publish_version : "2.2.1", + publish_version_code: 221, description : "Kotlin SharedPreferences, Simplified", website : "https://github.com/cioccarellia/ksprefs" diff --git a/test/src/main/kotlin/com/cioccarellia/ksprefsample/App.kt b/test/src/main/kotlin/com/cioccarellia/ksprefsample/App.kt index e9c8467..a2c5158 100644 --- a/test/src/main/kotlin/com/cioccarellia/ksprefsample/App.kt +++ b/test/src/main/kotlin/com/cioccarellia/ksprefsample/App.kt @@ -22,6 +22,7 @@ import com.cioccarellia.ksprefs.config.EncryptionType import com.cioccarellia.ksprefs.config.KspConfig import com.cioccarellia.ksprefs.config.model.AutoSavePolicy import com.cioccarellia.ksprefs.config.model.KeySize +import com.cioccarellia.ksprefsample.prefcenter.StartCounterPrefCenter class App : Application() { @@ -52,5 +53,7 @@ class App : Application() { override fun onCreate() { super.onCreate() appContext = this + + StartCounterPrefCenter.increment() } } \ No newline at end of file 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 192baad..66c03e9 100644 --- a/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/main/MainActivity.kt +++ b/test/src/main/kotlin/com/cioccarellia/ksprefsample/activities/main/MainActivity.kt @@ -34,6 +34,7 @@ import com.cioccarellia.ksprefsample.activities.dynamic.DynamicActivity import com.cioccarellia.ksprefsample.activities.json.JsonActivity import com.cioccarellia.ksprefsample.activities.numbers.NumbersActivity import com.cioccarellia.ksprefsample.activities.observer.ObserverActivity +import com.cioccarellia.ksprefsample.prefcenter.StartCounterPrefCenter import com.cioccarellia.ksprefsample.util.onClickDebounced class MainActivity : AppCompatActivity() { @@ -47,6 +48,8 @@ class MainActivity : AppCompatActivity() { private val detailsTestView by lazy { findViewById(R.id.detailsTestView) } + private val startCount = StartCounterPrefCenter.read() + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) @@ -98,6 +101,7 @@ class MainActivity : AppCompatActivity() { } else { appendLine("Encrypted using ${config.encryptionType.javaClass.simpleName}") } + appendLine("Start count $startCount") } private fun log(str: String) = Log.d("KsPref", str) diff --git a/test/src/main/kotlin/com/cioccarellia/ksprefsample/prefcenter/StartCounterPrefCenter.kt b/test/src/main/kotlin/com/cioccarellia/ksprefsample/prefcenter/StartCounterPrefCenter.kt new file mode 100644 index 0000000..6458333 --- /dev/null +++ b/test/src/main/kotlin/com/cioccarellia/ksprefsample/prefcenter/StartCounterPrefCenter.kt @@ -0,0 +1,26 @@ +/** + * 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. + */ +package com.cioccarellia.ksprefsample.prefcenter + +import com.cioccarellia.ksprefs.api.PrefsCenter +import com.cioccarellia.ksprefsample.App + +object StartCounterPrefCenter : PrefsCenter(App.prefs) { + private const val counterKey = "start_counter" + + fun increment() = prefs.push(counterKey, read() + 1) + fun read() = prefs.pull(counterKey, 0) +} \ No newline at end of file