riku
2022-01-20 5a0fff8095cd5356f57c181b7e7b820e0f7efacf
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
package cn.flightfeather.thirdappmodule.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.RequiresApi
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import butterknife.ButterKnife
import butterknife.Unbinder
import cn.flightfeather.thirdappmodule.CommonApplication
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.activity.PhotoViewerActivity
import cn.flightfeather.thirdappmodule.adapter.AllRecyclerViewAdapter
import cn.flightfeather.thirdappmodule.bean.entity.Mediafile
import cn.flightfeather.thirdappmodule.bean.vo.ProblemlistVo
import cn.flightfeather.thirdappmodule.module.inspectioninfo.ProblemChangeDetailActivity.TextData
import cn.flightfeather.thirdappmodule.util.Constant
import cn.flightfeather.thirdappmodule.util.DateFormatter
import cn.flightfeather.thirdappmodule.util.Domain
import cn.flightfeather.thirdappmodule.util.file.FileUtil
import cn.flightfeather.thirdappmodule.util.photo.PhotoUtil
import com.ping.greendao.gen.MediafileDao
import kotlinx.android.synthetic.main.fragment_rank_content.*
import java.io.File
import java.text.ParseException
import java.util.*
 
/**
 * 2019.1.25
 * @author riku
 * 本fragment有两种问题状态,已整改[Constant.UNCHANGED]和未整改[Constant.CHANGED]
 */
class ProblemChangeFragment : Fragment() {
    private var application: CommonApplication? = null
    private var problemlistVos = ArrayList<ProblemlistVo>()
    private var fragmentType: String? = null
    private var mediafileDao: MediafileDao? = null
    private var unbinder: Unbinder? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            problemlistVos = arguments!!.getSerializable(ARG_PARAM1) as ArrayList<ProblemlistVo>
            fragmentType = arguments!!.getString(ARG_PARAM2)
        }
    }
 
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_rank_content, container, false)
        unbinder = ButterKnife.bind(this, view)
        return view
    }
 
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        application = activity!!.application as CommonApplication
        initDataBase()
        initView()
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        //整改完成后,刷新问题列表
        if (requestCode == REQUEST_CODE_SUBMIT && resultCode == Activity.RESULT_OK && data != null) {
            val p = data.getSerializableExtra(ProblemChangeDetailActivity.ARG_PROBLEM) as ProblemlistVo
            val problemChangeActivity = activity as ProblemChangeActivity?
            if (problemChangeActivity != null) {
                problemChangeActivity.refreshProblemList(p)
                problemChangeActivity.refreshViewPager()
            }
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        if (unbinder != null) {
            unbinder!!.unbind()
        }
    }
 
    //初始化数据库
    private fun initDataBase() {
        val daoSession = application!!.daoSession
        mediafileDao = daoSession.mediafileDao
    }
 
    //initView
 
    var adapter: AllRecyclerViewAdapter<ProblemlistVo>? = null
    private fun initView() {
        adapter = object : AllRecyclerViewAdapter<ProblemlistVo>(problemlistVos, R.layout.item_problem_change_pollution_scene, activity) {
            override fun bindView(holder: MyViewHolder, obj: ProblemlistVo, isSelected: Boolean, position: Int) {
                var checkStatus = getString(R.string.unchanged)
                when (obj.extension3) {
                    Domain.CHANGE_UNCHECKED -> checkStatus = getString(R.string.change_unCheck)
                    Domain.CHANGE_CHECK_PASS -> checkStatus = getString(R.string.change_pass)
                    Domain.CHANGE_CHECK_FAIL -> checkStatus = getString(R.string.change_fail)
                }
                holder.setText(R.id.tv_problem_type_name, obj.typename)
                    .setText(R.id.tv_problem_time, DateFormatter.dateTimeFormat.format(obj.time))
                    .setText(R.id.tv_problem_location, obj.location)
                    .setText(R.id.tv_problem_name, obj.problemname)
                    .setText(R.id.txt_change_check, checkStatus)
 
                //<editor-fold desc="根据是否承诺整改显示不同内容">
                var deadline_tmp: Date? = null
                if (obj.extension1 != null) {
                    try {
                        deadline_tmp = DateFormatter.dateFormat.parse(obj.extension1)
                        val calendar = Calendar.getInstance()
                        calendar.time = deadline_tmp
                        calendar[Calendar.HOUR_OF_DAY] = 23
                        calendar[Calendar.MINUTE] = 59
                        calendar[Calendar.SECOND] = 59
                        deadline_tmp = calendar.time
                    } catch (e: ParseException) {
                        e.printStackTrace()
                    }
                }
                if (fragmentType == Constant.UNCHANGED && !obj.ischanged) {
                    generateView1(holder, obj, isSelected, position, deadline_tmp)
                } else if (fragmentType == Constant.CHANGED && obj.ischanged) {
                    generateView2(holder, obj, isSelected, position, deadline_tmp)
                }
                //</editor-fold>
                val textData = 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, OnProblemDetailClickListener(obj, fragmentType, textData))
                holder.setOnClickListener(R.id.image_goto_change, OnProblemDetailClickListener(obj, fragmentType, textData))
 
                //<editor-fold desc="初始化图片的显示及点击事件">
                val imageViewProblemList: MutableList<ImageView> = ArrayList()
                imageViewProblemList.add(holder.getViewById(R.id.iv_problem_photo1) as ImageView)
                imageViewProblemList.add(holder.getViewById(R.id.iv_problem_photo2) as ImageView)
                imageViewProblemList.add(holder.getViewById(R.id.iv_problem_photo3) as ImageView)
                val mediafileList = mediafileDao!!.queryBuilder()
                    .where(MediafileDao.Properties.Businessguid.eq(obj.guid))
                    .where(MediafileDao.Properties.Remark.eq("未上传"))
                    .list()
                if (obj.mediafileList != null) {
                    mediafileList.addAll(obj.mediafileList)
                }
                //将图片加载至对应的imageView上
                val showingFilelist: MutableList<Mediafile> = ArrayList()
                for (mediafile in mediafileList) {
                    if (!mediafile.ischanged) {
                        showingFilelist.add(mediafile)
                    }
                }
                val photoCount = PhotoUtil.loadImage(showingFilelist, imageViewProblemList, activity)
                //由于RecycleView的复用特性,没有添加图片的imageView必须设置为空白图片
                if (photoCount < imageViewProblemList.size) {
                    for (i in imageViewProblemList.size - 1 downTo photoCount) {
                        imageViewProblemList[i].setImageResource(R.drawable.icon_add_photo_blank)
                    }
                }
                //添加图片放大点击事件
                val files: MutableList<File> = ArrayList()
                for (m in showingFilelist) {
                    val tempFile = FileUtil.getFileFromMediaFile(m)
                    files.add(tempFile)
                }
                for (i in 0 until photoCount) {
                    imageViewProblemList[i]
                        .setOnClickListener(
                            PhotoUtil.OnPhotoViewClickListener(
                                activity,
                                files,
                                i,
                                showingFilelist,
                                PhotoViewerActivity.PROBLEM_PHOTO,
                                false
                            )
                        )
                }
                //</editor-fold>
            }
        }
        val lm = LinearLayoutManager(activity)
        lm.orientation = LinearLayoutManager.VERTICAL
        rv_content!!.layoutManager = lm
        rv_content!!.adapter = adapter
        //设置没有内容时的展示图片
        if (fragmentType == Constant.UNCHANGED) {
            image_problem_complete!!.setImageResource(R.drawable.ic_problem_complete)
        } else {
            image_problem_complete!!.setImageResource(R.drawable.ic_problem_uncomplete)
        }
        if (problemlistVos.isEmpty()) {
            image_problem_complete!!.visibility = View.VISIBLE
        } else {
            image_problem_complete!!.visibility = View.GONE
        }
    }
 
    /**
     * 当前状态是展示“未整改”的问题[.fragmentType] = [Constant.UNCHANGED]
     * @param holder
     * @param obj
     * @param isSelected
     * @param position
     */
    private fun generateView1(holder: AllRecyclerViewAdapter<ProblemlistVo>.MyViewHolder, obj: ProblemlistVo, isSelected: Boolean, position: Int, deadline_tmp: Date?) {
        //客户已承诺整改,截止日期存在,则显示
        var deadline = obj.extension1
        if (deadline != null && deadline != "" && 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 判断整改还剩余或已逾期几天
            val problemTime = obj.time
            if (problemTime.before(deadline_tmp)) {
                val now = Date()
                val days: Int
                //整改没有逾期
                if (deadline_tmp.time > now.time) {
                    days = Math.ceil(((deadline_tmp.time - now.time) / 1000 / 60 / 60 / 24).toDouble()).toInt()
                    holder.setText(R.id.text_deadline_status, R.string.remaining)
                        .setText(R.id.text_remaining_days, days.toString())
                } else {
                    days = Math.ceil(((now.time - deadline_tmp.time) / 1000 / 60 / 60 / 24).toDouble()).toInt()
                    holder.setText(R.id.text_deadline_status, R.string.overdue)
                        .setText(R.id.text_remaining_days, days.toString())
                }
            }
            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)
        }
    }
 
    /**
     * 当前状态是展示“已整改”的问题[.fragmentType] = [Constant.CHANGED]
     * @param holder
     * @param obj
     * @param isSelected
     * @param position
     */
    private fun generateView2(holder: AllRecyclerViewAdapter<ProblemlistVo>.MyViewHolder, obj: ProblemlistVo, isSelected: Boolean, position: Int, deadline_tmp: Date?) {
        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.changedtime != null) {
                //整改时间在承诺整改时间之前
                if (obj.changedtime.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)
                    val days2 = Math.ceil(((obj.changedtime.time - deadline_tmp.time) / 1000 / 60 / 60 / 24).toDouble()).toInt()
                    val 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)
        }
    }
 
    //点击事件
    internal inner class OnProblemDetailClickListener(private val problemlistVo: ProblemlistVo, private val problemType: String?, private val textData: TextData) : View.OnClickListener {
        private val cl_1: View
        private val tv_problem_name: View
        private val ll_problem_photo: View
        private val tv_problem_type_name: View
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        override fun onClick(v: View) {
            val intent = Intent(activity, ProblemChangeDetailActivity::class.java)
            val bundle = Bundle()
            bundle.putSerializable(ProblemChangeDetailActivity.ARG_PROBLEM, problemlistVo)
            bundle.putString(ProblemChangeDetailActivity.ARG_PROBLEM_TYPE, problemType)
            bundle.putSerializable(ProblemChangeDetailActivity.ARG_TEXT_DATA, textData)
            intent.putExtras(bundle)
            val optionsCompat = ActivityOptions.makeSceneTransitionAnimation(activity)
            startActivityForResult(intent, REQUEST_CODE_SUBMIT, optionsCompat.toBundle())
        }
 
        init {
            val itemView = LayoutInflater.from(activity).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)
        }
    }
 
    //更新列表
    fun notifyDataSetChanged() {
//        this.problemlistVos.clear();
//        this.problemlistVos.addAll(p);
        if (problemlistVos.isEmpty()) {
            image_problem_complete!!.visibility = View.VISIBLE
        } else {
            image_problem_complete!!.visibility = View.GONE
        }
        adapter!!.notifyDataSetChanged()
    }
 
    companion object {
        const val ARG_PARAM1 = "problems"
        const val ARG_PARAM2 = "fragmentType"
        const val REQUEST_CODE_SUBMIT = 11
        fun newInstance(problemlistVos: ArrayList<ProblemlistVo>?, fragmentType: String?): ProblemChangeFragment {
            val fragment = ProblemChangeFragment()
            val args = Bundle()
            args.putSerializable(ARG_PARAM1, problemlistVos)
            args.putString(ARG_PARAM2, fragmentType)
            fragment.arguments = args
            return fragment
        }
    }
}