wxr
2022-11-17 dc8cb616469c080deaa192b9379fd59be2187f36
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package com.lechange.demo.ui;
 
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
 
import com.common.openapi.ClassInstanceManager;
import com.common.openapi.DeviceRecordService;
import com.common.openapi.IGetDeviceInfoCallBack;
import com.common.openapi.MethodConst;
import com.common.openapi.entity.DeviceDetailListData;
import com.common.openapi.entity.LocalRecordsData;
import com.common.openapi.entity.RecordListData;
import com.common.openapi.entity.RecordsData;
import com.lechange.demo.R;
import com.lechange.demo.adapter.DeviceRecordListAdapter;
import com.lechange.demo.tools.DateHelper;
import com.lechange.demo.tools.DialogUtils;
import com.lechange.demo.view.LcPullToRefreshRecyclerView;
import com.lechange.pulltorefreshlistview.Mode;
import com.lechange.pulltorefreshlistview.PullToRefreshBase;
import com.mm.android.deviceaddmodule.mobilecommon.utils.LogUtil;
import com.mm.android.deviceaddmodule.mobilecommon.utils.TimeUtils;
import com.mm.android.deviceaddmodule.mobilecommon.utils.UIUtils;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
 
public class DeviceLocalRecordListFragment extends Fragment implements View.OnClickListener, IGetDeviceInfoCallBack.IDeviceLocalRecordCallBack, PullToRefreshBase.OnRefreshListener2 {
    private static final String TAG = DeviceLocalRecordListFragment.class.getSimpleName();
    private Bundle arguments;
    private String searchDate;
    private long searchDate1;
    private DeviceDetailListData.ResponseData.DeviceListBean deviceListBean;
    private RecyclerView recyclerView;
    private TextView tvMonthDay;
    private long oneDay = 24 * 60 * 60 * 1000;
    private List<RecordListData> recordListDataList = new ArrayList<>();
    private DeviceRecordListAdapter deviceRecordListAdapter;
    private DeviceRecordService deviceRecordService = ClassInstanceManager.newInstance().getDeviceRecordService();
    private static DeviceLocalRecordListFragment fragment;
    private DeviceRecordListActivity deviceRecordListActivity;
    private LcPullToRefreshRecyclerView deviceList;
    private int pageSize = 30;
    private int pageIndex = 1;
    private String time = "";
    private TextView tvToday;
    private ImageView  mNextDayTv;
    public static DeviceLocalRecordListFragment newInstance() {
        fragment = new DeviceLocalRecordListFragment();
        return fragment;
    }
 
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        arguments = getArguments();
        searchDate = DateHelper.dateFormat(new Date(System.currentTimeMillis()));
        searchDate1 = DateHelper.parseMills(searchDate + " 00:00:00");
        deviceRecordListActivity = (DeviceRecordListActivity) getActivity();
        deviceRecordListActivity.llEdit.setVisibility(View.GONE);
    }
 
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_device_cloud_list, container, false);
    }
 
    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initView(view);
        if (arguments == null) {
            return;
        }
        deviceListBean = (DeviceDetailListData.ResponseData.DeviceListBean) arguments.getSerializable(MethodConst.ParamConst.deviceDetail);
        if (deviceListBean == null) {
            return;
        }
        initData();
    }
 
    private void initView(View view) {
        view.findViewById(R.id.iv_day_pre).setOnClickListener(this);
        mNextDayTv =view.findViewById(R.id.iv_day_next);
        tvMonthDay = view.findViewById(R.id.tv_month_day);
        deviceList = view.findViewById(R.id.record_list);
        tvToday = view.findViewById(R.id.tv_today);
        deviceList.setOnRefreshListener(this);
        mNextDayTv.setOnClickListener(this);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                refreshMode(Mode.PULL_FROM_START);
                refreshState(true);
            }
        }, 200);
        recyclerView = deviceList.getRefreshableView();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
    }
 
    private void refreshState(boolean refresh) {
        if (refresh) {
            deviceList.setRefreshing(true);
        } else {
            deviceList.onRefreshComplete();
        }
    }
 
    private void refreshMode(Mode mode) {
        deviceList.setMode(mode);
    }
 
    private void initData() {
        tvMonthDay.setText(searchDate);
        mCalendar.setTime(TimeUtils.stringToDate(searchDate,"yyyy-MM-dd HH:mm:ss"));
        UIUtils.setEnabledEX(TimeUtils.isBeforeToday(mCalendar), mNextDayTv);
    }
 
    protected Calendar mCalendar = Calendar.getInstance();
 
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_day_pre) {
            searchDate1 = searchDate1 - oneDay;
            searchDate = DateHelper.dateFormat(new Date(searchDate1));
            tvMonthDay.setText(searchDate);
            mCalendar.setTime(new Date(searchDate1));
            UIUtils.setEnabledEX(TimeUtils.isBeforeToday(mCalendar), mNextDayTv);
            initLocalRecord(false);
        } else if (id == R.id.iv_day_next) {
            searchDate1 = searchDate1 + oneDay;
            searchDate = DateHelper.dateFormat(new Date(searchDate1));
            tvMonthDay.setText(searchDate);
            mCalendar.setTime(new Date(searchDate1));
            UIUtils.setEnabledEX(TimeUtils.isBeforeToday(mCalendar), mNextDayTv);
            initLocalRecord(false);
        }
    }
 
 
 
    @Override
    public void deviceLocalRecord(LocalRecordsData.Response result) {
        if (!isAdded()){
            return;
        }
        tvToday.setVisibility(View.GONE);
        recyclerView.setVisibility(View.VISIBLE);
        DialogUtils.dismiss();
        refreshState(false);
        if (result != null && result.data != null && result.data.records != null && result.data.records.size() > 0) {
            if (result.data.records.size() >= pageSize) {
                refreshMode(Mode.BOTH);
            } else {
                refreshMode(Mode.PULL_FROM_START);
            }
            recordListDataList = dealLocalRecord(result);
        } else {
            if (pageIndex == 1){
                tvToday.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
            }
            refreshMode(Mode.PULL_FROM_START);
        }
        showList();
    }
 
    @Override
    public void onError(Throwable throwable) {
        if (!isAdded()){
            return;
        }
        LogUtil.errorLog(TAG, "error", throwable);
        DialogUtils.dismiss();
        refreshState(false);
        refreshMode(Mode.PULL_FROM_START);
        Toast.makeText(getContext(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
        pageIndex = 1;
        time = "";
        recordListDataList.clear();
        showList();
    }
 
    private void initLocalRecord(boolean isLoadMore) {
        if (isLoadMore) {
            pageIndex = pageIndex + pageSize;
        } else {
            pageIndex = 1;
            time = "";
            recordListDataList.clear();
        }
        DialogUtils.show(getActivity());
        getLocalData();
      /*  deviceRecordService.querySDUse(deviceListBean.deviceId, new IGetDeviceInfoCallBack.ICommon<String>() {
 
            @Override
            public void onCommonBack(String response) {
                if (!"empty".equals(response)){
                    getLocalData();
                }else{
                    DialogUtils.dismiss();
                    refreshState(false);
                    tvToday.setVisibility(View.VISIBLE);
                    tvToday.setText(getActivity().getResources().getString(R.string.lc_demo_device_local_sd));
                }
            }
 
            @Override
            public void onError(Throwable throwable) {
                Toast.makeText(getActivity(),throwable.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });*/
    }
 
    private void getLocalData(){
        LocalRecordsData localRecordsData = new LocalRecordsData();
        localRecordsData.data.deviceId = deviceListBean.deviceId;
        localRecordsData.data.channelId = deviceListBean.channels.get(deviceListBean.checkedChannel).channelId;
        localRecordsData.data.beginTime = searchDate + " 00:00:00";
        localRecordsData.data.endTime = searchDate + " 23:59:59";
        localRecordsData.data.type = "All";
        localRecordsData.data.queryRange =  pageIndex + "-" + (pageIndex + pageSize - 1);
        deviceRecordService.queryLocalRecords(localRecordsData, this);
    }
 
    private void showList() {
        if (deviceRecordListAdapter == null) {
            deviceRecordListAdapter = new DeviceRecordListAdapter(getContext(), recordListDataList);
            recyclerView.setAdapter(deviceRecordListAdapter);
        } else {
            deviceRecordListAdapter.notifyDataSetChanged();
        }
        deviceRecordListAdapter.setEditClickListener(new DeviceRecordListAdapter.EditClickListener() {
            @Override
            public void edit(int outPosition, int innerPosition) {
                LogUtil.debugLog(TAG, outPosition + "..." + innerPosition);
                Bundle bundle = new Bundle();
                bundle.putSerializable(MethodConst.ParamConst.deviceDetail, deviceListBean);
                bundle.putSerializable(MethodConst.ParamConst.recordData, recordListDataList.get(outPosition).recordsData.get(innerPosition));
                bundle.putInt(MethodConst.ParamConst.recordType, MethodConst.ParamConst.recordTypeLocal);
                Intent intent = new Intent(getContext(), DeviceRecordPlayActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
    }
 
    private List<RecordListData> dealLocalRecord(LocalRecordsData.Response result) {
        for (LocalRecordsData.ResponseData.RecordsBean recordsBean : result.data.records) {
            String innerTime = recordsBean.beginTime.substring(11, 13);
            RecordsData a = new RecordsData();
            a.recordType = 1;
            a.recordId = recordsBean.recordId;
            a.channelID = recordsBean.channelID;
            a.beginTime = recordsBean.beginTime;
            a.endTime = recordsBean.endTime;
            a.fileLength = recordsBean.fileLength;
            a.type = recordsBean.type;
            if (!innerTime.equals(time)) {
                RecordListData r = new RecordListData();
                r.period = innerTime + ":00";
                r.recordsData = new ArrayList<>();
                r.recordsData.add(a);
                recordListDataList.add(r);
                time = innerTime;
            } else {
                RecordListData b = recordListDataList.get(recordListDataList.size() - 1);
                b.recordsData.add(a);
            }
        }
        LogUtil.debugLog(TAG, recordListDataList.size() + "");
        return recordListDataList;
    }
 
    @Override
    public void onPullDownToRefresh(PullToRefreshBase pullToRefreshBase) {
        initLocalRecord(false);
 
    }
 
    @Override
    public void onPullUpToRefresh(PullToRefreshBase pullToRefreshBase) {
        initLocalRecord(true);
    }
}