|
|
|
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() |