From 798128053bb17ed293aa8d5424db3b5ed007d375 Mon Sep 17 00:00:00 2001
From: zuoxiao <470321431@qq.com>
Date: 星期一, 23 十二月 2024 10:20:57 +0800
Subject: [PATCH] 1.优化订单列表框架,升级为viewPager2,提升性能。 2.添加收到新工单后的红点提醒。 3.修复查询数据为空时报错。 4.优化修改经纬度的流程。 5.修复mqtt的CLIENT_ID一致导致的连接错误。 6.修复收到新工单后点击消息通知栏跳转详情时不更新数据的bug。

---
 app/src/main/java/com/dayu/pipirrapp/fragment/OrderFragment.java |  144 ++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 122 insertions(+), 22 deletions(-)

diff --git a/app/src/main/java/com/dayu/pipirrapp/fragment/OrderFragment.java b/app/src/main/java/com/dayu/pipirrapp/fragment/OrderFragment.java
index 68aa844..92c61ba 100644
--- a/app/src/main/java/com/dayu/pipirrapp/fragment/OrderFragment.java
+++ b/app/src/main/java/com/dayu/pipirrapp/fragment/OrderFragment.java
@@ -1,21 +1,32 @@
 package com.dayu.pipirrapp.fragment;
 
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.Context;
 import android.content.Intent;
 import android.graphics.Typeface;
+import android.os.Build;
 import android.os.Bundle;
 import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 
+import androidx.activity.result.ActivityResultLauncher;
+import androidx.activity.result.contract.ActivityResultContracts;
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.core.app.NotificationCompat;
+import androidx.lifecycle.Observer;
 import androidx.recyclerview.widget.LinearLayoutManager;
 
 import com.dayu.pipirrapp.MyApplication;
 import com.dayu.pipirrapp.R;
 import com.dayu.pipirrapp.activity.OrderDetailActivity;
 import com.dayu.pipirrapp.adapter.OrderAdapter;
+import com.dayu.pipirrapp.bean.net.OrderDetailResult;
 import com.dayu.pipirrapp.bean.net.OrderListResult;
 import com.dayu.pipirrapp.databinding.FragmentOrderBinding;
 import com.dayu.pipirrapp.net.ApiManager;
@@ -23,6 +34,7 @@
 import com.dayu.pipirrapp.net.Constants;
 import com.dayu.pipirrapp.net.subscribers.SubscriberListener;
 import com.dayu.pipirrapp.utils.CommonKeyName;
+import com.dayu.pipirrapp.utils.MyLog;
 import com.dayu.pipirrapp.utils.ToastUtil;
 import com.jeremyliao.liveeventbus.LiveEventBus;
 import com.scwang.smart.refresh.footer.ClassicsFooter;
@@ -30,6 +42,7 @@
 import com.scwang.smart.refresh.layout.api.RefreshLayout;
 
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -41,8 +54,8 @@
  * 澶囨敞锛氶椤�
  */
 public class OrderFragment extends BaseFragment {
-
-
+    public static final int RESULT_REFRESH = 1001;
+    private static final String CHANNEL_ID = "order_channel";
     private final int STATE_DONE = 2;
     private final int STATE_UNDONE = 1;
 
@@ -54,6 +67,21 @@
     int state = 1;
     List<OrderListResult.Data> recordsListDone = new ArrayList<>();
     List<OrderListResult.Data> recordsList = new ArrayList<>();
+    RefreshLayout myRefreshLayout;
+    //鏈�鍚庡埛鏂扮殑鏃堕棿
+    long lastRefreshDate;
+    //鏄剧ず绾㈢偣鐨勬椂闂�
+    long showRedlotDate;
+
+
+    private ActivityResultLauncher<Intent> activityResultLauncher =
+            registerForActivityResult(
+                    new ActivityResultContracts.StartActivityForResult(),
+                    result -> {
+                        if (result.getResultCode() == RESULT_REFRESH) {
+                            myRefreshLayout.autoRefresh();
+                        }
+                    });
 
     /**
      * 璁剧疆澶╂皵鐩稿叧淇℃伅
@@ -81,12 +109,15 @@
     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
         binding = FragmentOrderBinding.inflate(inflater, container, false);
         initView();
-        //鑾峰彇瀹氫綅鏈嶅姟浼犺繃鏉ョ殑鍧愭爣鐐�
-        LiveEventBus.get(CommonKeyName.MQTTData).observeForever(o -> {
-
+        //mq浼犳潵鐨勬秷鎭�
+        LiveEventBus.get(CommonKeyName.MQTTData).observeForever(new Observer<Object>() {
+            @Override
+            public void onChanged(Object o) {
+                showRedlotDate = new Date().getTime();
+                binding.redDotImg.setVisibility(View.VISIBLE);
+                getMarkerData((String) o);
+            }
         });
-
-
         return binding.getRoot();
     }
 
@@ -104,12 +135,18 @@
             state = STATE_DONE;//1 鏈紑濮� 2 宸插畬鎴�
         });
         //鏈畬鎴�
-        RefreshLayout myRefreshLayout = (RefreshLayout) binding.refreshLayout;
+        myRefreshLayout = (RefreshLayout) binding.refreshLayout;
         myRefreshLayout.setRefreshHeader(new ClassicsHeader(this.getContext()));
         myRefreshLayout.setRefreshFooter(new ClassicsFooter(this.getContext()));
 
         myRefreshLayout.setOnRefreshListener(refreshlayout -> {
 //                refreshlayout.finishRefresh(2000/*,false*/);//浼犲叆false琛ㄧず鍒锋柊澶辫触
+            lastRefreshDate = new Date().getTime();
+            if (lastRefreshDate > showRedlotDate) {
+                //娑堥櫎绾㈢偣
+                LiveEventBus.get(CommonKeyName.RedLotRefresh).post(true);
+                binding.redDotImg.setVisibility(View.GONE);
+            }
             recordsList.clear();
             page = 0;
             getMarkerData(refreshlayout, true, STATE_UNDONE);
@@ -147,8 +184,20 @@
 
 
     @Override
-    public void onStart() {
-        super.onStart();
+    public void onResume() {
+        super.onResume();
+        MyLog.d("OrderFragment>>onResume");
+        if (showRedlotDate > lastRefreshDate) {
+            binding.redDotImg.setVisibility(View.VISIBLE);
+        } else {
+            binding.redDotImg.setVisibility(View.GONE);
+        }
+    }
+
+    @Override
+    public void onPause() {
+        super.onPause();
+        MyLog.d("OrderFragment>>onPause");
     }
 
     /**
@@ -181,7 +230,7 @@
                             } else {
                                 recordsListDone.addAll(t.getContent().getObj());
                             }
-                            if (t.getContent().getObj().size() < pageSize) {
+                            if (t.getContent().getPageTotal() == page) {
                                 refreshlayout.finishLoadMoreWithNoMoreData();
                             }
                         } else {
@@ -202,8 +251,6 @@
                 } else {
                     mDoneAdapter.notifyDataSetChanged();
                 }
-
-
             }
 
         });
@@ -215,7 +262,7 @@
         if (!TextUtils.isEmpty(proResultId)) {
             intent.putExtra("proResultId", proResultId);
         }
-        startActivity(intent);
+        activityResultLauncher.launch(intent);
     }
 
 
@@ -227,20 +274,20 @@
 
     void chooseStateView(boolean state) {
         if (state) {
-            binding.manageStateProgress.setTextColor(getResources().getColor(R.color.title_color));
-            binding.manageStateProgress.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit));
+            binding.manageStateProgress.setTextColor(getResources().getColor(R.color.title_color, null));
+            binding.manageStateProgress.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit, null));
             binding.manageStateProgress.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
-            binding.manageStateFinish.setTextColor(getResources().getColor(R.color.black));
-            binding.manageStateFinish.setBackground(getResources().getDrawable(R.color.title_choose_bg));
+            binding.manageStateFinish.setTextColor(getResources().getColor(R.color.black, null));
+            binding.manageStateFinish.setBackground(getResources().getDrawable(R.color.title_choose_bg, null));
             binding.manageStateFinish.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
             binding.refreshLayout.setVisibility(View.VISIBLE);
             binding.refreshLayoutDone.setVisibility(View.GONE);
         } else {
-            binding.manageStateFinish.setTextColor(getResources().getColor(R.color.title_color));
-            binding.manageStateFinish.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit));
+            binding.manageStateFinish.setTextColor(getResources().getColor(R.color.title_color, null));
+            binding.manageStateFinish.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit, null));
             binding.manageStateFinish.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
-            binding.manageStateProgress.setTextColor(getResources().getColor(R.color.black));
-            binding.manageStateProgress.setBackground(getResources().getDrawable(R.color.title_choose_bg));
+            binding.manageStateProgress.setTextColor(getResources().getColor(R.color.black, null));
+            binding.manageStateProgress.setBackground(getResources().getDrawable(R.color.title_choose_bg, null));
             binding.manageStateProgress.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
             binding.refreshLayout.setVisibility(View.GONE);
             binding.refreshLayoutDone.setVisibility(View.VISIBLE);
@@ -248,4 +295,57 @@
     }
 
 
+    /**
+     * 鍒涘缓宸ュ崟鎻愮ず閫氱煡
+     *
+     * @param notifucId
+     * @param data
+     */
+    private void creatOrderNotification(int notifucId, String data, String workOrderId) {
+        NotificationManager notificationManager = (NotificationManager) requireContext().getSystemService(Context.NOTIFICATION_SERVICE);
+        // 鍒涘缓NotificationChannel锛堜粎閽堝Android O鍙婁互涓婄増鏈級
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+            CharSequence name = getString(R.string.channel_name);
+            String description = getString(R.string.channel_description);
+            int importance = NotificationManager.IMPORTANCE_DEFAULT;
+            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
+            channel.setDescription(description);
+            // 娉ㄥ唽閫氶亾
+            notificationManager.createNotificationChannel(channel);
+        }
+
+        Intent notificationIntent = new Intent(this.getContext(), OrderDetailActivity.class);
+        notificationIntent.putExtra("workOrderId", workOrderId);
+        int requestCode = workOrderId.hashCode(); // 浣跨敤workOrderId鐨勫搱甯岀爜浣滀负requestCode
+        PendingIntent pendingIntent = PendingIntent.getActivity(this.getContext(), requestCode, notificationIntent, PendingIntent.FLAG_MUTABLE);
+        Notification notification = new NotificationCompat.Builder(this.getContext(), CHANNEL_ID)
+                .setContentTitle("鏂板伐鍗�")
+                .setContentText(data)
+                .setSmallIcon(R.mipmap.ic_launcher)
+                .setContentIntent(pendingIntent)
+                .setAutoCancel(true) // 鐐瑰嚮閫氱煡鍚庤嚜鍔ㄦ秷澶�
+                .setDefaults(Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE)// 浣跨敤榛樿鐨勯煶鏁堝拰闇囧姩
+                .build();
+        notificationManager.notify(notifucId, notification);
+
+    }
+
+    /**
+     * 鑾峰彇宸ュ崟璇︽儏
+     */
+    private void getMarkerData(String workOrderId) {
+        Map<String, Object> params = new HashMap<>();
+        params.put("workOrderId", workOrderId);
+        ApiManager.getInstance().requestGetHideLoading(this.getContext(), Constants.BASE_URL + "/app/workOrder/getOneWorkOrder", OrderDetailResult.class, params, new SubscriberListener<BaseResponse<OrderDetailResult>>() {
+            @Override
+            public void onNext(BaseResponse<OrderDetailResult> t) {
+                if (t.isSuccess()) {
+                    if (t.isSuccess()) {
+                        creatOrderNotification(workOrderId.hashCode(), t.getContent().getTaskType(), workOrderId);
+                    }
+                }
+            }
+        });
+    }
+
 }

--
Gitblit v1.8.0