From ec6193939600ca7930193b1d5942b1700baf9e06 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期五, 21 二月 2025 09:20:15 +0800
Subject: [PATCH] 1.巡检记录列表页的实现 2.巡检记录地图展示路径的详情页。 3.处理巡检记录详情页因打开同一个网址导致的webView不显示问题。 4.实现地图页搜索后的地图跳转功能。 5.巡检记录详情页数据加载的优化,默认加载本地数据,当本地没有数据时从服务端获取数据。

---
 app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java |  179 ++++++++++++++++++++++++++++++++++++++++++++++-------------
 1 files changed, 139 insertions(+), 40 deletions(-)

diff --git a/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java b/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java
index dc7a736..e21da6e 100644
--- a/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java
+++ b/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java
@@ -1,21 +1,36 @@
 package com.dayu.pipirrapp.net;
 
+import static com.dayu.pipirrapp.bean.net.UplodFileState.STATE_DONE;
+import static com.dayu.pipirrapp.bean.net.UplodFileState.STATE_ERROR;
+import static com.dayu.pipirrapp.bean.net.UplodFileState.STATE_UPDING;
+import static com.luck.picture.lib.thread.PictureThreadUtils.runOnUiThread;
+
+import android.annotation.SuppressLint;
 import android.content.Context;
+import android.content.Intent;
+import android.text.TextUtils;
 import android.util.Log;
 
 import com.dayu.pipirrapp.MyApplication;
+import com.dayu.pipirrapp.activity.LoginActivity;
+import com.dayu.pipirrapp.adapter.AddPictureAdapter;
 import com.dayu.pipirrapp.bean.net.CodeResult;
 import com.dayu.pipirrapp.bean.net.UplodFileState;
 import com.dayu.pipirrapp.bean.net.WeatherResponse;
+import com.dayu.pipirrapp.bean.net.UploadFileResult;
 import com.dayu.pipirrapp.net.subscribers.BaseProgressSubscriber;
 import com.dayu.pipirrapp.net.subscribers.CodeListener;
 import com.dayu.pipirrapp.net.subscribers.ProgressSubscriber;
 import com.dayu.pipirrapp.net.subscribers.SubscriberListener;
+import com.dayu.pipirrapp.net.upload.ProgressRequestBody;
 import com.dayu.pipirrapp.net.upload.UploadFileListener;
+import com.dayu.pipirrapp.utils.CleanDataUtils;
 import com.dayu.pipirrapp.utils.MapJpgUtils;
 import com.dayu.pipirrapp.utils.MyJsonParser;
+import com.dayu.pipirrapp.utils.MyLog;
 import com.dayu.pipirrapp.utils.NetUtils;
 
+import java.io.File;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -48,8 +63,10 @@
     //鏂囦欢涓婁紶澶辫触閲嶅娆℃暟
     int uplodFilerepeatSize = 3;
 
+
     ApiService apiService;
-    private CompositeDisposable compositeDisposable = new CompositeDisposable(); // 绠$悊璁㈤槄浜嬩欢
+    // 绠$悊璁㈤槄浜嬩欢
+    private final CompositeDisposable compositeDisposable = new CompositeDisposable();
 
     /**
      * 鍒濆鍖栭�氫俊妗嗘灦
@@ -124,9 +141,20 @@
                     public BaseResponse<T> apply(Object o) {
                         if (o instanceof BaseResponse) {
                             BaseResponse tem = (BaseResponse) o;
+                            if (tem.getCode().equals(Constants.TOKEN_INVALID)) {
+                                //褰揷ode涓�0000鏄烦杞埌鐧诲綍鐣岄潰
+                                tem.setMsg("鐧诲綍澶辨晥璇烽噸鏂扮櫥褰�");
+                                redirectToLogin();
+                                return tem;
+                            }
                             BaseResponse<T> response = new BaseResponse<>();
                             response.setCode(tem.getCode());
-                            response.setMsg(tem.getMsg());
+                            if (!TextUtils.isEmpty(tem.getMsg())) {
+                                response.setMsg(tem.getMsg());
+                            } else {
+                                response.setMsg("");
+                            }
+
 
                             if (tClass != null) {
                                 if (tem.getContent() instanceof Map) {
@@ -171,6 +199,7 @@
             return null;
         }
     }
+
 
     /**
      * 鑾峰彇澶╂皵
@@ -222,13 +251,16 @@
      *
      * @param urlPath
      */
+    @SuppressLint("CheckResult")
     public void donwLoadTile(Context context, String urlPath) {
         if (NetUtils.isNetworkAvailable(context)) {
             Observable<ResponseBody> observable;
             observable = apiService.downloadTile(urlPath);
             observable
-                    .subscribeOn(Schedulers.io())  // 缃戠粶璇锋眰鍦� I/O 绾跨▼涓繘琛�
-                    .observeOn(Schedulers.io())    // 鍥炶皟澶勭悊涔熷湪 I/O 绾跨▼
+                    // 缃戠粶璇锋眰鍦� I/O 绾跨▼涓繘琛�
+                    .subscribeOn(Schedulers.io())
+                    // 鍥炶皟澶勭悊涔熷湪 I/O 绾跨▼
+                    .observeOn(Schedulers.io())
                     .subscribe(
                             responseBody -> {
 
@@ -257,55 +289,122 @@
     }
 
 
+//    public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener listener) {
+//        uploadFile(context, uplodData, listener, null);
+//    }
+
     /**
-     * 涓婁紶鏂囦欢
+     * 涓婁紶鍥剧墖鏂囦欢
      *
      * @param context
-     * @param file
+     * @param uplodData
      * @param listener
      */
-    public void uploadFile(final Context context, final UplodFileState file, final UploadFileListener listener) {
+    public void uploadFile(final Context context, final UplodFileState uplodData, final UploadFileListener listener, final AddPictureAdapter adapter) {
 
-        // 鍒涘缓 RequestBody锛岀敤浜庡皝瑁呮瀯寤篟equestBody
-        RequestBody requestFile =
-                RequestBody.create(MediaType.parse("multipart/form-data"), file.getFile());
-        // MultipartBody.Part  鍜屽悗绔害瀹氬ソKey锛岃繖閲岀殑partName鏄敤image
-        MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getFile().getName(), requestFile);
-        // 娣诲姞鎻忚堪
-        String descriptionString = "hello, 杩欐槸鏂囦欢鎻忚堪";
-        RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);
-        apiService.uploadFile(description, body).enqueue(new Callback<BaseResponse>() {
-            @Override
-            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
-                if (response.body() != null) {
-                    if (response.body().isSuccess()) {
-                        file.setState(1);
-                        file.setUrl(response.body().getMsg());
-                        listener.onBack(file);
-                    } else {
-                        if (file.getNumber() <= uplodFilerepeatSize) {
-                            file.setNumber(file.getNumber() + 1);
-                            file.setState(2);
-                            uploadFile(context, file, listener);
+        if (!TextUtils.isEmpty(uplodData.getFilePath())) {
+            MyLog.d("progressRequestBody_uploadFile>>>" + "path:" + uplodData.getFilePath() + ">>>uploadType:" + uplodData.getUploadType());
+            uplodData.setState(STATE_UPDING);
+            File file = new File(uplodData.getFilePath());
+            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
+            ProgressRequestBody progressRequestBody = new ProgressRequestBody(requestBody, (bytesWritten, contentLength, done) -> {
+//            MyLog.d("progressRequestBody>>>" + "bytesWritten:" + bytesWritten + ">>>contentLength: " + contentLength );
+                // 鏇存柊杩涘害鏉℃垨杩涜鍏朵粬鎿嶄綔
+                int progress = (int) (100 * bytesWritten / contentLength);
+                // 渚嬪锛氭洿鏂� ProgressBar 鎴栨樉绀鸿繘搴�
+                runOnUiThread(() -> {
+                    if (progress <5) {
+                        uplodData.setProgress(0);
+                    }
+                    // 杩涘害鏉℃洿鏂颁唬鐮�
+                    if (adapter != null && uplodData.getAdapterPosition() != -1) {
+                        //浼樺寲涓嶉绻佹洿鏂拌繘搴�
+                        if (progress - uplodData.getProgress() > 5 || progress == 100) {
+                            uplodData.setProgress(progress);
+                            adapter.updateProgress(uplodData);
+
+                        }
+
+                    }
+                    MyLog.d("progressRequestBodyHolder>>>path" + "adapterPosition:" + uplodData.getAdapterPosition() + ">>>涓婁紶杩涘害: " + progress + "%");
+                });
+
+            });
+            MultipartBody.Part body = MultipartBody.Part.createFormData("file", file.getName(), progressRequestBody);
+            RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "file description");
+            Call<BaseResponse<UploadFileResult>> uploadFile = null;
+            switch (uplodData.getUploadType()) {
+                case UplodFileState.IMG_TYPE:
+                    uploadFile = apiService.uploadImgFile(body, description);
+                    break;
+                case UplodFileState.VIDEO_TYPE:
+                    uploadFile = apiService.uploadVideoFile(body, description);
+                    break;
+                default:
+                    uploadFile = apiService.uploadImgFile(body, description);
+                    break;
+            }
+
+            uplodData.setThisCall(uploadFile);
+            uploadFile.enqueue(new Callback<BaseResponse<UploadFileResult>>() {
+                @Override
+                public void onResponse(Call<BaseResponse<UploadFileResult>> call, Response<BaseResponse<UploadFileResult>> response) {
+                    if (response.body() != null) {
+                        if (response.body().isSuccess()) {
+                            UploadFileResult uploadFileResult=response.body().getContent();
+                            uplodData.setState(STATE_DONE);
+                            uplodData.setPostId(uploadFileResult.getId());
+                            uplodData.setUoloadFilePath(uploadFileResult.getFilePath());
+                            uplodData.setHash(uploadFileResult.getHash());
+                            uplodData.setWebPathZip(uploadFileResult.getWebPathZip());
+                            listener.onBack(uplodData);
                         } else {
-                            listener.onBack(file);
+                            MyLog.d("progressRequestBody>>>" + response.body().getMsg());
+                            if (uplodData.getNumber() <= uplodFilerepeatSize) {
+                                uplodData.setNumber(uplodData.getNumber() + 1);
+                                uplodData.setState(STATE_ERROR);
+                                uploadFile(context, uplodData, listener, adapter);
+                            } else {
+                                uplodData.setState(STATE_ERROR);
+                                uplodData.setNumber(0);
+                                listener.onBack(uplodData);
+                            }
                         }
                     }
                 }
-            }
 
-            @Override
-            public void onFailure(Call<BaseResponse> call, Throwable t) {
-                if (file.getNumber() <= uplodFilerepeatSize) {
-                    file.setNumber(file.getNumber() + 1);
-                    file.setState(2);
-                    uploadFile(context, file, listener);
-                } else {
-                    listener.onBack(file);
+                @Override
+                public void onFailure(Call<BaseResponse<UploadFileResult>> call, Throwable t) {
+                    MyLog.d("progressRequestBody>>>" + "onFailure:" + t.getMessage());
+                    if (!t.getMessage().equals("Canceled")) {
+                        uplodData.setState(STATE_ERROR);
+                        if (uplodData.getNumber() <= uplodFilerepeatSize) {
+                            uplodData.setNumber(uplodData.getNumber() + 1);
+                            uploadFile(context, uplodData, listener, adapter);
+                        } else {
+                            uplodData.setNumber(0);
+                            listener.onBack(uplodData);
+                        }
+                    }
+
                 }
-            }
-        });
+            });
+        } else {
+            uplodData.setState(STATE_ERROR);
+            listener.onBack(uplodData);
+        }
     }
 
 
+    /**
+     * 褰揷ode涓衡��0000鈥濇椂璺宠浆鍒扮櫥褰曠晫闈�
+     */
+    private void redirectToLogin() {
+
+        Context context = MyApplication.myApplication.getApplicationContext();
+        CleanDataUtils.cleanUserData(context);
+        Intent intent = new Intent(context, LoginActivity.class); // Assuming LoginActivity is your login screen
+        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); // Clear task stack
+        context.startActivity(intent);
+    }
 }
\ No newline at end of file

--
Gitblit v1.8.0