Commit 16e2f477f26671ef97723b89722dfb23e344d7b5

Authored by 王琳
1 parent 70670522

封装MvpLceActivity及Fragment,并实现测试demo

app/build.gradle
@@ -35,12 +35,13 @@ dependencies { @@ -35,12 +35,13 @@ dependencies {
35 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 35 androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
36 exclude group: 'com.android.support', module: 'support-annotations' 36 exclude group: 'com.android.support', module: 'support-annotations'
37 }) 37 })
38 - compile 'com.android.support:appcompat-v7:26.+'  
39 - compile 'com.android.support.constraint:constraint-layout:1.0.2'  
40 - testCompile 'junit:junit:4.12'  
41 compile 'com.android.support.constraint:constraint-layout:1.0.2' 38 compile 'com.android.support.constraint:constraint-layout:1.0.2'
42 // TODO 本地酷使用说明 代码仓库标识 39 // TODO 本地酷使用说明 代码仓库标识
43 // compile 'com.cnlive.libs:base:1.0.0@aar' 40 // compile 'com.cnlive.libs:base:1.0.0@aar'
44 -// compile 'com.cnlive:cnlive:0.1.1'  
45 - compile project(':cnlive') 41 + // compile 'com.cnlive:cnlive:0.1.1'
  42 + compile project(':cnlive')
  43 + compile 'com.android.support:appcompat-v7:26.+'
  44 + compile 'com.android.support.constraint:constraint-layout:1.0.2'
  45 + testCompile 'junit:junit:4.12'
  46 + compile 'com.android.support:recyclerview-v7:26.0.0-alpha1'
46 } 47 }
app/src/main/AndroidManifest.xml
@@ -2,7 +2,6 @@ @@ -2,7 +2,6 @@
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.cnlive.libs.demo"> 3 package="com.cnlive.libs.demo">
4 4
5 -  
6 <application 5 <application
7 android:name=".App" 6 android:name=".App"
8 android:allowBackup="true" 7 android:allowBackup="true"
@@ -11,9 +10,7 @@ @@ -11,9 +10,7 @@
11 android:roundIcon="@mipmap/ic_launcher_round" 10 android:roundIcon="@mipmap/ic_launcher_round"
12 android:supportsRtl="true" 11 android:supportsRtl="true"
13 android:theme="@style/AppTheme"> 12 android:theme="@style/AppTheme">
14 -  
15 - <activity android:name=".MainActivity"/>  
16 - 13 + <activity android:name=".MainActivity" />
17 <activity 14 <activity
18 android:name=".mvptest.ui.activity.LaunchActivity" 15 android:name=".mvptest.ui.activity.LaunchActivity"
19 android:configChanges="keyboardHidden|screenSize|orientation" 16 android:configChanges="keyboardHidden|screenSize|orientation"
@@ -21,11 +18,11 @@ @@ -21,11 +18,11 @@
21 android:theme="@style/AppFullScreenTheme"> 18 android:theme="@style/AppFullScreenTheme">
22 <intent-filter> 19 <intent-filter>
23 <action android:name="android.intent.action.MAIN" /> 20 <action android:name="android.intent.action.MAIN" />
  21 +
24 <category android:name="android.intent.category.LAUNCHER" /> 22 <category android:name="android.intent.category.LAUNCHER" />
25 </intent-filter> 23 </intent-filter>
26 </activity> 24 </activity>
27 -  
28 - 25 + <activity android:name=".mvptest.ui.activity.LceTestActivity"></activity>
29 </application> 26 </application>
30 27
31 </manifest> 28 </manifest>
32 \ No newline at end of file 29 \ No newline at end of file
app/src/main/java/com/cnlive/libs/demo/MainActivity.java
1 package com.cnlive.libs.demo; 1 package com.cnlive.libs.demo;
2 2
  3 +import android.content.Intent;
3 import android.os.Bundle; 4 import android.os.Bundle;
4 import android.support.v7.app.AppCompatActivity; 5 import android.support.v7.app.AppCompatActivity;
  6 +import android.view.View;
  7 +
  8 +import com.cnlive.libs.demo.mvptest.ui.activity.LceTestActivity;
5 9
6 public class MainActivity extends AppCompatActivity { 10 public class MainActivity extends AppCompatActivity {
7 11
@@ -15,5 +19,12 @@ public class MainActivity extends AppCompatActivity { @@ -15,5 +19,12 @@ public class MainActivity extends AppCompatActivity {
15 ToastTest.test(this); 19 ToastTest.test(this);
16 20
17 Java8Test.test(this); 21 Java8Test.test(this);
  22 +
  23 + findViewById(R.id.lce_test).setOnClickListener(new View.OnClickListener() {
  24 + @Override
  25 + public void onClick(View view) {
  26 + startActivity(new Intent(MainActivity.this, LceTestActivity.class));
  27 + }
  28 + });
18 } 29 }
19 } 30 }
app/src/main/java/com/cnlive/libs/demo/data/network/testapi/ApiService.java
@@ -2,6 +2,7 @@ package com.cnlive.libs.demo.data.network.testapi; @@ -2,6 +2,7 @@ package com.cnlive.libs.demo.data.network.testapi;
2 2
3 3
4 import com.cnlive.libs.demo.data.model.AppInfo; 4 import com.cnlive.libs.demo.data.model.AppInfo;
  5 +import com.cnlive.libs.demo.mvptest.model.ExpertLibraryListBean;
5 6
6 import io.reactivex.Observable; 7 import io.reactivex.Observable;
7 import retrofit2.adapter.rxjava2.Result; 8 import retrofit2.adapter.rxjava2.Result;
@@ -21,4 +22,6 @@ public interface ApiService { @@ -21,4 +22,6 @@ public interface ApiService {
21 @GET("apps/info") 22 @GET("apps/info")
22 Observable<Result<AppInfo>> appInfo(@Query("app_id") int app_id); 23 Observable<Result<AppInfo>> appInfo(@Query("app_id") int app_id);
23 24
  25 + @GET("questions/experts")
  26 + Observable<Result<ExpertLibraryListBean>> questionsExperts();
24 } 27 }
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/CountriesPresenter.java 0 → 100644
  1 +package com.cnlive.libs.demo.mvptest.frame;
  2 +
  3 +import android.content.Context;
  4 +
  5 +import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
  6 +
  7 +/**
  8 + * Created by Lynn on 2017/8/29.
  9 + */
  10 +
  11 +public interface CountriesPresenter extends MvpPresenter<CountriesView> {
  12 +
  13 + void loadData(Context context, final boolean pullToRefresh);
  14 +}
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/CountriesView.java 0 → 100644
  1 +package com.cnlive.libs.demo.mvptest.frame;
  2 +
  3 +import com.cnlive.libs.demo.mvptest.model.ExpertLibraryListBean;
  4 +import com.hannesdorfmann.mosby3.mvp.lce.MvpLceView;
  5 +
  6 +import java.util.List;
  7 +
  8 +
  9 +/**
  10 + * Created by Lynn on 2017/8/29.
  11 + */
  12 +
  13 +public interface CountriesView extends MvpLceView<List<ExpertLibraryListBean>> {
  14 +}
app/src/main/java/com/cnlive/libs/demo/mvptest/frame/presenter/SimpleCountriesPresenter.java 0 → 100644
  1 +package com.cnlive.libs.demo.mvptest.frame.presenter;
  2 +
  3 +import android.content.Context;
  4 +import android.util.Log;
  5 +
  6 +import com.cnlive.libs.base.data.network.Subscriber;
  7 +import com.cnlive.libs.demo.data.network.testapi.ApiRequest;
  8 +import com.cnlive.libs.demo.data.network.testapi.ApiService;
  9 +import com.cnlive.libs.demo.mvptest.frame.CountriesPresenter;
  10 +import com.cnlive.libs.demo.mvptest.frame.CountriesView;
  11 +import com.cnlive.libs.demo.mvptest.model.ExpertLibraryListBean;
  12 +import com.hannesdorfmann.mosby3.mvp.MvpBasePresenter;
  13 +
  14 +import io.reactivex.disposables.Disposable;
  15 +
  16 +
  17 +/**
  18 + * Created by Lynn on 2017/8/29.
  19 + */
  20 +
  21 +public class SimpleCountriesPresenter extends MvpBasePresenter<CountriesView> implements CountriesPresenter {
  22 +
  23 + private static final String TAG = "CountriesPresenter";
  24 + private Disposable subscription;
  25 +
  26 + @Override
  27 + public void loadData(Context context, final boolean pullToRefresh) {
  28 + if (subscription != null) subscription.dispose();
  29 +
  30 + getView().showLoading(pullToRefresh);
  31 +
  32 + subscription = ApiRequest.subscribe(
  33 + ApiRequest.service(ApiService.class).questionsExperts(),
  34 + new Subscriber<ExpertLibraryListBean>(context) {
  35 + @Override
  36 + public void onCompleted(String code, String message, ExpertLibraryListBean data) {
  37 + getView().setData(data.getData());
  38 + getView().showContent();
  39 + }
  40 +
  41 + @Override
  42 + public void onError(String code, String message) {
  43 + getView().showError(null, pullToRefresh);
  44 + }
  45 + });
  46 + }
  47 +
  48 + @Override
  49 + public void detachView(boolean retainInstance) {
  50 + super.detachView(retainInstance);
  51 +
  52 + StringBuilder builder = new StringBuilder("---- detachView(" + retainInstance + ") ");
  53 + if (!retainInstance) {
  54 + builder.append(" --> cancel Loader");
  55 + }
  56 +
  57 + builder.append(" ----");
  58 + Log.d(TAG, builder.toString());
  59 + }
  60 +
  61 + @Override
  62 + public void attachView(CountriesView view) {
  63 + super.attachView(view);
  64 + Log.d(TAG, "attach view " + view.toString());
  65 + }
  66 +}
app/src/main/java/com/cnlive/libs/demo/mvptest/model/ExpertLibraryListBean.java 0 → 100644
  1 +package com.cnlive.libs.demo.mvptest.model;
  2 +
  3 +
  4 +import android.os.Parcel;
  5 +import android.os.Parcelable;
  6 +
  7 +import com.cnlive.libs.demo.BR;
  8 +import com.cnlive.libs.demo.data.model.BaseInfo;
  9 +
  10 +import java.util.List;
  11 +
  12 +/**
  13 + * 创建:wukuiqing
  14 + * <p>
  15 + * 时间:2017/7/23
  16 + * <p>
  17 + * 描述:
  18 + */
  19 +
  20 +public class ExpertLibraryListBean extends BaseInfo<List<ExpertLibraryListBean>> {
  21 +
  22 +
  23 + /**
  24 + * id : 9
  25 + * name : 徐明(畜牧专家)
  26 + * sex : 男
  27 + * major : 反刍动物碳水化合物营养调控与奶牛营养工程技术体系
  28 + * title : 内蒙古农业大学教授
  29 + * avatar_url : //ymzx.asia-cloud.com/uploads/images/2017/0729/20170729134319730.png
  30 + * created_at : 2017-07-29 12:33:27
  31 + */
  32 +
  33 + private int id;
  34 + private String name;
  35 + private String sex;
  36 + private String major;
  37 + private String title;
  38 + private String avatar_url;
  39 + private String created_at;
  40 +
  41 + public int getId() {
  42 + return id;
  43 + }
  44 +
  45 + public void setId(int id) {
  46 + this.id = id;
  47 + }
  48 +
  49 + public String getName() {
  50 + return name;
  51 + }
  52 +
  53 + public void setName(String name) {
  54 + this.name = name;
  55 + }
  56 +
  57 + public String getSex() {
  58 + return sex;
  59 + }
  60 +
  61 + public void setSex(String sex) {
  62 + this.sex = sex;
  63 + }
  64 +
  65 + public String getMajor() {
  66 + return major;
  67 + }
  68 +
  69 + public void setMajor(String major) {
  70 + this.major = major;
  71 + }
  72 +
  73 + public String getTitle() {
  74 + return title;
  75 + }
  76 +
  77 + public void setTitle(String title) {
  78 + this.title = title;
  79 + }
  80 +
  81 + public String getAvatar_url() {
  82 + return avatar_url;
  83 + }
  84 +
  85 + public void setAvatar_url(String avatar_url) {
  86 + this.avatar_url = avatar_url;
  87 + }
  88 +
  89 + public String getCreated_at() {
  90 + return created_at;
  91 + }
  92 +
  93 + public void setCreated_at(String created_at) {
  94 + this.created_at = created_at;
  95 + }
  96 +}
app/src/main/java/com/cnlive/libs/demo/mvptest/ui/activity/LceTestActivity.java 0 → 100644
  1 +package com.cnlive.libs.demo.mvptest.ui.activity;
  2 +
  3 +import android.support.annotation.NonNull;
  4 +import android.support.v4.widget.SwipeRefreshLayout;
  5 +import android.os.Bundle;
  6 +import android.support.v7.widget.LinearLayoutManager;
  7 +import android.support.v7.widget.RecyclerView;
  8 +import android.view.View;
  9 +
  10 +import com.cnlive.libs.base.frame.activity.MvpLceBindingActivity;
  11 +import com.cnlive.libs.demo.BR;
  12 +import com.cnlive.libs.demo.R;
  13 +import com.cnlive.libs.demo.databinding.ActivityLceTestBinding;
  14 +import com.cnlive.libs.demo.mvptest.frame.CountriesPresenter;
  15 +import com.cnlive.libs.demo.mvptest.frame.CountriesView;
  16 +import com.cnlive.libs.demo.mvptest.frame.presenter.SimpleCountriesPresenter;
  17 +import com.cnlive.libs.demo.mvptest.model.ExpertLibraryListBean;
  18 +import com.cnlive.libs.demo.mvptest.ui.activity.adapter.SimpleAdapter;
  19 +
  20 +import java.util.List;
  21 +
  22 +public class LceTestActivity extends MvpLceBindingActivity<SwipeRefreshLayout, List<ExpertLibraryListBean>, CountriesView, CountriesPresenter, ActivityLceTestBinding> implements CountriesView, SwipeRefreshLayout.OnRefreshListener {
  23 +
  24 + SimpleAdapter<ExpertLibraryListBean> adapter;
  25 +
  26 + @Override
  27 + protected void onCreate(Bundle savedInstanceState) {
  28 + super.onCreate(savedInstanceState);
  29 +
  30 + contentView.setOnRefreshListener(this);
  31 +// errorView.setOnClickListener(new View.OnClickListener() {
  32 +// @Override
  33 +// public void onClick(View view) {
  34 +// loadData(false);
  35 +// }
  36 +// });
  37 +
  38 + loadData(false);
  39 + }
  40 +
  41 + @NonNull
  42 + @Override
  43 + public int loadingViewId() {
  44 + return R.id.loading_view;
  45 + }
  46 +
  47 + @NonNull
  48 + @Override
  49 + public int contentViewId() {
  50 + return R.id.content_view;
  51 + }
  52 +
  53 + @NonNull
  54 + @Override
  55 + public int errorViewId() {
  56 + return R.id.error_view;
  57 + }
  58 +
  59 + @Override
  60 + public int getLayoutId() {
  61 + return R.layout.activity_lce_test;
  62 + }
  63 +
  64 + @Override
  65 + protected String getErrorMessage(Throwable e, boolean pullToRefresh) {
  66 + return null;
  67 + }
  68 +
  69 + @Override
  70 + public void onRefresh() {
  71 + loadData(true);
  72 + }
  73 +
  74 + @Override
  75 + public void showContent() {
  76 + super.showContent();
  77 + contentView.setRefreshing(false);
  78 + }
  79 +
  80 + @Override
  81 + public void setData(List<ExpertLibraryListBean> data) {
  82 + adapter = new SimpleAdapter<ExpertLibraryListBean>(data, R.layout.item_test, com.cnlive.libs.demo.BR.bean);
  83 + binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
  84 + binding.recyclerView.setAdapter(adapter);
  85 + }
  86 +
  87 + @NonNull
  88 + @Override
  89 + public CountriesPresenter createPresenter() {
  90 + return new SimpleCountriesPresenter();
  91 + }
  92 +
  93 + @Override
  94 + public void loadData(boolean pullToRefresh) {
  95 + presenter.loadData(this, pullToRefresh);
  96 + }
  97 +}
app/src/main/java/com/cnlive/libs/demo/mvptest/ui/activity/adapter/SimpleAdapter.java 0 → 100644
  1 +package com.cnlive.libs.demo.mvptest.ui.activity.adapter;
  2 +
  3 +import android.databinding.DataBindingUtil;
  4 +import android.databinding.ViewDataBinding;
  5 +import android.support.v7.widget.RecyclerView;
  6 +import android.view.LayoutInflater;
  7 +import android.view.ViewGroup;
  8 +
  9 +import java.util.List;
  10 +
  11 +/**
  12 + * Created by Lynn on 2017/8/30.
  13 + */
  14 +
  15 +public class SimpleAdapter<T> extends RecyclerView.Adapter<ViewHolder> {
  16 +
  17 + private List<T> mDatas;
  18 +
  19 + private int layoutId;
  20 +
  21 + private int brId;
  22 +
  23 + public SimpleAdapter(List<T> mDatas, int layoutId, int brId) {
  24 + this.mDatas = mDatas;
  25 + this.layoutId = layoutId;
  26 + this.brId = brId;
  27 + }
  28 +
  29 + @Override
  30 + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  31 + LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  32 + ViewDataBinding binding = DataBindingUtil.inflate(inflater, layoutId, parent, false);
  33 + ViewHolder viewHolder = new ViewHolder(binding.getRoot());
  34 + viewHolder.setBinding(binding);
  35 + return viewHolder;
  36 + }
  37 +
  38 + @Override
  39 + public void onBindViewHolder(ViewHolder holder, int position) {
  40 + holder.getBinding().setVariable(brId, mDatas.get(position));
  41 + holder.getBinding().executePendingBindings();
  42 + }
  43 +
  44 + @Override
  45 + public int getItemCount() {
  46 + return mDatas == null ? 0 : mDatas.size();
  47 + }
  48 +}
app/src/main/java/com/cnlive/libs/demo/mvptest/ui/activity/adapter/ViewHolder.java 0 → 100644
  1 +package com.cnlive.libs.demo.mvptest.ui.activity.adapter;
  2 +
  3 +import android.databinding.ViewDataBinding;
  4 +import android.support.v7.widget.RecyclerView;
  5 +import android.view.View;
  6 +
  7 +/**
  8 + * Created by Lynn on 2017/8/30.
  9 + */
  10 +
  11 +public class ViewHolder extends RecyclerView.ViewHolder {
  12 + private ViewDataBinding binding;
  13 +
  14 + public ViewDataBinding getBinding() {
  15 + return binding;
  16 + }
  17 +
  18 + public void setBinding(ViewDataBinding binding) {
  19 + this.binding = binding;
  20 + }
  21 +
  22 + public ViewHolder(View itemView) {
  23 + super(itemView);
  24 + }
  25 +}
app/src/main/res/drawable-hdpi/load_error.png 0 → 100644

1.83 KB

app/src/main/res/layout/activity_lce_test.xml 0 → 100644
  1 +<layout xmlns:android="http://schemas.android.com/apk/res/android"
  2 + xmlns:tools="http://schemas.android.com/tools">
  3 +
  4 + <FrameLayout
  5 + android:layout_width="match_parent"
  6 + android:layout_height="match_parent">
  7 +
  8 + <include layout="@layout/loading_view" />
  9 +
  10 + <include layout="@layout/error_view" />
  11 +
  12 + <android.support.v4.widget.SwipeRefreshLayout
  13 + android:id="@+id/content_view"
  14 + android:layout_width="match_parent"
  15 + android:layout_height="match_parent">
  16 +
  17 + <android.support.v7.widget.RecyclerView
  18 + android:id="@+id/recycler_view"
  19 + android:layout_width="match_parent"
  20 + android:layout_height="match_parent" />
  21 +
  22 + </android.support.v4.widget.SwipeRefreshLayout>
  23 +
  24 + </FrameLayout>
  25 +
  26 +</layout>
0 \ No newline at end of file 27 \ No newline at end of file
app/src/main/res/layout/activity_main.xml
@@ -6,13 +6,20 @@ @@ -6,13 +6,20 @@
6 android:layout_height="match_parent" 6 android:layout_height="match_parent"
7 tools:context="com.cnlive.libs.demo.MainActivity"> 7 tools:context="com.cnlive.libs.demo.MainActivity">
8 8
9 - <TextView android:id="@+id/textView"  
10 - android:layout_width="wrap_content" 9 + <!--<TextView-->
  10 + <!--android:id="@+id/textView"-->
  11 + <!--android:layout_width="wrap_content"-->
  12 + <!--android:layout_height="wrap_content"-->
  13 + <!--android:text="Hello World!"-->
  14 + <!--app:layout_constraintBottom_toBottomOf="parent"-->
  15 + <!--app:layout_constraintLeft_toLeftOf="parent"-->
  16 + <!--app:layout_constraintRight_toRightOf="parent"-->
  17 + <!--app:layout_constraintTop_toTopOf="parent" />-->
  18 +
  19 + <Button
  20 + android:id="@+id/lce_test"
  21 + android:layout_width="match_parent"
11 android:layout_height="wrap_content" 22 android:layout_height="wrap_content"
12 - android:text="Hello World!"  
13 - app:layout_constraintBottom_toBottomOf="parent"  
14 - app:layout_constraintLeft_toLeftOf="parent"  
15 - app:layout_constraintRight_toRightOf="parent"  
16 - app:layout_constraintTop_toTopOf="parent" /> 23 + android:text="Lce Test" />
17 24
18 </android.support.constraint.ConstraintLayout> 25 </android.support.constraint.ConstraintLayout>
app/src/main/res/layout/error_view.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<merge xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:layout_width="match_parent"
  4 + android:layout_height="match_parent">
  5 +
  6 + <TextView
  7 + android:id="@+id/error_view"
  8 + android:layout_width="wrap_content"
  9 + android:layout_height="wrap_content"
  10 + android:layout_gravity="center"
  11 + android:drawablePadding="16dp"
  12 + android:drawableTop="@drawable/load_error"
  13 + android:gravity="center"
  14 + android:padding="16dp"
  15 + android:text="加载失败,请点击重试"
  16 + android:textColor="#656565"
  17 + android:textSize="20sp" />
  18 +
  19 +
  20 +</merge>
0 \ No newline at end of file 21 \ No newline at end of file
app/src/main/res/layout/item_test.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<layout xmlns:android="http://schemas.android.com/apk/res/android"
  3 + xmlns:app="http://schemas.android.com/apk/res-auto">
  4 +
  5 + <data>
  6 +
  7 + <variable
  8 + name="bean"
  9 + type="com.cnlive.libs.demo.mvptest.model.ExpertLibraryListBean" />
  10 + </data>
  11 +
  12 + <LinearLayout
  13 + android:layout_width="match_parent"
  14 + android:layout_height="wrap_content"
  15 + android:orientation="vertical">
  16 +
  17 + <TextView
  18 + android:id="@+id/name"
  19 + android:layout_width="match_parent"
  20 + android:layout_height="wrap_content"
  21 + android:padding="16dp"
  22 + android:text="@{bean.name}" />
  23 +
  24 + </LinearLayout>
  25 +</layout>
0 \ No newline at end of file 26 \ No newline at end of file
app/src/main/res/layout/loading_view.xml 0 → 100644
  1 +<?xml version="1.0" encoding="utf-8"?>
  2 +<merge xmlns:android="http://schemas.android.com/apk/res/android"
  3 + android:layout_width="match_parent"
  4 + android:layout_height="match_parent">
  5 +
  6 + <ProgressBar
  7 + android:id="@+id/loading_view"
  8 + android:layout_width="wrap_content"
  9 + android:layout_height="wrap_content"
  10 + android:layout_gravity="center"
  11 + android:indeterminate="true" />
  12 +</merge>
0 \ No newline at end of file 13 \ No newline at end of file
cnlive/src/main/java/com/cnlive/libs/base/frame/activity/MvpLceActivity.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.activity;
  2 +
  3 +import android.os.Bundle;
  4 +import android.support.annotation.NonNull;
  5 +import android.view.View;
  6 +import android.widget.Toast;
  7 +
  8 +import com.hannesdorfmann.mosby3.mvp.*;
  9 +import com.hannesdorfmann.mosby3.mvp.lce.LceAnimator;
  10 +import com.hannesdorfmann.mosby3.mvp.lce.MvpLceView;
  11 +
  12 +/**
  13 + * Created by Lynn on 2017/8/30.
  14 + */
  15 +
  16 +public abstract class MvpLceActivity<CV extends View, M, V extends MvpLceView<M>, P extends MvpPresenter<V>>
  17 + extends com.hannesdorfmann.mosby3.mvp.MvpActivity<V, P> implements MvpLceView<M> {
  18 +
  19 + protected View loadingView;
  20 + protected CV contentView;
  21 + protected View errorView;
  22 +
  23 + @Override
  24 + protected void onCreate(Bundle savedInstanceState) {
  25 + super.onCreate(savedInstanceState);
  26 + setContentView(getLayoutId());
  27 + }
  28 +
  29 + @Override
  30 + public void onContentChanged() {
  31 + super.onContentChanged();
  32 +
  33 + loadingView = findViewById(loadingViewId());
  34 + contentView = (CV) findViewById(contentViewId());
  35 + errorView = findViewById(errorViewId());
  36 +
  37 + if (loadingView == null) {
  38 + throw new NullPointerException(
  39 + "Loading view is null! Have you specified a loading view in your layout xml file?");
  40 + }
  41 +
  42 + if (contentView == null) {
  43 + throw new NullPointerException(
  44 + "Content view is null! Have you specified a content view in your layout xml file?");
  45 + }
  46 +
  47 + if (errorView == null) {
  48 + throw new NullPointerException(
  49 + "Error view is null! Have you specified an error view in your layout xml file?");
  50 + }
  51 +
  52 +// errorView.setOnClickListener(new View.OnClickListener() {
  53 +// @Override
  54 +// public void onClick(View v) {
  55 +// onErrorViewClicked();
  56 +// }
  57 +// });
  58 + }
  59 +
  60 + @NonNull
  61 + public abstract int loadingViewId();
  62 +
  63 + @NonNull
  64 + public abstract int contentViewId();
  65 +
  66 + @NonNull
  67 + public abstract int errorViewId();
  68 +
  69 + public abstract int getLayoutId();
  70 +
  71 + @NonNull
  72 + @Override
  73 + public P createPresenter() {
  74 + return null;
  75 + }
  76 +
  77 +// protected void onErrorViewClicked() {
  78 +// loadData(false);
  79 +// }
  80 +
  81 + @Override
  82 + public void showLoading(boolean pullToRefresh) {
  83 +
  84 + if (!pullToRefresh) {
  85 + animateLoadingViewIn();
  86 + }
  87 +
  88 + }
  89 +
  90 + protected void animateLoadingViewIn() {
  91 + LceAnimator.showLoading(loadingView, contentView, errorView);
  92 + }
  93 +
  94 + @Override
  95 + public void showContent() {
  96 + animateContentViewIn();
  97 + }
  98 +
  99 + protected void animateContentViewIn() {
  100 + LceAnimator.showContent(loadingView, contentView, errorView);
  101 + }
  102 +
  103 + protected abstract String getErrorMessage(Throwable e, boolean pullToRefresh);
  104 +
  105 + protected void showLightError(String msg) {
  106 + Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
  107 + }
  108 +
  109 + @Override
  110 + public void showError(Throwable e, boolean pullToRefresh) {
  111 +
  112 + String errorMsg = getErrorMessage(e, pullToRefresh);
  113 +
  114 + if (pullToRefresh) {
  115 + showLightError(errorMsg);
  116 + } else {
  117 + animateErrorViewIn();
  118 + }
  119 + }
  120 +
  121 + protected void animateErrorViewIn() {
  122 + LceAnimator.showErrorView(loadingView, contentView, errorView);
  123 + }
  124 +}
cnlive/src/main/java/com/cnlive/libs/base/frame/activity/MvpLceBindingActivity.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.activity;
  2 +
  3 +import android.databinding.DataBindingUtil;
  4 +import android.databinding.ViewDataBinding;
  5 +import android.os.Bundle;
  6 +import android.support.annotation.NonNull;
  7 +import android.view.View;
  8 +
  9 +import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
  10 +import com.hannesdorfmann.mosby3.mvp.lce.MvpLceView;
  11 +
  12 +/**
  13 + * Created by Lynn on 2017/8/30.
  14 + */
  15 +
  16 +public abstract class MvpLceBindingActivity<CV extends View, M, V extends MvpLceView<M>, P extends MvpPresenter<V>, T extends ViewDataBinding> extends MvpLceActivity<CV, M, V, P> {
  17 +
  18 + public T binding;
  19 +
  20 + @Override
  21 + protected void onCreate(Bundle savedInstanceState) {
  22 + super.onCreate(savedInstanceState);
  23 + binding = DataBindingUtil.setContentView(this, getLayoutId());
  24 + }
  25 +}
cnlive/src/main/java/com/cnlive/libs/base/frame/fragment/MvpLceBindingFragment.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.fragment;
  2 +
  3 +import android.databinding.DataBindingUtil;
  4 +import android.databinding.ViewDataBinding;
  5 +import android.os.Bundle;
  6 +import android.support.annotation.Nullable;
  7 +import android.view.LayoutInflater;
  8 +import android.view.View;
  9 +import android.view.ViewGroup;
  10 +
  11 +import com.hannesdorfmann.mosby3.mvp.MvpPresenter;
  12 +import com.hannesdorfmann.mosby3.mvp.lce.MvpLceView;
  13 +
  14 +/**
  15 + * Created by Lynn on 2017/8/30.
  16 + */
  17 +
  18 +public abstract class MvpLceBindingFragment<CV extends View, M, V extends MvpLceView<M>, P extends MvpPresenter<V>, T extends ViewDataBinding> extends MvpLceFragment<CV, M, V, P> {
  19 +
  20 + public T binding;
  21 +
  22 + @Nullable
  23 + @Override
  24 + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  25 + binding = DataBindingUtil.inflate(inflater, getLayoutId(), container, false);
  26 + return binding.getRoot();
  27 + }
  28 +
  29 +}
cnlive/src/main/java/com/cnlive/libs/base/frame/fragment/MvpLceFragment.java 0 → 100644
  1 +package com.cnlive.libs.base.frame.fragment;
  2 +
  3 +import android.os.Bundle;
  4 +import android.support.annotation.CallSuper;
  5 +import android.support.annotation.NonNull;
  6 +import android.support.annotation.Nullable;
  7 +import android.view.LayoutInflater;
  8 +import android.view.View;
  9 +import android.view.ViewGroup;
  10 +import android.widget.Toast;
  11 +
  12 +import com.hannesdorfmann.mosby3.mvp.*;
  13 +import com.hannesdorfmann.mosby3.mvp.lce.LceAnimator;
  14 +import com.hannesdorfmann.mosby3.mvp.lce.MvpLceView;
  15 +
  16 +/**
  17 + * Created by Lynn on 2017/8/30.
  18 + */
  19 +
  20 +public abstract class MvpLceFragment<CV extends View, M, V extends MvpLceView<M>, P extends MvpPresenter<V>>
  21 + extends com.hannesdorfmann.mosby3.mvp.MvpFragment<V, P> implements MvpLceView<M> {
  22 + protected View loadingView;
  23 + protected CV contentView;
  24 + protected View errorView;
  25 +
  26 + @CallSuper
  27 + @Override
  28 + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  29 + super.onViewCreated(view, savedInstanceState);
  30 + setUpViews(view);
  31 + }
  32 +
  33 +
  34 + @Nullable
  35 + @Override
  36 + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  37 + return inflater.inflate(getLayoutId(), container, false);
  38 + }
  39 +
  40 + public void setUpViews(View view) {
  41 + loadingView = view.findViewById(loadingViewId());
  42 + contentView = view.findViewById(contentViewId());
  43 + errorView = view.findViewById(errorViewId());
  44 +
  45 + if (loadingView == null) {
  46 + throw new NullPointerException(
  47 + "Loading view is null! Have you specified a loading view in your layout xml file?");
  48 + }
  49 +
  50 + if (contentView == null) {
  51 + throw new NullPointerException(
  52 + "Content view is null! Have you specified a content view in your layout xml file?");
  53 + }
  54 +
  55 + if (errorView == null) {
  56 + throw new NullPointerException(
  57 + "Error view is null! Have you specified a error view in your layout xml file?");
  58 + }
  59 +
  60 +// errorView.setOnClickListener(new View.OnClickListener() {
  61 +// @Override
  62 +// public void onClick(View view) {
  63 +// onErrorViewClicked();
  64 +// }
  65 +// });
  66 + }
  67 +
  68 +
  69 + @NonNull
  70 + public abstract int loadingViewId();
  71 +
  72 + @NonNull
  73 + public abstract int contentViewId();
  74 +
  75 + @NonNull
  76 + public abstract int errorViewId();
  77 +
  78 + public abstract int getLayoutId();
  79 +
  80 +
  81 + @Override
  82 + public void showLoading(boolean pullToRefresh) {
  83 + if (!pullToRefresh) {
  84 + animateLoadingViewIn();
  85 + }
  86 + }
  87 +
  88 + protected void animateLoadingViewIn() {
  89 + LceAnimator.showLoading(loadingView, contentView, errorView);
  90 + }
  91 +
  92 + @Override
  93 + public void showContent() {
  94 + animateContentViewIn();
  95 + }
  96 +
  97 + protected void animateContentViewIn() {
  98 + LceAnimator.showContent(loadingView, contentView, errorView);
  99 + }
  100 +
  101 + protected abstract String getErrorMessage(Throwable e, boolean pullToRefresh);
  102 +
  103 + protected void showLightError(String msg) {
  104 + if (getActivity() != null) {
  105 + Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
  106 + }
  107 + }
  108 +
  109 +// protected void onErrorViewClicked() {
  110 +// loadData(false);
  111 +// }
  112 +
  113 + @Override
  114 + public void showError(Throwable e, boolean pullToRefresh) {
  115 + String errorMsg = getErrorMessage(e, pullToRefresh);
  116 + if (pullToRefresh) {
  117 + showLightError(errorMsg);
  118 + } else {
  119 + animateErrorViewIn();
  120 + }
  121 + }
  122 +
  123 + protected void animateErrorViewIn() {
  124 + LceAnimator.showErrorView(loadingView, contentView, errorView);
  125 + }
  126 +
  127 + @Override
  128 + public void onDestroyView() {
  129 + super.onDestroyView();
  130 + loadingView = null;
  131 + contentView = null;
  132 + errorView.setOnClickListener(null);
  133 + errorView = null;
  134 + }
  135 +}