Skip to content

Commit

Permalink
Allow any data object or its arrays to be saved.
Browse files Browse the repository at this point in the history
  • Loading branch information
muddassir235 committed Jul 16, 2021
1 parent 2cb62d7 commit 57e09e0
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 85 deletions.
27 changes: 10 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,49 +60,49 @@ toast("Hello World!")
toastLong("Hello World!!!!!!")
```
#### Save to SharedPreferences
Save any boolean, int, long, float, string, or any array of (boolean, int, long, float, string), any Serializable Object, or an ArrayList of any Serializable Objects in SharedPreferences from within a Context. (Throws unsupported type exception in case the object is not serializable.)
Save any boolean, int, long, float, string, or any array of (boolean, int, long, float, string) or any data object in SharedPreferences from within a Context.
```kotlin
save("mykey", true) // Boolean
save("mykey", 1) // Int
save("mykey", 1L) // Long
save("mykey", 1.0f) // Float
save("mykey", "My String") // String
save("mykey", arrayOf("my", "strings")) // Arrays of all primitive types are supported
save("mykey", anyDataObject) // Data Object
save("mykey", arrayOf(true, false)) // Arrays of all primitive types are supported
.
.
.
save("mykey", anySerializableObject) // Any Serializable object can be saved.
save("mykey", arrayListOfAnySerializableObject) // An ArrayList of any Serializable Object can also be saved.
save("mykey", Array<AnyDataObject>) // Any data object array can be saved.
```
#### Load from SharedPreferences
Load any saved boolean, int, long, float, string, or array of (boolean, int, long, float, string), a Serializable Object, or an ArrayList of any Serializable Objects from SharedPreferences within a Context. (Throws unsupported type exception in case the object is not serializable.)
Load any saved boolean, int, long, float, string, or array of (boolean, int, long, float, string) or any data object from SharedPreferences within a Context.
```kotlin
val value = load<Boolean>("mykey")
val value = load<Int>("mykey")
val value = load<Long>("mykey")
val value = load<Float>("mykey")
val value = load<String>("mykey")
val value = load<MyDataObject>("mykey")
val value = load<Array<String>>("mykey")
.
.
.
val value = load<MySerializableObject>("mykey")
val value = load<ArrayList<MySerializableObject>>("mykey")
val value = load<Array<MyDataObject>>("mykey")
```
#### Delete from SharedPreferences
Load any saved boolean, int, long, float, string, or array of (boolean, int, long, float, string), a Serializable Object, or an ArrayList of any Serializable Objects from SharedPreferences within a Context. (Throws unsupported type exception in case the object is not serializable.)
Load any saved boolean, int, long, float, string, or array of (boolean, int, long, float, string), any data object from SharedPreferences within a Context.
```kotlin
delete<Boolean>("mykey")
delete<Int>("mykey")
delete<Long>("mykey")
delete<Float>("mykey")
delete<String>("mykey")
delete<MyDataObject>("mykey")
delete<Array<String>>("mykey")
.
.
.
delete<MySerializableObject>("mykey")
delete<ArrayList<MySerializableObject>>("mykey")
delete<Array<MyDataObject>>("mykey")
```
#### Safe Save, Load and Delete
Safe versions of the above save and load functions which avoid/ignore any exceptions.
Expand Down Expand Up @@ -176,13 +176,6 @@ Convert a Kotlin Integer to an arabic number String.
val arabic123 = 123.arabicNumber // ١٢٣
```

#### Serializable Object To Base64 Encoded String
Convert a Kotlin Serializable Object to a Base64 encoded string and vice-versa. This can be useful in certain situations (e.g. Saving objects to SharedPreferences if required, Sending data over the network e.t.c)
```kotlin
val base64Str = objectToString(serializableObject)
val originalObject = stringToObject(base64Str)
```

## Dialog
#### Set Nav Bar Color
Set the navigation bar color for when a dialog get displayed (Useful for top to bottom dialogs, BottomSheet Dialogs)
Expand Down
16 changes: 8 additions & 8 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
applicationId "com.muddassir.kmacros"
minSdkVersion 18
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand All @@ -26,11 +26,11 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

}
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.72"
ext.kotlin_version = "1.5.20"
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.1"
classpath 'com.android.tools.build:gradle:4.2.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -17,7 +17,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Expand Down
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-6.1.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-all.zip
16 changes: 8 additions & 8 deletions kmacros/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ apply plugin: 'kotlin-android-extensions'
apply plugin: 'maven-publish'

android {
compileSdkVersion 29
compileSdkVersion 30

defaultConfig {
minSdkVersion 18
targetSdkVersion 29
targetSdkVersion 30
versionCode 1
versionName "1.0"

Expand Down Expand Up @@ -41,11 +41,11 @@ android {
dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.appcompat:appcompat:1.3.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

api 'com.github.muddassir235:eprefs:1.6'
api 'com.github.muddassir235:eprefs:1.8'
}
48 changes: 1 addition & 47 deletions kmacros/src/main/java/com/muddassir/kmacros/primitive.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,50 +63,4 @@ val Long.time: String
time = time + ":" + String.format("%02d", seconds)

return time
}

/**
* objectToString - Convert any Serializable to a byte String that can be stored in
* SharedPreferences.
*
* @param `object`: Any Serializable object
*
* @return The object encoded as a Base64 bytecode string
*/
fun objectToString(`object`: Serializable?): String? {
var encoded: String? = null
try {
val byteArrayOutputStream = ByteArrayOutputStream()
val objectOutputStream = ObjectOutputStream(byteArrayOutputStream)
objectOutputStream.writeObject(`object`)
objectOutputStream.close()
encoded = Base64.encodeToString(byteArrayOutputStream.toByteArray(), 0)
} catch (e: IOException) {
e.printStackTrace()
}
return encoded
}

/**
* stringToObject - Convert bytecode string to a Serializable object
*
* @param string: Base64 encoded bytecode string
*
* @return Serializable object
*/
fun stringToObject(string: String?): Serializable? {
val bytes = Base64.decode(string, 0)
var `object`: Serializable? = null
try {
val objectInputStream =
ObjectInputStream(ByteArrayInputStream(bytes))
`object` = objectInputStream.readObject() as Serializable
} catch (e: IOException) {
e.printStackTrace()
} catch (e: ClassNotFoundException) {
e.printStackTrace()
} catch (e: ClassCastException) {
e.printStackTrace()
}
return `object`
}
}

0 comments on commit 57e09e0

Please sign in to comment.