Skip to content

Instantly share code, notes, and snippets.

@CyxouD
Created July 19, 2018 15:05
Show Gist options
  • Save CyxouD/145a0229307c979a554dcb3f25576666 to your computer and use it in GitHub Desktop.
Save CyxouD/145a0229307c979a554dcb3f25576666 to your computer and use it in GitHub Desktop.
This file is responsible for handling network request using Retrofit and pass result with RxJava Observables
open class RemoteDataSource private constructor() : DataSource {
private val service by lazy {
RetrofitService.create()
}
companion object {
private var INSTANCE: RemoteDataSource? = null
fun getInstance(): RemoteDataSource {
if (INSTANCE == null) {
INSTANCE = RemoteDataSource()
}
return INSTANCE!!
}
}
//... some other methods
override fun authorize(credentials: Credentials): Flowable<String> {
return service.authenticate(credentials.email!!, credentials.password!!).map { it.token }
}
override fun getLayerIdentityToken(identityTokenBody: RetrofitService.IdentityTokenBody): Flowable<String> {
return service.getIdentityToken(identityTokenBody)
.map { identityToken -> identityToken.identityToken }
}
override fun getConversationId(): Flowable<Optional<String>> {
return service.getConversationId()
.map { conversationId -> conversationId.conversationId.toOptional() }
}
override fun selectWidget(model: Model, id: Int): Completable {
return when (model) {
Model.FLIGHT -> service.selectFlightWidget(id)
Model.HOTEL -> service.selectHotelWidget(id)
Model.CAR -> service.selectCarWidget(id)
Model.TRAIN -> service.selectTrainWidget(id)
}
}
override fun selectWidgetWithCF(model: Model, optionId: Int, taskId: Int, state: String): Completable {
val selectCfBody = SelectCFBody(optionId, taskId, state)
return when (model) {
Model.FLIGHT -> service.selectFlightWidgetWithCF(selectCfBody)
Model.HOTEL -> service.selectHotelWidgetWithCF(selectCfBody)
Model.CAR -> service.selectCarWidgetWithCF(selectCfBody)
Model.TRAIN -> service.selectTrainWidgetWithCF(selectCfBody)
}
}
override fun resetPassword(email: String): Completable {
return service.resetPassword(RetrofitService.Email(email))
}
override fun saveProfile(profile: Profile): Completable {
return service.saveProfile(profile)
}
override fun getSettings(): Single<Optional<UserSettings>> {
return service.getSettings().map { it.toOptional() }
}
override fun saveAccount(account: UserAccount): Completable {
return service.saveAccount(account)
}
override fun changePassword(newPassword: RetrofitService.NewPassword): Completable {
return service.changePassword(newPassword)
}
override fun saveBusinessAddress(address: Address): Completable {
return service.saveBusinessAddress(address)
}
//... some other methods
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment