Skip to content
This repository has been archived by the owner on Feb 25, 2019. It is now read-only.

Commit

Permalink
x)Data Binding
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaobailong24 committed Nov 23, 2017
1 parent e500965 commit 24d55fc
Show file tree
Hide file tree
Showing 12 changed files with 55 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.OnLifecycleEvent;
import android.databinding.ObservableField;

import org.simple.eventbus.EventBus;

import me.xiaobailong24.mvvmarms.repository.http.Status;

/**
* @author xiaobailong24
* @date 2017/6/16
Expand All @@ -23,6 +26,11 @@ public class BaseViewModel<M extends IModel> extends AndroidViewModel

protected M mModel;

/**
* 数据请求状态
*/
public final ObservableField<Status> STATUS = new ObservableField<>();

public BaseViewModel(Application application) {
super(application);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
package me.xiaobailong24.mvvmarms.repository.http;

import android.databinding.ObservableField;

/**
* @author xiaobailong24
* @date 2017/11/22
* 网络请求状态与重试
*/
public interface IRetry {
/**
* 数据请求状态
*/
ObservableField<Status> STATUS = new ObservableField<>();

/**
* 用于封装刷新操作,如果子类的业务有刷新逻辑,可以重写此方法
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
* @author xiaobailong24
* @date 2017/7/29
* Room Database DAO
* @see <a href="http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2017/0726/8268.html">在Room中使用RxJava</a>
*/
@Dao
public interface WeatherNowDao {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -17,6 +18,7 @@
import me.xiaobailong24.mvvmarms.weather.mvvm.view.adapter.WeatherDailyAdapter;
import me.xiaobailong24.mvvmarms.weather.mvvm.viewmodel.WeatherDailyViewModel;
import me.xiaobailong24.mvvmarms.weather.mvvm.viewmodel.WeatherViewModel;
import timber.log.Timber;

/**
* @author xiaobailong24
Expand All @@ -31,6 +33,7 @@ public class WeatherDailyFragment extends BaseFragment<FragmentWeatherDailyBindi
* 共享 Activity 数据
*/
WeatherViewModel mWeatherViewModel;
private String mLocation;

public static WeatherDailyFragment newInstance(String location) {
WeatherDailyFragment weatherDailyFragment = new WeatherDailyFragment();
Expand All @@ -48,8 +51,9 @@ public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedI
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_weather_daily, container, false);
//设置ViewModel
mBinding.setViewModel(mViewModel);
mBinding.retry.setViewModel(mViewModel);
mBinding.retry.setRetry(mViewModel);
mBinding.weatherSource.setRetry(mViewModel);
mBinding.weatherSource.setViewModel(mViewModel);
//RecyclerView设置Adapter
mBinding.recyclerWeatherDaily.setAdapter(mAdapter);
//设置Refresh
Expand All @@ -66,13 +70,16 @@ public void initData(Bundle savedInstanceState) {
mWeatherViewModel.getLocation().observe(this, s -> {
//位置变化时,需要重新加载
mFirst = true;
onFragmentVisibleChange(mVisible && savedInstanceState == null);
if (mLocation == null || !mLocation.equals(s)) {
onFragmentVisibleChange(mVisible);
}
});
mViewModel.getWeatherDaily().observe(this, dailies -> {
mBinding.refresh.setRefreshing(false);
mAdapter.replaceData(dailies);
//加载完成
mFirst = false;
if (mLocation.equals(mWeatherViewModel.getLocation().getValue())) {
mFirst = false;
}
});
}

Expand All @@ -91,17 +98,19 @@ public void setData(Object data) {
@Override
protected void onFragmentVisibleChange(boolean isVisible) {
//当 Fragment 显示/隐藏变化时执行该方法,根据是否显示 Fragment 加载数据
super.onFragmentVisibleChange(isVisible);
if (mViewModel != null && isVisible) {
//调用ViewModel的方法获取天气
mViewModel.loadWeatherDaily(mWeatherViewModel.getLocation().getValue());
mLocation = mWeatherViewModel.getLocation().getValue();
mViewModel.loadWeatherDaily(mLocation);
}
}

@Override
public void onDestroy() {
mWeatherViewModel.getLocation().removeObservers(this);
mViewModel.getWeatherDaily().removeObservers(this);
super.onDestroy();
this.mLocation = null;
this.mAdapter = null;
this.mWeatherViewModel = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.arch.lifecycle.ViewModelProviders;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
Expand All @@ -17,6 +18,7 @@
import me.xiaobailong24.mvvmarms.weather.mvvm.view.adapter.TextContentAdapter;
import me.xiaobailong24.mvvmarms.weather.mvvm.viewmodel.WeatherNowViewModel;
import me.xiaobailong24.mvvmarms.weather.mvvm.viewmodel.WeatherViewModel;
import timber.log.Timber;

/**
* @author xiaobailong24
Expand All @@ -31,6 +33,7 @@ public class WeatherNowFragment extends BaseFragment<FragmentWeatherNowBinding,
* 共享 Activity 数据
*/
WeatherViewModel mWeatherViewModel;
private String mLocation;

public static WeatherNowFragment newInstance(String location) {
WeatherNowFragment weatherNowFragment = new WeatherNowFragment();
Expand All @@ -48,8 +51,9 @@ public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedI
mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_weather_now, container, false);
//设置ViewModel
mBinding.setViewModel(mViewModel);
mBinding.retry.setViewModel(mViewModel);
mBinding.retry.setRetry(mViewModel);
mBinding.weatherSource.setRetry(mViewModel);
mBinding.weatherSource.setViewModel(mViewModel);
//RecyclerView设置Adapter
mBinding.recyclerWeatherNow.setAdapter(mAdapter);
//设置Refresh
Expand All @@ -66,13 +70,16 @@ public void initData(Bundle savedInstanceState) {
mWeatherViewModel.getLocation().observe(this, s -> {
//位置变化时,需要重新加载
mFirst = true;
onFragmentVisibleChange(mVisible && savedInstanceState == null);
if (mLocation == null || !mLocation.equals(s)) {
onFragmentVisibleChange(mVisible);
}
});
mViewModel.getWeatherNow().observe(this, contents -> {
mBinding.refresh.setRefreshing(false);
mAdapter.replaceData(contents);
//加载完成
mFirst = false;
if (mLocation.equals(mWeatherViewModel.getLocation().getValue())) {
mFirst = false;
}
// TODO: 2017/8/19
// DiffUtil.DiffResult diffResult = DiffUtil
// .calculateDiff(new RecyclerViewDiffCallback<>(mAdapter.getData(), contents));
Expand All @@ -97,16 +104,19 @@ protected void onFragmentVisibleChange(boolean isVisible) {
//当 Fragment 显示/隐藏变化时执行该方法,根据是否显示 Fragment 加载数据
if (mViewModel != null && isVisible) {
//调用ViewModel的方法获取天气
if (mWeatherViewModel.getLocation().getValue() != null) {
mViewModel.loadWeatherNow(mWeatherViewModel.getLocation().getValue());
mLocation = mWeatherViewModel.getLocation().getValue();
if (mLocation != null) {
mViewModel.loadWeatherNow(mLocation);
}
}
}

@Override
public void onDestroy() {
mWeatherViewModel.getLocation().removeObservers(this);
mViewModel.getWeatherNow().removeObservers(this);
super.onDestroy();
this.mLocation = null;
this.mAdapter = null;
this.mWeatherViewModel = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ public void loadWeatherDaily(String locationName) {
if (newResource.status == Status.LOADING) {
STATUS.set(Status.LOADING);
} else if (newResource.status == Status.SUCCESS) {
STATUS.set(Status.SUCCESS);
mDailies.postValue(newResource.data.getResults().get(0).getDaily());
STATUS.set(Status.SUCCESS);
} else if (newResource.status == Status.ERROR) {
STATUS.set(Status.ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ public void loadWeatherNow(String locationName) {
if (newResource.status == Status.LOADING) {
STATUS.set(Status.LOADING);
} else if (newResource.status == Status.SUCCESS) {
STATUS.set(Status.SUCCESS);
List<TextContent> contents = new ArrayList<>(3);
WeatherNowResponse.NowResult result = newResource.data.getResults().get(0);
contents.add(new TextContent("地点", result.getLocation().getPath()));
contents.add(new TextContent("天气", result.getNow().getText()));
contents.add(new TextContent("温度", result.getNow().getTemperature() + "º"));
mContents.postValue(contents);
STATUS.set(Status.SUCCESS);
} else if (newResource.status == Status.ERROR) {
STATUS.set(Status.ERROR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void loadLocations() {
}
Timber.d("Load history locations: %s", newResource.status);
if (newResource.status == Status.LOADING) {
// TODO: 2017/11/15
STATUS.set(Status.LOADING);
} else if (newResource.status == Status.SUCCESS) {
mLocationPaths.postValue(newResource.data);
String location = newResource.data.get(0);
Expand All @@ -63,8 +63,9 @@ public void loadLocations() {
mFirst = false;
mLocation.postValue(location);
}
STATUS.set(Status.SUCCESS);
} else if (newResource.status == Status.ERROR) {
// TODO: 2017/11/15
STATUS.set(Status.ERROR);
}
});
});
Expand Down
2 changes: 1 addition & 1 deletion weather/src/main/res/layout/fragment_weather_daily.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/weather_source"
app:visibleGone="@{viewModel.STATUS != Status.ERROR}"
app:visibleGone="@{viewModel.STATUS == Status.SUCCESS}"

tools:listitem="@layout/super_item_daily"
/>
Expand Down
2 changes: 1 addition & 1 deletion weather/src/main/res/layout/fragment_weather_now.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/weather_source"
app:visibleGone="@{viewModel.STATUS != Status.ERROR}"
app:visibleGone="@{viewModel.STATUS == Status.SUCCESS}"
tools:listitem="@layout/super_text_item"/>

<include
Expand Down
6 changes: 3 additions & 3 deletions weather/src/main/res/layout/include_logo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<import type="me.xiaobailong24.mvvmarms.repository.http.Status"/>

<variable
name="retry"
type="me.xiaobailong24.mvvmarms.repository.http.IRetry"/>
name="viewModel"
type="me.xiaobailong24.mvvmarms.mvvm.BaseViewModel"/>
</data>

<ImageView
Expand All @@ -19,5 +19,5 @@
android:contentDescription="@string/weather_source"
android:scaleType="fitCenter"
android:src="@mipmap/ic_seniverse_logo"
app:visibleGone="@{retry.STATUS != Status.ERROR}"/>
app:visibleGone="@{viewModel.STATUS != Status.ERROR}"/>
</layout>
6 changes: 5 additions & 1 deletion weather/src/main/res/layout/include_retry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<variable
name="retry"
type="me.xiaobailong24.mvvmarms.repository.http.IRetry"/>

<variable
name="viewModel"
type="me.xiaobailong24.mvvmarms.mvvm.BaseViewModel"/>
</data>

<TextView
Expand All @@ -22,6 +26,6 @@
android:onClick="@{() -> retry.retry()}"
android:text="@string/error_tap_to_retry"
android:textColor="@color/colorSecondary"
app:visibleGone="@{retry.STATUS == Status.ERROR}"/>
app:visibleGone="@{viewModel.STATUS == Status.ERROR}"/>

</layout>

0 comments on commit 24d55fc

Please sign in to comment.