Last active
March 18, 2020 09:42
-
-
Save stoichoandreev/dbbef978c96ba74014a6e2d24ccc60de to your computer and use it in GitHub Desktop.
Gist 2 - 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
class ApiService { | |
fun requestDataFromBackend(): Observable<MyRawDataModel> { | |
return BackendAPI().returnSomeDummyData() | |
} | |
fun readCachedUserName(rawDataModel: MyRawDataModel) : Observable<Pair<MyRawDataModel, String>> { | |
val cachedUserName = PreferenceManager.getDefaultSharedPreferences(MyApplication.INSTANCE).getString("USER_NAME", "") ?: "" | |
return Observable.just(Pair(rawDataModel, cachedUserName)) | |
} | |
} |
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 BackendAPI { | |
fun returnSomeDummyData(): Observable<MyRawDataModel> { | |
return Observable.just(MyRawDataModel(123, "Bobi", "Davis", 50)) | |
} | |
} |
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 MyActivity: AppCompatActivity(), MyPresenter.View { | |
private val presenter = MyPresenter() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
presenter.setView(this) | |
presenter.requestData(ApiService(), resources) | |
} | |
override fun drawViews(viewModel: MyViewModel) { | |
//Unused at the moment, but could be used to display some data | |
} | |
override fun showLoader(show: Boolean) { | |
//Unused at the moment, but could be used to show or hide screen loader | |
} | |
} |
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 MyPresenter { | |
private lateinit var view: View | |
interface View { | |
fun drawViews(viewModel: MyViewModel) | |
fun showLoader(show: Boolean) | |
} | |
fun setView(view: View) { | |
this.view = view | |
} | |
fun requestData(service: ApiService, resources: Resources) { | |
service.requestDataFromBackend() | |
.observeOn(Schedulers.io()) | |
.subscribeOn(AndroidSchedulers.mainThread()) | |
.flatMap { service.readCachedUserName(it) } | |
.subscribe { covertDataModelAndDraw(it, resources) } | |
} | |
private fun covertDataModelAndDraw(pair: Pair<MyRawDataModel, String>, resources: Resources) { | |
val fullName = String.format(resources.getString(R.string.full_name_pattern), pair.first.firstName, pair.first.lastName) | |
val userName = pair.second | |
val age = pair.first.age | |
val message = resources.getString(R.string.my_message) | |
val viewModel = MyViewModel(fullName, userName, age, message) | |
view.drawViews(viewModel) | |
} | |
} |
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 MyRawDataModel(@SerializedName("id") val id: Int, | |
@SerializedName("firstName") val firstName: String, | |
@SerializedName("lastName") val lastName: String, | |
@SerializedName("age") val age: Int) | |
data class MyViewModel(private val name: String, | |
private val userName: String, | |
private val age: Int, | |
private val message: String) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment