Created
July 19, 2018 15:04
-
-
Save CyxouD/0f1857cd7498df65a984de809044c4ae to your computer and use it in GitHub Desktop.
This file is responsible for handling of saving data locally in Realm Database, Shared preferences, Android Secure storage, retrieving data and passing it with RxJava Observables
This file contains 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
open class LocalDataSource private constructor() : DataSource, AnkoLogger { | |
private val authorizeTokenAlias = "authorizeTokenAlias" | |
private val conversationIdAlias = "conversationIdAlias" | |
companion object { | |
private var INSTANCE: LocalDataSource? = null | |
fun getInstance(): LocalDataSource { | |
if (INSTANCE == null) { | |
INSTANCE = LocalDataSource() | |
} | |
return INSTANCE!! | |
} | |
} | |
//... some other methods | |
override fun getToken(): Flowable<Optional<String>> { | |
val secureStorage = RxSecureStorage.create(App.context, authorizeTokenAlias) | |
return secureStorage | |
.getString(authorizeTokenAlias) | |
.toFlowable(BackpressureStrategy.BUFFER) | |
} | |
override fun getConversationId(): Flowable<Optional<String>> { | |
val secureStorage = RxSecureStorage.create(App.context, conversationIdAlias) | |
return secureStorage.getString(conversationIdAlias) | |
.toFlowable(BackpressureStrategy.BUFFER) | |
} | |
override fun saveToken(token: String): Single<Boolean> { | |
val secureStorage = RxSecureStorage.create(App.context, authorizeTokenAlias) | |
return secureStorage | |
.putString(authorizeTokenAlias, token) | |
} | |
override fun removeAll(): Single<Boolean> { | |
val deleteRealmSingle = Single.create<Boolean> { emitter -> | |
try { | |
Realm.getDefaultInstance().use { realm -> | |
realm.executeTransaction(Realm::deleteAll) | |
emitter.onSuccess(true) | |
} | |
} catch (e: Exception) { | |
emitter.onSuccess(false) | |
} | |
} | |
return Single.merge(removeToken(), removeConversationId(), deleteRealmSingle) | |
.all { deleted -> deleted } | |
} | |
override fun removeToken(): Single<Boolean> { | |
return removeFromRxSecureStorage(authorizeTokenAlias) | |
} | |
override fun removeConversationId(): Single<Boolean> { | |
return removeFromRxSecureStorage(conversationIdAlias) | |
} | |
override fun saveConversationId(conversationId: String): Single<Boolean> { | |
val secureStorage = RxSecureStorage.create(App.context, conversationIdAlias) | |
return secureStorage | |
.putString(conversationIdAlias, conversationId) | |
} | |
private fun removeFromRxSecureStorage(alias: String): Single<Boolean> { | |
val secureStorage = RxSecureStorage.create(App.context, alias) | |
return secureStorage.putString(alias, null) | |
.map { notPut -> !notPut } //reverse boolean to show true like it successfully removed | |
} | |
//... some other methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment