|
|
@@ -8,15 +8,17 @@ import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
|
|
|
import cn.seecoder.courselearningdemo.model.CourseOrder;
|
|
|
+import cn.seecoder.courselearningdemo.model.PageInfo;
|
|
|
import cn.seecoder.courselearningdemo.model.ResultVO;
|
|
|
|
|
|
|
|
|
+import cn.seecoder.courselearningdemo.repository.CourseOrderRepository;
|
|
|
+import cn.seecoder.courselearningdemo.repository.CourseRepository;
|
|
|
import cn.seecoder.courselearningdemo.utils.Constant;
|
|
|
import retrofit2.Call;
|
|
|
import retrofit2.Callback;
|
|
|
import retrofit2.Response;
|
|
|
import cn.seecoder.courselearningdemo.model.Course;
|
|
|
-import cn.seecoder.courselearningdemo.model.PageInfo;
|
|
|
import cn.seecoder.courselearningdemo.network.RetrofitClient;
|
|
|
import cn.seecoder.courselearningdemo.utils.LoadState;
|
|
|
import cn.seecoder.courselearningdemo.utils.LogUtil;
|
|
|
@@ -26,9 +28,14 @@ public class HomeViewModel extends ViewModel {
|
|
|
private String queryKey = "";
|
|
|
private Integer currentPage = 1;
|
|
|
private MutableLiveData<LoadState> stateData;
|
|
|
- private MutableLiveData<List<Course>> courseData;
|
|
|
- private final MutableLiveData<Boolean> courseOrderFlag = new MutableLiveData<>();
|
|
|
+ private MutableLiveData<List<Course>> courseListData;
|
|
|
+ private MutableLiveData<Boolean> courseOrderFlag = new MutableLiveData<>();
|
|
|
+ private CourseOrderRepository courseOrderRepository= new CourseOrderRepository();
|
|
|
+ private CourseRepository courseRepository= CourseRepository.getCourseRepository();
|
|
|
|
|
|
+ public HomeViewModel(){
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
public void setUid(String uid) {
|
|
|
this.uid = uid;
|
|
|
@@ -48,12 +55,12 @@ public class HomeViewModel extends ViewModel {
|
|
|
}
|
|
|
|
|
|
public LiveData<List<Course>> getData() {
|
|
|
- if (courseData == null) {
|
|
|
- courseData = new MutableLiveData<>();
|
|
|
+ if (courseListData == null) {
|
|
|
+ courseListData = new MutableLiveData<>();
|
|
|
stateData = new MutableLiveData<>();
|
|
|
loadData();
|
|
|
}
|
|
|
- return courseData;
|
|
|
+ return courseListData;
|
|
|
}
|
|
|
|
|
|
public LiveData<Boolean> getCourseOrderFlag(){
|
|
|
@@ -66,39 +73,14 @@ public class HomeViewModel extends ViewModel {
|
|
|
|
|
|
public void query(){
|
|
|
currentPage = 1;
|
|
|
- courseData.setValue(new ArrayList<>());
|
|
|
+ courseListData.setValue(new ArrayList<>());
|
|
|
loadData();
|
|
|
}
|
|
|
|
|
|
private void loadData(){
|
|
|
stateData.setValue(LoadState.LOADING);
|
|
|
- Call<PageInfo<Course>> courseCall = RetrofitClient.courseDao.getAllCourses(currentPage, uid, queryKey);
|
|
|
- // 异步调用
|
|
|
- courseCall.enqueue(new Callback<PageInfo<Course>>() {
|
|
|
- @Override
|
|
|
- public void onResponse(Call<PageInfo<Course>> call, Response<PageInfo<Course>> response) {
|
|
|
- LogUtil.i(courseCall, "Get response");
|
|
|
- if (response.body() == null){
|
|
|
- stateData.setValue(LoadState.ERROR);
|
|
|
- currentPage --;
|
|
|
- } else if (response.body().getSize() == 0 || currentPage > response.body().getPages() ){
|
|
|
- stateData.setValue(LoadState.EMPTY);
|
|
|
- currentPage --;
|
|
|
- } else {
|
|
|
- stateData.setValue(LoadState.SUCCESS);
|
|
|
- List<Course> temp = (courseData.getValue() != null) ? courseData.getValue() : new ArrayList<>();
|
|
|
- temp.addAll(response.body().getList());
|
|
|
- courseData.setValue(temp);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onFailure(Call<PageInfo<Course>> call, Throwable t) {
|
|
|
- LogUtil.e(courseCall, "loadCourseListPageInfo failure: "+ t.getMessage() + " caused by: "+t.getCause());
|
|
|
- stateData.setValue(LoadState.ERROR);
|
|
|
- currentPage --;
|
|
|
- }
|
|
|
- });
|
|
|
+ courseRepository.getCourseListData(currentPage,uid,queryKey,courseListData,stateData);
|
|
|
+ currentPage =courseRepository.getCurrentPage();
|
|
|
}
|
|
|
|
|
|
public void createCourseOrder(Course course){
|
|
|
@@ -111,26 +93,9 @@ public class HomeViewModel extends ViewModel {
|
|
|
// 订单状态设为未支付
|
|
|
order.setStatus(Constant.ORDER_STATUS_UNPAID);
|
|
|
// 调用后端生成订单的接口
|
|
|
- Call<ResultVO<CourseOrder>> courseOrderCall = RetrofitClient.courseOrderService.createCourseOrder(order);
|
|
|
- courseOrderCall.enqueue(new Callback<ResultVO<CourseOrder>>() {
|
|
|
- @Override
|
|
|
- public void onResponse(Call<ResultVO<CourseOrder>> call, Response<ResultVO<CourseOrder>> response) {
|
|
|
- if(response.body() == null || response.body().getCode() == null)
|
|
|
- LogUtil.e("createCourseOrder", "response Null Error!");
|
|
|
- else if(response.body().getCode().equals(Constant.REQUEST_SUCCESS)){
|
|
|
- courseOrderFlag.setValue(true);
|
|
|
- sendCourseOrderSuccessNotify(course.getId());
|
|
|
- }else {
|
|
|
- courseOrderFlag.setValue(false);
|
|
|
- LogUtil.e("createCourseOrder", response.body().getMsg());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- @Override
|
|
|
- public void onFailure(Call<ResultVO<CourseOrder>> call, Throwable t) {
|
|
|
- LogUtil.e("createCourseOrder Fail", t.getMessage());
|
|
|
- }
|
|
|
- });
|
|
|
+ courseOrderFlag=courseOrderRepository.createCourseOrder(order);
|
|
|
+ if(courseOrderRepository.getSuccessCreatedFlag()==true)
|
|
|
+ sendCourseOrderSuccessNotify(course.getId());
|
|
|
}
|
|
|
|
|
|
private void sendCourseOrderSuccessNotify(Integer courseId){
|