diff --git a/lib_base/src/main/java/com/android/base/adapter/DataManager.java b/lib_base/src/main/java/com/android/base/adapter/DataManager.java index f92f4ad..f35a37b 100644 --- a/lib_base/src/main/java/com/android/base/adapter/DataManager.java +++ b/lib_base/src/main/java/com/android/base/adapter/DataManager.java @@ -56,6 +56,8 @@ public interface DataManager { void removeItems(List elements); + void removeItems(List elements, boolean isSuccessive); + //get T getItem(int position); diff --git a/lib_base/src/main/java/com/android/base/adapter/list/BaseListAdapter.java b/lib_base/src/main/java/com/android/base/adapter/list/BaseListAdapter.java index ba792db..6fc5a36 100644 --- a/lib_base/src/main/java/com/android/base/adapter/list/BaseListAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/list/BaseListAdapter.java @@ -136,6 +136,11 @@ public abstract class BaseListAdapter extends BaseAdap mDataManager.removeItems(elements); } + @Override + public void removeItems(List elements, boolean isSuccessive) { + mDataManager.removeItems(elements, isSuccessive); + } + @Override public void removeAt(int index) { mDataManager.removeAt(index); diff --git a/lib_base/src/main/java/com/android/base/adapter/list/ListDataManagerImpl.java b/lib_base/src/main/java/com/android/base/adapter/list/ListDataManagerImpl.java index c6efa6a..52221f0 100644 --- a/lib_base/src/main/java/com/android/base/adapter/list/ListDataManagerImpl.java +++ b/lib_base/src/main/java/com/android/base/adapter/list/ListDataManagerImpl.java @@ -123,6 +123,11 @@ final class ListDataManagerImpl implements DataManager { } } + @Override + public void removeItems(List elements, boolean isSuccessive) { + removeItems(elements); + } + @Override public void removeAt(int index) { if (mData != null && mData.size() > index) { diff --git a/lib_base/src/main/java/com/android/base/adapter/recycler/DiffRecyclerAdapter.java b/lib_base/src/main/java/com/android/base/adapter/recycler/DiffRecyclerAdapter.java index 6054520..5a29991 100644 --- a/lib_base/src/main/java/com/android/base/adapter/recycler/DiffRecyclerAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/recycler/DiffRecyclerAdapter.java @@ -195,6 +195,11 @@ public abstract class DiffRecyclerAdapter extends Recy mAsyncListDiffer.submitList(newList); } + @Override + public void removeItems(List elements, boolean isSuccessive) { + removeItems(elements); + } + @Override public void removeAt(int index) { if (getDataSize() > index) { diff --git a/lib_base/src/main/java/com/android/base/adapter/recycler/MultiTypeAdapter.java b/lib_base/src/main/java/com/android/base/adapter/recycler/MultiTypeAdapter.java index 7571c1a..cc4f88e 100644 --- a/lib_base/src/main/java/com/android/base/adapter/recycler/MultiTypeAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/recycler/MultiTypeAdapter.java @@ -120,6 +120,11 @@ public class MultiTypeAdapter extends me.drakeet.multitype.MultiTypeAdapter impl mRecyclerDataManager.removeItems(elements); } + @Override + public void removeItems(List elements, boolean isSuccessive) { + mRecyclerDataManager.removeItems(elements, isSuccessive); + } + @Override public Object getItem(int position) { return mRecyclerDataManager.getItem(position); diff --git a/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerAdapter.java b/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerAdapter.java index 4326a66..b8f897c 100644 --- a/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerAdapter.java @@ -112,6 +112,11 @@ public abstract class RecyclerAdapter extends Recycler mDataManager.removeItems(elements); } + @Override + public void removeItems(List elements, boolean isSuccessive) { + mDataManager.removeItems(elements, isSuccessive); + } + @Override public void removeAt(int index) { mDataManager.removeAt(index); diff --git a/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerDataManagerImpl.java b/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerDataManagerImpl.java index 1524222..e923ec4 100644 --- a/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerDataManagerImpl.java +++ b/lib_base/src/main/java/com/android/base/adapter/recycler/RecyclerDataManagerImpl.java @@ -170,6 +170,25 @@ final class RecyclerDataManagerImpl implements DataManager { } } + @Override + public void removeItems(List elements, boolean isSuccessive) { + if (Checker.isEmpty(elements) || Checker.isEmpty(mData)) { + return; + } + if (!isSuccessive) { + removeItems(elements); + return; + } + T data = elements.get(0); + int index = mData.indexOf(data); + if (index == -1) { + removeItems(elements); + } else { + mData.removeAll(elements); + notifyItemRangeRemoved(index, elements.size()); + } + } + @Override public T getItem(int position) { position = position - getHeaderSize(); @@ -241,6 +260,10 @@ final class RecyclerDataManagerImpl implements DataManager { mAdapter.notifyItemRemoved(index); } + private void notifyItemRangeRemoved(int index, int size) { + mAdapter.notifyItemRangeRemoved(index, size); + } + void setHeaderSize(HeaderSize headerSize) { mHeaderSize = headerSize; } diff --git a/lib_base/src/main/java/com/android/base/app/aac/AutoDisposeLiveData.kt b/lib_base/src/main/java/com/android/base/app/aac/AutoDisposeLiveData.kt index db50540..57b9438 100644 --- a/lib_base/src/main/java/com/android/base/app/aac/AutoDisposeLiveData.kt +++ b/lib_base/src/main/java/com/android/base/app/aac/AutoDisposeLiveData.kt @@ -8,38 +8,6 @@ import com.github.dmstocking.optional.java.util.Optional import com.uber.autodispose.* -fun ObservableSubscribeProxy.toLiveData(): LiveData { - val liveData = MutableLiveData() - this.subscribeIgnoreError { - liveData.postValue(it) - } - return liveData -} - -fun FlowableSubscribeProxy.toLiveData(): LiveData { - val liveData = MutableLiveData() - this.subscribeIgnoreError { - liveData.postValue(it) - } - return liveData -} - -fun SingleSubscribeProxy.toLiveData(): LiveData { - val liveData = MutableLiveData() - this.subscribeIgnoreError { - liveData.postValue(it) - } - return liveData -} - -fun MaybeSubscribeProxy.toLiveData(): LiveData { - val liveData = MutableLiveData() - this.subscribeIgnoreError { - liveData.postValue(it) - } - return liveData -} - //----------------------------------------------------------------------------------------- fun ObservableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData>) { @@ -152,6 +120,18 @@ fun CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData CompletableSubscribeProxy.subscribeWithLiveData(liveData: MutableLiveData>, provider: () -> T) { + liveData.postValue(Resource.loading()) + this.subscribe( + { + liveData.postValue(Resource.success(provider())) + }, + { + liveData.postValue(Resource.error(it)) + } + ) +} + //----------------------------------------------------------------------------------------- fun ObservableSubscribeProxy.toResourceLiveData(): LiveData> { @@ -222,4 +202,38 @@ fun CompletableSubscribeProxy.toResourceLiveData(): LiveData> { } ) return mutableLiveData +} + +//----------------------------------------------------------------------------------------- + +fun ObservableSubscribeProxy.toLiveData(): LiveData { + val liveData = MutableLiveData() + this.subscribeIgnoreError { + liveData.postValue(it) + } + return liveData +} + +fun FlowableSubscribeProxy.toLiveData(): LiveData { + val liveData = MutableLiveData() + this.subscribeIgnoreError { + liveData.postValue(it) + } + return liveData +} + +fun SingleSubscribeProxy.toLiveData(): LiveData { + val liveData = MutableLiveData() + this.subscribeIgnoreError { + liveData.postValue(it) + } + return liveData +} + +fun MaybeSubscribeProxy.toLiveData(): LiveData { + val liveData = MutableLiveData() + this.subscribeIgnoreError { + liveData.postValue(it) + } + return liveData } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/aac/ResourceLiveData.kt b/lib_base/src/main/java/com/android/base/app/aac/ResourceLiveData.kt index d2c21fb..d8553d8 100644 --- a/lib_base/src/main/java/com/android/base/app/aac/ResourceLiveData.kt +++ b/lib_base/src/main/java/com/android/base/app/aac/ResourceLiveData.kt @@ -120,6 +120,18 @@ fun Completable.subscribeWithLiveData(liveData: MutableLiveData>) ) } +fun Completable.subscribeWithLiveData(liveData: MutableLiveData>, provider: () -> T) { + liveData.postValue(Resource.loading()) + this.subscribe( + { + liveData.postValue(Resource.success(provider())) + }, + { + liveData.postValue(Resource.error(it)) + } + ) +} + //----------------------------------------------------------------------------------------- fun Observable.toResourceLiveData(): LiveData> { diff --git a/lib_base/src/main/java/com/android/base/app/fragment/Fragments.kt b/lib_base/src/main/java/com/android/base/app/fragment/Fragments.kt index d77e9e3..b0cef5f 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/Fragments.kt +++ b/lib_base/src/main/java/com/android/base/app/fragment/Fragments.kt @@ -86,7 +86,7 @@ fun FragmentManager.findFragmentByTag(clazz: KClass): T? { return findFragmentByTag(clazz.java.name) as? T } -fun FragmentManager.popBackTo(flag: String, immediate: Boolean = false) { +fun FragmentManager.popBackUntil(flag: String, immediate: Boolean = false) { if (immediate) { popBackStackImmediate(flag, FragmentManager.POP_BACK_STACK_INCLUSIVE) } else { @@ -94,6 +94,14 @@ fun FragmentManager.popBackTo(flag: String, immediate: Boolean = false) { } } +fun FragmentManager.popBackTo(flag: String, immediate: Boolean = false) { + if (immediate) { + popBackStackImmediate(flag, 0) + } else { + popBackStack(flag,0) + } +} + fun FragmentManager.clearBackStack(immediate: Boolean = false) { if (immediate) { this.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE) diff --git a/lib_base/src/main/java/com/android/base/app/fragment/RefreshableStateLayoutImpl.java b/lib_base/src/main/java/com/android/base/app/fragment/RefreshableStateLayoutImpl.java index 5fe8cdb..e088fa4 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/RefreshableStateLayoutImpl.java +++ b/lib_base/src/main/java/com/android/base/app/fragment/RefreshableStateLayoutImpl.java @@ -120,7 +120,7 @@ final class RefreshableStateLayoutImpl implements RefreshStateLayout, StateLayou @Override public void showServerErrorLayout() { refreshCompleted(); - checkMultiStateView().showNetErrorLayout(); + checkMultiStateView().showServerErrorLayout(); } @Override diff --git a/lib_base/src/main/java/com/android/base/app/ui/UIEx.kt b/lib_base/src/main/java/com/android/base/app/ui/UIEx.kt index 1c7ec40..96b1b60 100644 --- a/lib_base/src/main/java/com/android/base/app/ui/UIEx.kt +++ b/lib_base/src/main/java/com/android/base/app/ui/UIEx.kt @@ -148,8 +148,14 @@ fun RefreshStateLayout.processErrorWithStatus(throwable: Throwable?) { val errorTypeClassifier = BaseKit.get().errorClassifier() if (errorTypeClassifier != null) { when { - errorTypeClassifier.isNetworkError(throwable) -> showNetErrorLayout() - errorTypeClassifier.isServerError(throwable) -> showServerErrorLayout() + errorTypeClassifier.isNetworkError(throwable) -> { + Timber.d("isNetworkError showNetErrorLayout") + showNetErrorLayout() + } + errorTypeClassifier.isServerError(throwable) -> { + Timber.d("isServerError showServerErrorLayout") + showServerErrorLayout() + } else -> showErrorLayout() } } else { diff --git a/lib_base/src/main/java/com/android/base/kotlin/Collections.kt b/lib_base/src/main/java/com/android/base/kotlin/Collections.kt index 8f33905..78e2629 100644 --- a/lib_base/src/main/java/com/android/base/kotlin/Collections.kt +++ b/lib_base/src/main/java/com/android/base/kotlin/Collections.kt @@ -2,6 +2,18 @@ package com.android.base.kotlin import com.android.base.utils.common.CollectionUtils -fun List?.toArrayList(): ArrayList { +fun List?.toArrayList(): ArrayList { return CollectionUtils.toArrayList(this) +} + +fun MutableList.removeWhich(filter: (E) -> Boolean): Boolean { + var removed = false + val each = iterator() + while (each.hasNext()) { + if (filter(each.next())) { + each.remove() + removed = true + } + } + return removed } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/widget/MultiStateView.java b/lib_base/src/main/java/com/android/base/widget/MultiStateView.java index 2898281..fc1a733 100644 --- a/lib_base/src/main/java/com/android/base/widget/MultiStateView.java +++ b/lib_base/src/main/java/com/android/base/widget/MultiStateView.java @@ -1,6 +1,5 @@ package com.android.base.widget; -import android.annotation.SuppressLint; import android.content.Context; import android.content.res.TypedArray; import android.support.annotation.NonNull; @@ -8,6 +7,7 @@ import android.support.annotation.Nullable; import android.util.AttributeSet; import android.util.SparseArray; import android.view.LayoutInflater; +import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; @@ -32,7 +32,7 @@ public class MultiStateView extends FrameLayout { private LayoutInflater mInflater; private SparseArray mChildren; private View mContentView; - private boolean mDisableOperationWhenRequesting = true; + private boolean mDisableOperationWhenRequesting = false; @Nullable private StateListener mListener; @@ -64,7 +64,7 @@ public class MultiStateView extends FrameLayout { int emptyViewResId = a.getResourceId(R.styleable.MultiStateView_msv_emptyView, -1); int errorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_errorView, -1); int netErrorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_net_errorView, -1); - int serverErrorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_net_errorView, -1); + int serverErrorViewResId = a.getResourceId(R.styleable.MultiStateView_msv_server_errorView, -1); mChildren.put(LOADING, new ViewHolder(loadingViewResId)); mChildren.put(REQUESTING, new ViewHolder(requestingViewResId)); @@ -73,7 +73,7 @@ public class MultiStateView extends FrameLayout { mChildren.put(NET_ERROR, new ViewHolder(netErrorViewResId)); mChildren.put(SERVER_ERROR, new ViewHolder(serverErrorViewResId)); - mDisableOperationWhenRequesting = a.getBoolean(R.styleable.MultiStateView_msv_disable_when_requesting, true); + mDisableOperationWhenRequesting = a.getBoolean(R.styleable.MultiStateView_msv_disable_when_requesting, false); ensureInitState(a.getInt(R.styleable.MultiStateView_msv_viewState, CONTENT)); @@ -272,25 +272,35 @@ public class MultiStateView extends FrameLayout { View curStateView = ensureStateView(mViewState); int size = mChildren.size(); + for (int i = 0; i < size; i++) { ViewHolder viewHolder = mChildren.valueAt(i); if (viewHolder.mView == null) { continue; } + if (viewHolder.mView != curStateView) { - if (mViewState == REQUESTING && viewHolder.mView != mContentView) { - viewHolder.mView.setVisibility(GONE); + if (mViewState == REQUESTING && viewHolder.mView == mContentView) { + viewHolder.mView.setVisibility(VISIBLE); } else { viewHolder.mView.setVisibility(GONE); } } } + curStateView.setVisibility(VISIBLE); + } - if (mViewState == REQUESTING) { - curStateView.setOnTouchListener(mDisableOperationWhenRequesting ? NO_ACTION_TOUCH_LISTENER : null); + @Override + public boolean onInterceptTouchEvent(MotionEvent ev) { + if (ev.getAction() == MotionEvent.ACTION_DOWN) { + if (mViewState == REQUESTING) { + return mDisableOperationWhenRequesting; + } else { + return false; + } } - + return super.onInterceptTouchEvent(ev); } /** @@ -322,17 +332,6 @@ public class MultiStateView extends FrameLayout { public void setDisableOperationWhenRequesting(boolean disableOperationWhenRequesting) { mDisableOperationWhenRequesting = disableOperationWhenRequesting; - ViewHolder viewHolder = mChildren.get(LOADING); - if (viewHolder == null || viewHolder.mView == null) { - return; - } - if (disableOperationWhenRequesting) { - if (mViewState == REQUESTING) { - viewHolder.mView.setOnTouchListener(NO_ACTION_TOUCH_LISTENER); - } - } else { - viewHolder.mView.setOnTouchListener(null); - } } /** @@ -361,21 +360,18 @@ public class MultiStateView extends FrameLayout { void onStateInflated(@ViewState int viewState, @NonNull View view); } - @SuppressLint("ClickableViewAccessibility") - private static final OnTouchListener NO_ACTION_TOUCH_LISTENER = (v, event) -> true; - private class ViewHolder { private View mView; private int mViewLayoutId; - public ViewHolder(int viewLayoutId) { + ViewHolder(int viewLayoutId) { this(null, viewLayoutId); } - public ViewHolder(View view, int viewLayoutId) { + ViewHolder(View view, int viewLayoutId) { mView = view; mViewLayoutId = viewLayoutId; } } -} +} \ No newline at end of file diff --git a/lib_base/src/main/res/values/base_ids.xml b/lib_base/src/main/res/values/base_ids.xml index c2d29c2..7256f19 100644 --- a/lib_base/src/main/res/values/base_ids.xml +++ b/lib_base/src/main/res/values/base_ids.xml @@ -4,7 +4,6 @@ - @@ -14,5 +13,4 @@ - \ No newline at end of file