Skip to content

Instantly share code, notes, and snippets.

@brescia123
Last active May 5, 2017 13:25
Show Gist options
  • Save brescia123/9bd8afe09ced134083ccc8fd18619a47 to your computer and use it in GitHub Desktop.
Save brescia123/9bd8afe09ced134083ccc8fd18619a47 to your computer and use it in GitHub Desktop.
Base Activity/Fragment that enables composition within the Activity/Fragment lifecycle
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
abstract class CompositionActivity : AppCompatActivity() {
private val onCreateFeatures = mutableListOf<CompositionActivity.OnCreateFeature>()
private val onStartFeatures = mutableListOf<CompositionActivity.OnStartFeature>()
private val onResumeFeatures = mutableListOf<CompositionActivity.OnResumeFeature>()
private val onPauseFeatures = mutableListOf<CompositionActivity.OnPauseFeature>()
private val onStopFeatures = mutableListOf<CompositionActivity.OnStopFeature>()
private val onRestartFeatures = mutableListOf<CompositionActivity.OnRestartFeature>()
private val onDestroyFeatures = mutableListOf<CompositionActivity.OnDestroyFeature>()
private val onActivityResultFeatures = mutableListOf<CompositionActivity.OnActivityResultFeature>()
protected fun addFeature(feature: CompositionFeature) {
if (feature is CompositionActivity.OnCreateFeature) onCreateFeatures.add(feature)
if (feature is CompositionActivity.OnStartFeature) onStartFeatures.add(feature)
if (feature is CompositionActivity.OnResumeFeature) onResumeFeatures.add(feature)
if (feature is CompositionActivity.OnPauseFeature) onPauseFeatures.add(feature)
if (feature is CompositionActivity.OnStopFeature) onStopFeatures.add(feature)
if (feature is CompositionActivity.OnRestartFeature) onRestartFeatures.add(feature)
if (feature is CompositionActivity.OnDestroyFeature) onDestroyFeatures.add(feature)
if (feature is CompositionActivity.OnActivityResultFeature) onActivityResultFeatures.add(feature)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onCreateFeatures.forEach { it.onCreate(this, savedInstanceState) }
}
override fun onStart() {
super.onStart()
onStartFeatures.forEach { it.onStart(this) }
}
override fun onResume() {
super.onResume()
onResumeFeatures.forEach { it.onResume(this) }
}
override fun onPause() {
super.onPause()
onPauseFeatures.forEach { it.onPause(this) }
}
override fun onStop() {
super.onStop()
onStopFeatures.forEach { it.onStop(this) }
}
override fun onRestart() {
super.onRestart()
onRestartFeatures.forEach { it.onRestart(this) }
}
override fun onDestroy() {
super.onDestroy()
onDestroyFeatures.forEach { it.onDestroy(this) }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
onActivityResultFeatures.forEach { it.onActivityResult(this, requestCode, resultCode, data) }
}
interface OnCreateFeature : CompositionFeature {
fun onCreate(activity: Activity, savedInstanceState: Bundle?)
}
interface OnStartFeature : CompositionFeature {
fun onStart(activity: Activity)
}
interface OnResumeFeature : CompositionFeature {
fun onResume(activity: Activity)
}
interface OnPauseFeature : CompositionFeature {
fun onPause(activity: Activity)
}
interface OnStopFeature : CompositionFeature {
fun onStop(activity: Activity)
}
interface OnRestartFeature : CompositionFeature {
fun onRestart(activity: Activity)
}
interface OnDestroyFeature : CompositionFeature {
fun onDestroy(activity: Activity)
}
interface OnActivityResultFeature : CompositionFeature {
fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?)
}
}
interface CompositionFeature
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
abstract class CompositionFragment : Fragment() {
private val onAttachFeatures = mutableListOf<CompositionFragment.OnAttachFeature>()
private val onCreateFeatures = mutableListOf<CompositionFragment.OnCreateFeature>()
private val onActivityCreatedFeatures = mutableListOf<CompositionFragment.OnActivityCreatedFeature>()
private val onStartFeatures = mutableListOf<CompositionFragment.OnStartFeature>()
private val onResumeFeatures = mutableListOf<CompositionFragment.OnResumeFeature>()
private val onPauseFeatures = mutableListOf<CompositionFragment.OnPauseFeature>()
private val onStopFeatures = mutableListOf<CompositionFragment.OnStopFeature>()
private val onDestroyFeatures = mutableListOf<CompositionFragment.OnDestroyFeature>()
private val onDetachFeatures = mutableListOf<CompositionFragment.OnDetachFeature>()
private val onActivityResultFeatures = mutableListOf<CompositionFragment.OnActivityResultFeature>()
protected fun addFeature(feature: CompositionFeature) {
if (feature is CompositionFragment.OnAttachFeature) onAttachFeatures.add(feature)
if (feature is CompositionFragment.OnCreateFeature) onCreateFeatures.add(feature)
if (feature is CompositionFragment.OnActivityCreatedFeature) onActivityCreatedFeatures.add(feature)
if (feature is CompositionFragment.OnStartFeature) onStartFeatures.add(feature)
if (feature is CompositionFragment.OnResumeFeature) onResumeFeatures.add(feature)
if (feature is CompositionFragment.OnPauseFeature) onPauseFeatures.add(feature)
if (feature is CompositionFragment.OnStopFeature) onStopFeatures.add(feature)
if (feature is CompositionFragment.OnDestroyFeature) onDestroyFeatures.add(feature)
if (feature is CompositionFragment.OnDetachFeature) onDetachFeatures.add(feature)
if (feature is CompositionFragment.OnActivityResultFeature) onActivityResultFeatures.add(feature)
}
override fun onAttach(context: Context?) {
super.onAttach(context)
onAttachFeatures.forEach { it.onAttach(this, context) }
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onCreateFeatures.forEach { it.onCreate(this, savedInstanceState) }
}
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
onActivityCreatedFeatures.forEach { it.onActivityCreated(this, savedInstanceState) }
}
override fun onStart() {
super.onStart()
onStartFeatures.map { it.onStart(this) }
}
override fun onResume() {
super.onResume()
onResumeFeatures.map { it.onResume(this) }
}
override fun onPause() {
super.onPause()
onPauseFeatures.forEach { it.onPause(this) }
}
override fun onStop() {
super.onStop()
onStopFeatures.forEach { it.onStop(this) }
}
override fun onDestroy() {
super.onDestroy()
onDestroyFeatures.forEach { it.onDestroy(this) }
}
override fun onDetach() {
super.onDetach()
onDetachFeatures.forEach { it.onDetach(this) }
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
onActivityResultFeatures.forEach { it.onActivityResult(this, requestCode, resultCode, data) }
}
interface OnAttachFeature : CompositionFeature {
fun onAttach(fragment: Fragment, context: Context?)
}
interface OnCreateFeature : CompositionFeature {
fun onCreate(fragment: Fragment, savedInstanceState: Bundle?)
}
interface OnActivityCreatedFeature : CompositionFeature {
fun onActivityCreated(fragment: Fragment, savedInstanceState: Bundle?)
}
interface OnStartFeature : CompositionFeature {
fun onStart(fragment: Fragment)
}
interface OnResumeFeature : CompositionFeature {
fun onResume(fragment: Fragment)
}
interface OnPauseFeature : CompositionFeature {
fun onPause(fragment: Fragment)
}
interface OnStopFeature : CompositionFeature {
fun onStop(fragment: Fragment)
}
interface OnDestroyFeature : CompositionFeature {
fun onDestroy(fragment: Fragment)
}
interface OnDetachFeature : CompositionFeature {
fun onDetach(fragment: Fragment)
}
interface OnActivityResultFeature : CompositionFeature {
fun onActivityResult(fragment: Fragment, requestCode: Int, resultCode: Int, data: Intent?)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment