Arrow 101 — Building an Android app using Functional Programming

Image for post
Image for post

1 — Intro

1.1 — About the project

2 — The View (Activity)

class MainActivity : AppCompatActivity(), RepositoriesView {

private val repoList : MutableList<Repository> = mutableListOf()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

setupList(repoList)
}

private fun setupList(listItems : List<Repository>) {
heroesListRV.layoutManager = LinearLayoutManager(this)
heroesListRV.adapter = RepositoriesAdapter(listItems, { Log.d("Click", "Got a click!") })
}

override fun onResume() {
super.onResume()
getRepositories().run(RepositoriesDeps(this))
}

override fun showGenericError() {
runOnUiThread {
alert("Generic error!!") {
yesButton { }
}
.show()
}
}

override fun drawRepositories(heroes: List<Repository>) {
runOnUiThread {
repoList
.addAll(heroes)
repositoryListRV.adapter.notifyDataSetChanged()
}
}
}
data class RepositoriesDeps(val view: RepositoriesView)
getRepositories().run(RepositoriesDeps(this))

2 — The Presenter

fun getRepositories() : Reader<RepositoriesDeps, Unit> =
ReaderApi.ask<RepositoriesDeps>()
fun <D, A, B> Reader<D, A>.map(f: (A) -> B): Reader<D, B> = map(IdBimonad, f)
fun getRepositories() : Reader<RepositoriesDeps, Unit> =
ReaderApi.ask<RepositoriesDeps>()
.map { (view) ->
repositoryUseCase()
.unsafeRunAsync { reposEither ->
reposEither.fold(
{ view.showGenericError() },
{ reposList -> drawRepos(reposList, view)})
}
}
private fun drawRepos(repositoryList: List<Repository>, view: RepositoriesView) {
repositoryList.
sortedByDescending {
repository -> repository.stargazersCount
}
pipe { view.drawHeroes(it) }
}

3 — The Model Layer

fun repositoryUseCase(): IO<List<Repository>> = fetchAllRepositories()
private fun queryForRepositories(): Try<List<Repository>> =
Try {
apiClient()
.getRepositories("Java", "star", 1)
.execute()
.body()!!
.items
}
fun fetchAllRepositories(): IO<List<Repository>> =
IO.async { either ->
async {
either(queryForRepositories().toEither())
}
}
Image for post
Image for post

4 — Improving the code (for tests)

data class RepositoriesDeps(val view: RepositoriesView, 
val apiClient: ApiClient)
override fun onResume() {
super.onResume()
getRepositories().run(RepositoriesDeps(this, apiClient()))
}
fun fetchAllRepositories(): Reader<RepositoriesDeps, IO<List<Repository>>> =
ReaderApi.ask<RepositoriesDeps>()
.map { deps ->
IO.async<List<Repository>> { either ->
async {
either(queryForRepositories(deps.apiClient).toEither())
}
}
}
private fun queryForRepositories(apiClient: ApiClient): Try<List<Repository>> =
Try {
apiClient
.getRepositories("Java", "star", 1)
.execute()
.body()!!
.items
}
fun repositoryUseCase() : Reader<RepositoriesDeps, IO<List<Repository>>> = fetchAllRepositories()
fun getRepositories(): Reader<RepositoriesDeps, Unit> =
ReaderApi.ask<RepositoriesDeps>()
.flatMap { (view, _) ->
repositoryUseCase()
.map { io ->
io.unsafeRunAsync { reposEither ->
reposEither.fold(
{ view.showGenericError() },
{ reposList -> drawRepos(reposList, view) })
}
}


}
Reader<D, A>.flatMap(f: (A) -> Reader<D, B>): Reader<D, B>

5 — Final considerations

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store