From 3a4343f647f038a9e8366f17b33a07fdfd8b680c Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期四, 05 十二月 2024 10:26:57 +0800
Subject: [PATCH] 1.添加巡检时的后台定位权限获取

---
 app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java |  212 +++++++++++++++++++++++++++++++++++++++++++----------
 1 files changed, 172 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 186cf43..dc7a736 100644
--- a/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java
+++ b/app/src/main/java/com/dayu/pipirrapp/net/ApiManager.java
@@ -1,29 +1,28 @@
 package com.dayu.pipirrapp.net;
 
 import android.content.Context;
+import android.util.Log;
 
-
-import com.dayu.pipirrapp.bean.WeatherResponse;
+import com.dayu.pipirrapp.MyApplication;
+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.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.UploadFileListener;
+import com.dayu.pipirrapp.utils.MapJpgUtils;
 import com.dayu.pipirrapp.utils.MyJsonParser;
+import com.dayu.pipirrapp.utils.NetUtils;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.Objects;
 
 import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
-import io.reactivex.rxjava3.annotations.NonNull;
 import io.reactivex.rxjava3.core.Observable;
-import io.reactivex.rxjava3.core.Observer;
-import io.reactivex.rxjava3.disposables.Disposable;
+import io.reactivex.rxjava3.disposables.CompositeDisposable;
 import io.reactivex.rxjava3.functions.Function;
 import io.reactivex.rxjava3.schedulers.Schedulers;
 import okhttp3.MediaType;
@@ -43,11 +42,14 @@
  */
 public class ApiManager {
 
+    static String TAG = "ApiManager";
+
     static ApiManager apiManager;
     //鏂囦欢涓婁紶澶辫触閲嶅娆℃暟
     int uplodFilerepeatSize = 3;
 
     ApiService apiService;
+    private CompositeDisposable compositeDisposable = new CompositeDisposable(); // 绠$悊璁㈤槄浜嬩欢
 
     /**
      * 鍒濆鍖栭�氫俊妗嗘灦
@@ -60,6 +62,7 @@
 
     public ApiManager() {
         apiService = RetrofitClient.getInstance().getApiService();
+
     }
 
     public static ApiManager getInstance() {
@@ -77,6 +80,14 @@
 
     public <T> void requestPost(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) {
         request(context, false, path, false, tClass, params, listener);
+    }
+
+    public <T> void requestGet(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) {
+        request(context, false, path, true, tClass, params, listener);
+    }
+
+    public <T> void requestGetHideLoading(final Context context, final String path, final Class<T> tClass, final Map<String, Object> params, final SubscriberListener listener) {
+        request(context, true, path, true, tClass, params, listener);
     }
 
     /**
@@ -98,10 +109,9 @@
 
         if (isGet) {
             if (params == null) {
-                observable = apiService.requestGet(path);
+                observable = apiService.requestGet(path, MyApplication.myApplication.token);
             } else {
-
-                observable = apiService.requestGet(path, params);
+                observable = apiService.requestGet(path, params, MyApplication.myApplication.token);
             }
         } else {
             observable = apiService.requestPost(path, params);
@@ -118,33 +128,16 @@
                             response.setCode(tem.getCode());
                             response.setMsg(tem.getMsg());
 
-                            if (tClass != null && tem.getContent() instanceof Map) {
-                                try {
-//                                    response.setData(MyJsonParser.getBeanFromMap((Map<String, Object>) tem.getData(), tClass));
-                                    String jsonData = MyJsonParser.getJsontoMap((Map) tem.getContent());
-                                    response.setContent(MyJsonParser.getBeanFromJson(jsonData, tClass));
-                                } catch (Exception e) {
-                                    e.printStackTrace();
-                                }
-                            } else if (tClass != null && tem.getContent() instanceof List) {
-                                try {
-                                    response.setContent((T) MyJsonParser.getListByJson(MyJsonParser.getJsonbyList((List) tem.getContent()), tClass));
-                                } catch (Exception e) {
-                                    e.printStackTrace();
-                                }
-                            } else if (tClass != null && tem.getContent() instanceof Integer) {
-                                response.setContent((T) tem.getContent());
-                            }
-                            if (tClass != null && tClass.getName() instanceof String && tem.getContent() instanceof String) {
-                                try {
+                            if (tClass != null) {
+                                if (tem.getContent() instanceof Map) {
+                                    response.setContent(parseMapContent(tem.getContent(), tClass));
+                                } else if (tem.getContent() instanceof List) {
+                                    response.setContent(parseListContent(tem.getContent(), tClass));
+                                } else if (tem.getContent() instanceof Integer || tem.getContent() instanceof String) {
                                     response.setContent((T) tem.getContent());
-                                } catch (Exception e) {
-                                    e.printStackTrace();
                                 }
                             }
                             return response;
-
-
                         }
 
                         return null;
@@ -157,14 +150,36 @@
     }
 
 
+    // 瑙f瀽 Map 绫诲瀷鐨勬暟鎹�
+    private <T> T parseMapContent(Object content, Class<T> tClass) {
+        try {
+            String jsonData = MyJsonParser.getJsontoMap((Map<String, Object>) content);
+            return MyJsonParser.getBeanFromJson(jsonData, tClass);
+        } catch (Exception e) {
+            Log.e(TAG, "Error parsing map content", e);
+            return null;
+        }
+    }
+
+    // 瑙f瀽 List 绫诲瀷鐨勬暟鎹�
+    private <T> T parseListContent(Object content, Class<T> tClass) {
+        try {
+            String jsonData = MyJsonParser.getJsonbyList((List<?>) content);
+            return (T) MyJsonParser.getListByJson(jsonData, tClass);
+        } catch (Exception e) {
+            Log.e(TAG, "Error parsing list content", e);
+            return null;
+        }
+    }
 
     /**
-     *  鑾峰彇澶╂皵
-     * @param data 缁忕含搴︼紙鏍煎紡鏄� 绾害:缁忓害锛岃嫳鏂囧啋鍙峰垎闅旓級
+     * 鑾峰彇澶╂皵
+     *
+     * @param data     缁忕含搴︼紙鏍煎紡鏄� 绾害:缁忓害锛岃嫳鏂囧啋鍙峰垎闅旓級
      * @param callback
      * @param <T>
      */
-    public <T> void requestWeather(String data,Callback<WeatherResponse> callback) {
+    public <T> void requestWeather(String data, Callback<WeatherResponse> callback) {
         Map<String, Object> params = new HashMap<>();
         params.put("key", "S6Tq4pvOakMuWRrg0");
         params.put("location", data);
@@ -176,4 +191,121 @@
     }
 
 
+    /**
+     * 鑾峰彇楠岃瘉鐮�
+     *
+     * @param params
+     * @param listener
+     * @param <T>
+     */
+    public <T> void getCode(Map<String, Object> params, CodeListener listener) {
+        if (NetUtils.isNetworkAvailable(MyApplication.myApplication)) {
+            Observable observable;
+            observable = apiService.getCode(params);
+            observable.subscribeOn(Schedulers.io())
+                    .unsubscribeOn(Schedulers.newThread())
+                    .observeOn(AndroidSchedulers.mainThread())
+                    .subscribe(responseBody -> {
+                        listener.onNext((CodeResult) responseBody);
+                    }, throwable -> {
+                        listener.error((Throwable) throwable);
+                    });
+        } else {
+            listener.onNext(null);
+        }
+
+    }
+
+
+    /**
+     * 涓嬭浇鍦板浘鐡︾墖
+     *
+     * @param urlPath
+     */
+    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 绾跨▼
+                    .subscribe(
+                            responseBody -> {
+
+                                boolean success = false;
+                                if (!MapJpgUtils.getInsatance().isHasFiles(urlPath)) {
+                                    success = MapJpgUtils.getInsatance().saveTileToCache(urlPath, responseBody);
+                                }
+                                if (success) {
+                                    Log.d(TAG, "Download success for tile: " + urlPath);
+                                } else {
+                                    Log.e(TAG, "Failed to save tile to disk: " + urlPath);
+                                }
+                            },
+                            throwable -> Log.e(TAG, "Download failed for tile: " + urlPath)
+                    );
+        } else {
+            Log.e(TAG, "娌℃湁缃戠粶涓嶄笅杞�: " + urlPath);
+        }
+    }
+
+    // 鍋滄绾跨▼姹狅紝閲婃斁璧勬簮
+    public void clearDisposables() {
+        if (!compositeDisposable.isDisposed()) {
+            compositeDisposable.dispose();
+        }
+    }
+
+
+    /**
+     * 涓婁紶鏂囦欢
+     *
+     * @param context
+     * @param file
+     * @param listener
+     */
+    public void uploadFile(final Context context, final UplodFileState file, final UploadFileListener listener) {
+
+        // 鍒涘缓 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);
+                        } else {
+                            listener.onBack(file);
+                        }
+                    }
+                }
+            }
+
+            @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);
+                }
+            }
+        });
+    }
+
+
 }
\ No newline at end of file

--
Gitblit v1.8.0