enhance dialog

androidx
Ztiany 5 years ago
parent 32008435cb
commit 260f6a8de5
  1. 19
      lib_base/src/main/java/com/android/base/app/Sword.java
  2. 35
      lib_base/src/main/java/com/android/base/app/aac/LiveResourceHandler.kt
  3. 15
      lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.kt
  4. 14
      lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.kt
  5. 38
      lib_base/src/main/java/com/android/base/app/fragment/utils.kt
  6. 3
      lib_base/src/main/java/com/android/base/app/ui/LoadingView.kt

@ -63,6 +63,11 @@ public final class Sword {
*/ */
private ErrorClassifier mErrorClassifier; private ErrorClassifier mErrorClassifier;
/**
* dialog 最小展示时间
*/
private long minimumShowingDialogMills;
/** /**
* 获取 Application 代理 * 获取 Application 代理
*/ */
@ -110,7 +115,7 @@ public final class Sword {
} }
/** /**
* {@link com.android.base.app.fragment.Fragments } 设置一个默认的容器 id在使用 其相关方法而没有传入特定的容器 id 则使用默认的容器 id * {@link com.android.base.app.fragment.tools.Fragments } 设置一个默认的容器 id在使用 其相关方法而没有传入特定的容器 id 则使用默认的容器 id
* *
* @param defaultContainerId 容器id * @param defaultContainerId 容器id
*/ */
@ -119,6 +124,14 @@ public final class Sword {
return this; return this;
} }
/**
* 设置 dialog 最小展示时间建议不超过 1
*/
public Sword setMinimumShowingDialogMills(long minimumShowingDialogMills) {
this.minimumShowingDialogMills = minimumShowingDialogMills;
return this;
}
public Sword setDefaultFragmentAnimator(FragmentAnimator animator) { public Sword setDefaultFragmentAnimator(FragmentAnimator animator) {
FragmentConfig.setDefaultFragmentAnimator(animator); FragmentConfig.setDefaultFragmentAnimator(animator);
return this; return this;
@ -210,4 +223,8 @@ public final class Sword {
return this; return this;
} }
public long minimumShowingDialogMills() {
return minimumShowingDialogMills;
}
} }

@ -3,6 +3,7 @@ package com.android.base.app.aac
import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import com.android.base.app.Sword
import com.android.base.app.ui.LoadingView import com.android.base.app.ui.LoadingView
import com.android.base.data.Resource import com.android.base.data.Resource
import timber.log.Timber import timber.log.Timber
@ -34,8 +35,17 @@ fun <H, T> H.handleLiveResource(
} }
it.isSuccess -> { it.isSuccess -> {
Timber.d("handleLiveResource -> isSuccess") Timber.d("handleLiveResource -> isSuccess")
dismissLoadingDialog() val minimumShowingDialogMills = Sword.get().minimumShowingDialogMills()
onSuccess(it.get())
if (minimumShowingDialogMills == 0L) {
dismissLoadingDialog()
onSuccess(it.get())
} else {
dismissLoadingDialog(minimumShowingDialogMills) {
onSuccess(it.get())
}
}
} }
} }
}) })
@ -62,11 +72,24 @@ fun <H, T> H.handleLiveResourceWithData(
} }
it.isSuccess -> { it.isSuccess -> {
Timber.d("handleLiveResourceWithData -> isSuccess") Timber.d("handleLiveResourceWithData -> isSuccess")
dismissLoadingDialog()
if (it.hasData()) { val minimumShowingDialogMills = Sword.get().minimumShowingDialogMills()
onSuccess(it.data())
if (minimumShowingDialogMills == 0L) {
dismissLoadingDialog()
if (it.hasData()) {
onSuccess(it.data())
} else {
onEmpty?.invoke()
}
} else { } else {
onEmpty?.invoke() dismissLoadingDialog(minimumShowingDialogMills) {
if (it.hasData()) {
onSuccess(it.data())
} else {
onEmpty?.invoke()
}
}
} }
} }
} }

@ -37,6 +37,8 @@ open class BaseDialogFragment : AppCompatDialogFragment(), LoadingView, OnBackPr
@Suppress("LeakingThis") @Suppress("LeakingThis")
private val fragmentDelegates = FragmentDelegates(this) private val fragmentDelegates = FragmentDelegates(this)
private var recentShowingDialogTime: Long = 0
private fun tag() = this.javaClass.simpleName private fun tag() = this.javaClass.simpleName
override fun onAttach(context: Context) { override fun onAttach(context: Context) {
@ -220,19 +222,24 @@ open class BaseDialogFragment : AppCompatDialogFragment(), LoadingView, OnBackPr
return null return null
} }
override fun showLoadingDialog() { override fun showLoadingDialog() {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(true) loadingView().showLoadingDialog(true)
} }
override fun showLoadingDialog(cancelable: Boolean) { override fun showLoadingDialog(cancelable: Boolean) {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(cancelable) loadingView().showLoadingDialog(cancelable)
} }
override fun showLoadingDialog(message: CharSequence, cancelable: Boolean) { override fun showLoadingDialog(message: CharSequence, cancelable: Boolean) {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(message, cancelable) loadingView().showLoadingDialog(message, cancelable)
} }
override fun showLoadingDialog(@StringRes messageId: Int, cancelable: Boolean) { override fun showLoadingDialog(@StringRes messageId: Int, cancelable: Boolean) {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(messageId, cancelable) loadingView().showLoadingDialog(messageId, cancelable)
} }
@ -240,6 +247,14 @@ open class BaseDialogFragment : AppCompatDialogFragment(), LoadingView, OnBackPr
loadingView().dismissLoadingDialog() loadingView().dismissLoadingDialog()
} }
override fun dismissLoadingDialog(minimumMills: Long, onDismiss: () -> Unit) {
dismissDialog(recentShowingDialogTime, minimumMills, onDismiss)
}
override fun isLoadingDialogShowing(): Boolean {
return loadingView().isLoadingDialogShowing()
}
override fun showMessage(message: CharSequence) { override fun showMessage(message: CharSequence) {
loadingView().showMessage(message) loadingView().showMessage(message)
} }

@ -48,6 +48,8 @@ open class BaseFragment : Fragment(), LoadingView, OnBackPressListener, Fragment
@Suppress("LeakingThis") @Suppress("LeakingThis")
private val fragmentDelegates = FragmentDelegates(this) private val fragmentDelegates = FragmentDelegates(this)
private var recentShowingDialogTime: Long = 0
private fun tag() = this.javaClass.simpleName private fun tag() = this.javaClass.simpleName
override fun onAttach(context: Context) { override fun onAttach(context: Context) {
@ -232,18 +234,22 @@ open class BaseFragment : Fragment(), LoadingView, OnBackPressListener, Fragment
} }
override fun showLoadingDialog() { override fun showLoadingDialog() {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(true) loadingView().showLoadingDialog(true)
} }
override fun showLoadingDialog(cancelable: Boolean) { override fun showLoadingDialog(cancelable: Boolean) {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(cancelable) loadingView().showLoadingDialog(cancelable)
} }
override fun showLoadingDialog(message: CharSequence, cancelable: Boolean) { override fun showLoadingDialog(message: CharSequence, cancelable: Boolean) {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(message, cancelable) loadingView().showLoadingDialog(message, cancelable)
} }
override fun showLoadingDialog(@StringRes messageId: Int, cancelable: Boolean) { override fun showLoadingDialog(@StringRes messageId: Int, cancelable: Boolean) {
recentShowingDialogTime = System.currentTimeMillis()
loadingView().showLoadingDialog(messageId, cancelable) loadingView().showLoadingDialog(messageId, cancelable)
} }
@ -251,6 +257,14 @@ open class BaseFragment : Fragment(), LoadingView, OnBackPressListener, Fragment
loadingView().dismissLoadingDialog() loadingView().dismissLoadingDialog()
} }
override fun dismissLoadingDialog(minimumMills: Long, onDismiss: () -> Unit) {
dismissDialog(recentShowingDialogTime, minimumMills, onDismiss)
}
override fun isLoadingDialogShowing(): Boolean {
return loadingView().isLoadingDialogShowing()
}
override fun showMessage(message: CharSequence) { override fun showMessage(message: CharSequence) {
loadingView().showMessage(message) loadingView().showMessage(message)
} }

@ -0,0 +1,38 @@
package com.android.base.app.fragment
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.android.base.app.ui.LoadingView
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
/**
*@author Ztiany
* Email: ztiany3@gmail.com
* Date : 2019-10-16 17:28
*/
internal fun <T> T.dismissDialog(recentShowingDialogTime: Long, minimumMills: Long, onDismiss: () -> Unit) where T : LoadingView, T : LifecycleOwner {
if (!isLoadingDialogShowing()) {
onDismiss()
return
}
val dialogShowingTime = System.currentTimeMillis() - recentShowingDialogTime
if (dialogShowingTime >= minimumMills) {
onDismiss()
} else {
lifecycleScope.launch {
try {
delay(minimumMills - dialogShowingTime)
dismissLoadingDialog()
onDismiss()
} catch (e: CancellationException) {
onDismiss()
}
}
}
}

@ -16,6 +16,9 @@ interface LoadingView {
fun showLoadingDialog(message: CharSequence, cancelable: Boolean) fun showLoadingDialog(message: CharSequence, cancelable: Boolean)
fun showLoadingDialog(@StringRes messageId: Int, cancelable: Boolean) fun showLoadingDialog(@StringRes messageId: Int, cancelable: Boolean)
fun dismissLoadingDialog() fun dismissLoadingDialog()
fun dismissLoadingDialog(minimumMills: Long, onDismiss: () -> Unit)
fun isLoadingDialogShowing(): Boolean
fun showMessage(message: CharSequence) fun showMessage(message: CharSequence)
fun showMessage(@StringRes messageId: Int) fun showMessage(@StringRes messageId: Int)

Loading…
Cancel
Save