Procházet zdrojové kódy

-Create ref Number when getting error
-Fixing toast and layout

ilhamitubagoes před 4 roky
rodič
revize
df1cd654f7

+ 1 - 1
app/src/main/java/com/fusi24/entryPermitScanner/base/BaseActivity.java

@@ -21,7 +21,7 @@ public abstract class BaseActivity extends AppCompatActivity {
21 21
 
22 22
     public RestService getRestService() {
23 23
         if (restService == null) {
24
-            restService = RestServiceFactory.create(getJwtToken(), this);
24
+            restService = RestServiceFactory.create(getJwtToken(), getSessionManager().getUserSid(), this);
25 25
         }
26 26
         return restService;
27 27
     }

+ 26 - 0
app/src/main/java/com/fusi24/entryPermitScanner/data/SessionManager.java

@@ -2,6 +2,13 @@ package com.fusi24.entryPermitScanner.data;
2 2
 
3 3
 import android.content.SharedPreferences;
4 4
 
5
+import com.fusi24.entryPermitScanner.data.entity.DataSite;
6
+import com.google.gson.Gson;
7
+import com.google.gson.reflect.TypeToken;
8
+
9
+import java.lang.reflect.Type;
10
+import java.util.List;
11
+
5 12
 public class SessionManager {
6 13
 
7 14
     private static final String IS_LOGIN                = "is_login";
@@ -13,6 +20,7 @@ public class SessionManager {
13 20
     private static final String USER_TELEPHONE          = "user_telephone";
14 21
     private static final String USER_RFID_NUMBER        = "user_rfid_number";
15 22
     private static final String GATE                    = "gate";
23
+    private static final String SITE_LIST               = "site_list";
16 24
     private static final String IS_SERIALPORT_2         = "serial_port_2";
17 25
     private static final String IS_DEVELOPMENT          = "development";
18 26
 
@@ -112,6 +120,24 @@ public class SessionManager {
112 120
         editor.apply();
113 121
     }
114 122
 
123
+    public List<DataSite> getSiteList(){
124
+        List<DataSite> siteList;
125
+        String jsonPreferences = preferences.getString(SITE_LIST, "");
126
+
127
+        Type type = new TypeToken<List<DataSite>>() {}.getType();
128
+        siteList = new Gson().fromJson(jsonPreferences, type);
129
+
130
+        return siteList;
131
+    }
132
+
133
+    public void setSiteList(List<DataSite> list){
134
+        String jsonCurProduct = new Gson().toJson(list);
135
+
136
+        SharedPreferences.Editor editor = preferences.edit();
137
+        editor.putString(SITE_LIST , jsonCurProduct);
138
+        editor.apply();
139
+    }
140
+
115 141
     public Boolean isSerialPort2() {
116 142
         return preferences.getBoolean(IS_SERIALPORT_2, false);
117 143
     }

+ 5 - 10
app/src/main/java/com/fusi24/entryPermitScanner/data/api/RestServiceFactory.java

@@ -7,12 +7,12 @@ import androidx.annotation.NonNull;
7 7
 import com.fusi24.entryPermitScanner.BuildConfig;
8 8
 import com.fusi24.entryPermitScanner.config.Constant;
9 9
 import com.fusi24.entryPermitScanner.data.SessionManager;
10
+import com.fusi24.entryPermitScanner.data.interceptor.RestHeaderModifierInterceptor;
10 11
 import com.fusi24.entryPermitScanner.util.Helper;
11 12
 import com.google.gson.Gson;
12 13
 import com.google.gson.GsonBuilder;
13 14
 
14 15
 import okhttp3.OkHttpClient;
15
-import okhttp3.Request;
16 16
 import okhttp3.logging.HttpLoggingInterceptor;
17 17
 import retrofit2.Retrofit;
18 18
 import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
@@ -20,8 +20,8 @@ import retrofit2.converter.gson.GsonConverterFactory;
20 20
 
21 21
 public class RestServiceFactory {
22 22
 
23
-    public static RestService create(String token, Context context) {
24
-        OkHttpClient client = makeClientService(makeLoggingInterceptor(), token);
23
+    public static RestService create(String token, String userId, Context context) {
24
+        OkHttpClient client = makeClientService(makeLoggingInterceptor(), token, userId, context);
25 25
         return makeRestService(client, makeGson(), context);
26 26
     }
27 27
 
@@ -49,14 +49,9 @@ public class RestServiceFactory {
49 49
 
50 50
     @NonNull
51 51
     private static OkHttpClient makeClientService(
52
-            HttpLoggingInterceptor loggingInterceptor, String token) {
52
+            HttpLoggingInterceptor loggingInterceptor, String token, String userId, Context context) {
53 53
         return new OkHttpClient.Builder()
54
-                .addInterceptor(chain -> {
55
-                    Request.Builder ongoing = chain.request().newBuilder();
56
-                    ongoing.addHeader("Content-Type", "application/json");
57
-                    ongoing.addHeader("x-api-key", token);
58
-                    return chain.proceed(ongoing.build());
59
-                })
54
+                .addInterceptor(new RestHeaderModifierInterceptor(token, userId, context))
60 55
                 .addInterceptor(loggingInterceptor)
61 56
                 .build();
62 57
     }

+ 47 - 0
app/src/main/java/com/fusi24/entryPermitScanner/data/entity/APIError.java

@@ -0,0 +1,47 @@
1
+package com.fusi24.entryPermitScanner.data.entity;
2
+
3
+public class APIError {
4
+
5
+    private int statusCode;
6
+    private String message;
7
+    private String refNumber;
8
+
9
+    public APIError(int statusCode, String message, String refNumber){
10
+        this.statusCode = statusCode;
11
+        this.message = message;
12
+        this.refNumber = refNumber;
13
+    }
14
+
15
+    public int getStatusCode() {
16
+        return statusCode;
17
+    }
18
+
19
+    public void setStatusCode(int statusCode) {
20
+        this.statusCode = statusCode;
21
+    }
22
+
23
+    public String getMessage() {
24
+        return message;
25
+    }
26
+
27
+    public void setMessage(String message) {
28
+        this.message = message;
29
+    }
30
+
31
+    public String getRefNumber() {
32
+        return refNumber;
33
+    }
34
+
35
+    public void setRefNumber(String refNumber) {
36
+        this.refNumber = refNumber;
37
+    }
38
+
39
+    @Override
40
+    public String toString() {
41
+        return "APIError{" +
42
+                "statusCode=" + statusCode +
43
+                ", message='" + message + '\'' +
44
+                ", refNumber='" + refNumber + '\'' +
45
+                '}';
46
+    }
47
+}

+ 59 - 0
app/src/main/java/com/fusi24/entryPermitScanner/data/interceptor/RestHeaderModifierInterceptor.java

@@ -0,0 +1,59 @@
1
+package com.fusi24.entryPermitScanner.data.interceptor;
2
+
3
+import android.content.Context;
4
+import android.os.Handler;
5
+import android.os.Looper;
6
+import android.widget.Toast;
7
+
8
+import com.fusi24.entryPermitScanner.data.entity.APIError;
9
+
10
+import org.jetbrains.annotations.NotNull;
11
+
12
+import java.io.IOException;
13
+
14
+import okhttp3.Interceptor;
15
+import okhttp3.Request;
16
+import okhttp3.Response;
17
+import timber.log.Timber;
18
+
19
+public class RestHeaderModifierInterceptor implements Interceptor {
20
+
21
+    private String token;
22
+    private String userId;
23
+    private Context context;
24
+
25
+    public RestHeaderModifierInterceptor(String token, String userId, Context context){
26
+        this.token = token;
27
+        this.userId = userId;
28
+        this.context = context;
29
+    }
30
+
31
+    @NotNull
32
+    @Override
33
+    public Response intercept(Chain chain) throws IOException {
34
+        Request request = chain.request()
35
+                .newBuilder()
36
+                .addHeader("Content-Type", "application/json")
37
+                .addHeader("x-api-key", token)
38
+                .addHeader("userId",userId)
39
+                .build();
40
+        Response response = chain.proceed(request);
41
+
42
+        if(!response.isSuccessful()){
43
+            APIError error = new APIError(response.code(),response.message(),response.header("refNumber"));
44
+            Timber.d("DEVELOPER : %s", error);
45
+            if(error.getRefNumber() != null)
46
+                backgroundThreadToast(context,"Error Rest API Service with Ref Number: "+error.getRefNumber());
47
+            else
48
+                backgroundThreadToast(context,"Error Occurred with Response: "+error.getStatusCode()+" "+error.getMessage());
49
+        }
50
+
51
+        return response;
52
+    }
53
+
54
+    public static void backgroundThreadToast(final Context context, final String msg){
55
+        if(context != null && msg != null){
56
+            new Handler(Looper.getMainLooper()).post(() -> Toast.makeText(context, msg, Toast.LENGTH_LONG).show());
57
+        }
58
+    }
59
+}

+ 9 - 10
app/src/main/java/com/fusi24/entryPermitScanner/ui/ScanActivity.java

@@ -8,7 +8,6 @@ import android.location.Location;
8 8
 import android.os.Bundle;
9 9
 import android.view.View;
10 10
 import android.widget.TextView;
11
-import android.widget.Toast;
12 11
 
13 12
 import androidx.annotation.NonNull;
14 13
 import androidx.annotation.Nullable;
@@ -137,11 +136,11 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
137 136
     }
138 137
 
139 138
     private void setStatusScan() {
140
-        switch (Integer.parseInt(idCheckType)) {
141
-            case 649:
139
+        switch (checkTypeName) {
140
+            case "Check In":
142 141
                 binding.tvStatusScan.setBackground(getDrawable(R.drawable.bg_green));
143 142
                 break;
144
-            case 650:
143
+            case "Check Out":
145 144
             default:
146 145
                 binding.tvStatusScan.setBackground(getDrawable(R.drawable.bg_blue));
147 146
                 break;
@@ -202,18 +201,18 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
202 201
                 double latitude = location.getLatitude();
203 202
                 double longitude = location.getLongitude();
204 203
                 longLat = longitude + "," + latitude;
205
-                System.out.println("DEVELOPER Location 1 : " + location.getLatitude() + " , " + location.getLongitude());
204
+                Timber.i("DEVELOPER Location 1 : " + location.getLatitude() + " , " + location.getLongitude());
206 205
             }
207 206
         });
208
-        locationClient.getLastLocation().addOnCanceledListener(() -> Toast.makeText(this, "Get last location cancelled", Toast.LENGTH_SHORT).show());
209
-        locationClient.getLastLocation().addOnFailureListener(e -> System.out.println(e.getMessage()));
207
+        locationClient.getLastLocation().addOnCanceledListener(() -> Timber.i("Get last location cancelled"));
208
+        locationClient.getLastLocation().addOnFailureListener(Timber::e);
210 209
     }
211 210
 
212 211
     private void getSimpleLocation(){
213 212
         double latitude = simpleLocation.getLatitude();
214 213
         double longitude = simpleLocation.getLongitude();
215 214
         longLat = longitude + "," + latitude;
216
-        Toast.makeText(this, "Simple Location ==> Latitude : " + latitude + ", Longitude : " + longitude, Toast.LENGTH_LONG).show();
215
+        Timber.i("Simple Location ==> Latitude : " + latitude + ", Longitude : " + longitude);
217 216
     }
218 217
 
219 218
     @Override
@@ -240,7 +239,7 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
240 239
 
241 240
             longLat = currentLongitude + "," + currentLatitude;
242 241
 
243
-            Toast.makeText(this, "Latitude : " + currentLatitude + ", Longitude : " + currentLongitude, Toast.LENGTH_LONG).show();
242
+            Timber.i("Latitude : " + currentLatitude + ", Longitude : " + currentLongitude);
244 243
         }
245 244
     }
246 245
 
@@ -284,6 +283,6 @@ public class ScanActivity extends LFScanProcessing implements GoogleApiClient.Co
284 283
         currentLongitude = location.getLongitude();
285 284
 
286 285
         longLat = currentLongitude + "," + currentLatitude;
287
-        Toast.makeText(this, "Latitude : " + currentLatitude + ", Longitude : " + currentLongitude, Toast.LENGTH_LONG).show();
286
+        Timber.i("Latitude : " + currentLatitude + ", Longitude : " + currentLongitude);
288 287
     }
289 288
 }

+ 4 - 2
app/src/main/java/com/fusi24/entryPermitScanner/ui/home/HomeActivity.java

@@ -214,8 +214,10 @@ public class HomeActivity extends BaseActivity implements HomeView, StatusScanAd
214 214
 
215 215
     @Override
216 216
     public void showDataDevice(List<DataDevice> dataDevices) {
217
-        binding.tvGateName.setText(dataDevices.get(0).getGate().getName());
218
-        siteAdapter.updateSiteList(Stream.of(dataDevices.get(0).getGate().getSites()).sortBy(DataSite::getId).toList());
217
+        getSessionManager().setGate(dataDevices.get(0).getGate().getName());
218
+        getSessionManager().setSiteList(Stream.of(dataDevices.get(0).getGate().getSites()).sortBy(DataSite::getId).toList());
219
+        binding.tvGateName.setText(getSessionManager().getGate());
220
+        siteAdapter.updateSiteList(getSessionManager().getSiteList());
219 221
         siteAdapter.notifyDataSetChanged();
220 222
     }
221 223
 

+ 6 - 4
app/src/main/java/com/fusi24/entryPermitScanner/ui/scanresult/ScanResultActivity.java

@@ -28,6 +28,8 @@ import java.util.ArrayList;
28 28
 import java.util.List;
29 29
 import java.util.Locale;
30 30
 
31
+import timber.log.Timber;
32
+
31 33
 public class ScanResultActivity extends BaseActivity implements ScanResultView {
32 34
 
33 35
     public static final String RFID_CARD_NUMBER = "rfid_card_number";
@@ -111,7 +113,7 @@ public class ScanResultActivity extends BaseActivity implements ScanResultView {
111 113
         String[] expand = {"employee.company, employee.structuralPosition, employee.functionalPosition, problems.documentType.category,problems.documentType.group"};
112 114
 
113 115
         String bebas = "Location = " + longLat;
114
-        Toast.makeText(this, bebas, Toast.LENGTH_LONG).show();
116
+        Timber.i(bebas);
115 117
 
116 118
         presenter.checkRfidCard(numberRfid, Integer.valueOf(idPermit), idDevice, Integer.valueOf(idCheckType), longLat, getSessionManager().getUserId(), expand);
117 119
     }
@@ -127,11 +129,11 @@ public class ScanResultActivity extends BaseActivity implements ScanResultView {
127 129
     }
128 130
 
129 131
     private void setStatusScan(){
130
-        switch (Integer.parseInt(idCheckType)){
131
-            case 649 :
132
+        switch (checkTypeName){
133
+            case "Check In" :
132 134
                 binding.tvStatusScan.setBackground(getDrawable(R.drawable.bg_green_no_rad));
133 135
                 break;
134
-            case 650 :
136
+            case "Check Out" :
135 137
             default:
136 138
                 binding.tvStatusScan.setBackground(getDrawable(R.drawable.bg_blue_no_rad));
137 139
                 break;

+ 5 - 0
app/src/main/res/layout/activity_home.xml

@@ -51,6 +51,7 @@
51 51
                             android:layout_width="match_parent"
52 52
                             android:layout_height="wrap_content"
53 53
                             android:layout_marginStart="12dp"
54
+                            android:layout_marginEnd="12dp"
54 55
                             android:layout_weight="1"
55 56
                             android:textSize="14sp"
56 57
                             android:textColor="@color/colorBlack"
@@ -83,6 +84,7 @@
83 84
                             android:layout_height="wrap_content"
84 85
                             android:layout_weight="1"
85 86
                             android:layout_marginStart="12dp"
87
+                            android:layout_marginEnd="12dp"
86 88
                             android:textSize="14sp"
87 89
                             android:textColor="@color/colorBlack"
88 90
                             tools:text="0009449244" />
@@ -114,6 +116,7 @@
114 116
                             android:layout_height="wrap_content"
115 117
                             android:layout_weight="1"
116 118
                             android:layout_marginStart="12dp"
119
+                            android:layout_marginEnd="12dp"
117 120
                             android:textSize="14sp"
118 121
                             android:textColor="@color/colorBlack"
119 122
                             tools:text="Deddy Corbuzier" />
@@ -145,6 +148,7 @@
145 148
                             android:layout_height="wrap_content"
146 149
                             android:layout_weight="1"
147 150
                             android:layout_marginStart="12dp"
151
+                            android:layout_marginEnd="12dp"
148 152
                             android:textSize="14sp"
149 153
                             android:textColor="@color/colorBlack"
150 154
                             android:text="@string/label_strip"
@@ -177,6 +181,7 @@
177 181
                             android:layout_height="wrap_content"
178 182
                             android:layout_weight="1"
179 183
                             android:layout_marginStart="12dp"
184
+                            android:layout_marginEnd="12dp"
180 185
                             android:textSize="14sp"
181 186
                             android:textColor="@color/colorBlack"
182 187
                             tools:listitem="@layout/row_site"