管灌系统巡查员智能手机App
app/src/main/java/com/dayu/pipirrapp/fragment/OrderFragment.java
@@ -1,7 +1,13 @@
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;
@@ -12,6 +18,7 @@
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;
@@ -19,6 +26,7 @@
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;
@@ -26,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;
@@ -33,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;
@@ -45,7 +55,7 @@
 */
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;
@@ -58,6 +68,11 @@
    List<OrderListResult.Data> recordsListDone = new ArrayList<>();
    List<OrderListResult.Data> recordsList = new ArrayList<>();
    RefreshLayout myRefreshLayout;
    //最后刷新的时间
    long lastRefreshDate;
    //显示红点的时间
    long showRedlotDate;
    private ActivityResultLauncher<Intent> activityResultLauncher =
            registerForActivityResult(
@@ -98,7 +113,9 @@
        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();
@@ -124,6 +141,12 @@
        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);
@@ -161,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");
    }
    /**
@@ -239,20 +274,20 @@
    void chooseStateView(boolean state) {
        if (state) {
            binding.manageStateProgress.setTextColor(getResources().getColor(R.color.title_color,null));
            binding.manageStateProgress.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit,null));
            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,null));
            binding.manageStateFinish.setBackground(getResources().getDrawable(R.color.title_choose_bg,null));
            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,null));
            binding.manageStateFinish.setBackground(getResources().getDrawable(R.drawable.ic_choose_bg_whit,null));
            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,null));
            binding.manageStateProgress.setBackground(getResources().getDrawable(R.color.title_choose_bg,null));
            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);
@@ -260,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);
                    }
                }
            }
        });
    }
}