Created
March 18, 2020 09:45
-
-
Save stoichoandreev/17711bde75bf95e815a7d63f5fea2837 to your computer and use it in GitHub Desktop.
Gist 3 - Android Example
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
object DataApiService { | |
fun loadData(): Observable<DataModel> { | |
return Observable.just(DataModel("data_model")) | |
} | |
fun loadAnoteherData(): Observable<AnotherDataModel> { | |
return Observable.just(AnotherDataModel("another_data_model")) | |
} | |
} |
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
data class DataModel(val id: String) | |
data class AnotherDataModel(val id: String) | |
data class FullDataModel(val data: DataModel, val anotherData: AnotherDataModel) |
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
object DataRepository { | |
private var data: FullDataModel? = null | |
fun saveFullData(data: FullDataModel): Completable { | |
return Completable.fromAction { this.data = data } | |
} | |
} |
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
class LoadingDataPresenter(private val view: LoadingDataView) { | |
private val api: DataApiService = DataApiService | |
fun loadData() { | |
updateLabel(R.string.loading_data) | |
getFullData() | |
.doOnNext { saveFullData(it) } | |
.subscribe( | |
{ | |
updateLabel(R.string.data_loaded) | |
view.loadingDataProgressBar.visibility = View.GONE | |
}, | |
{ | |
updateLabel(R.string.load_data) | |
view.loadingDataProgressBar.visibility = View.GONE | |
view.showToast(R.string.error_message) | |
} | |
) | |
} | |
private fun updateLabel(resId: Int) { | |
view.loadingDataTextView.text = view.getString(resId) | |
} | |
private fun getFullData(): Observable<FullDataModel> { | |
return Observable.zip( | |
api.loadData(), | |
api.loadAnoteherData(), | |
BiFunction { data: DataModel, anotherData: AnotherDataModel -> | |
FullDataModel(data, anotherData) | |
} | |
).subscribeOn(Schedulers.io()) | |
} | |
private fun saveFullData(data: FullDataModel) { | |
DataRepository.saveFullData(data) | |
.subscribeOn(Schedulers.io()) | |
.subscribe() | |
} | |
} |
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
class LoadingDataView : FrameLayout { | |
constructor(context: Context) : super(context) | |
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) | |
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( | |
context, | |
attrs, | |
defStyleAttr | |
) | |
init { | |
View.inflate(context, R.layout.view_loading_data, this) | |
val presenter = LoadingDataPresenter(this) | |
setOnClickListener { | |
loadingDataProgressBar.visibility = View.VISIBLE | |
presenter.loadData() | |
} | |
} | |
fun getString(resId: Int): String = context.getString(resId) | |
fun showToast(resId: Int) { | |
Toast.makeText(context, resId, Toast.LENGTH_SHORT).show() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment