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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
package cn.flightfeather.thirdapp.module.inspectioninfo;
 
 
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
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 com.ping.greendao.gen.DaoSession;
import com.ping.greendao.gen.MediafileDao;
 
import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
 
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.activity.PhotoViewerActivity;
import cn.flightfeather.thirdapp.adapter.AllRecyclerViewAdapter;
import cn.flightfeather.thirdapp.bean.entity.Mediafile;
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo;
import cn.flightfeather.thirdapp.util.Constant;
import cn.flightfeather.thirdapp.util.DateFormatter;
import cn.flightfeather.thirdapp.util.Domain;
import cn.flightfeather.thirdapp.util.file.FileUtil;
import cn.flightfeather.thirdapp.util.photo.PhotoUtil;
 
/**
 * 2019.1.25
 * @author riku
 * 本fragment有两种问题状态,已整改{@link Constant#UNCHANGED}和未整改{@link Constant#CHANGED}
 */
public class ProblemChangeFragment extends Fragment {
    //<editor-fold desc="全局变量">
    public static final String ARG_PARAM1 = "problems";
    public static final String ARG_PARAM2 = "fragmentType";
    public static final int REQUEST_CODE_SUBMIT = 11;
 
    private CommonApplication application;
    private ArrayList<ProblemlistVo> problemlistVos = new ArrayList<>();
    private String fragmentType;
    private MediafileDao mediafileDao;
    private Unbinder unbinder;
    //</editor-fold>
 
    //<editor-fold desc="constructor">
    public ProblemChangeFragment() {
        // Required empty public constructor
    }
 
    public static ProblemChangeFragment newInstance(ArrayList<ProblemlistVo> problemlistVos, String fragmentType) {
        ProblemChangeFragment fragment = new ProblemChangeFragment();
        Bundle args = new Bundle();
        args.putSerializable(ARG_PARAM1, problemlistVos);
        args.putString(ARG_PARAM2, fragmentType);
        fragment.setArguments(args);
        return fragment;
    }
    //</editor-fold>
 
    //<editor-fold desc="lifecycle">
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            problemlistVos = (ArrayList<ProblemlistVo>) getArguments().getSerializable(ARG_PARAM1);
            fragmentType = getArguments().getString(ARG_PARAM2);
        }
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_rank_content, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }
 
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        application = (CommonApplication) getActivity().getApplication();
        initDataBase();
        initView();
    }
 
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //整改完成后,刷新问题列表
        if (requestCode == REQUEST_CODE_SUBMIT
                && resultCode == Activity.RESULT_OK) {
            ProblemlistVo p = (ProblemlistVo) data.getSerializableExtra(ProblemChangeDetailActivity.ARG_PROBLEM);
            ProblemChangeActivity problemChangeActivity = (ProblemChangeActivity) getActivity();
            if (problemChangeActivity != null) {
                problemChangeActivity.refreshProblemList(p);
                problemChangeActivity.refreshViewPager();
            }
        }
    }
 
 
    @Override
    public void onDestroy() {
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
    //</editor-fold>
 
    //<editor-fold desc="初始化数据库">
    private void initDataBase() {
        DaoSession daoSession = application.getDaoSession();
        mediafileDao = daoSession.getMediafileDao();
    }
    //</editor-fold>
 
    //<editor-fold desc="initView">
    @BindView(R.id.rv_content) RecyclerView rv_rank_content;
    @BindView(R.id.image_problem_complete) ImageView image_problem_complete;
    AllRecyclerViewAdapter<ProblemlistVo> adapter;
 
    private void initView() {
        adapter = new AllRecyclerViewAdapter<ProblemlistVo>(problemlistVos, R.layout.item_problem_change_pollution_scene, getActivity()) {
            @Override
            public void bindView(AllRecyclerViewAdapter.MyViewHolder holder, ProblemlistVo obj, boolean isSelected, int position) {
                String checkStatus = getString(R.string.unchanged);
                switch (obj.getExtension3()) {
                    case Domain.CHANGE_UNCHECKED:
                        checkStatus = getString(R.string.change_unCheck);
                        break;
                    case Domain.CHANGE_CHECK_PASS:
                        checkStatus = getString(R.string.change_pass);
                        break;
                    case Domain.CHANGE_CHECK_FAIL:
                        checkStatus = getString(R.string.change_fail);
                        break;
                }
                holder.setText(R.id.tv_problem_type_name, obj.getTypename())
                        .setText(R.id.tv_problem_time, DateFormatter.dateTimeFormat.format(obj.getTime()))
                        .setText(R.id.tv_problem_location, obj.getLocation())
                        .setText(R.id.tv_problem_name, obj.getProblemname())
                        .setText(R.id.txt_change_check, checkStatus);
 
                //<editor-fold desc="根据是否承诺整改显示不同内容">
                Date deadline_tmp = null;
                if (obj.getExtension1() != null) {
                    try {
                        deadline_tmp = DateFormatter.dateFormat.parse(obj.getExtension1());
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTime(deadline_tmp);
                        calendar.set(Calendar.HOUR_OF_DAY, 23);
                        calendar.set(Calendar.MINUTE, 59);
                        calendar.set(Calendar.SECOND, 59);
                        deadline_tmp = calendar.getTime();
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                }
                if (fragmentType.equals(Constant.UNCHANGED) && !obj.getIschanged()) {
                    generateView1(holder, obj, isSelected, position, deadline_tmp);
                } else if (fragmentType.equals(Constant.CHANGED) && obj.getIschanged()) {
                    generateView2(holder, obj, isSelected, position, deadline_tmp);
                }
                //</editor-fold>
 
                ProblemChangeDetailActivity.TextData textData
                        = new ProblemChangeDetailActivity.TextData(
                        holder.getText(R.id.text_deadline_status),
                        holder.getText(R.id.text_remaining_days),
                        holder.getText(R.id.text_promise),
                        holder.isSelected(R.id.image_promise),
                        holder.getText(R.id.text_change_deadline),
                        holder.getVisibility(R.id.text_change_deadline),
                        holder.getVisibility(R.id.cl_deadline)
                );
                holder.setOnClickListener(R.id.cl_content ,new OnProblemDetailClickListener(obj, fragmentType, textData));
                holder.setOnClickListener(R.id.image_goto_change ,new OnProblemDetailClickListener(obj, fragmentType, textData));
 
                //<editor-fold desc="初始化图片的显示及点击事件">
                List<ImageView> imageViewProblemList = new ArrayList<>();
                imageViewProblemList.add((ImageView) holder.getViewById(R.id.iv_problem_photo1));
                imageViewProblemList.add((ImageView) holder.getViewById(R.id.iv_problem_photo2));
                imageViewProblemList.add((ImageView) holder.getViewById(R.id.iv_problem_photo3));
                List<Mediafile> mediafileList = mediafileDao.queryBuilder()
                        .where(MediafileDao.Properties.Businessguid.eq(obj.getGuid()))
                        .where(MediafileDao.Properties.Remark.eq("未上传"))
                        .list();
                if (obj.getMediafileList() != null) {
                    mediafileList.addAll(obj.getMediafileList());
                }
                //将图片加载至对应的imageView上
                List<Mediafile> showingFilelist = new ArrayList<>();
                for (Mediafile mediafile : mediafileList) {
                    if (!mediafile.getIschanged()) {
                        showingFilelist.add(mediafile);
                    }
                }
                int photoCount = PhotoUtil.loadImage(showingFilelist, imageViewProblemList, getActivity());
                //由于RecycleView的复用特性,没有添加图片的imageView必须设置为空白图片
                if (photoCount < imageViewProblemList.size()) {
                    for (int i = imageViewProblemList.size() - 1; i >= photoCount; i--) {
                        imageViewProblemList.get(i).setImageResource(R.drawable.icon_add_photo_blank);
                    }
                }
                //添加图片放大点击事件
                List<File> files = new ArrayList<>();
                for (Mediafile m :
                        showingFilelist) {
                    File tempFile = FileUtil.getFileFromMediaFile(m);
                    files.add(tempFile);
                }
                for (int i = 0; i < photoCount; i++) {
                    imageViewProblemList.get(i)
                            .setOnClickListener(PhotoUtil.OnPhotoViewClickListener(getActivity(),
                                    files,
                                    i,
                                    showingFilelist,
                                    PhotoViewerActivity.PROBLEM_PHOTO,
                                    false));
                }
                //</editor-fold>
            }
        };
 
        LinearLayoutManager lm = new LinearLayoutManager(getActivity());
        lm.setOrientation(LinearLayoutManager.VERTICAL);
        rv_rank_content.setLayoutManager(lm);
        rv_rank_content.setAdapter(adapter);
        //设置没有内容时的展示图片
        if (fragmentType.equals(Constant.UNCHANGED)) {
            image_problem_complete.setImageResource(R.drawable.ic_problem_complete);
        }else{
            image_problem_complete.setImageResource(R.drawable.ic_problem_uncomplete);
        }
        if (this.problemlistVos.isEmpty()) {
            image_problem_complete.setVisibility(View.VISIBLE);
        } else {
            image_problem_complete.setVisibility(View.GONE);
        }
    }
 
    /**
     * 当前状态是展示“未整改”的问题{@link #fragmentType} = {@link Constant#UNCHANGED}
     * @param holder
     * @param obj
     * @param isSelected
     * @param position
     */
    private void generateView1(AllRecyclerViewAdapter.MyViewHolder holder, ProblemlistVo obj, boolean isSelected, int position, Date deadline_tmp) {
        //客户已承诺整改,截止日期存在,则显示
        String deadline = obj.getExtension1();
        if (deadline != null && !deadline.equals("") && deadline_tmp != null) {
            deadline = "截至" + " " + deadline;
            holder.setSelected(R.id.image_promise, true)
                    .setText(R.id.text_promise, R.string.change_promised)
                    .setVisibility(R.id.text_change_deadline, View.VISIBLE)
                    .setText(R.id.text_change_deadline, deadline);
//                            .setOnClickListener(R.id.image_goto_change, new OnProblemDetailClickListener())
//                            .setOnClickListener(R.id.cl_content, new OnProblemDetailClickListener())
            //region 判断整改还剩余或已逾期几天
            Date problemTime = obj.getTime();
            if (problemTime.before(deadline_tmp)) {
                Date now = new Date();
                int days;
                //整改没有逾期
                if (deadline_tmp.getTime() > now.getTime()) {
                    days = (int) Math.ceil((deadline_tmp.getTime() - now.getTime()) / 1000 / 60 / 60 / 24);
                    holder.setText(R.id.text_deadline_status, R.string.remaining)
                            .setText(R.id.text_remaining_days, String.valueOf(days));
                }
                //整改已逾期
                else {
                    days = (int) Math.ceil((now.getTime() - deadline_tmp.getTime()) / 1000 / 60 / 60 / 24);
                    holder.setText(R.id.text_deadline_status, R.string.overdue)
                            .setText(R.id.text_remaining_days, String.valueOf(days));
                }
            }
            holder.setVisibility(R.id.cl_deadline, View.VISIBLE);
            //endregion
        }
        //客户未承诺整改,截止日期不存在,不显示
        else {
            holder.setSelected(R.id.image_promise, false)
                    .setText(R.id.text_promise, R.string.change_refused)
                    .setVisibility(R.id.text_change_deadline, View.INVISIBLE)
                    .setVisibility(R.id.cl_deadline, View.INVISIBLE);
        }
    }
 
    /**
     * 当前状态是展示“已整改”的问题{@link #fragmentType} = {@link Constant#CHANGED}
     * @param holder
     * @param obj
     * @param isSelected
     * @param position
     */
    private void generateView2(AllRecyclerViewAdapter.MyViewHolder holder, ProblemlistVo obj, boolean isSelected, int position, Date deadline_tmp) {
        holder.setVisibility(R.id.cl_deadline, View.INVISIBLE);
//                            .setOnClickListener(R.id.image_goto_change, new OnProblemDetailClickListener())
//                            .setOnClickListener(R.id.cl_content, new OnProblemDetailClickListener())
        //已承诺,有截止时间
        if (deadline_tmp != null) {
            //整改时间有记录,按照记录显示是否逾期
            if (obj.getChangedtime() != null) {
                //整改时间在承诺整改时间之前
                if (obj.getChangedtime().before(deadline_tmp)) {
                    holder.setSelected(R.id.image_promise, true)
                            .setText(R.id.text_promise, R.string.change_in_time)
                            .setVisibility(R.id.text_change_deadline, View.GONE);
                }
                //整改时间在承诺整改时间之后
                else {
                    holder.setSelected(R.id.image_promise, false)
                            .setText(R.id.text_promise, R.string.change_over_time);
                    int days2 = (int) Math.ceil((obj.getChangedtime().getTime() - deadline_tmp.getTime()) / 1000 / 60 / 60 / 24);
                    String s = "逾期" + days2 + "天";
                    holder.setVisibility(R.id.text_change_deadline, View.VISIBLE)
                            .setText(R.id.text_change_deadline, s);
                }
            }
            //整改时间无记录,直接显示已整改
            else {
                holder.setSelected(R.id.image_promise, true)
                        .setText(R.id.text_promise, R.string.changed)
                        .setVisibility(R.id.text_change_deadline, View.GONE);
            }
        }
        //没有承诺,没有截止时间
        else {
            holder.setSelected(R.id.image_promise, true)
                    .setText(R.id.text_promise, R.string.changed)
                    .setVisibility(R.id.text_change_deadline, View.GONE);
        }
 
    }
    //</editor-fold>
 
    //<editor-fold desc="点击事件">
    class OnProblemDetailClickListener implements View.OnClickListener {
 
        private ProblemlistVo problemlistVo;
        private String problemType;
        private ProblemChangeDetailActivity.TextData textData;
 
        private View cl_1;
        private View tv_problem_name;
        private View ll_problem_photo;
        private View tv_problem_type_name;
 
        public OnProblemDetailClickListener(ProblemlistVo problemlistVo, String problemType, ProblemChangeDetailActivity.TextData textData) {
            this.problemlistVo = problemlistVo;
            this.problemType = problemType;
            this.textData = textData;
            View itemView = LayoutInflater.from(getActivity()).inflate(R.layout.item_problem_change_pollution_scene, null);
            cl_1 = itemView.findViewById(R.id.cl_1);
            tv_problem_name = itemView.findViewById(R.id.tv_problem_name);
            ll_problem_photo = itemView.findViewById(R.id.ll_problem_photo);
            tv_problem_type_name = itemView.findViewById(R.id.tv_problem_type_name);
        }
 
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getActivity(), ProblemChangeDetailActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable(ProblemChangeDetailActivity.ARG_PROBLEM, problemlistVo);
            bundle.putString(ProblemChangeDetailActivity.ARG_PROBLEM_TYPE, problemType);
            bundle.putSerializable(ProblemChangeDetailActivity.ARG_TEXT_DATA, textData);
            intent.putExtras(bundle);
 
            ActivityOptions optionsCompat = ActivityOptions.makeSceneTransitionAnimation(getActivity());
            startActivityForResult(intent, REQUEST_CODE_SUBMIT, optionsCompat.toBundle());
        }
    }
    //</editor-fold>
 
    //<editor-fold desc="更新列表">
    public void notifyDataSetChanged() {
//        this.problemlistVos.clear();
//        this.problemlistVos.addAll(p);
        if (this.problemlistVos.isEmpty()) {
            image_problem_complete.setVisibility(View.VISIBLE);
        } else {
            image_problem_complete.setVisibility(View.GONE);
        }
        adapter.notifyDataSetChanged();
    }
    //</editor-fold>
}