Skip to content

Commit

Permalink
Initial work towards controlling the lifecycle and the app tasks with…
Browse files Browse the repository at this point in the history
… coroutines
  • Loading branch information
jrgonzalezg committed Mar 17, 2017
1 parent dccb8b9 commit b429166
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import com.antonioleiva.bandhookkotlin.App
import com.antonioleiva.bandhookkotlin.di.ApplicationComponent
import org.jetbrains.anko.setContentView

abstract class BaseActivity<out UI : ActivityAnkoComponent<out AppCompatActivity>> : AppCompatActivity() {
abstract class BaseActivity<out UI : ActivityAnkoComponent<out AppCompatActivity>> : CoroutineActivity() {

companion object {
val IMAGE_TRANSITION_NAME = "activity_image_transition"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.antonioleiva.bandhookkotlin.ui.activity

import android.support.v7.app.AppCompatActivity
import com.github.finecinnamon.JobHolder
import kotlinx.coroutines.experimental.Job

/**
* Base class for all activities that use coroutine trees for handling their lifecycle and tasks
*/
abstract class CoroutineActivity : AppCompatActivity(), JobHolder {
override var job: Job? = null

override fun onPause() {
super.onPause()

job?.cancel()
}

override fun onResume() {
super.onResume()

job = Job()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class MainPresenter(
val recommendedArtistsInteractor: GetRecommendedArtistsInteractor,
val mapper: ImageTitleDataMapper) : Presenter<MainView> {

override fun onResume() {

suspend override fun onResume() {
super.onResume()
recommendedArtistsInteractor.getRecommendedArtists().onComplete(
onSuccess = { view.showArtists(mapper.transformArtists(it)) },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ interface Presenter<out T> {

val view: T

fun onResume() {
suspend fun onResume() {
}

fun onPause() {
suspend fun onPause() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ import com.antonioleiva.bandhookkotlin.ui.util.supportsLollipop
import com.antonioleiva.bandhookkotlin.ui.view.AlbumView
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.dimen
import javax.inject.Inject

Expand Down Expand Up @@ -100,13 +102,15 @@ class AlbumActivity : BaseActivity<AlbumLayout>(), AlbumView {

override fun onResume() {
super.onResume()
presenter.onResume()
presenter.init(getNavigationId())
}

override fun onPause() {
super.onPause()
presenter.onPause()
launch(job!! + UI) {
try {
presenter.onResume()
presenter.init(getNavigationId())
} finally {
presenter.onPause()
}
}
}

override fun showAlbum(albumDetail: AlbumDetail) = runOnUiThread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import com.antonioleiva.bandhookkotlin.ui.util.supportsLollipop
import com.antonioleiva.bandhookkotlin.ui.view.ArtistView
import com.squareup.picasso.Callback
import com.squareup.picasso.Picasso
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import javax.inject.Inject

class ArtistActivity : BaseActivity<ArtistLayout>(), ArtistView, AlbumsFragmentContainer {
Expand Down Expand Up @@ -98,13 +100,15 @@ class ArtistActivity : BaseActivity<ArtistLayout>(), ArtistView, AlbumsFragmentC

override fun onResume() {
super.onResume()
presenter.onResume()
presenter.init(getNavigationId())
}

override fun onPause() {
super.onPause()
presenter.onPause()
launch(job!! + UI) {
try {
presenter.onResume()
presenter.init(getNavigationId())
} finally {
presenter.onPause()
}
}
}

override fun showArtist(artistDetail: ArtistDetail) = runOnUiThread {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import com.antonioleiva.bandhookkotlin.ui.screens.detail.ArtistActivity
import com.antonioleiva.bandhookkotlin.ui.util.navigate
import com.antonioleiva.bandhookkotlin.ui.view.MainView
import com.github.finecinnamon.NonEmptyList
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.delay
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.longToast
import org.jetbrains.anko.toast
import javax.inject.Inject

class MainActivity : BaseActivity<MainLayout>(), MainView {
Expand Down Expand Up @@ -56,12 +61,23 @@ class MainActivity : BaseActivity<MainLayout>(), MainView {

override fun onResume() {
super.onResume()
presenter.onResume()
}

override fun onPause() {
super.onPause()
presenter.onPause()
launch(job!! + UI) {
try {
presenter.onResume()

// TODO: Delete the delay and the toasts. They are only included now to illustrate
// that the main thread is not blocked due to pending tasks and that cleanup occurs
// automatically in both of the following scenarios
// 1) This block of code ends
// 2) The activity is suspended before 1) occurs (try rotating the phone)
delay(10000)
toast("All tasks finished")
} finally {
toast("Cleaning up")
presenter.onPause()
}
}
}

override fun showArtists(artists: NonEmptyList<ImageTitle>) = runOnUiThread {
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/java/com/github/finecinnamon/JobHolder.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.github.finecinnamon

import kotlinx.coroutines.experimental.Job

/**
* Represents classes that can execute tasks on a coroutine based job tree
*/
interface JobHolder {
val job: Job?
}

0 comments on commit b429166

Please sign in to comment.