管灌系统巡查员智能手机App
zuoxiao
2024-12-23 5aa4aff7a2ea7953283cffccf3b1835931295d55
app/src/main/java/com/dayu/pipirrapp/fragment/OrderFragment.java
@@ -1,5 +1,7 @@
package com.dayu.pipirrapp.fragment;
import static androidx.core.content.ContextCompat.getSystemService;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
@@ -9,6 +11,8 @@
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.os.VibrationEffect;
import android.os.Vibrator;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
@@ -24,6 +28,7 @@
import com.dayu.pipirrapp.MyApplication;
import com.dayu.pipirrapp.R;
import com.dayu.pipirrapp.activity.MainActivity;
import com.dayu.pipirrapp.activity.OrderDetailActivity;
import com.dayu.pipirrapp.adapter.OrderAdapter;
import com.dayu.pipirrapp.bean.net.OrderDetailResult;
@@ -55,7 +60,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;
@@ -65,8 +70,8 @@
    int page = 1;
    int pageSize = 10;
    int state = 1;
    List<OrderListResult.Data> recordsListDone = new ArrayList<>();
    List<OrderListResult.Data> recordsList = new ArrayList<>();
    List<OrderDetailResult> recordsListDone = new ArrayList<>();
    List<OrderDetailResult> recordsList = new ArrayList<>();
    RefreshLayout myRefreshLayout;
    //最后刷新的时间
    long lastRefreshDate;
@@ -114,7 +119,7 @@
            @Override
            public void onChanged(Object o) {
                showRedlotDate = new Date().getTime();
                binding.redDotImg.setVisibility(View.VISIBLE);
//                binding.redDotImg.setVisibility(View.VISIBLE);
                getMarkerData((String) o);
            }
        });
@@ -124,7 +129,7 @@
    void initView() {
        //未完成
        binding.manageStateProgress.setOnClickListener(v -> {
        binding.manageStateProgressRL.setOnClickListener(v -> {
            chooseStateView(true);
            state = STATE_UNDONE;//1 未开始 2 已完成
@@ -145,7 +150,7 @@
            if (lastRefreshDate > showRedlotDate) {
                //消除红点
                LiveEventBus.get(CommonKeyName.RedLotRefresh).post(true);
                binding.redDotImg.setVisibility(View.GONE);
//                binding.redDotImg.setVisibility(View.GONE);
            }
            recordsList.clear();
            page = 0;
@@ -162,11 +167,10 @@
        binding.recyclerView.setAdapter(mAdapter);
        getMarkerData(myRefreshLayout, true, STATE_UNDONE);
        //已完成
        RefreshLayout myRefreshLayoutDone = (RefreshLayout) binding.refreshLayoutDone;
        RefreshLayout myRefreshLayoutDone = binding.refreshLayoutDone;
        myRefreshLayoutDone.setRefreshHeader(new ClassicsHeader(this.getContext()));
        myRefreshLayoutDone.setRefreshFooter(new ClassicsFooter(this.getContext()));
        myRefreshLayoutDone.setOnRefreshListener(refreshlayout -> {
//                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
            recordsListDone.clear();
            page = 0;
            getMarkerData(refreshlayout, true, STATE_DONE);
@@ -187,11 +191,11 @@
    public void onResume() {
        super.onResume();
        MyLog.d("OrderFragment>>onResume");
        if (showRedlotDate > lastRefreshDate) {
            binding.redDotImg.setVisibility(View.VISIBLE);
        } else {
            binding.redDotImg.setVisibility(View.GONE);
        }
//        if (showRedlotDate > lastRefreshDate) {
//            binding.redDotImg.setVisibility(View.VISIBLE);
//        } else {
//            binding.redDotImg.setVisibility(View.GONE);
//        }
    }
    @Override
@@ -223,7 +227,6 @@
            @Override
            public void onNext(BaseResponse<OrderListResult> t) {
                if (t.isSuccess()) {
                    if (t.isSuccess()) {
                        if (t.getContent().getObj() != null && t.getContent().getObj().size() > 0) {
                            if (state == STATE_UNDONE) {
                                recordsList.addAll(t.getContent().getObj());
@@ -235,7 +238,6 @@
                            }
                        } else {
                            refreshlayout.finishLoadMoreWithNoMoreData();
                        }
                    }
                } else {
                    ToastUtil.showToast(OrderFragment.this.getContext(), t.getMsg());
@@ -276,59 +278,27 @@
        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.manageStateProgressRL.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.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.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.manageStateProgressRL.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);
        }
    }
    /**
     * 创建工单提示通知
     *
     * @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);
    }
    /**
     * 获取工单详情
@@ -340,8 +310,12 @@
            @Override
            public void onNext(BaseResponse<OrderDetailResult> t) {
                if (t.isSuccess()) {
                    if (t.isSuccess()) {
                        creatOrderNotification(workOrderId.hashCode(), t.getContent().getTaskType(), workOrderId);
                    if (MainActivity.workerIddata != null) {
                        if (!MainActivity.workerIddata.containsKey(workOrderId)) {
                            LiveEventBus.get(CommonKeyName.CreateNotification).post(t.getContent());
                            recordsList.add(0, t.getContent());
                            mAdapter.notifyItemInserted(0);
                        }
                    }
                }
            }