소스 검색

Refactor: Refactor CourseOrderRepository. Remove the callback from ViewModel to Repository.

kenny67nju 5 년 전
부모
커밋
7805918c13

+ 41 - 6
app/src/main/java/cn/seecoder/courselearningdemo/repository/CourseOrderRepository.java

@@ -16,9 +16,26 @@ import retrofit2.http.Body;
 public class CourseOrderRepository {
     private final MutableLiveData<Boolean> courseOrderFlag = new MutableLiveData<>();
     private CourseOrderDao courseOrderDao=RetrofitClient.getCourseOrderDao();
-    private boolean successCreatedFlag;
 
-    public MutableLiveData<Boolean> createCourseOrder(CourseOrder order)
+    private static CourseOrderRepository uniqueInstance = null;
+
+    public static CourseOrderRepository getCourseRepository(){
+        if (uniqueInstance == null)
+        {
+            uniqueInstance = new CourseOrderRepository();
+        }
+        return uniqueInstance;
+    }
+
+    private CourseOrderRepository(){
+
+    }
+
+    public MutableLiveData<Boolean> getCourseOrderFlag(){
+        return courseOrderFlag;
+    }
+
+    public void createCourseOrder(CourseOrder order)
     {
 
         // 调用后端生成订单的接口
@@ -29,8 +46,8 @@ public class CourseOrderRepository {
                 if(response.body() == null || response.body().getCode() == null)
                     LogUtil.e("createCourseOrder", "response Null Error!");
                 else if(response.body().getCode().equals(Constant.REQUEST_SUCCESS)){
-                    successCreatedFlag = true;
                     courseOrderFlag.setValue(true);
+                    sendCourseOrderSuccessNotify(order.getUserId(),order.getCourseId());
                 }else {
                     courseOrderFlag.setValue(false);
                     LogUtil.e("createCourseOrder", response.body().getMsg());
@@ -43,9 +60,27 @@ public class CourseOrderRepository {
             }
         });
 
-        return courseOrderFlag;
     }
-    public boolean getSuccessCreatedFlag(){
-        return successCreatedFlag;
+
+    private void sendCourseOrderSuccessNotify(String uid,Integer courseId){
+        System.out.println("sendCourseOrderSuccessNotify uid = "+uid);
+        Call<ResultVO<String>> sendCourseOrderSuccessNotify = RetrofitClient.notifyService.sendCourseOrderSuccessNotify(uid, courseId);
+        sendCourseOrderSuccessNotify.enqueue(new Callback<ResultVO<String>>() {
+            @Override
+            public void onResponse(Call<ResultVO<String>> call, Response<ResultVO<String>> response) {
+                if(response.body() == null || response.body().getCode() == null)
+                    LogUtil.e("sendCourseOrderSuccessNotify", "response Null Error!");
+                else if(response.body().getCode().equals(Constant.REQUEST_SUCCESS))
+                    LogUtil.i("sendCourseOrderSuccessNotify", "success");
+                else
+                    LogUtil.i("sendCourseOrderSuccessNotify", "fail");
+            }
+
+            @Override
+            public void onFailure(Call<ResultVO<String>> call, Throwable t) {
+                LogUtil.i("sendCourseOrderSuccessNotify", "Call fail"+ t.getMessage() + "  caused by: "+t.getCause());
+            }
+        });
     }
+
 }

+ 3 - 1
app/src/main/java/cn/seecoder/courselearningdemo/repository/CourseRepository.java

@@ -26,8 +26,8 @@ public class CourseRepository {
     private final MutableLiveData<LoadState> stateData = new MutableLiveData<>();
     private final MutableLiveData<Course> courseData = new MutableLiveData<>();
     private final MutableLiveData<List<Course>> courseListData = new MutableLiveData<>();
-    private static CourseRepository uniqueInstance = null;
     private Integer currentPage = 1;
+    private static CourseRepository uniqueInstance = null;
 
     public static CourseRepository getCourseRepository(){
         if (uniqueInstance == null)
@@ -108,4 +108,6 @@ public class CourseRepository {
     }
 
 
+
+
 }

+ 4 - 25
app/src/main/java/cn/seecoder/courselearningdemo/viewmodel/HomeViewModel.java

@@ -29,13 +29,14 @@ public class HomeViewModel extends ViewModel {
     private Integer currentPage = 1;
     private MutableLiveData<LoadState> stateData;
     private MutableLiveData<List<Course>> courseListData;
-    private MutableLiveData<Boolean> courseOrderFlag = new MutableLiveData<>();
-    private CourseOrderRepository courseOrderRepository= new CourseOrderRepository();
+    private MutableLiveData<Boolean> courseOrderFlag;
+    private CourseOrderRepository courseOrderRepository= CourseOrderRepository.getCourseRepository();
     private CourseRepository courseRepository= CourseRepository.getCourseRepository();
 
     public HomeViewModel(){
         courseListData = courseRepository.getCourseListLiveData();
         stateData = courseRepository.getStateDataLiveData();
+        courseOrderFlag = courseOrderRepository.getCourseOrderFlag();
 
     }
 
@@ -95,29 +96,7 @@ public class HomeViewModel extends ViewModel {
         // 订单状态设为未支付
         order.setStatus(Constant.ORDER_STATUS_UNPAID);
         // 调用后端生成订单的接口
-        courseOrderFlag=courseOrderRepository.createCourseOrder(order);
-        if(courseOrderRepository.getSuccessCreatedFlag())
-            sendCourseOrderSuccessNotify(course.getId());
+        courseOrderRepository.createCourseOrder(order);
     }
 
-    private void sendCourseOrderSuccessNotify(Integer courseId){
-        System.out.println("sendCourseOrderSuccessNotify uid = "+uid);
-        Call<ResultVO<String>> sendCourseOrderSuccessNotify = RetrofitClient.notifyService.sendCourseOrderSuccessNotify(uid, courseId);
-        sendCourseOrderSuccessNotify.enqueue(new Callback<ResultVO<String>>() {
-            @Override
-            public void onResponse(Call<ResultVO<String>> call, Response<ResultVO<String>> response) {
-                if(response.body() == null || response.body().getCode() == null)
-                    LogUtil.e("sendCourseOrderSuccessNotify", "response Null Error!");
-                else if(response.body().getCode().equals(Constant.REQUEST_SUCCESS))
-                    LogUtil.i("sendCourseOrderSuccessNotify", "success");
-                else
-                    LogUtil.i("sendCourseOrderSuccessNotify", "fail");
-            }
-
-            @Override
-            public void onFailure(Call<ResultVO<String>> call, Throwable t) {
-                LogUtil.i("sendCourseOrderSuccessNotify", "Call fail"+ t.getMessage() + "  caused by: "+t.getCause());
-            }
-        });
-    }
 }