update UIKit

androidx
Ztiany 5 years ago
parent 5bc68bb48e
commit 5c8f152686
  1. 81
      lib_base/src/main/java/com/android/base/app/aac/LiveStateHandler.kt
  2. 11
      lib_base/src/main/java/com/android/base/app/ui/LiveStateHandler.kt
  3. 213
      lib_base/src/main/java/com/android/base/app/ui/UIKit.kt
  4. 2
      lib_base/src/main/java/com/android/base/utils/android/SoftKeyboardUtils.java
  5. 2
      lib_base/src/main/java/com/android/base/widget/pulltozoom/PullToZoomScrollView.java

@ -1,81 +0,0 @@
package com.android.base.app.aac
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import com.android.base.app.Sword
import com.android.base.app.ui.LoadingView
import com.android.base.data.State
import timber.log.Timber
interface LiveStateHandler {
/**处理异常*/
fun handleError(throwable: Throwable)
}
fun <H, T> H.handleLiveState(
liveData: LiveData<State<T>>,
forceLoading: Boolean = true,
onSuccess: (T?) -> Unit
) where H : LiveStateHandler, H : LoadingView, H : LifecycleOwner {
liveData.observe(this, Observer {
when {
it.isError -> {
Timber.d("handleLiveState -> isError")
dismissLoadingDialog()
handleError(it.error())
}
it.isLoading -> {
Timber.d("handleLiveState -> isLoading")
showLoadingDialog(!forceLoading)
}
it.isSuccess -> {
Timber.d("handleLiveState -> isSuccess")
val minimumShowingDialogMills = Sword.get().minimumShowingDialogMills()
dismissLoadingDialog(minimumShowingDialogMills) {
onSuccess(it.get())
}
}//success end
}
})
}
fun <H, T> H.handleLiveStateWithData(
liveData: LiveData<State<T>>,
forceLoading: Boolean = true,
onEmpty: (() -> Unit)? = null,
onSuccess: (T) -> Unit
) where H : LiveStateHandler, H : LoadingView, H : LifecycleOwner {
liveData.observe(this, Observer {
when {
it.isError -> {
Timber.d("handleLiveStateWithData -> isError")
dismissLoadingDialog()
handleError(it.error())
}
it.isLoading -> {
Timber.d("handleLiveStateWithData -> isLoading")
showLoadingDialog(!forceLoading)
}
it.isSuccess -> {
Timber.d("handleLiveStateWithData -> isSuccess")
val minimumShowingDialogMills = Sword.get().minimumShowingDialogMills()
dismissLoadingDialog(minimumShowingDialogMills) {
if (it.hasData()) {
onSuccess(it.data())
} else {
onEmpty?.invoke()
}
}
}//success end
}
})
}

@ -0,0 +1,11 @@
package com.android.base.app.ui
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import com.android.base.app.Sword
import com.android.base.data.State
import timber.log.Timber

@ -2,11 +2,89 @@
package com.android.base.app.ui package com.android.base.app.ui
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.LiveData
import androidx.lifecycle.Observer
import com.android.base.app.Sword import com.android.base.app.Sword
import com.android.base.data.State
import com.android.base.data.StateHandler
import com.android.base.utils.common.isEmpty import com.android.base.utils.common.isEmpty
import timber.log.Timber import timber.log.Timber
fun <T> RefreshListLayout<T>.handleListResultWithStatus(list: List<T>?, onEmpty: (() -> Unit)? = null) { //----------------------------------------------Common->Loading->Dialog ----------------------------------------------
interface UIErrorHandler {
/**处理异常*/
fun handleError(throwable: Throwable)
/**异常描述*/
fun generateErrorMessage(throwable: Throwable)
}
fun <H, T> H.handleLiveState(
liveData: LiveData<State<T>>,
forceLoading: Boolean = true,
onSuccess: (T?) -> Unit
) where H : UIErrorHandler, H : LoadingView, H : LifecycleOwner {
liveData.observe(this, Observer { state ->
when {
state.isError -> {
Timber.d("handleLiveState -> isError")
dismissLoadingDialog()
handleError(state.error())
}
state.isLoading -> {
Timber.d("handleLiveState -> isLoading")
showLoadingDialog(!forceLoading)
}
state.isSuccess -> {
Timber.d("handleLiveState -> isSuccess")
val minimumShowingDialogMills = Sword.get().minimumShowingDialogMills()
dismissLoadingDialog(minimumShowingDialogMills) {
onSuccess(state.get())
}
}//success end
}
})
}
fun <T> LoadingView.handleState(
state: State<T>,
forceLoading: Boolean = true,
handler: StateHandler<T>.() -> Unit
) {
val stateHandler = StateHandler<T>()
handler(stateHandler)
when {
state.isError -> {
Timber.d("handleState -> isError")
dismissLoadingDialog()
stateHandler.onError?.invoke(state.error())
}
state.isLoading -> {
Timber.d("handleState -> isLoading")
showLoadingDialog(!forceLoading)
}
state.isSuccess -> {
Timber.d("handleState -> isSuccess")
val minimumShowingDialogMills = Sword.get().minimumShowingDialogMills()
dismissLoadingDialog(minimumShowingDialogMills) {
stateHandler.onSuccess?.invoke(state.get())
if (state.hasData()) {
stateHandler.onSuccessWithData?.invoke(state.data())
} else {
stateHandler.onEmpty?.invoke()
}
}
}//success end
}
}
//----------------------------------------------Loading In List----------------------------------------------
fun <T> RefreshListLayout<T>.handleListResult(list: List<T>?, onEmpty: (() -> Unit)? = null) {
if (isLoadingMore) { if (isLoadingMore) {
if (!isEmpty(list)) { if (!isEmpty(list)) {
addData(list) addData(list)
@ -31,27 +109,30 @@ fun <T> RefreshListLayout<T>.handleListResultWithStatus(list: List<T>?, onEmpty:
} }
} }
fun <T> RefreshListLayout<T>.handleListResultWithoutStatus(list: List<T>?, onEmpty: (() -> Unit)? = null) { fun RefreshListLayout<*>.handleListError(throwable: Throwable) {
if (isRefreshing) {
refreshCompleted()
}
if (isLoadingMore) { if (isLoadingMore) {
if (!isEmpty(list)) { loadMoreFailed()
addData(list)
} }
} else { if (isEmpty) {
replaceData(list) val errorTypeClassifier = Sword.get().errorClassifier()
refreshCompleted() if (errorTypeClassifier != null) {
when {
errorTypeClassifier.isNetworkError(throwable) -> showNetErrorLayout()
errorTypeClassifier.isServerError(throwable) -> showServerErrorLayout()
else -> showErrorLayout()
} }
} else {
if (pager != null) { showErrorLayout()
loadMoreCompleted(list != null && pager.hasMore(list.size))
} }
} else {
if (onEmpty != null && isEmpty) { showContentLayout()
onEmpty()
} }
} }
fun <T> RefreshListLayout<T>.submitListResultWithStatus(list: List<T>?, hasMore: Boolean, onEmpty: (() -> Unit)? = null) { fun <T> RefreshListLayout<T>.submitListResult(list: List<T>?, hasMore: Boolean, onEmpty: (() -> Unit)? = null) {
if (isRefreshing) { if (isRefreshing) {
refreshCompleted() refreshCompleted()
} }
@ -70,75 +151,119 @@ fun <T> RefreshListLayout<T>.submitListResultWithStatus(list: List<T>?, hasMore:
} }
} }
fun <T> RefreshListLayout<T>.submitListResultWithoutStatus(list: List<T>?, hasMore: Boolean, onEmpty: (() -> Unit)? = null) { fun <T> RefreshListLayout<T>.handleStateList(state: State<List<T>>, onEmpty: (() -> Unit)? = null) {
when {
state.isLoading -> {
showLoadingIfEmpty()
}
state.isError -> {
handleListError(state.error())
}
state.isSuccess -> {
handleListResult(state.get(), onEmpty)
}
}
}
fun <T> RefreshListLayout<T>.handleStateFullList(state: State<List<T>>, hasMore: Boolean, onEmpty: (() -> Unit)? = null) {
when {
state.isLoading -> {
showLoadingIfEmpty()
}
state.isError -> {
handleListError(state.error())
}
state.isSuccess -> {
submitListResult(state.get(), hasMore, onEmpty)
}
}
}
fun RefreshListLayout<*>.showLoadingIfEmpty() {
if (isEmpty) {
if (isRefreshing) { if (isRefreshing) {
refreshCompleted() showBlank()
} else {
showLoadingLayout()
} }
}
}
//----------------------------------------------Loading In List And Without State----------------------------------------------
fun <T> RefreshListLayout<T>.handleListResultWithoutState(list: List<T>?, onEmpty: (() -> Unit)? = null) {
if (isLoadingMore) {
if (!isEmpty(list)) {
addData(list)
}
} else {
replaceData(list) replaceData(list)
loadMoreCompleted(hasMore) refreshCompleted()
}
if (pager != null) {
loadMoreCompleted(list != null && pager.hasMore(list.size))
}
if (onEmpty != null && isEmpty) { if (onEmpty != null && isEmpty) {
onEmpty() onEmpty()
} }
} }
fun RefreshListLayout<*>.handleListErrorWithStatus(throwable: Throwable) { fun RefreshListLayout<*>.handleListErrorWithoutState() {
if (isRefreshing) { if (isRefreshing) {
refreshCompleted() refreshCompleted()
} }
if (isLoadingMore) { if (isLoadingMore) {
loadMoreFailed() loadMoreFailed()
} }
if (isEmpty) {
val errorTypeClassifier = Sword.get().errorClassifier()
if (errorTypeClassifier != null) {
when {
errorTypeClassifier.isNetworkError(throwable) -> showNetErrorLayout()
errorTypeClassifier.isServerError(throwable) -> showServerErrorLayout()
else -> showErrorLayout()
}
} else {
showErrorLayout()
}
} else {
showContentLayout()
}
} }
fun RefreshListLayout<*>.handleListErrorWithoutStatus() { fun <T> RefreshListLayout<T>.submitListResultWithoutState(list: List<T>?, hasMore: Boolean, onEmpty: (() -> Unit)? = null) {
if (isRefreshing) { if (isRefreshing) {
refreshCompleted() refreshCompleted()
} }
if (isLoadingMore) {
loadMoreFailed() replaceData(list)
loadMoreCompleted(hasMore)
if (onEmpty != null && isEmpty) {
onEmpty()
} }
} }
fun RefreshListLayout<*>.showLoadingIfEmpty() { //----------------------------------------------Loading In StateView----------------------------------------------
if (isEmpty) { fun <T> RefreshStateLayout.handleStateResult(state: State<T>, onEmpty: (() -> Unit)? = null, onResult: ((T) -> Unit)) {
if (isRefreshing) { when {
showBlank() state.isLoading -> {
} else {
showLoadingLayout() showLoadingLayout()
} }
state.isError -> {
handleResultError(state.error())
}
state.isSuccess -> {
handleResult(state.get(), onEmpty, onResult)
}
} }
} }
fun <T> RefreshStateLayout.handleResult(t: T?, onEmpty: (() -> Unit)? = null, onResult: ((T) -> Unit)) {
fun <T> RefreshStateLayout.handleResultWithStatus(t: T?, onResult: ((T) -> Unit)) {
if (isRefreshing) { if (isRefreshing) {
refreshCompleted() refreshCompleted()
} }
if (t == null || (t is Collection<*> && t.isEmpty()) || (t is Map<*, *> && t.isEmpty())) { if (t == null || (t is Collection<*> && t.isEmpty()) || (t is Map<*, *> && t.isEmpty())) {
if (onEmpty != null) {
onEmpty()
} else {
showEmptyLayout() showEmptyLayout()
}
} else { } else {
onResult.invoke(t) onResult.invoke(t)
showContentLayout() showContentLayout()
} }
} }
fun RefreshStateLayout.handleErrorWithStatus(throwable: Throwable) { fun RefreshStateLayout.handleResultError(throwable: Throwable) {
if (isRefreshing) { if (isRefreshing) {
refreshCompleted() refreshCompleted()
} }

@ -42,7 +42,7 @@ public class SoftKeyboardUtils {
} }
public static boolean hideSoftInput(Activity activity) { public static boolean hideSoftInput(Activity activity) {
if (activity.getCurrentFocus() != null) { if (activity != null && activity.getCurrentFocus() != null) {
InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
return imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); return imm.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
} }

@ -229,9 +229,11 @@ public class PullToZoomScrollView extends NestedScrollView {
mZoomView.setPivotY(mOriginContainerViewHeight / 3F); mZoomView.setPivotY(mOriginContainerViewHeight / 3F);
float addOffset = (height - mOriginContainerViewHeight) * mZoomFactory / mOriginContainerViewHeight; float addOffset = (height - mOriginContainerViewHeight) * mZoomFactory / mOriginContainerViewHeight;
float scale = height * 1.0F / mOriginContainerViewHeight + addOffset; float scale = height * 1.0F / mOriginContainerViewHeight + addOffset;
if (!Float.isInfinite(scale)) {
mZoomView.setScaleX(scale); mZoomView.setScaleX(scale);
mZoomView.setScaleY(scale); mZoomView.setScaleY(scale);
} }
}
private void getInnerViewHeight() { private void getInnerViewHeight() {
post(() -> { post(() -> {

Loading…
Cancel
Save