| | |
| | | import androidx.lifecycle.MutableLiveData; |
| | | import androidx.lifecycle.ViewModelProvider; |
| | | |
| | | import com.android.app_base.base.StateViewEnum; |
| | | import com.android.app_base.http.ResultData; |
| | | import com.android.app_base.manager.AppManager; |
| | | import com.android.app_base.utils.RxUtils; |
| | | import com.android.app_base.utils.ToastUtils; |
| | | import com.android.app_base.base.viewmodel.BaseViewModel; |
| | | import com.application.zhangshi_app_android.R; |
| | | import com.application.zhangshi_app_android.bean.FamilyAssetsBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyAssetsResponseBean; |
| | | import com.application.zhangshi_app_android.bean.FamilyMemorabiliaBean; |
| | | import com.application.zhangshi_app_android.bean.GrowthExperienceBean; |
| | | import com.application.zhangshi_app_android.bean.GrowthExperienceResponseBean; |
| | | import com.application.zhangshi_app_android.bean.HomeDevicesBean; |
| | | import com.application.zhangshi_app_android.data.DataRepository; |
| | | |
| | | import java.text.ParseException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Comparator; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.List; |
| | | |
| | | import io.reactivex.Observer; |
| | |
| | | * @desc 成长经历 viewModel |
| | | */ |
| | | public class GrowthExperienceActivityViewModel extends BaseViewModel<DataRepository> { |
| | | private MutableLiveData<GrowthExperienceBean> beanMutableLiveData; |
| | | private MutableLiveData<List<GrowthExperienceBean>> checkListLiveData; |
| | | private MutableLiveData<List<GrowthExperienceBean>> dataListLiveData; |
| | | private MutableLiveData<String> stringMutableLiveData; |
| | | |
| | | |
| | | |
| | | private MutableLiveData<List<GrowthExperienceBean>> dataListLiveData;//数据 |
| | | |
| | | public GrowthExperienceActivityViewModel(@NonNull Application application) { |
| | | super(application); |
| | |
| | | protected DataRepository initModel() { |
| | | return DataRepository.getInstance(); |
| | | } |
| | | public void getGrowthExperience(){ |
| | | model.getGrowthExperience(new HashMap<>()) |
| | | .compose(RxUtils.schedulersTransformer()) |
| | | .subscribe(new Observer<ResultData<GrowthExperienceResponseBean>>() { |
| | | @Override |
| | | public void onSubscribe(Disposable d) { |
| | | addSubscribe(d); |
| | | } |
| | | |
| | | @Override |
| | | public void onCreate(@NonNull LifecycleOwner owner) { |
| | | super.onCreate(owner); |
| | | GrowthExperienceActivityViewModel viewModel=new ViewModelProvider(this).get(GrowthExperienceActivityViewModel.class); |
| | | DataBindingUtil.setContentView(this, R.layout.activity_growth_experience).setViewModel(viewModel); |
| | | @Override |
| | | public void onNext(ResultData<GrowthExperienceResponseBean> data) { |
| | | if (data.getCode() == CODE_SUCCESS){ |
| | | if (data.getData().getData().isEmpty()){ |
| | | changeStateView(StateViewEnum.DATA_NULL); |
| | | }else { |
| | | changeStateView(StateViewEnum.HIDE); |
| | | dataListLiveData.postValue(data.getData().getData()); |
| | | } |
| | | }else { |
| | | messageLiveData.postValue(data.getMsg()); |
| | | } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void onError(Throwable e) { |
| | | messageLiveData.postValue(e.getMessage()); |
| | | } |
| | | |
| | | @Override |
| | | public void onComplete() { |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | public MutableLiveData<List<GrowthExperienceBean>> getDataListLiveData() { |
| | | if (dataListLiveData == null){ |
| | | dataListLiveData = new MutableLiveData<>(); |
| | | } |
| | | return dataListLiveData; |
| | | } |
| | | |
| | | public void setDataListLiveData(MutableLiveData<List<GrowthExperienceBean>> dataListLiveData) { |
| | | this.dataListLiveData = dataListLiveData; |
| | | } |
| | | |
| | | public void sortDataList(int type){ |
| | | List<GrowthExperienceBean> list = dataListLiveData.getValue(); |
| | | if (list == null || list.isEmpty()){ |
| | | return; |
| | | } |
| | | |
| | | list.sort(new Comparator<GrowthExperienceBean>() { |
| | | @Override |
| | | public int compare(GrowthExperienceBean o1, GrowthExperienceBean o2) { |
| | | SimpleDateFormat format = new SimpleDateFormat("yyyy-yyyy", java.util.Locale.getDefault()); |
| | | Date date1 = null; |
| | | Date date2 = null; |
| | | try { |
| | | date1 = format.parse(o1.getDuringYear()); |
| | | date2 = format.parse(o2.getDuringYear()); |
| | | } catch (ParseException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | if (date1 != null && date2 != null) { |
| | | if (type == 0){ |
| | | return date2.compareTo(date1); |
| | | }else { |
| | | return date1.compareTo(date2); |
| | | } |
| | | } |
| | | return 0; |
| | | } |
| | | }); |
| | | dataListLiveData.postValue(list); |
| | | } |
| | | } |