Skip to content

Commit

Permalink
Made kotlin first util
Browse files Browse the repository at this point in the history
  • Loading branch information
islamdidarmd committed Oct 30, 2019
1 parent 2e60975 commit 3fbfca0
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 66 deletions.
134 changes: 109 additions & 25 deletions .idea/codeStyles/Project.xml

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

20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ Step 2. Add the dependency

```groovy
dependencies {
implementation 'com.github.islamdidarmd:Kpref:0.1.0'
implementation 'com.github.islamdidarmd:Kpref:0.1.1'
}
```

### How to use

```kotlin
//save values in a default sharedPreference
Kprefs.initDefault(this)
//save values in a default sharedPreference
Kprefs.initDefault(this)

Kprefs.save("test", 10)
Kprefs.save("test", 10)

val x: Int? = Kprefs.get("test", Int::class.java)
val x: Int? = Kprefs.get("")


//save values in your preference
val pref = getSharedPreferences("my_preference", Context.MODE_PRIVATE)
Kprefs.init(pref)
//save values in your preference
val pref = getSharedPreferences("my_preference", Context.MODE_PRIVATE)
Kprefs.init(pref)

Kprefs.save("testMyPref", "Saved")
Kprefs.save("testMyPref", "Saved")

val value: String? = Kprefs.get("testMyPref", String::class.java)
val value: String? = Kprefs.get("")
```


Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/didar/sharedprefshelper/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class MainActivity : AppCompatActivity() {

Kprefs.save("test", 10)

val x: Int? = Kprefs.get("", Int::class.java)
val x: Int? = Kprefs.get("")


//save values in your preference
Expand All @@ -25,6 +25,6 @@ class MainActivity : AppCompatActivity() {

Kprefs.save("testMyPref", "Saved")

val value: String? = Kprefs.get("", String::class.java)
val value: String? = Kprefs.get("")
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.3.2'
classpath 'com.android.tools.build:gradle:3.5.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath 'com.github.dcendents:android-maven-gradle-plugin:2.1'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Wed Mar 20 15:08:04 BDT 2019
#Wed Oct 30 10:56:16 BDT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
52 changes: 26 additions & 26 deletions kprefs/src/main/java/com/islamdidarmd/kprefs/Helper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ import android.util.Log
import com.google.gson.Gson
import java.lang.reflect.Type

class Kprefs {
object Kprefs {
val TAG = "Kprefs"

companion object {
val TAG = "Kprefs"

private var prefs: SharedPreferences? = null
var prefs: SharedPreferences? = null
val gson by lazy {
Gson()
}

fun initDefault(context: Context) {
if (prefs == null) {
prefs = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
}
fun initDefault(context: Context) {
if (prefs == null) {
prefs = PreferenceManager.getDefaultSharedPreferences(context.applicationContext)
}
}

fun init(prefs: SharedPreferences) {
this.prefs = prefs
}
fun init(prefs: SharedPreferences) {
this.prefs = prefs
}

fun save(key: String, value: Any) {
if (prefs == null) {
throw NullPointerException("Kprefs not initialized. Data won't save...")
} else {
prefs!!.edit().putString(key, Gson().toJson(value)).apply()
}
fun save(key: String, value: Any) {
if (prefs == null) {
throw NullPointerException("Kprefs not initialized. Data won't save...")
} else {
prefs!!.edit().putString(key, gson.toJson(value)).apply()
}
}

fun <T> get(key: String, type: Type): T? {
val x: T?
inline fun <reified T> get(key: String): T? {
val x: T?

if (prefs == null) {
throw NullPointerException("Kprefs not initialized. Data won't save...")
} else {
x = Gson().fromJson(prefs!!.getString(key, ""), type)
}
return x
if (prefs == null) {
throw NullPointerException("Kprefs not initialized. Data won't save...")
} else {
x = gson.fromJson(prefs!!.getString(key, ""), T::class.java)
}
return x
}
}

0 comments on commit 3fbfca0

Please sign in to comment.