Created
May 29, 2024 22:10
-
-
Save oguzhanaslann/7bf18d50c764c13469dc7e9bcb0c7ced to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import com.thelifeco.wannawell.data.netwok.MutableTokenProvider | |
| import com.thelifeco.wannawell.utils.AppUtils | |
| import com.thelifeco.wannawell.utils.extension.EMPTY | |
| @JvmInline | |
| value class Key(val value: String) | |
| interface MemorySource: MutableTokenProvider { | |
| fun <T> save(key: Key, value: T) | |
| fun <T> get(key: Key): T? | |
| fun <T> get(key: Key, defaultValue: T): T | |
| fun remove(key: Key): Boolean | |
| } | |
| class MemorySourceImpl : MemorySource { | |
| private val memory = mutableMapOf<Key, Any>() | |
| override fun <T> save(key: Key, value: T) { | |
| memory[key] = value as Any | |
| } | |
| @kotlin.jvm.Throws(ClassCastException::class) | |
| override fun <T> get(key: Key): T? { | |
| return memory[key] as T | |
| } | |
| @kotlin.jvm.Throws(ClassCastException::class) | |
| override fun <T> get(key: Key, defaultValue: T): T { | |
| return memory[key] as? T ?: defaultValue | |
| } | |
| override fun remove(key: Key): Boolean { | |
| return memory.remove(key) != null | |
| } | |
| override fun setToken(token: String) { | |
| save(tokenKey, token) | |
| } | |
| override fun getToken(): String { | |
| return get(tokenKey, String.EMPTY) | |
| } | |
| override fun clearToken() { | |
| remove(tokenKey) | |
| } | |
| companion object { | |
| val tokenKey = Key("gH9A4R9xIKZCMQT") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment