Skip to content

Commit

Permalink
fix same bug and issues
Browse files Browse the repository at this point in the history
  • Loading branch information
👾 AMIR ABBAS NAGHDI committed Apr 25, 2022
1 parent c0dc02e commit 7cb6ad9
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 26 deletions.
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ buildscript {
ext {
minV = 15
targetV = 31
vCode = 151
vName = "1.5.1"
vCode = 152
vName = "1.5.2"
}

repositories {
Expand All @@ -13,8 +13,8 @@ buildscript {
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.3'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.0'
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21'
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@ class Reactor @JvmOverloads constructor(
val securityController = SecurityController(appContext, isEncrypt)
val serializationHelper = SerializationHelper()

inline fun <reified T : Serializable> put(key: String, value: T?): Boolean {
inline fun <reified T : Serializable> put(key: String, value: T?): Boolean? {
if (value == null) {
remove<T>(key)
return true
}

val typeName = T::class.java.simpleName
val serializeValue = serializationHelper.serialize(value)
return securityController.put(key, serializeValue, typeName)
return serializeValue?.let {
securityController.put(key, it, typeName)
}
}

inline fun <reified T : Serializable> get(key: String): T? {
Expand All @@ -31,7 +33,7 @@ class Reactor @JvmOverloads constructor(
inline fun <reified T : Serializable> get(key: String, default: T): T {
val typeName = T::class.java.simpleName
val value = securityController.get(key, typeName) ?: return default
return serializationHelper.deserialize(value)
return serializationHelper.deserialize(value) ?: return default
}

inline fun <reified T : Serializable> remove(vararg keys: String): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,49 @@ import android.util.Base64OutputStream
import java.io.*


@Suppress("UNCHECKED_CAST")
class SerializationHelper {
fun <T : Serializable> serialize(data: T): String {
val outputStream = ByteArrayOutputStream()
val objectOutput = ObjectOutputStream(
Base64OutputStream(outputStream, Base64.NO_PADDING or Base64.NO_WRAP)
)
objectOutput.writeObject(data)
objectOutput.close()

return outputStream.toString("UTF-8")
@Synchronized
fun <T : Serializable> serialize(data: T): String? {
kotlin.runCatching {
val outputStream = ByteArrayOutputStream()
val objectOutput = ObjectOutputStream(
Base64OutputStream(outputStream, Base64.NO_PADDING or Base64.NO_WRAP)
)

objectOutput.use {
it.writeObject(data)
}

outputStream.use {
it.toString("UTF-8")
}
}.onFailure {
it.printStackTrace()
return null
}.onSuccess {
return it
}

return null
}

@Suppress("UNCHECKED_CAST")
fun <T : Serializable> deserialize(data: String): T {
return ObjectInputStream(
Base64InputStream(
ByteArrayInputStream(data.toByteArray()),
Base64.NO_PADDING or Base64.NO_WRAP
@Synchronized
fun <T : Serializable> deserialize(data: String): T? {
kotlin.runCatching {
val inputStream = ByteArrayInputStream(data.toByteArray(charset("UTF-8")))
val objectInput = ObjectInputStream(
Base64InputStream(inputStream, Base64.NO_PADDING or Base64.NO_WRAP)
)
).readObject() as T

objectInput.use { it.readObject() as T }
}.onFailure {
it.printStackTrace()
return null
}.onSuccess {
return it
}

return null
}
}
6 changes: 3 additions & 3 deletions sample/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ dependencies {
implementation project(':library')

implementation 'androidx.core:core-ktx:1.7.0'
implementation 'androidx.appcompat:appcompat:1.4.0'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.2'
implementation 'androidx.appcompat:appcompat:1.4.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
}

0 comments on commit 7cb6ad9

Please sign in to comment.