Parcourir la source

-change to MVP pattern

ilhamitubagoes il y a 4 ans
Parent
commit
89d1f624f6

+ 12 - 0
app/src/main/java/com/fusi24/rfid/data/DataManager.java

@@ -2,9 +2,13 @@ package com.fusi24.rfid.data;
2 2
 
3 3
 import com.fusi24.rfid.data.api.RestService;
4 4
 import com.fusi24.rfid.data.entity.DataAuth;
5
+import com.fusi24.rfid.data.entity.DataDetail;
6
+import com.fusi24.rfid.data.entity.DataEntryPermit;
5 7
 import com.fusi24.rfid.data.entity.DataResultRfid;
6 8
 import com.fusi24.rfid.data.entity.DataUser;
7 9
 
10
+import java.util.List;
11
+
8 12
 import io.reactivex.Observable;
9 13
 
10 14
 public class DataManager {
@@ -19,6 +23,14 @@ public class DataManager {
19 23
         return restService.userLogin(dataAuth);
20 24
     }
21 25
 
26
+    public Observable<List<DataEntryPermit>> getEntryPermit() {
27
+        return restService.getEntryPermit();
28
+    }
29
+
30
+    public Observable<List<DataDetail>> getStatusScan(String code) {
31
+        return restService.getLookUp(code);
32
+    }
33
+
22 34
     public Observable<DataResultRfid> checkRfidCard(String numberRfid, Integer permitId, String rfidDeviceId, Integer checkTypeId, String longLat, String[] expand) {
23 35
         return restService.checkRfidCard(numberRfid, permitId, rfidDeviceId, checkTypeId, longLat, expand);
24 36
     }

+ 4 - 4
app/src/main/java/com/fusi24/rfid/data/api/RestService.java

@@ -23,15 +23,15 @@ public interface RestService {
23 23
     @POST(Constant.BEATS_ENDPOINT + "/api/mobile/login/rfid")
24 24
     Observable<DataUser> userLogin(@Body DataAuth dataAuth);
25 25
 
26
+    @GET(Constant.SID2_ENDPOINT + "/api/entryPermit")
27
+    Observable<List<DataEntryPermit>> getEntryPermit();
28
+
26 29
     @GET(Constant.BEATS2_ENDPOINT + "/api/lookup")
27
-    Call<List<DataDetail>> getLookUp(@Query("filter[parent.code]") String code);
30
+    Observable<List<DataDetail>> getLookUp(@Query("filter[parent.code]") String code);
28 31
 
29 32
     @GET(Constant.BEATS2_ENDPOINT + "/api/location")
30 33
     Call<List<DataSite>> getDataSite(@Query("filter[type.id]") Integer typeId);
31 34
 
32
-    @GET(Constant.SID2_ENDPOINT + "/api/entryPermit")
33
-    Call<List<DataEntryPermit>> getEntryPermit();
34
-
35 35
     @GET(Constant.SID2_ENDPOINT + Constant.RFID_ENDPOINT + "/permit/check/{number_rfid}")
36 36
     Observable<DataResultRfid> checkRfidCard(@Path("number_rfid") String numberRfid,
37 37
                                              @Query("entryPermitId") Integer permitId,

+ 29 - 52
app/src/main/java/com/fusi24/rfid/ui/home/HomeActivity.java

@@ -24,29 +24,23 @@ import com.fusi24.rfid.SplashScreenActivity;
24 24
 import com.fusi24.rfid.adapter.StatusScanAdapter;
25 25
 import com.fusi24.rfid.base.BaseActivity;
26 26
 import com.fusi24.rfid.config.Constant;
27
-import com.fusi24.rfid.data.api.RestService;
28
-import com.fusi24.rfid.data.api.RestServiceFactory;
29 27
 import com.fusi24.rfid.data.entity.DataDetail;
30 28
 import com.fusi24.rfid.data.entity.DataEntryPermit;
31 29
 import com.fusi24.rfid.databinding.ActivityHomeBinding;
32 30
 import com.fusi24.rfid.ui.ScanActivity;
33 31
 
34
-import org.jetbrains.annotations.NotNull;
35
-
36 32
 import java.util.ArrayList;
37 33
 import java.util.List;
38 34
 
39
-import retrofit2.Call;
40
-import retrofit2.Callback;
41
-import retrofit2.Response;
42 35
 import timber.log.Timber;
43 36
 
44
-public class HomeActivity extends BaseActivity implements StatusScanAdapter.OnItemSelected {
37
+public class HomeActivity extends BaseActivity implements HomeView, StatusScanAdapter.OnItemSelected {
45 38
 
46 39
     private DataEntryPermit dataEntryPermit;
47 40
     private List<DataEntryPermit> entryPermitList;
48 41
     private List<DataDetail> statusScanList;
49 42
     private ActivityHomeBinding binding;
43
+    private HomePresenter presenter;
50 44
     private StatusScanAdapter adapter;
51 45
     private Integer idCheckType = null;
52 46
     private String idDevice, checkTypeName;
@@ -70,10 +64,11 @@ public class HomeActivity extends BaseActivity implements StatusScanAdapter.OnIt
70 64
             getSupportActionBar().setDisplayShowTitleEnabled(false);
71 65
         }
72 66
 
67
+        presenter = new HomePresenter(getManager(), getAndroidScheduler(), getProcessScheduler());
68
+        presenter.attachView(this);
69
+
73 70
         initView();
74 71
         loadingData();
75
-        getDataPermit();
76
-        getDataStatusScan();
77 72
     }
78 73
 
79 74
     @Override
@@ -129,6 +124,9 @@ public class HomeActivity extends BaseActivity implements StatusScanAdapter.OnIt
129 124
         //TODO STILL HARDCODE
130 125
         binding.tvGateName.setText("Suzuran");
131 126
         binding.tvSiteName.setText("BMO 1");
127
+
128
+        presenter.getEntryPermit();
129
+        presenter.getStatusScan();
132 130
     }
133 131
 
134 132
     private void initEvent(){
@@ -167,45 +165,23 @@ public class HomeActivity extends BaseActivity implements StatusScanAdapter.OnIt
167 165
 
168 166
     }
169 167
 
170
-    private void getDataPermit() {
171
-        RestService restService = RestServiceFactory.create(Constant.JWT_TOKEN);
172
-        Call<List<DataEntryPermit>> call = restService.getEntryPermit();
173
-        call.enqueue(new Callback<List<DataEntryPermit>>() {
174
-            @Override
175
-            public void onResponse(@NotNull Call<List<DataEntryPermit>> call, @NotNull Response<List<DataEntryPermit>> response) {
176
-                if (response.body() != null) {
177
-                    entryPermitList.addAll(response.body());
178
-                    setDataPermit();
179
-                }
180
-            }
181
-
182
-            @Override
183
-            public void onFailure(@NotNull Call<List<DataEntryPermit>> call, @NotNull Throwable t) {
184
-                Toast.makeText(HomeActivity.this, "Failed to get data permit", Toast.LENGTH_SHORT).show();
185
-                Timber.i("DEVELOPER : %s", t.getMessage());
186
-            }
187
-        });
168
+    @Override
169
+    public void onSelected(DataDetail detail) {
170
+        idCheckType = detail.getId();
171
+        checkTypeName = detail.getName();
188 172
     }
189 173
 
190
-    private void getDataStatusScan(){
191
-        RestService restService = RestServiceFactory.create(Constant.JWT_TOKEN);
192
-        Call<List<DataDetail>> call = restService.getLookUp("BeSCAN_CHECK_TYPE");
193
-        call.enqueue(new Callback<List<DataDetail>>() {
194
-            @Override
195
-            public void onResponse(@NotNull Call<List<DataDetail>> call, @NotNull Response<List<DataDetail>> response) {
196
-                if (response.body() != null){
197
-                    statusScanList.addAll(response.body());
198
-                    adapter.updateDetailList(statusScanList);
199
-                    adapter.notifyDataSetChanged();
200
-                }
201
-            }
174
+    @Override
175
+    public void showDataPermit(List<DataEntryPermit> dataEntryPermitList) {
176
+        entryPermitList.addAll(dataEntryPermitList);
177
+        setDataPermit();
178
+    }
202 179
 
203
-            @Override
204
-            public void onFailure(@NotNull Call<List<DataDetail>> call, @NotNull Throwable t) {
205
-                Toast.makeText(HomeActivity.this, "Failed to get data status Scan", Toast.LENGTH_SHORT).show();
206
-                Timber.i("DEVELOPER : %s", t.getMessage());
207
-            }
208
-        });
180
+    @Override
181
+    public void showDataStatusScan(List<DataDetail> dataStatusScanList) {
182
+        statusScanList.addAll(dataStatusScanList);
183
+        adapter.updateDetailList(statusScanList);
184
+        adapter.notifyDataSetChanged();
209 185
     }
210 186
 
211 187
     private void setDataPermit(){
@@ -214,14 +190,15 @@ public class HomeActivity extends BaseActivity implements StatusScanAdapter.OnIt
214 190
     }
215 191
 
216 192
     @Override
217
-    protected void onDestroy() {
218
-        super.onDestroy();
219
-        binding = null;
193
+    public void showError(Throwable e) {
194
+        Toast.makeText(HomeActivity.this, "Failed get data", Toast.LENGTH_SHORT).show();
195
+        Timber.i("DEVELOPER : %s", e.getMessage());
220 196
     }
221 197
 
222 198
     @Override
223
-    public void onSelected(DataDetail detail) {
224
-        idCheckType = detail.getId();
225
-        checkTypeName = detail.getName();
199
+    protected void onDestroy() {
200
+        super.onDestroy();
201
+        presenter.detachView();
202
+        binding = null;
226 203
     }
227 204
 }

+ 50 - 0
app/src/main/java/com/fusi24/rfid/ui/home/HomePresenter.java

@@ -0,0 +1,50 @@
1
+package com.fusi24.rfid.ui.home;
2
+
3
+import com.fusi24.rfid.base.BasePresenter;
4
+import com.fusi24.rfid.data.DataManager;
5
+
6
+import io.reactivex.Scheduler;
7
+
8
+class HomePresenter extends BasePresenter<HomeView> {
9
+
10
+    private DataManager manager;
11
+    private Scheduler androidScheduler;
12
+    private Scheduler processScheduler;
13
+
14
+    HomePresenter(DataManager manager, Scheduler androidScheduler, Scheduler processScheduler) {
15
+        this.manager = manager;
16
+        this.androidScheduler = androidScheduler;
17
+        this.processScheduler = processScheduler;
18
+    }
19
+
20
+    void getEntryPermit() {
21
+        disposable.add(manager.getEntryPermit()
22
+                .observeOn(androidScheduler)
23
+                .subscribeOn(processScheduler)
24
+                .subscribe(result -> {
25
+                    if (isViewAttached()) {
26
+                        getView().showDataPermit(result);
27
+                    }
28
+                }, throwable -> {
29
+                    if (isViewAttached()) {
30
+                        getView().showError(throwable);
31
+                    }
32
+                }));
33
+    }
34
+
35
+    void getStatusScan() {
36
+        disposable.add(manager.getStatusScan("BeSCAN_CHECK_TYPE")
37
+                .observeOn(androidScheduler)
38
+                .subscribeOn(processScheduler)
39
+                .subscribe(result -> {
40
+                    if (isViewAttached()) {
41
+                        getView().showDataStatusScan(result);
42
+                    }
43
+                }, throwable -> {
44
+                    if (isViewAttached()) {
45
+                        getView().showError(throwable);
46
+                    }
47
+                }));
48
+    }
49
+
50
+}

+ 15 - 0
app/src/main/java/com/fusi24/rfid/ui/home/HomeView.java

@@ -0,0 +1,15 @@
1
+package com.fusi24.rfid.ui.home;
2
+
3
+import com.fusi24.rfid.base.BaseView;
4
+import com.fusi24.rfid.data.entity.DataDetail;
5
+import com.fusi24.rfid.data.entity.DataEntryPermit;
6
+import com.fusi24.rfid.data.entity.DataResultRfid;
7
+
8
+import java.util.List;
9
+
10
+public interface HomeView extends BaseView {
11
+
12
+    void showDataPermit(List<DataEntryPermit> dataEntryPermitList);
13
+    void showDataStatusScan(List<DataDetail> dataStatusScanList);
14
+
15
+}