Skip to content

Commit

Permalink
v2.2.1: KsPrefs API: created PrefsCenter & implemented counter
Browse files Browse the repository at this point in the history
  • Loading branch information
cioccarellia committed Oct 1, 2020
1 parent 7593a02 commit 5e05480
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
)
4 changes: 2 additions & 2 deletions library_info.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions test/src/main/kotlin/com/cioccarellia/ksprefsample/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {

Expand Down Expand Up @@ -52,5 +53,7 @@ class App : Application() {
override fun onCreate() {
super.onCreate()
appContext = this

StartCounterPrefCenter.increment()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -47,6 +48,8 @@ class MainActivity : AppCompatActivity() {

private val detailsTestView by lazy { findViewById<TextView>(R.id.detailsTestView) }

private val startCount = StartCounterPrefCenter.read()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 5e05480

Please sign in to comment.