Created
July 19, 2018 15:07
-
-
Save CyxouD/233cca2d7b9727fe98dccf390bddf3d2 to your computer and use it in GitHub Desktop.
This file is responsible for organizing communication between local and remote data sources (storages) and passing result 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 Repository private constructor( | |
remoteDataSource: DataSource, | |
localDataSource: DataSource) | |
: DataSource { | |
private val mRemoteDataSource = remoteDataSource | |
private val mLocalDataSource = localDataSource | |
companion object { | |
private var INSTANCE: Repository? = null | |
fun getInstance(remoteDataSource: DataSource, localDataSource: DataSource): Repository { | |
if (INSTANCE == null) { | |
INSTANCE = Repository(remoteDataSource, localDataSource) | |
} | |
return INSTANCE!! | |
} | |
/** | |
* Used to force {@link #getInstance(TasksDataSource, TasksDataSource)} to create a new instance | |
* next time it's called. IMPORTANT FOR TESTS | |
*/ | |
@VisibleForTesting | |
fun destroyInstance() { | |
INSTANCE = null | |
} | |
} | |
//... some other methods | |
override fun getToken(): Flowable<Optional<String>> { | |
return mLocalDataSource.getToken() | |
} | |
override fun authorize(credentials: Credentials): Flowable<String> { | |
return mRemoteDataSource.authorize(credentials) | |
.doOnNext { token -> | |
saveToken(token).subscribe() | |
} | |
} | |
override fun getConversationId(): Flowable<Optional<String>> { | |
//TODO rewrite to pass exception, @see getCreditCards() | |
val localConversationId = mLocalDataSource.getConversationId() | |
val remoteConversationId = mRemoteDataSource.getConversationId() | |
.doOnNext { conversationId -> | |
mLocalDataSource.saveConversationId(conversationId.toNullable()!!).subscribe() | |
}.onErrorReturn { null.toOptional() } | |
return Flowable.zip(localConversationId, remoteConversationId, | |
BiFunction<Optional<String>, Optional<String>, Optional<String>> { local, remote -> | |
remote.toNullable()?.let { remote } ?: local | |
}) | |
} | |
override fun getLayerIdentityToken(identityTokenBody: DevGetVoilaService.IdentityTokenBody): Flowable<String> { | |
return mRemoteDataSource.getLayerIdentityToken(identityTokenBody) | |
} | |
override fun saveToken(token: String): Single<Boolean> { | |
return mLocalDataSource.saveToken(token) | |
} | |
override fun removeToken(): Single<Boolean> { | |
return mLocalDataSource.removeToken() | |
} | |
override fun selectWidget(model: Model, id: Int): Completable { | |
return mRemoteDataSource.selectWidget(model, id) | |
} | |
override fun selectWidgetWithCF(model: Model, optionId: Int, taskId: Int, state: String): Completable { | |
return mRemoteDataSource.selectWidgetWithCF(model, optionId, taskId, state) | |
} | |
override fun removeAll(): Single<Boolean> { | |
return mLocalDataSource.removeAll() | |
} | |
override fun removeConversationId(): Single<Boolean> { | |
return mLocalDataSource.removeConversationId() | |
} | |
override fun saveConversationId(conversationId: String): Single<Boolean> { | |
return mLocalDataSource.saveConversationId(conversationId) | |
} | |
override fun resetPassword(email: String): Completable { | |
return mRemoteDataSource.resetPassword(email) | |
} | |
override fun saveProfile(profile: Profile): Completable { | |
return mRemoteDataSource.saveProfile(profile) | |
.andThen(mLocalDataSource.saveProfile(profile)) | |
} | |
override fun getProfile(): Single<Profile> { | |
return getSettings().map { ObjectMapper.mapToProfile(it.toNullable()) } | |
} | |
override fun saveSettings(settings: UserSettings): Completable { | |
return mLocalDataSource.saveSettings(settings) | |
} | |
override fun saveAccount(account: UserAccount): Completable { | |
return mRemoteDataSource.saveAccount(account) | |
.andThen(mLocalDataSource.saveAccount(account)) | |
} | |
//... some other methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment