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
package cn.flightfeather.thirdappmodule.module.inspection
 
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import cn.flightfeather.thirdappmodule.R
import cn.flightfeather.thirdappmodule.adapter.AllRecyclerViewAdapter
import cn.flightfeather.thirdappmodule.module.base.BaseFragment
import cn.flightfeather.thirdappmodule.util.ScreenUtils
import cn.flightfeather.thirdappmodule.util.photo.GlideLoader
import cn.flightfeather.thirdappmodule.util.photo.ImagePicker2
import com.bumptech.glide.Glide
import com.lcw.library.imagepicker.ImagePicker
import kotlinx.android.synthetic.main.fragment_share_problem.*
 
/**
 * @author riku
 * Date: 2020/5/15
 */
class ShareProblemFragment : BaseFragment() {
 
    companion object {
        const val TAKE_PROBLEM_PICTURE = 1000
        const val TAKE_CHANGE_PICTURE = 1001
        const val MAX_PIC = 3
 
        const val ARG_PROBLEM = "problemVo"
 
        fun newInstance(problemVo: ShareProblemPreViewActivity.Problem): ShareProblemFragment {
            return ShareProblemFragment().apply {
                arguments=Bundle().apply {
                    putParcelable(ARG_PROBLEM, problemVo)
                }
            }
        }
    }
 
    private var problemVo: ShareProblemPreViewActivity.Problem? = null
 
    private val problemImageList = mutableListOf<Pic>()
    private val changeImageList = mutableListOf<Pic>()
 
    private var problemAdapter: AllRecyclerViewAdapter<Pic>? = null
    private var changeAdapter: AllRecyclerViewAdapter<Pic>? = null
 
    override fun getLayoutId(): Int = R.layout.fragment_share_problem
 
    override fun onCreateView() {
        super.onCreateView()
        problemVo = arguments?.getParcelable(ARG_PROBLEM) as ShareProblemPreViewActivity.Problem?
    }
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        initData()
        problemAdapter = initRecyclerView(false, problemImageList, rv_problem, TAKE_PROBLEM_PICTURE)
        changeAdapter = initRecyclerView(true, changeImageList, rv_problem_change, TAKE_CHANGE_PICTURE)
        initText()
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK) {
            val picList = mutableListOf<Pic>()
            val tempAdapter: AllRecyclerViewAdapter<Pic>?
            val tempImageList:MutableList<Pic>
            data?.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES)?.forEach {
                picList.add(Pic(url = it))
            }
            when (requestCode) {
                TAKE_PROBLEM_PICTURE -> {
                    tempImageList = problemImageList
                    tempAdapter = problemAdapter
                    addPicToProblemVo(false, picList)
                }
                TAKE_CHANGE_PICTURE -> {
                    tempImageList = changeImageList
                    tempAdapter = changeAdapter
                    addPicToProblemVo(true, picList)
                }
                else -> {
                    tempImageList = problemImageList
                    tempAdapter = problemAdapter
                }
            }
            val lastIndex = tempImageList.lastIndex
            tempImageList.removeAt(lastIndex)
            tempAdapter?.notifyItemRemoved(lastIndex)
            addNewPic(picList, tempImageList)
            tempAdapter?.notifyItemRangeInserted(lastIndex, picList.size)
        }
    }
 
    private fun initData() {
        val changeList = mutableListOf<Pic>()
        val problemList = mutableListOf<Pic>()
        problemVo?.mediaFileList?.forEach {
            if (it.first) {
                changeList.add(Pic(false, it.second))
            } else {
                problemList.add(Pic(false, it.second))
            }
        }
        addNewPic(changeList, changeImageList)
        addNewPic(problemList, problemImageList)
    }
 
    @SuppressLint("SetTextI18n")
    private fun initText() {
        val p = problemVo
        ed_problem_content.setText(p?.problems)
        ed_change_advice.setText(p?.advise)
        ed_change_info.setText(p?.changeInfo)
 
        addTextListener(ed_problem_content) {
            problemVo?.problems = it
        }
        addTextListener(ed_change_advice) {
            problemVo?.advise = it
        }
        addTextListener(ed_change_info) {
            problemVo?.changeInfo = it
        }
    }
 
    private fun addTextListener(editText: EditText, onTextChanged: (s: String) -> Unit) {
        editText.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                s?.let {
                    onTextChanged(it.toString())
                }
            }
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
        })
    }
 
    private fun addNewPic(newPics: List<Pic>, dataList: MutableList<Pic>) {
        if (dataList.isNotEmpty() && dataList.last().isPlaceHolder) {
            dataList.removeAt(dataList.lastIndex)
        }
        dataList.addAll(newPics)
        if (dataList.size < MAX_PIC) {
            dataList.add(Pic(true))
        }
    }
 
    private fun initRecyclerView(isChanged: Boolean, dataList: MutableList<Pic>, recyclerView: RecyclerView, requestCode: Int): AllRecyclerViewAdapter<Pic> {
        if (dataList.size == 1) {
            recyclerView.visibility = View.GONE
        }
        val adapter = object : AllRecyclerViewAdapter<Pic>(dataList, R.layout.item_image_display, context) {
            override fun bindView(holder: MyViewHolder?, obj: Pic?, isSelected: Boolean, position: Int) {
                if (obj?.isPlaceHolder == true) {
                    holder?.setVisibility(R.id.img_add, View.VISIBLE)
                            ?.setVisibility(R.id.img_photo, View.GONE)
                            ?.setVisibility(R.id.img_delete, View.GONE)
 
                } else {
                    holder?.setVisibility(R.id.img_add, View.GONE)
                            ?.setVisibility(R.id.img_photo, View.VISIBLE)
                            ?.setVisibility(R.id.img_delete, View.VISIBLE)
                            ?.getViewById<ImageView>(R.id.img_photo)?.let {
                                Glide.with(this@ShareProblemFragment).load(obj?.url).into(it)
                            }
                }
                holder?.setOnClickListener(R.id.img_photo) {
 
                }
                        ?.setOnClickListener(R.id.img_add) {
                            var picCount = 0
                            dataList.forEach {
                                if (!it.isPlaceHolder) {
                                    picCount++
                                }
                            }
                            ImagePicker2.instance
                                    .setTitle("选择图片")
                                    .showCamera(true)
                                    .showImage(true)
                                    .showVideo(false)
                                    .setMaxCount(MAX_PIC - picCount)
                                    .setSingleType(true)
                                    .setImageLoader(GlideLoader())
                                    .start(this@ShareProblemFragment, requestCode)
                        }
                        ?.setOnClickListener(R.id.img_delete) {
                            dataList.removeAt(holder.adapterPosition)
                            notifyItemRemoved(holder.adapterPosition)
                            obj?.let {
                                dropPicFromProblemVo(isChanged, listOf(it))
                            }
                            val size = if (dataList.last().isPlaceHolder) {
                                dataList.size - 1
                            } else {
                                dataList.size
                            }
                            if (size == MAX_PIC - 1) {
                                dataList.add(Pic(true))
                                notifyItemInserted(MAX_PIC - 1)
                            }
                        }
            }
        }
        val span = getRecyclerViewSpan()
        recyclerView.apply {
            this.adapter = adapter
            layoutManager = GridLayoutManager(context, span)
        }
        return adapter
    }
 
    /**
     * 根据屏幕大小获取列表中每行展示的图片数量
     */
    private fun getRecyclerViewSpan(): Int {
        val recyclerViewWidth = ScreenUtils.getScreenWidth(context) - resources.getDimension(R.dimen.dimen16) * 2
 
        val imageSize = resources.getDimension(R.dimen.grid_image_size)
 
        return (recyclerViewWidth / imageSize).toInt()
    }
 
    private fun addPicToProblemVo(isChanged: Boolean, urls: List<Pic>) {
        urls.forEach {
            problemVo?.mediaFileList?.add(Pair(isChanged, it.url))
        }
    }
 
    private fun dropPicFromProblemVo(isChanged: Boolean, urls: List<Pic>) {
        urls.forEach {
            val m = problemVo?.mediaFileList ?: mutableListOf()
            for (i in m.indices) {
                if (m[i].first == isChanged && m[i].second == it.url) {
                    m.removeAt(i)
                    break
                }
            }
        }
    }
 
    inner class Pic(
            val isPlaceHolder: Boolean = false,
            val url: String = ""
    )
}