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 { constraints {
implementation("androidx.appcompat:appcompat") { implementation("androidx.appcompat:appcompat") {
version { 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) 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 var isKeyboardVisible: Boolean = false
private val keyboardHideListener = OnBackPressedListener { private val keyboardHidingOnBackPressedListener = OnBackPressedListener {
if (isKeyboardVisible) { if (isKeyboardVisible) {
UiUtils.OfViews.hideSoftInput(activity) UiUtils.OfViews.hideSoftInput(activity)
true true
@ -46,39 +58,31 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
isHideKeyboardOnBackEnabled = true isHideKeyboardOnBackEnabled = true
} }
@CallSuper
override fun onStart() {
super.onStart()
activity.keyboardBehaviorDetector?.apply {
addOnHideListener(onKeyboardHideListener)
addOnShowListener(onKeyboardShowListener)
}
}
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
if (isHideKeyboardOnBackEnabled) activity.addOnBackPressedListener(keyboardHideListener) if (isHideKeyboardOnBackEnabled) activity.addOnBackPressedListener(keyboardHidingOnBackPressedListener)
} }
override fun onPause() { override fun onPause() {
super.onPause() super.onPause()
notifyKeyboardHidden() notifyKeyboardHidden()
if (isHideKeyboardOnBackEnabled) activity.removeOnBackPressedListener(keyboardHideListener) if (isHideKeyboardOnBackEnabled) activity.removeOnBackPressedListener(keyboardHidingOnBackPressedListener)
}
@CallSuper
override fun onStart() {
super.onStart()
activity.keyboardBehaviorDetector?.apply {
keyboardHideListener = {
if (isKeyboardVisible) {
onKeyboardHide()
}
isKeyboardVisible = false
}
keyboardShowListener = { diff ->
onKeyboardShow(diff)
isKeyboardVisible = true
}
}
} }
override fun onStop() { override fun onStop() {
super.onStop() super.onStop()
activity.keyboardBehaviorDetector?.apply { activity.keyboardBehaviorDetector?.apply {
keyboardHideListener = null removeOnHideListener(onKeyboardHideListener)
keyboardShowListener = null removeOnShowListener(onKeyboardShowListener)
} }
} }
@ -86,5 +90,4 @@ abstract class KeyboardResizeableViewController<TActivity : BaseActivity, TState
if (isKeyboardVisible) onKeyboardHide() if (isKeyboardVisible) onKeyboardHide()
isKeyboardVisible = false 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) val view: View = creationContext.inflater.inflate(layoutRes, creationContext.container, false)
override val lifecycle: Lifecycle
get() = fragment.viewLifecycleOwner.lifecycle
init { init {
lifecycle.addObserver(LifecycleLoggingObserver(this)) 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. * 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) 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 * Get the current List - any diffing to present this list has already been computed and
* dispatched via the ListUpdateCallback. * dispatched via the ListUpdateCallback.

View File

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