diff --git a/lib_base/build.gradle b/lib_base/build.gradle index 4ed366a..4d96246 100644 --- a/lib_base/build.gradle +++ b/lib_base/build.gradle @@ -83,6 +83,7 @@ dependencies { api androidLibraries.lifecycleViewModelKtx api androidLibraries.lifecycleExtensions api androidLibraries.lifecycleReactiveStreams + api androidLibraries.ktx //Kotlin api kotlinLibraries.kotlinStdlib 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 f35a37b..8534a4b 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 @@ -2,6 +2,8 @@ package com.android.base.adapter; import java.util.List; +import androidx.annotation.Nullable; + /** *
* 注意数据源引用的替换,只有setDataSource方法会把elements替换掉之前的数据源引用,其他方法都是基于现有数据集合做删除与添加操作。 @@ -13,7 +15,6 @@ import java.util.List; */ public interface DataManager- * - * @param defaultData 如果不存在数据则返回 defaultData 所设置的默认数据。 - * @return Resource 中保存的数据。 - */ - @Nullable - public T orElse(@Nullable T defaultData) { - if (mData == null) { - return defaultData; - } - return mData; - } - - /** - * 获取 Resource 中保存的数据,在不同状态下获取的数据具有不同的意义: - * - *{ - //Add op void add(T element); void addAt(int location, T element); @@ -21,7 +22,7 @@ public interface DataManager { void addItems(List elements); /** - * 添加元素前会使用equals方法进行比较,荣旧的数据集合中删除相同的数据在添加 + * 添加元素前会使用equals方法进行比较。 * * @param elements 元素 */ @@ -29,7 +30,6 @@ public interface DataManager { void addItemsAt(int location, List elements); - //update op void replace(T oldElement, T newElement); void replaceAt(int index, T element); @@ -42,14 +42,13 @@ public interface DataManager { void replaceAll(List elements); /** - * 此方法会使用elements替换掉之前的数据源,而不对之前的数据源做任何操作 + * 此方法会使用 newDataSource 替换掉之前的数据源,而不对之前的数据源做任何操作。 * * @param newDataSource 新的数据集 * @param notifyDataSetChanged 是否调用adapter的notifyDataSetChanged方法 */ void setDataSource(List newDataSource, boolean notifyDataSetChanged); - //remove opt void remove(T element); void removeAt(int index); @@ -58,26 +57,23 @@ public interface DataManager { void removeItems(List elements, boolean isSuccessive); - //get + @Nullable T getItem(int position); List getItems(); int getDataSize(); - //contains boolean contains(T element); boolean isEmpty(); - //clear opt void clear(); - //Utils - /** * @param t element * @return -1 if not contains this element */ - int getItemPosition(T t); + int indexItem(T t); + } diff --git a/lib_base/src/main/java/com/android/base/adapter/ItemHelper.java b/lib_base/src/main/java/com/android/base/adapter/ItemHelper.java index 4be1188..e35ea79 100644 --- a/lib_base/src/main/java/com/android/base/adapter/ItemHelper.java +++ b/lib_base/src/main/java/com/android/base/adapter/ItemHelper.java @@ -11,8 +11,8 @@ import androidx.annotation.StringRes; /** * @author Ztiany - * Email :1169654504@qq.com - * Date :015-12-29 20:47 + * Email :1169654504@qq.com + * Date :015-12-29 20:47 */ public class ItemHelper { @@ -24,7 +24,7 @@ public class ItemHelper { views = new SparseArray<>(); } - @SuppressWarnings("unchecked") + @SuppressWarnings("unchecked,WeakerAccess") public T getView(int viewId) { View view = views.get(viewId); if (view == null) { 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 833adec..bfcc415 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 @@ -12,6 +12,7 @@ import com.android.base.adapter.DataManager; import java.util.List; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; /** * absListView通用的Adapter,注意:只有setDataSource才能替换原有数据源的引用。 @@ -22,7 +23,7 @@ import androidx.annotation.NonNull; @SuppressWarnings("unused") public abstract class BaseListAdapter extends BaseAdapter implements DataManager { - protected final Context mContext; + private final Context mContext; private final static int ITEM_ID = R.id.base_item_tag_view_id; private DataManager mDataManager; private final LayoutInflater mLayoutInflater; @@ -38,6 +39,10 @@ public abstract class BaseListAdapter extends BaseAdap mDataManager = new ListDataManagerImpl<>(data, this); } + public Context getContext() { + return mContext; + } + @Override public int getCount() { return getDataSize(); @@ -66,7 +71,6 @@ public abstract class BaseListAdapter extends BaseAdap return viewHolder.mItemView; } - @SuppressWarnings("all") protected abstract void onBindData(@NonNull VH viewHolder, T item); @NonNull @@ -82,11 +86,6 @@ public abstract class BaseListAdapter extends BaseAdap return super.getViewTypeCount(); } - - /////////////////////////////////////////////////////////////////////////// - // DataManager - /////////////////////////////////////////////////////////////////////////// - @Override public void add(T elem) { mDataManager.add(elem); @@ -147,6 +146,7 @@ public abstract class BaseListAdapter extends BaseAdap mDataManager.removeAt(index); } + @Nullable @Override public T getItem(int position) { return mDataManager.getItem(position); @@ -168,8 +168,8 @@ public abstract class BaseListAdapter extends BaseAdap } @Override - public int getItemPosition(T t) { - return mDataManager.getItemPosition(t); + public int indexItem(T t) { + return mDataManager.indexItem(t); } @Override @@ -181,4 +181,5 @@ public abstract class BaseListAdapter extends BaseAdap public List getItems() { return mDataManager.getItems(); } -} + +} \ No newline at end of file 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 52221f0..1ec5690 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 @@ -8,6 +8,8 @@ import com.android.base.utils.common.Checker; import java.util.ArrayList; import java.util.List; +import androidx.annotation.Nullable; + final class ListDataManagerImpl implements DataManager { @@ -137,6 +139,7 @@ final class ListDataManagerImpl implements DataManager { } @Override + @Nullable public T getItem(int position) { if (mData != null && mData.size() > position) { return mData.get(position); @@ -168,7 +171,7 @@ final class ListDataManagerImpl implements DataManager { } @Override - public int getItemPosition(T t) { + public int indexItem(T t) { List items = getItems(); if (items == null) { return -1; @@ -185,4 +188,4 @@ final class ListDataManagerImpl implements DataManager { mBaseAdapter.notifyDataSetChanged(); } -} +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/adapter/list/SmartViewHolder.java b/lib_base/src/main/java/com/android/base/adapter/list/SmartViewHolder.java index 71c0123..e849c9b 100644 --- a/lib_base/src/main/java/com/android/base/adapter/list/SmartViewHolder.java +++ b/lib_base/src/main/java/com/android/base/adapter/list/SmartViewHolder.java @@ -7,7 +7,7 @@ import com.android.base.adapter.ItemHelper; @SuppressWarnings("unused") public class SmartViewHolder extends ViewHolder { - protected final ItemHelper mHelper; + private final ItemHelper mHelper; public SmartViewHolder(View itemView) { super(itemView); @@ -18,4 +18,4 @@ public class SmartViewHolder extends ViewHolder { return mHelper; } -} +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/adapter/list/ViewHolder.java b/lib_base/src/main/java/com/android/base/adapter/list/ViewHolder.java index ba9fc47..3387909 100644 --- a/lib_base/src/main/java/com/android/base/adapter/list/ViewHolder.java +++ b/lib_base/src/main/java/com/android/base/adapter/list/ViewHolder.java @@ -4,7 +4,6 @@ import android.view.View; public class ViewHolder { - @SuppressWarnings("all") protected final View mItemView; private int mPosition; private int mType; @@ -32,4 +31,5 @@ public class ViewHolder { public View getItemView() { return mItemView; } + } diff --git a/lib_base/src/main/java/com/android/base/adapter/pager/recycler/RecycleBin.java b/lib_base/src/main/java/com/android/base/adapter/pager/RecycleBin.java similarity index 98% rename from lib_base/src/main/java/com/android/base/adapter/pager/recycler/RecycleBin.java rename to lib_base/src/main/java/com/android/base/adapter/pager/RecycleBin.java index 2b2d01e..b58784d 100644 --- a/lib_base/src/main/java/com/android/base/adapter/pager/recycler/RecycleBin.java +++ b/lib_base/src/main/java/com/android/base/adapter/pager/RecycleBin.java @@ -1,4 +1,4 @@ -package com.android.base.adapter.pager.recycler; +package com.android.base.adapter.pager; import android.os.Build; import android.util.SparseArray; @@ -15,6 +15,7 @@ import android.view.View; * is copyrighted 2006 The Android Open Source Project. */ public class RecycleBin { + /** * Views that were on screen at the start of layout. This array is populated at the start of * layout, and at the end of layout all view in activeViews are moved to scrapViews. @@ -149,4 +150,5 @@ public class RecycleBin { return null; } } -} + +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/adapter/pager/recycler/RecyclingPagerAdapter.java b/lib_base/src/main/java/com/android/base/adapter/pager/RecyclingPagerAdapter.java similarity index 92% rename from lib_base/src/main/java/com/android/base/adapter/pager/recycler/RecyclingPagerAdapter.java rename to lib_base/src/main/java/com/android/base/adapter/pager/RecyclingPagerAdapter.java index 2a2b026..03d84db 100644 --- a/lib_base/src/main/java/com/android/base/adapter/pager/recycler/RecyclingPagerAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/pager/RecyclingPagerAdapter.java @@ -1,9 +1,11 @@ -package com.android.base.adapter.pager.recycler; +package com.android.base.adapter.pager; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; +import org.jetbrains.annotations.NotNull; + import androidx.viewpager.widget.PagerAdapter; /** @@ -33,7 +35,7 @@ public abstract class RecyclingPagerAdapter extends PagerAdapter { } @Override - public final Object instantiateItem(ViewGroup container, int position) { + public final Object instantiateItem(@NotNull ViewGroup container, int position) { int viewType = getItemViewType(position); View view = null; if (viewType != IGNORE_ITEM_VIEW_TYPE) { @@ -45,7 +47,7 @@ public abstract class RecyclingPagerAdapter extends PagerAdapter { } @Override - public final void destroyItem(ViewGroup container, int position, Object object) { + public final void destroyItem(ViewGroup container, int position, @NotNull Object object) { View view = (View) object; container.removeView(view); int viewType = getItemViewType(position); @@ -55,7 +57,7 @@ public abstract class RecyclingPagerAdapter extends PagerAdapter { } @Override - public final boolean isViewFromObject(View view, Object object) { + public final boolean isViewFromObject(@NotNull View view, @NotNull Object object) { return view == object; } @@ -108,8 +110,9 @@ public abstract class RecyclingPagerAdapter extends PagerAdapter { * Heterogeneous lists can specify their number of view types, so that this View is * always of the right type (see {@link #getViewTypeCount()} and * {@link #getItemViewType(int)}). - * @param parent The parent that this view will eventually be attached to + * @param container The parent that this view will eventually be attached to * @return A View corresponding to the data at the specified position. */ public abstract View getView(int position, View convertView, ViewGroup container); + } diff --git a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerAdapter.java b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerAdapter.java index af92869..421f5c3 100644 --- a/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerAdapter.java +++ b/lib_base/src/main/java/com/android/base/adapter/pager/ViewPagerAdapter.java @@ -4,7 +4,6 @@ import android.view.View; import android.view.ViewGroup; import com.android.base.R; -import com.android.base.adapter.pager.recycler.RecyclingPagerAdapter; import com.android.base.utils.common.Checker; import java.util.List; @@ -13,7 +12,7 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; /** - * 如果使用可缩放的 View 作为 pager,可能不适合使用此Adapter + * 如果使用可缩放的 View 作为 pager,可能不适合使用此 Adapter * * @param 数据 * @param View Holder类型 @@ -67,4 +66,4 @@ public abstract class ViewPagerAdapter extends RecyclerView.Adapter implements DataManager { @NonNull - protected Context mContext; + private Context mContext; private AsyncListDiffer mAsyncListDiffer; @@ -67,6 +67,11 @@ public abstract class DiffRecyclerAdapter extends Recy } } + @NonNull + public Context getContext() { + return mContext; + } + @Override public int getItemCount() { return getDataSize(); @@ -85,7 +90,7 @@ public abstract class DiffRecyclerAdapter extends Recy public abstract void onBindViewHolder(@NonNull VH viewHolder, int position); public void notifyEntryChanged(T t) { - int itemPosition = getItemPosition(t); + int itemPosition = indexItem(t); if (itemPosition != -1) { notifyItemChanged(itemPosition); } @@ -246,7 +251,7 @@ public abstract class DiffRecyclerAdapter extends Recy } @Override - public int getItemPosition(T t) { + public int indexItem(T t) { return isEmpty() ? -1 : getItems().indexOf(t); } 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 2c4bf64..7b93b18 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 @@ -8,6 +8,7 @@ import java.util.ArrayList; import java.util.List; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; /** @@ -45,7 +46,7 @@ public class MultiTypeAdapter extends com.drakeet.multitype.MultiTypeAdapter imp @SuppressWarnings("unused") public void notifyEntryChanged(Object entry) { - int itemPosition = getItemPosition(entry); + int itemPosition = indexItem(entry); if (itemPosition != -1) { notifyItemChanged(itemPosition); } @@ -117,6 +118,7 @@ public class MultiTypeAdapter extends com.drakeet.multitype.MultiTypeAdapter imp mRecyclerDataManager.removeItems(elements, isSuccessive); } + @Nullable @Override public Object getItem(int position) { return mRecyclerDataManager.getItem(position); @@ -149,8 +151,8 @@ public class MultiTypeAdapter extends com.drakeet.multitype.MultiTypeAdapter imp } @Override - public int getItemPosition(Object o) { - return mRecyclerDataManager.getItemPosition(o); + public int indexItem(Object o) { + return mRecyclerDataManager.indexItem(o); } @Override 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 6a3d7db..726c3be 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 @@ -8,6 +8,7 @@ import com.android.base.adapter.DataManager; import java.util.List; import androidx.annotation.NonNull; +import androidx.annotation.Nullable; import androidx.recyclerview.widget.RecyclerView; /** @@ -24,7 +25,7 @@ public abstract class RecyclerAdapter extends Recycler private RecyclerDataManagerImpl mDataManager; @NonNull - protected final Context mContext; + private final Context mContext; public RecyclerAdapter(@NonNull Context context, List data) { mDataManager = new RecyclerDataManagerImpl<>(data, this); @@ -35,6 +36,11 @@ public abstract class RecyclerAdapter extends Recycler this(context, null); } + @NonNull + public Context getContext() { + return mContext; + } + @Override public int getItemCount() { return getDataSize(); @@ -53,7 +59,7 @@ public abstract class RecyclerAdapter extends Recycler public abstract void onBindViewHolder(@NonNull VH viewHolder, int position); public void notifyEntryChanged(T t) { - int itemPosition = getItemPosition(t); + int itemPosition = indexItem(t); if (itemPosition != -1) { notifyItemChanged(itemPosition); } @@ -123,6 +129,7 @@ public abstract class RecyclerAdapter extends Recycler mDataManager.removeAt(index); } + @Nullable @Override public T getItem(int position) { return mDataManager.getItem(position); @@ -163,8 +170,8 @@ public abstract class RecyclerAdapter extends Recycler } @Override - public int getItemPosition(T t) { - return mDataManager.getItemPosition(t); + public int indexItem(T t) { + return mDataManager.indexItem(t); } } \ No newline at end of file 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 5b2b675..7654d96 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 @@ -218,7 +218,7 @@ final class RecyclerDataManagerImpl implements DataManager { } @Override - public int getItemPosition(T t) { + public int indexItem(T t) { List items = getItems(); if (items == null) { return -1; diff --git a/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt b/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt index d36aa62..c749d88 100644 --- a/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt +++ b/lib_base/src/main/java/com/android/base/adapter/recycler/SimpleRecyclerAdapter.kt @@ -14,7 +14,7 @@ import android.view.ViewGroup */ abstract class SimpleRecyclerAdapter (context: Context, data: List ? = null) : RecyclerAdapter (context, data) { - private var layoutInflater: LayoutInflater = LayoutInflater.from(mContext) + private var layoutInflater: LayoutInflater = LayoutInflater.from(context) final override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): KtViewHolder { val layout = provideLayout(parent, viewType) @@ -33,7 +33,7 @@ abstract class SimpleRecyclerAdapter (context: Context, data: List ? = null) abstract fun provideLayout(parent: ViewGroup, viewType: Int): Any override fun onBindViewHolder(viewHolder: KtViewHolder, position: Int) { - bind(viewHolder, getItem(position)) + bind(viewHolder, getItem(position) ?: throw NullPointerException("SimpleRecyclerAdapter onBindViewHolder getItem return null")) } protected abstract fun bind(viewHolder: KtViewHolder, item: T) diff --git a/lib_base/src/main/java/com/android/base/adapter/recycler/SmartViewHolder.java b/lib_base/src/main/java/com/android/base/adapter/recycler/SmartViewHolder.java index b22d6d5..e84e14b 100644 --- a/lib_base/src/main/java/com/android/base/adapter/recycler/SmartViewHolder.java +++ b/lib_base/src/main/java/com/android/base/adapter/recycler/SmartViewHolder.java @@ -7,8 +7,7 @@ import com.android.base.adapter.ItemHelper; public class SmartViewHolder extends ViewHolder { - @SuppressWarnings("all") - protected final ItemHelper mHelper; + private final ItemHelper mHelper; public SmartViewHolder(View itemView) { super(itemView); diff --git a/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.java b/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.java index 67d09a9..b06284f 100644 --- a/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.java +++ b/lib_base/src/main/java/com/android/base/app/ApplicationDelegate.java @@ -103,7 +103,7 @@ public final class ApplicationDelegate { return mAppStatus; } - void setCrashProcessor(BaseKit.CrashProcessor crashProcessor) { + void setCrashProcessor(Sword.CrashProcessor crashProcessor) { if (mCrashHandler != null) { mCrashHandler.setCrashProcessor(crashProcessor); } diff --git a/lib_base/src/main/java/com/android/base/app/BaseAppContext.java b/lib_base/src/main/java/com/android/base/app/BaseAppContext.java index 72a3d3c..cf879b8 100644 --- a/lib_base/src/main/java/com/android/base/app/BaseAppContext.java +++ b/lib_base/src/main/java/com/android/base/app/BaseAppContext.java @@ -10,37 +10,37 @@ public class BaseAppContext extends Application { @Override protected void attachBaseContext(Context base) { super.attachBaseContext(base); - BaseKit.get().getApplicationDelegate().attachBaseContext(base); + Sword.get().getApplicationDelegate().attachBaseContext(base); } @Override public void onCreate() { super.onCreate(); - BaseKit.get().getApplicationDelegate().onCreate(this); + Sword.get().getApplicationDelegate().onCreate(this); } @Override public void onLowMemory() { super.onLowMemory(); - BaseKit.get().getApplicationDelegate().onLowMemory(); + Sword.get().getApplicationDelegate().onLowMemory(); } @Override public void onTrimMemory(int level) { super.onTrimMemory(level); - BaseKit.get().getApplicationDelegate().onTrimMemory(level); + Sword.get().getApplicationDelegate().onTrimMemory(level); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); - BaseKit.get().getApplicationDelegate().onConfigurationChanged(newConfig); + Sword.get().getApplicationDelegate().onConfigurationChanged(newConfig); } @Override public void onTerminate() { super.onTerminate(); - BaseKit.get().getApplicationDelegate().onTerminate(); + Sword.get().getApplicationDelegate().onTerminate(); } } \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/CrashHandler.java b/lib_base/src/main/java/com/android/base/app/CrashHandler.java index 1509ee0..83e2c33 100644 --- a/lib_base/src/main/java/com/android/base/app/CrashHandler.java +++ b/lib_base/src/main/java/com/android/base/app/CrashHandler.java @@ -22,7 +22,7 @@ import timber.log.Timber; final class CrashHandler implements Thread.UncaughtExceptionHandler { private Context mContext; - private BaseKit.CrashProcessor mCrashProcessor; + private Sword.CrashProcessor mCrashProcessor; public static CrashHandler register(Application application) { CrashHandler crashHandler = new CrashHandler(application); @@ -30,7 +30,7 @@ final class CrashHandler implements Thread.UncaughtExceptionHandler { return crashHandler; } - void setCrashProcessor(BaseKit.CrashProcessor crashProcessor) { + void setCrashProcessor(Sword.CrashProcessor crashProcessor) { mCrashProcessor = crashProcessor; } diff --git a/lib_base/src/main/java/com/android/base/app/BaseKit.java b/lib_base/src/main/java/com/android/base/app/Sword.java similarity index 85% rename from lib_base/src/main/java/com/android/base/app/BaseKit.java rename to lib_base/src/main/java/com/android/base/app/Sword.java index 91d77be..5ebaae5 100644 --- a/lib_base/src/main/java/com/android/base/app/BaseKit.java +++ b/lib_base/src/main/java/com/android/base/app/Sword.java @@ -11,7 +11,7 @@ import com.android.base.app.fragment.LoadingViewFactory; import com.android.base.app.ui.PageNumber; import com.android.base.app.ui.RefreshLoadViewFactory; import com.android.base.app.ui.RefreshViewFactory; -import com.android.base.interfaces.adapter.ActivityLifecycleCallbacksAdapter; +import com.android.base.interfaces.ActivityLifecycleCallbacksAdapter; import com.android.base.receiver.NetworkState; import com.blankj.utilcode.util.ActivityUtils; import com.blankj.utilcode.util.AppUtils; @@ -27,22 +27,23 @@ import dagger.android.support.AndroidSupportInjection; import io.reactivex.Flowable; /** - * 基础库工具 + * useful tools for android development, just like a sword. * * @author Ztiany * Email: ztiany3@gmail.com * Date : 2018-04-16 17:12 */ +@SuppressWarnings("unused") @UiThread -public final class BaseKit { +public final class Sword { - private static final BaseKit ONLY_BASE = new BaseKit(); + private static final Sword ONLY_BASE = new Sword(); - private BaseKit() { + private Sword() { mApplicationDelegate = new ApplicationDelegate(); } - public static BaseKit get() { + public static Sword get() { return ONLY_BASE; } @@ -64,12 +65,11 @@ public final class BaseKit { /** * 获取 Application 代理 */ - @SuppressWarnings("WeakerAccess") public ApplicationDelegate getApplicationDelegate() { return mApplicationDelegate; } - public BaseKit registerLoadingFactory(LoadingViewFactory loadingViewFactory) { + public Sword registerLoadingFactory(LoadingViewFactory loadingViewFactory) { if (mLoadingViewFactory != null) { throw new UnsupportedOperationException("LoadingViewFactory had already set"); } @@ -93,17 +93,17 @@ public final class BaseKit { void uncaughtException(Thread thread, Throwable ex); } - public BaseKit setCrashProcessor(CrashProcessor crashProcessor) { + public Sword setCrashProcessor(CrashProcessor crashProcessor) { mApplicationDelegate.setCrashProcessor(crashProcessor); return this; } - public BaseKit setDefaultPageStart(int pageStart) { + public Sword setDefaultPageStart(int pageStart) { PageNumber.setDefaultPageStart(pageStart); return this; } - public BaseKit setDefaultPageSize(int defaultPageSize) { + public Sword setDefaultPageSize(int defaultPageSize) { PageNumber.setDefaultPageSize(defaultPageSize); return this; } @@ -113,12 +113,12 @@ public final class BaseKit { * * @param defaultContainerId 容器id */ - public BaseKit setDefaultFragmentContainerId(int defaultContainerId) { + public Sword setDefaultFragmentContainerId(int defaultContainerId) { FragmentConfig.setDefaultContainerId(defaultContainerId); return this; } - public BaseKit enableAutoInject() { + public Sword enableAutoInject() { Application.ActivityLifecycleCallbacks activityLifecycleCallbacks = new ActivityLifecycleCallbacksAdapter() { @Override @@ -182,8 +182,7 @@ public final class BaseKit { boolean isServerError(Throwable throwable); } - @SuppressWarnings("all") - public BaseKit setErrorClassifier(ErrorClassifier errorClassifier) { + public Sword setErrorClassifier(ErrorClassifier errorClassifier) { if (mErrorClassifier != null) { throw new UnsupportedOperationException("ErrorClassifier had already set"); } @@ -195,12 +194,12 @@ public final class BaseKit { return mErrorClassifier; } - public BaseKit registerRefreshLoadViewFactory(RefreshLoadViewFactory.Factory factory) { + public Sword registerRefreshLoadViewFactory(RefreshLoadViewFactory.Factory factory) { RefreshLoadViewFactory.registerFactory(factory); return this; } - public BaseKit registerRefreshViewFactory(RefreshViewFactory.Factory factory) { + public Sword registerRefreshViewFactory(RefreshViewFactory.Factory factory) { RefreshViewFactory.registerFactory(factory); return this; } diff --git a/lib_base/src/main/java/com/android/base/app/aac/SingleLiveData.java b/lib_base/src/main/java/com/android/base/app/aac/SingleLiveData.java index 09be4bb..a2bca91 100644 --- a/lib_base/src/main/java/com/android/base/app/aac/SingleLiveData.java +++ b/lib_base/src/main/java/com/android/base/app/aac/SingleLiveData.java @@ -19,7 +19,6 @@ public class SingleLiveData extends MediatorLiveData { private final List >> mWrapperObserverList = new ArrayList<>(); - @Override public void setValue(T value) { mVersion++; diff --git a/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.java b/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.java index 48ff010..c4ff5d0 100644 --- a/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.java +++ b/lib_base/src/main/java/com/android/base/app/activity/BaseActivity.java @@ -7,6 +7,8 @@ import android.view.View; import com.android.base.utils.android.compat.AndroidVersion; import com.github.dmstocking.optional.java.util.function.Predicate; +import org.jetbrains.annotations.NotNull; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.UiThread; @@ -121,7 +123,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Activity } @Override - protected void onSaveInstanceState(Bundle outState) { + protected void onSaveInstanceState(@NotNull Bundle outState) { super.onSaveInstanceState(outState); mActivityDelegates.callOnSaveInstanceState(outState); } @@ -219,4 +221,4 @@ public abstract class BaseActivity extends AppCompatActivity implements Activity } } -} +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.java b/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.java index 87a2b7e..efbc09f 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.java +++ b/lib_base/src/main/java/com/android/base/app/fragment/BaseDialogFragment.java @@ -7,12 +7,14 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import com.android.base.app.BaseKit; +import com.android.base.app.Sword; import com.android.base.app.activity.BackHandlerHelper; import com.android.base.app.activity.OnBackPressListener; import com.android.base.app.ui.LoadingView; import com.github.dmstocking.optional.java.util.function.Predicate; +import org.jetbrains.annotations.NotNull; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; @@ -49,7 +51,7 @@ public class BaseDialogFragment extends AppCompatDialogFragment implements Loadi } @Override - public void onAttach(Context context) { + public void onAttach(@NotNull Context context) { super.onAttach(context); Timber.tag(tag()).d("onAttach() called with: context = [" + context + "]"); mFragmentDelegates.onAttach(context); @@ -235,7 +237,7 @@ public class BaseDialogFragment extends AppCompatDialogFragment implements Loadi private LoadingView getLoadingViewImpl() { if (mLoadingViewImpl == null) { - mLoadingViewImpl = BaseKit.get().getLoadingViewFactory().createLoadingDelegate(getContext()); + mLoadingViewImpl = Sword.get().getLoadingViewFactory().createLoadingDelegate(getContext()); } return mLoadingViewImpl; } diff --git a/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java b/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java index 1574d36..da41900 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java +++ b/lib_base/src/main/java/com/android/base/app/fragment/BaseFragment.java @@ -8,12 +8,14 @@ import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; -import com.android.base.app.BaseKit; +import com.android.base.app.Sword; import com.android.base.app.activity.BackHandlerHelper; import com.android.base.app.activity.OnBackPressListener; import com.android.base.app.ui.LoadingView; import com.github.dmstocking.optional.java.util.function.Predicate; +import org.jetbrains.annotations.NotNull; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.StringRes; @@ -50,7 +52,7 @@ public class BaseFragment extends Fragment implements LoadingView, OnBackPressLi } @Override - public void onAttach(Context context) { + public void onAttach(@NotNull Context context) { super.onAttach(context); Timber.tag(tag()).d("onAttach() called with: context = [" + context + "]"); mFragmentDelegates.onAttach(context); @@ -249,7 +251,7 @@ public class BaseFragment extends Fragment implements LoadingView, OnBackPressLi mLoadingViewImpl = onCreateLoadingView(); } if (mLoadingViewImpl == null) { - mLoadingViewImpl = BaseKit.get().getLoadingViewFactory().createLoadingDelegate(getContext()); + mLoadingViewImpl = Sword.get().getLoadingViewFactory().createLoadingDelegate(getContext()); } return mLoadingViewImpl; } diff --git a/lib_base/src/main/java/com/android/base/app/fragment/FragmentInfo.java b/lib_base/src/main/java/com/android/base/app/fragment/FragmentInfo.java index f464a1c..002cfa2 100644 --- a/lib_base/src/main/java/com/android/base/app/fragment/FragmentInfo.java +++ b/lib_base/src/main/java/com/android/base/app/fragment/FragmentInfo.java @@ -2,10 +2,11 @@ package com.android.base.app.fragment; import android.content.Context; import android.os.Bundle; -import androidx.fragment.app.Fragment; import java.lang.ref.WeakReference; +import androidx.fragment.app.Fragment; + @SuppressWarnings("WeakerAccess,unused") public class FragmentInfo { @@ -129,4 +130,4 @@ public class FragmentInfo { } } -} +} \ No newline at end of file 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 a63f3b4..80ea785 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 @@ -173,7 +173,7 @@ fun T.inSafelyFragmentTransaction( var delegate = findDelegate { it is SafelyFragmentTransactionActivityDelegate - }as? SafelyFragmentTransactionActivityDelegate + } as? SafelyFragmentTransactionActivityDelegate if (delegate == null) { delegate = SafelyFragmentTransactionActivityDelegate() @@ -241,22 +241,22 @@ private class SafelyFragmentTransactionActivityDelegate : ActivityDelegate { - private val mPendingTransactions = mutableListOf () + private val pendingTransactions = mutableListOf () fun safeCommit(@NonNull fragment: Fragment, @NonNull transaction: FragmentTransaction): Boolean { return if (fragment.isResumed) { transaction.commit() false } else { - mPendingTransactions.add(transaction) + pendingTransactions.add(transaction) true } } override fun onResume() { - if (mPendingTransactions.isNotEmpty()) { - mPendingTransactions.forEach { it.commit() } - mPendingTransactions.clear() + if (pendingTransactions.isNotEmpty()) { + pendingTransactions.forEach { it.commit() } + pendingTransactions.clear() } } @@ -273,7 +273,7 @@ class EnhanceFragmentTransaction constructor( /** * 把 [fragment] 添加到回退栈中,并 hide 其他 fragment, - * 如果 [containerId]==0,则使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id, + * 如果 [containerId]==0,则使用 [com.android.base.app.Sword.setDefaultFragmentContainerId] 中配置的 id, * 如果 [tag] ==null 则使用 fragment 对应 class 的全限定类名。 */ fun addWithStack(containerId: Int = 0, fragment: Fragment, tag: String? = null, transition: Boolean = true): EnhanceFragmentTransaction { @@ -292,7 +292,7 @@ class EnhanceFragmentTransaction constructor( } /** - * replace 方式把 [fragment] 添加到回退栈中,如果 [containerId]==0,则使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id, + * replace 方式把 [fragment] 添加到回退栈中,如果 [containerId]==0,则使用 [com.android.base.app.Sword.setDefaultFragmentContainerId] 中配置的 id, * 如果 [tag] ==null 则使用 fragment 对应 class 的全限定类名。 */ fun replaceWithStack(containerId: Int = 0, fragment: Fragment, tag: String? = null, transition: Boolean = true): EnhanceFragmentTransaction { @@ -317,17 +317,17 @@ class EnhanceFragmentTransaction constructor( } /** - * 添加 [fragment],默认使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id,如果 [tag] 为null,则使用 [fragment] 的全限定类。 + * 添加 [fragment],默认使用 [com.android.base.app.Sword.setDefaultFragmentContainerId] 中配置的 id,如果 [tag] 为null,则使用 [fragment] 的全限定类。 */ - fun addWithDefaultContainer(fragment: Fragment, tag: String? = null): FragmentTransaction { + fun addFragment(fragment: Fragment, tag: String? = null): FragmentTransaction { val nonnullTag = (tag ?: fragment.javaClassName()) return fragmentTransaction.add(FragmentConfig.defaultContainerId(), fragment, nonnullTag) } /** - * 替换为 [fragment],id 使用 [com.android.base.app.BaseKit.setDefaultFragmentContainerId] 中配置的 id,如果 [tag] 为null,则使用 [fragment] 的全限定类名。 + * 替换为 [fragment],id 使用 [com.android.base.app.Sword.setDefaultFragmentContainerId] 中配置的 id,如果 [tag] 为null,则使用 [fragment] 的全限定类名。 */ - fun replaceWithDefaultContainer(fragment: Fragment, tag: String? = null, transition: Boolean = true): FragmentTransaction { + fun replaceFragment(fragment: Fragment, tag: String? = null, transition: Boolean = true): FragmentTransaction { val nonnullTag = (tag ?: fragment.javaClassName()) if (transition) { setOpeningTransition() 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 2e9d427..e9e4413 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 @@ -2,7 +2,7 @@ package com.android.base.app.ui -import com.android.base.app.BaseKit +import com.android.base.app.Sword import com.android.base.utils.common.isEmpty import timber.log.Timber @@ -91,7 +91,7 @@ fun RefreshListLayout<*>.processListErrorWithStatus(throwable: Throwable) { loadMoreFailed() } if (isEmpty) { - val errorTypeClassifier = BaseKit.get().errorClassifier() + val errorTypeClassifier = Sword.get().errorClassifier() if (errorTypeClassifier != null) { when { errorTypeClassifier.isNetworkError(throwable) -> showNetErrorLayout() @@ -145,7 +145,7 @@ fun RefreshStateLayout.processErrorWithStatus(throwable: Throwable?) { if (isRefreshing) { refreshCompleted() } - val errorTypeClassifier = BaseKit.get().errorClassifier() + val errorTypeClassifier = Sword.get().errorClassifier() if (errorTypeClassifier != null) { when { errorTypeClassifier.isNetworkError(throwable) -> { @@ -161,4 +161,4 @@ fun RefreshStateLayout.processErrorWithStatus(throwable: Throwable?) { } else { showErrorLayout() } -} +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/data/Resource.java b/lib_base/src/main/java/com/android/base/data/Resource.java deleted file mode 100644 index 5996635..0000000 --- a/lib_base/src/main/java/com/android/base/data/Resource.java +++ /dev/null @@ -1,152 +0,0 @@ -package com.android.base.data; - - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - - -/** - * @author Ztiany - * Email: ztiany3@gmail.com - * Date : 2018-05-15 16:23 - */ -public class Resource { - - private final Throwable mError; - private final Status mStatus; - private final T mData; //data or default data - - private Resource(Throwable error, T data, Status status) { - mError = error; - mStatus = status; - mData = data; - } - - public boolean isSuccess() { - return mStatus == Status.SUCCESS; - } - - public boolean isNoChange() { - return mStatus == Status.NOT_CHANGED; - } - - public boolean isLoading() { - return mStatus == Status.LOADING; - } - - public final boolean isError() { - return mStatus == Status.ERROR; - } - - public final boolean hasData() { - return mData != null; - } - - public static Resource success() { - return new Resource<>(null, null, Status.SUCCESS); - } - - public static Resource success(@Nullable T data) { - return new Resource<>(null, data, Status.SUCCESS); - } - - public static Resource error(@NonNull Throwable error) { - return error(error, null); - } - - /** - * 创建一个 error 状态,且设置一个默认的数据 - */ - public static Resource error(@NonNull Throwable error, T defaultValue) { - return new Resource<>(error, defaultValue, Status.ERROR); - } - - public static Resource loading() { - return loading(null); - } - - /** - * 创建一个 loading 状态,且设置一个默认的数据 - */ - public static Resource loading(T defaultValue) { - return new Resource<>(null, defaultValue, Status.LOADING); - } - - /** - * 如果数据源(比如 Repository)缓存了上一次请求的数据,然后对其当前请求返回的数据,发现数据是一样的,可以使用此状态表示 - * - * @return Resource - */ - public static Resource noChange() { - return new Resource<>(null, null, Status.NOT_CHANGED); - } - - /** - * 获取 Resource 中保存的数据,只有在 success 状态并且存在数据时下才能调用此方法,否则将抛出异常。 - * - * @return Resource 中保存的数据。 - * @throws UnsupportedOperationException 非 success 状态调用此方法。 - * @throws NullPointerException Resource 中没有保存数据时调用此方法。 - */ - @NonNull - public T data() { - if (isError() || isLoading() || isNoChange()) { - throw new UnsupportedOperationException("This method can only be called when the state success"); - } - if (mData == null) { - throw new NullPointerException("Data is null"); - } - return mData; - } - - /** - * 获取 Resource 中保存的数据,如果不存在数据则返回 defaultData 所设置的默认数据,在不同状态下获取的数据具有不同的意义: - * - * - *success 状态下,返回一个成功操作所产生的数据 - *error 状态下,返回一个默认的数据,如果存在的话 - *loading 状态下,返回一个默认的数据,如果存在的话 - *
- *