Skip to content

Instantly share code, notes, and snippets.

@marcRDZ
Last active March 31, 2022 09:44
Show Gist options
  • Save marcRDZ/fa41098c053918696cbb4cae821c1ea4 to your computer and use it in GitHub Desktop.
Save marcRDZ/fa41098c053918696cbb4cae821c1ea4 to your computer and use it in GitHub Desktop.
An extensions utils to deal with SharedPreferences using Arrow https://arrow-kt.io/
import android.content.SharedPreferences
import arrow.core.Either
import arrow.core.left
import arrow.core.right
import org.json.JSONException
import org.json.JSONObject
object Success
sealed class Failure {
object Unknown : Failure()
object NoData : Failure()
data class Exception(val type: String, val message: String?) : Failure()
}
suspend fun SharedPreferences.fetchString(key: String): Either<Failure, String> =
try {
this.getString(key, null)?.right() ?: run { Failure.NoData.left() }
} catch (e: ClassCastException) {
Failure.Exception(type = e.toString(), e.message).left()
}
suspend fun SharedPreferences.saveString(key: String, value: String): Either<Failure, Success> =
this.edit()
.putString(key, value)
.commit()
.takeIf { it }?.let {
Success.right()
} ?: Failure.Unknown.left()
suspend fun SharedPreferences.fetchInt(key: String): Either<Failure, Int> =
try {
this.getInt(key, -1)
.takeIf {
it != -1
}?.right() ?: run { Failure.NoData.left() }
} catch (e: ClassCastException) {
Failure.Exception(type = e.toString(), e.message).left()
}
suspend fun SharedPreferences.saveInt(key: String, value: Int): Either<Failure, Success> =
this.edit()
.putInt(key, value)
.commit()
.takeIf { it }?.let {
Success.right()
} ?: Failure.Unknown.left()
suspend fun SharedPreferences.fetchFloat(key: String): Either<Failure, Float> =
try {
this.getFloat(key, -1F)
.takeIf {
it != -1F
}?.right() ?: run { Failure.NoData.left() }
} catch (e: ClassCastException) {
Failure.Exception(type = e.toString(), e.message).left()
}
suspend fun SharedPreferences.saveFloat(key: String, value: Float): Either<Failure, Success> =
this.edit()
.putFloat(key, value)
.commit()
.takeIf { it }?.let {
Success.right()
} ?: Failure.Unknown.left()
suspend fun SharedPreferences.fetchLong(key: String): Either<Failure, Long> =
try {
this.getLong(key, -1L)
.takeIf {
it != -1L
}?.right() ?: run { Failure.NoData.left() }
} catch (e: ClassCastException) {
Failure.Exception(type = e.toString(), e.message).left()
}
suspend fun SharedPreferences.saveLong(key: String, value: Long): Either<Failure, Success> =
this.edit()
.putLong(key, value)
.commit()
.takeIf { it }?.let {
Success.right()
} ?: Failure.Unknown.left()
suspend fun SharedPreferences.fetchBoolean(key: String): Either<Failure, Boolean> =
try {
this.getBoolean(key, false).right()
} catch (e: ClassCastException) {
Failure.Exception(type = e.toString(), e.message).left()
}
suspend fun SharedPreferences.saveBoolean(key: String, value: Boolean): Either<Failure, Success> =
this.edit()
.putBoolean(key, value)
.commit()
.takeIf { it }?.let {
Success.right()
} ?: Failure.Unknown.left()
suspend fun SharedPreferences.fetchObject(key: String): Either<Failure, JSONObject> =
try {
getString(key, null)?.let {
JSONObject(it).right()
} ?: run { Failure.NoData.left() }
} catch (e: ClassCastException) {
Failure.Exception(type = e.toString(), e.message).left()
} catch (e: JSONException) {
Failure.Exception(type = e.toString(), e.message).left()
}
suspend fun SharedPreferences.saveObject(key: String, value: JSONObject): Either<Failure, Success> =
this.edit()
.putString(key, value.toString())
.commit()
.takeIf { it }?.let {
Success.right()
} ?: Failure.Unknown.left()
suspend fun SharedPreferences.clear(): Either<Failure, Success> =
this.edit()
.clear()
.commit()
.takeIf { it }?.let {
Success.right()
} ?: Failure.Unknown.left()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment