package cn.flightfeather.thirdapp.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.thirdapp.R
|
import cn.flightfeather.thirdapp.adapter.AllRecyclerViewAdapter
|
import cn.flightfeather.thirdapp.module.base.BaseFragment
|
import cn.flightfeather.thirdapp.util.ScreenUtils
|
import cn.flightfeather.thirdapp.util.photo.GlideLoader
|
import cn.flightfeather.thirdapp.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 = ""
|
)
|
}
|