Last active
November 10, 2015 21:09
-
-
Save jmfayard/2f7b4e7d29f76f6d4999 to your computer and use it in GitHub Desktop.
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
package LifecycleDemo | |
import java.util.* | |
/** | |
* The purpose of this pattern is to give the ability to group actions that belong together | |
* but that have to be executed at a different point of the android activity lifecycle | |
* | |
* This extension function creates a delegate for all children of LifecycleActivity | |
* For comparaison puropose, you can find the standard android version here | |
https://gist.github.com/jmfayard/4740cba818a998323f9b | |
*/ | |
fun LifecycleActivity.activityTask(action : (event : ActivityEvent) -> Unit) | |
= lazy { ActivityTask(action) } | |
/** parameter is one of **/ | |
enum class ActivityEvent { | |
OnCreate, OnStart, OnResume, OnPause, OnStop, OnDestroy | |
} | |
/** Your activity class must extends LifecycleActivity **/ | |
class MyActivity : LifecycleActivity() { | |
override fun onCreate() { | |
super.onCreate() | |
} | |
/** | |
* Tasks that will be called in LifecycleActivity's onCreate onStart onResume onPause onStop onDestroy | |
* You must override this function | |
*/ | |
override fun listOfLifecycleTasks() = listOf(firstTask, secondTask, thirdTask) | |
// Do something depending on the ActivityEvent parameter | |
val firstTask by activityTask { event -> | |
when (event) { | |
ActivityEvent.OnStart -> println("SDK XYZ needs to do something in onStart()") | |
ActivityEvent.OnStop -> println("SDK XYZ needs to do something in Stop()") | |
else -> {} | |
} | |
} | |
// activityTask is lazy so you don't have to worry about using variables not initialized in onCreate | |
val secondTask by activityTask { event -> | |
when (event) { | |
ActivityEvent.OnCreate -> hockeySdkStartMonitoring() | |
ActivityEvent.OnDestroy -> hockeySdkStopMonitoring() | |
else -> {} | |
} | |
} | |
val thirdTask by activityTask { event -> | |
when (event) { | |
ActivityEvent.OnResume -> println("Do this in onPause()") | |
ActivityEvent.OnPause -> println("Do taht in onResume()") | |
else -> {} | |
} | |
} | |
private fun hockeySdkStopMonitoring() { | |
println("HockeySdk : start monigoring") | |
} | |
private fun hockeySdkStartMonitoring() { | |
println("HockeySdk : stop monigoring") | |
} | |
} | |
/** | |
* Store your task | |
*/ | |
class ActivityTask(val action : (event : ActivityEvent) -> Unit) | |
/** | |
* The lifecycle-aware base clss | |
*/ | |
open class LifecycleActivity : Activity() { | |
/** Used by the children classes to tell LifecycleActivity | |
* which ActivityTask should be run | |
*/ | |
open fun listOfLifecycleTasks(): List<ActivityTask> = emptyList<ActivityTask>() | |
override fun onCreate() { | |
super.onCreate() | |
runAllActivityTasks(ActivityEvent.OnCreate) | |
} | |
override fun onDestroy() { | |
super.onDestroy() | |
runAllActivityTasks(event = ActivityEvent.OnDestroy) | |
} | |
override fun onPause() { | |
super.onPause() | |
runAllActivityTasks(event = ActivityEvent.OnPause) | |
} | |
override fun onResume() { | |
super.onResume() | |
runAllActivityTasks(event = ActivityEvent.OnResume) | |
} | |
override fun onStart() { | |
super.onStart() | |
runAllActivityTasks(event = ActivityEvent.OnStart) | |
} | |
override fun onStop() { | |
super.onStop() | |
runAllActivityTasks(event = ActivityEvent.OnStop) | |
} | |
private fun runAllActivityTasks(event: ActivityEvent) { | |
listOfLifecycleTasks().forEach { task -> run { | |
task.action(event) | |
}} | |
} | |
} | |
// fake emulation of android activities | |
open class Activity { | |
open fun onCreate() { } | |
open fun onDestroy() { } | |
open fun onStart() { } | |
open fun onStop() { } | |
open fun onResume() { } | |
open fun onPause() { } | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment