Skip to content

Commit

Permalink
Merge pull request #175 from nhaarman/activity
Browse files Browse the repository at this point in the history
Allow ActivityControllers to start Activities themselves
  • Loading branch information
nhaarman committed May 25, 2021
2 parents a3bb863 + 1a44a02 commit bb4cd33
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 16 deletions.
8 changes: 8 additions & 0 deletions .ops/publishing-base.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ publishing {

publications {
maven(MavenPublication) {
versionMapping {
usage("java-api") {
fromResolutionResult()
}
usage("java-runtime") {
fromResolutionResult()
}
}

version = git.versionName()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ class AcornActivityDelegate private constructor(
dispatcher.onUIVisible()
}

// We suppress the unused parameter to keep a uniform API.
@Suppress("UNUSED_PARAMETER")
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
dispatcher.onActivityResult(resultCode, data)
dispatcher.onActivityResult(requestCode, resultCode, data)
}

fun onBackPressed(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ class AcornSceneDispatcher internal constructor(
return uiHandler.onBackPressed()
}

override fun onActivityResult(resultCode: Int, data: Intent?) {
activityHandler.onActivityResult(resultCode, data)
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
activityHandler.onActivityResult(requestCode, resultCode, data)
}

override fun saveInstanceState(): SavedState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ interface SceneDispatcher {
* To be invoked when the [Activity] receives an invocation to its
* [Activity.onActivityResult] method.
*/
fun onActivityResult(resultCode: Int, data: Intent?)
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

/**
* Saves any state for this [SceneDispatcher].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal interface ActivityHandler {

fun withoutScene()

fun onActivityResult(resultCode: Int, data: Intent?)
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)

fun saveInstanceState(): SavedState
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,19 @@ internal class DefaultActivityHandler(
lastScene = scene
lastActivityController = activityController

scene.forceAttach(activityController)

val intent = activityController.createIntent()
if (intent != null) {
v("ActivityHandler", "Starting Intent: $intent.")
callback.startForResult(intent)
scene.forceDetach(activityController)
return
}

v("ActivityHandler", "Starting Intent: $intent.")
callback.startForResult(intent)
v("ActivityHandler", "Starting ActivityController $activityController")
activityController.start()
scene.forceDetach(activityController)
}

override fun withoutScene() {
Expand All @@ -73,8 +82,8 @@ internal class DefaultActivityHandler(
}
}

override fun onActivityResult(resultCode: Int, data: Intent?) {
d("ActivityHandler", "Activity result: resultCode=$resultCode, data=$data")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
d("ActivityHandler", "Activity result: requestCode=$requestCode, resultCode=$resultCode, data=$data")

val scene = lastScene
val activityController = lastActivityController
Expand All @@ -90,7 +99,7 @@ internal class DefaultActivityHandler(
scene.forceAttach(activityController)

v("ActivityHandler", "Notifying ActivityController of result.")
activityController.onResult(resultCode, data)
activityController.onResult(requestCode, resultCode, data)

v("ActivityHandler", "Detaching container from $scene.")
scene.forceDetach(activityController)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,53 @@ import com.nhaarman.acorn.presentation.Scene

/**
* A [Container] specialization that can be used to dispatch [Scene]s as Activities.
*
* Implementers must either implement [createIntent] or [start].
*/
interface ActivityController : Container {

/**
* Creates the [Intent] that can be used to start the [Activity].
*
* Override this function if you simply want to build an Intent
* and don't worry about anything else. Use [start] if you want
* to have more control.
*/
fun createIntent(): Intent? {
return null
}

/**
* Starts the designated Activity.
*
* Override this function to start the Activity. To be able to
* call [Activity.startActivityForResult], implementers are
* responsible to deliver the receiving Activity instance.
*
* Implementations should invoke [Activity.startActivityForResult]
* rather than [Activity.startActivity], to ensure Acorn is notified
* when the Activity has finished.
*
* If you simply want to provide an Intent that should be started
* for you, override [createIntent].
*/
fun start() {
error("Either override createIntent() or start() to start an Activity.")
}

/**
* Called when the [Activity] started with the [Intent] provided by
* [createIntent] finishes.
*/
fun createIntent(): Intent
fun onResult(resultCode: Int, data: Intent?) {
error("You must override onResult")
}

/**
* Called when the [Activity] started with the [Intent] provided by
* [createIntent] finishes.
*/
fun onResult(resultCode: Int, data: Intent?)
fun onResult(requestCode: Int, resultCode: Int, data: Intent?) {
onResult(resultCode, data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ internal class DefaultActivityHandlerTest {
activityHandler.withScene(scene, activityController)

/* When */
activityHandler.onActivityResult(3, null)
activityHandler.onActivityResult(42, 3, null)

/* Then */
inOrder(scene, activityController) {
Expand All @@ -232,7 +232,7 @@ internal class DefaultActivityHandlerTest {
activityHandler.withScene(scene, activityController)

/* When */
activityHandler.onActivityResult(3, null)
activityHandler.onActivityResult(42, 3, null)

/* Then */
inOrder(scene, activityController) {
Expand Down

0 comments on commit bb4cd33

Please sign in to comment.