Merge pull request 'feature/lifecycle_update' (#7) from feature/lifecycle_update into master

Reviewed-on: #7
This commit is contained in:
Kirill Khoroshkov 2023-12-28 17:01:58 +03:00
commit 5a6ea76d33
5 changed files with 63 additions and 44 deletions

View File

@ -12,7 +12,7 @@ dependencies {
constraints {
implementation("androidx.appcompat:appcompat") {
version {
require '1.0.2'
require '1.7.0-alpha03'
}
}
}

View File

@ -25,9 +25,21 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
lifecycle.addObserver(activity.keyboardBehaviorDetector as LifecycleObserver)
}
private val onKeyboardHideListener = {
if (isKeyboardVisible) {
onKeyboardHide()
}
isKeyboardVisible = false
}
private val onKeyboardShowListener = { diff: Int ->
onKeyboardShow(diff)
isKeyboardVisible = true
}
private var isKeyboardVisible: Boolean = false
private val keyboardHideListener = OnBackPressedListener {
private val keyboardHidingOnBackPressedListener = OnBackPressedListener {
if (isKeyboardVisible) {
UiUtils.OfViews.hideSoftInput(activity)
true
@ -46,39 +58,31 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
isHideKeyboardOnBackEnabled = true
}
@CallSuper
override fun onStart() {
super.onStart()
activity.keyboardBehaviorDetector?.apply {
addOnHideListener(onKeyboardHideListener)
addOnShowListener(onKeyboardShowListener)
}
}
override fun onResume() {
super.onResume()
if (isHideKeyboardOnBackEnabled) activity.addOnBackPressedListener(keyboardHideListener)
if (isHideKeyboardOnBackEnabled) activity.addOnBackPressedListener(keyboardHidingOnBackPressedListener)
}
override fun onPause() {
super.onPause()
notifyKeyboardHidden()
if (isHideKeyboardOnBackEnabled) activity.removeOnBackPressedListener(keyboardHideListener)
}
@CallSuper
override fun onStart() {
super.onStart()
activity.keyboardBehaviorDetector?.apply {
keyboardHideListener = {
if (isKeyboardVisible) {
onKeyboardHide()
}
isKeyboardVisible = false
}
keyboardShowListener = { diff ->
onKeyboardShow(diff)
isKeyboardVisible = true
}
}
if (isHideKeyboardOnBackEnabled) activity.removeOnBackPressedListener(keyboardHidingOnBackPressedListener)
}
override fun onStop() {
super.onStop()
activity.keyboardBehaviorDetector?.apply {
keyboardHideListener = null
keyboardShowListener = null
removeOnHideListener(onKeyboardHideListener)
removeOnShowListener(onKeyboardShowListener)
}
}
@ -86,5 +90,4 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
if (isKeyboardVisible) onKeyboardHide()
isKeyboardVisible = false
}
}

View File

@ -69,12 +69,13 @@ open class ViewController<TActivity : FragmentActivity, TState : Parcelable>(
val view: View = creationContext.inflater.inflate(layoutRes, creationContext.container, false)
override val lifecycle: Lifecycle
get() = fragment.viewLifecycleOwner.lifecycle
init {
lifecycle.addObserver(LifecycleLoggingObserver(this))
}
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.
*

View File

@ -71,6 +71,19 @@ open class DelegationListAdapter<TItem>(config: AsyncDifferConfig<TItem>) : Recy
*/
fun submitList(list: List<TItem>) = differ.submitList(list)
/**
* Submits a new list to be diffed, and displayed.
*
* The commit callback can be used to know when the List is committed, but note that it
* may not be executed. If List B is submitted immediately after List A, and is
* committed directly, the callback associated with List A will not be run.
*
* @param newList The new List.
* @param commitCallback Optional runnable that is executed when the List is committed, if
* it is committed.
*/
fun submitList(list: List<TItem>?, commitCallback: (() -> Unit)?) = differ.submitList(list, commitCallback)
/**
* Get the current List - any diffing to present this list has already been computed and
* dispatched via the ListUpdateCallback.

View File

@ -36,24 +36,26 @@ class LoadingContentView @JvmOverloads constructor(
}
private fun updateView(state: State) {
if (state == State.ShowContent) {
getChildAt(childCount - 1)?.let { showChild(it.id) }
} else {
when (state) {
is State.Stub -> {
setStubText(state.stubText)
showChild(R.id.text_stub)
}
is State.Loading -> {
showChild(R.id.progress_bar)
}
is State.Error -> {
binding.apply {
errorText.text = state.errorText
errorRepeatButton.setOnRippleClickListener { state.action.invoke() }
errorRepeatButton.text = state.repeatButtonText
showChild(R.id.error_with_repeat)
}
when (state) {
State.ShowContent -> {
getChildAt(childCount - 1)?.let { showChild(it.id) }
}
is State.Stub -> {
setStubText(state.stubText)
showChild(R.id.text_stub)
}
State.Loading -> {
showChild(R.id.progress_bar)
}
is State.Error -> {
binding.apply {
errorText.text = state.errorText
errorRepeatButton.setOnRippleClickListener { state.action.invoke() }
errorRepeatButton.text = state.repeatButtonText
showChild(R.id.error_with_repeat)
}
}
}