Merge pull request #8 from TouchInstinct/lifecycle

Using viewLifecycleOwner, lifecycle logging moved to observer
This commit is contained in:
Denis Karmyshakov 2019-01-09 15:54:54 +03:00 committed by GitHub
commit d41946db39
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 81 deletions

View File

@ -23,6 +23,7 @@ import android.content.Intent
import android.os.Bundle
import androidx.collection.ArraySet
import androidx.appcompat.app.AppCompatActivity
import ru.touchin.roboswag.components.navigation.viewcontrollers.LifecycleLoggingObserver
import ru.touchin.roboswag.core.log.Lc
import ru.touchin.roboswag.core.log.LcGroup
@ -35,9 +36,8 @@ abstract class BaseActivity : AppCompatActivity() {
private val onBackPressedListeners = ArraySet<OnBackPressedListener>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
init {
lifecycle.addObserver(LifecycleLoggingObserver())
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
@ -45,55 +45,25 @@ abstract class BaseActivity : AppCompatActivity() {
LcGroup.UI_LIFECYCLE.i("${Lc.getCodePoint(this)} requestCode: $requestCode; resultCode: $resultCode")
}
override fun onStart() {
super.onStart()
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
override fun onResume() {
super.onResume()
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
override fun onPause() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
super.onPause()
}
override fun onSaveInstanceState(stateToSave: Bundle) {
super.onSaveInstanceState(stateToSave)
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
override fun onLowMemory() {
super.onLowMemory()
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
override fun onStop() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
super.onStop()
}
override fun onDestroy() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
super.onDestroy()
}
override fun onSupportNavigateUp(): Boolean {
onBackPressed()
return true
}
fun addOnBackPressedListener(onBackPressedListener: OnBackPressedListener) {
open fun addOnBackPressedListener(onBackPressedListener: OnBackPressedListener) {
onBackPressedListeners.add(onBackPressedListener)
}
fun removeOnBackPressedListener(onBackPressedListener: OnBackPressedListener) {
open fun removeOnBackPressedListener(onBackPressedListener: OnBackPressedListener) {
onBackPressedListeners.remove(onBackPressedListener)
}
fun removeAllOnBackPressedListeners() {
open fun removeAllOnBackPressedListeners() {
onBackPressedListeners.clear()
}

View File

@ -0,0 +1,16 @@
package ru.touchin.roboswag.components.navigation.viewcontrollers
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import ru.touchin.roboswag.core.log.Lc
import ru.touchin.roboswag.core.log.LcGroup
class LifecycleLoggingObserver : LifecycleObserver {
@OnLifecycleEvent(Lifecycle.Event.ON_ANY)
fun onAnyLifecycleEvent() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
}

View File

@ -32,7 +32,6 @@ import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import androidx.annotation.CallSuper
import androidx.annotation.ColorInt
import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
@ -45,11 +44,8 @@ import androidx.fragment.app.FragmentActivity
import androidx.fragment.app.FragmentTransaction
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LifecycleRegistry
import ru.touchin.roboswag.components.navigation.fragments.ViewControllerFragment
import ru.touchin.roboswag.components.utils.UiUtils
import ru.touchin.roboswag.core.log.Lc
import ru.touchin.roboswag.core.log.LcGroup
/**
* Created by Gavriil Sitnikov on 21/10/2015.
@ -72,9 +68,11 @@ open class ViewController<TActivity : FragmentActivity, TState : Parcelable>(
val view: View = creationContext.inflater.inflate(layoutRes, creationContext.container, false)
private val lifecycleRegistry = LifecycleRegistry(this)
init {
lifecycle.addObserver(LifecycleLoggingObserver())
}
override fun getLifecycle(): Lifecycle = lifecycleRegistry
override fun getLifecycle(): Lifecycle = fragment.viewLifecycleOwner.lifecycle
/**
* Look for a child view with the given id. If this view has the given id, return this view.
@ -166,11 +164,7 @@ open class ViewController<TActivity : FragmentActivity, TState : Parcelable>(
* Calls right after construction of [ViewController].
* Happens at [ViewControllerFragment.onActivityCreated].
*/
@CallSuper
open fun onCreate() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)
}
open fun onCreate() = Unit
/**
* Called when a fragment loads an animation. Note that if
@ -217,10 +211,7 @@ open class ViewController<TActivity : FragmentActivity, TState : Parcelable>(
* Calls when [ViewController] have started.
* Happens at [ViewControllerFragment.onStart].
*/
@CallSuper
open fun onStart() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
UiUtils.OfViews.hideSoftInput(view)
}
@ -228,74 +219,50 @@ open class ViewController<TActivity : FragmentActivity, TState : Parcelable>(
* Called when fragment is moved in started state and it's [.getFragment] sets to true.
* Usually it is indicating that user can't see fragment on screen and useful to track analytics events.
*/
open fun onAppear() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
open fun onAppear() = Unit
/**
* Calls when [ViewController] have resumed.
* Happens at [ViewControllerFragment.onResume].
*/
@CallSuper
open fun onResume() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_RESUME)
}
open fun onResume() = Unit
/**
* Calls when [ViewController] have goes near out of memory state.
* Happens at [ViewControllerFragment.onLowMemory].
*/
@CallSuper
open fun onLowMemory() = Unit
/**
* Calls when [ViewController] have paused.
* Happens at [ViewControllerFragment.onPause].
*/
@CallSuper
open fun onPause() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE)
}
open fun onPause() = Unit
/**
* Calls when [ViewController] should save it's state.
* Happens at [ViewControllerFragment.onSaveInstanceState].
* Try not to use such method for saving state but use [ViewControllerFragment.state] from [.getFragment].
*/
@CallSuper
open fun onSaveInstanceState(savedInstanceState: Bundle) {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
open fun onSaveInstanceState(savedInstanceState: Bundle) = Unit
/**
* Called when fragment is moved in stopped state or it's [.getFragment] sets to false.
* Usually it is indicating that user can't see fragment on screen and useful to track analytics events.
*/
open fun onDisappear() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
}
open fun onDisappear() = Unit
/**
* Calls when [ViewController] have stopped.
* Happens at [ViewControllerFragment.onStop].
*/
@CallSuper
open fun onStop() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)
}
open fun onStop() = Unit
/**
* Calls when [ViewController] have destroyed.
* Happens usually at [ViewControllerFragment.onDestroyView]. In some cases at [ViewControllerFragment.onDestroy].
*/
@CallSuper
open fun onDestroy() {
LcGroup.UI_LIFECYCLE.i(Lc.getCodePoint(this))
lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_DESTROY)
}
open fun onDestroy() = Unit
/**
* Calls when [ViewController] have requested permissions results.