Skip to content
Permalink
Browse files

Model-Layer

Newly added Model-Layer Demo
  • Loading branch information...
goldze committed Mar 27, 2019
1 parent 11671b5 commit 614c6a0c69a8f0ea58c19a3c1d3aaa673d5816ec
@@ -24,7 +24,7 @@ public static AppViewModelFactory getInstance(Application application) {
if (INSTANCE == null) {
synchronized (AppViewModelFactory.class) {
if (INSTANCE == null) {
INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository(application.getApplicationContext()));
INSTANCE = new AppViewModelFactory(application, Injection.provideDemoRepository());
}
}
}
@@ -1,31 +1,27 @@
package com.goldze.mvvmhabit.app;

import android.annotation.SuppressLint;
import android.content.Context;

import com.goldze.mvvmhabit.data.DemoRepository;
import com.goldze.mvvmhabit.data.source.HttpDataSource;
import com.goldze.mvvmhabit.data.source.LocalDataSource;
import com.goldze.mvvmhabit.data.source.http.HttpDataSourceImpl;
import com.goldze.mvvmhabit.data.source.http.service.DemoApiService;
import com.goldze.mvvmhabit.data.source.local.LocalDataSourceImpl;
import com.goldze.mvvmhabit.utils.RetrofitClient;

import io.reactivex.annotations.NonNull;

import static android.support.v4.util.Preconditions.checkNotNull;


/**
* 注入全局的数据仓库,可以考虑使用Dagger2。(根据项目实际情况搭建,千万不要为了架构而架构)
* Created by goldze on 2019/3/26.
*/
public class Injection {
@SuppressLint("RestrictedApi")
public static DemoRepository provideDemoRepository(@NonNull Context context) {
//检查Context是否为空
checkNotNull(context);
public static DemoRepository provideDemoRepository() {
//网络API服务
DemoApiService apiService = RetrofitClient.getInstance().create(DemoApiService.class);
return DemoRepository.getInstance(HttpDataSourceImpl.getInstance(apiService), LocalDataSourceImpl.getInstance());
//网络数据源
HttpDataSource httpDataSource = HttpDataSourceImpl.getInstance(apiService);
//本地数据源
LocalDataSource localDataSource = LocalDataSourceImpl.getInstance();
//两条分支组成一个数据仓库
return DemoRepository.getInstance(httpDataSource, localDataSource);
}
}
@@ -45,6 +45,16 @@ public static void destroyInstance() {
}


@Override
public Observable<Object> simulationLogin() {
return mHttpDataSource.simulationLogin();
}

@Override
public Observable<DemoEntity> simulationLoadMore() {
return mHttpDataSource.simulationLoadMore();
}

@Override
public Observable<BaseResponse<DemoEntity>> demoGet() {
return mHttpDataSource.demoGet();
@@ -9,7 +9,15 @@
* Created by goldze on 2019/3/26.
*/
public interface HttpDataSource {
//模拟登录
Observable<Object> simulationLogin();

//模拟上拉加载
Observable<DemoEntity> simulationLoadMore();

Observable<BaseResponse<DemoEntity>> demoGet();

Observable<BaseResponse<DemoEntity>> demoPost(String catalog);


}
@@ -1,12 +1,16 @@
package com.goldze.mvvmhabit.data.source.http;

import com.goldze.mvvmhabit.data.DemoRepository;
import com.goldze.mvvmhabit.data.source.HttpDataSource;
import com.goldze.mvvmhabit.data.source.http.service.DemoApiService;
import com.goldze.mvvmhabit.entity.DemoEntity;
import com.goldze.mvvmhabit.utils.RetrofitClient;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import me.goldze.mvvmhabit.http.BaseResponse;

/**
@@ -35,6 +39,31 @@ private HttpDataSourceImpl(DemoApiService apiService) {
this.apiService = apiService;
}

@Override
public Observable<Object> simulationLogin() {
return Observable.just(new Object()).delay(3, TimeUnit.SECONDS); //延迟3秒
}

@Override
public Observable<DemoEntity> simulationLoadMore() {
return Observable.create(new ObservableOnSubscribe<DemoEntity>() {
@Override
public void subscribe(ObservableEmitter<DemoEntity> observableEmitter) throws Exception {
DemoEntity entity = new DemoEntity();
List<DemoEntity.ItemsEntity> itemsEntities = new ArrayList<>();
//模拟一部分假数据
for (int i = 0; i < 10; i++) {
DemoEntity.ItemsEntity item = new DemoEntity.ItemsEntity();
item.setId(-1);
item.setName("模拟条目");
itemsEntities.add(item);
}
entity.setItems(itemsEntities);
observableEmitter.onNext(entity);
}
}).delay(3, TimeUnit.SECONDS); //延迟3秒
}

@Override
public Observable<BaseResponse<DemoEntity>> demoGet() {
return apiService.demoGet();
@@ -10,7 +10,7 @@
import retrofit2.http.POST;

/**
* Created by 曾宪泽 on 2017/6/15.
* Created by goldze on 2017/6/15.
*/

public interface DemoApiService {
@@ -1,8 +1,6 @@
package com.goldze.mvvmhabit.ui.login;

import android.app.Application;
import android.arch.lifecycle.MediatorLiveData;
import android.databinding.ObservableBoolean;
import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.support.annotation.NonNull;
@@ -97,9 +95,8 @@ private void login() {
ToastUtils.showShort("请输入密码!");
return;
}
//RaJava模拟一个延迟操作
addSubscribe(Observable.just("")
.delay(3, TimeUnit.SECONDS) //延迟3秒
//RaJava模拟登录
addSubscribe(model.simulationLogin()
.compose(RxUtils.schedulersTransformer()) //线程调度
.doOnSubscribe(new Consumer<Disposable>() {
@Override
@@ -16,7 +16,6 @@
import com.goldze.mvvmhabit.R;
import com.goldze.mvvmhabit.app.AppViewModelFactory;
import com.goldze.mvvmhabit.databinding.FragmentNetworkBinding;
import com.goldze.mvvmhabit.ui.login.LoginViewModel;

import me.goldze.mvvmhabit.base.BaseFragment;
import me.goldze.mvvmhabit.utils.MaterialDialogUtils;
@@ -1,6 +1,7 @@
package com.goldze.mvvmhabit.ui.network;

import android.databinding.ObservableField;
import android.databinding.ObservableInt;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
@@ -22,6 +23,7 @@
public class NetWorkItemViewModel extends ItemViewModel<NetWorkViewModel> {
public ObservableField<DemoEntity.ItemsEntity> entity = new ObservableField<>();
public Drawable drawableImg;
public ObservableField<Integer> position = new ObservableField<>();

public NetWorkItemViewModel(@NonNull NetWorkViewModel viewModel, DemoEntity.ItemsEntity entity) {
super(viewModel);
@@ -30,13 +32,25 @@ public NetWorkItemViewModel(@NonNull NetWorkViewModel viewModel, DemoEntity.Item
drawableImg = ContextCompat.getDrawable(viewModel.getApplication(), R.mipmap.ic_launcher);
}

/**
* 获取position的方式有很多种,indexOf是其中一种,常见的还有在Adapter中、ItemBinding.of回调里
*
* @return
*/
public int position() {
if (position.get() == null) {
position.set(viewModel.observableList.indexOf(this));
}
return position.get();
}

//条目的点击事件
public BindingCommand itemClick = new BindingCommand(new BindingAction() {
@Override
public void call() {
//这里可以通过一个标识,做出判断,已达到跳入不同界面的逻辑
if (entity.get().getId() == -1) {
ToastUtils.showShort(entity.get().getName());
viewModel.deleteItemLiveData.setValue(NetWorkItemViewModel.this);
} else {
//跳转到详情界面,传入条目的实体对象
Bundle mBundle = new Bundle();
@@ -52,7 +66,7 @@ public void call() {
//以前是使用Messenger发送事件,在NetWorkViewModel中完成删除逻辑
// Messenger.getDefault().send(NetWorkItemViewModel.this, NetWorkViewModel.TOKEN_NETWORKVIEWMODEL_DELTE_ITEM);
//现在ItemViewModel中存在ViewModel引用,可以直接拿到LiveData去做删除
viewModel.deleteItemLiveData.setValue(NetWorkItemViewModel.this);
ToastUtils.showShort(entity.get().getName());
}
});
// /**
@@ -1,6 +1,7 @@
package com.goldze.mvvmhabit.ui.network;

import android.app.Application;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.databinding.ObservableArrayList;
import android.databinding.ObservableBoolean;
@@ -21,6 +22,7 @@
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.BaseResponse;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
@@ -33,8 +35,7 @@
*/

public class NetWorkViewModel extends BaseViewModel<DemoRepository> {
private int itemIndex = 0;
public MutableLiveData<NetWorkItemViewModel> deleteItemLiveData = new MutableLiveData();
public SingleLiveEvent<NetWorkItemViewModel> deleteItemLiveData = new SingleLiveEvent<>();
//封装一个界面发生改变的观察者
public UIChangeObservable uc = new UIChangeObservable();

@@ -67,35 +68,30 @@ public void call() {
public BindingCommand onLoadMoreCommand = new BindingCommand(new BindingAction() {
@Override
public void call() {
if (itemIndex > 50) {
if (observableList.size() > 50) {
ToastUtils.showLong("兄dei,你太无聊啦~崩是不可能的~");
uc.finishLoadmore.set(!uc.finishLoadmore.get());
return;
}
//模拟网络上拉加载更多
addSubscribe(Observable.just("")
.delay(3, TimeUnit.SECONDS) //延迟3秒
addSubscribe(model.simulationLoadMore()
.compose(RxUtils.schedulersTransformer()) //线程调度
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(Disposable disposable) throws Exception {
ToastUtils.showShort("上拉加载");
}
})
.subscribe(new Consumer<Object>() {
.subscribe(new Consumer<DemoEntity>() {
@Override
public void accept(Object o) throws Exception {
//刷新完成收回
uc.finishLoadmore.set(!uc.finishLoadmore.get());
//模拟一部分假数据
for (int i = 0; i < 10; i++) {
DemoEntity.ItemsEntity item = new DemoEntity.ItemsEntity();
item.setId(-1);
item.setName("模拟条目" + itemIndex++);
NetWorkItemViewModel itemViewModel = new NetWorkItemViewModel(NetWorkViewModel.this, item);
public void accept(DemoEntity entity) throws Exception {
for (DemoEntity.ItemsEntity itemsEntity : entity.getItems()) {
NetWorkItemViewModel itemViewModel = new NetWorkItemViewModel(NetWorkViewModel.this, itemsEntity);
//双向绑定动态添加Item
observableList.add(itemViewModel);
}
//刷新完成收回
uc.finishLoadmore.set(!uc.finishLoadmore.get());
}
}));
}
@@ -120,12 +116,10 @@ public void accept(Disposable disposable) throws Exception {
.subscribe(new Consumer<BaseResponse<DemoEntity>>() {
@Override
public void accept(BaseResponse<DemoEntity> response) throws Exception {
itemIndex = 0;
//清除列表
observableList.clear();
//请求成功
if (response.getCode() == 1) {
//将实体赋给LiveData
for (DemoEntity.ItemsEntity entity : response.getResult().getItems()) {
NetWorkItemViewModel itemViewModel = new NetWorkItemViewModel(NetWorkViewModel.this, entity);
//双向绑定动态添加Item
@@ -144,7 +138,6 @@ public void accept(ResponseThrowable throwable) throws Exception {
//请求刷新完成收回
uc.finishRefreshing.set(!uc.finishRefreshing.get());
ToastUtils.showShort(throwable.message);
throwable.printStackTrace();
}
}, new Action() {
@Override
@@ -32,6 +32,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="6dp"
android:text="@{viewModel.entity.name}" />
android:text="@{viewModel.entity.id == -1 ? viewModel.position() + viewModel.entity.name : viewModel.entity.name}" />
</LinearLayout>
</layout>
@@ -4,20 +4,16 @@
import android.arch.lifecycle.AndroidViewModel;
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LiveData;
import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.Observer;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.trello.rxlifecycle2.LifecycleProvider;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;

0 comments on commit 614c6a0

Please sign in to comment.
You can’t perform that action at this time.