package cn.flightfeather.thirdappmodule.module.inspectioninfo
|
|
import android.app.Dialog
|
import android.content.Intent
|
import android.graphics.Bitmap
|
import android.graphics.BitmapFactory
|
import android.graphics.Matrix
|
import android.os.Build
|
import android.os.Bundle
|
import android.os.Environment
|
import android.support.annotation.RequiresApi
|
import android.support.v7.app.AlertDialog
|
import android.support.v7.app.AppCompatActivity
|
import android.transition.Explode
|
import android.view.View
|
import android.widget.ImageView
|
import android.widget.Toast
|
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.bean.entity.Mediafile
|
import cn.flightfeather.thirdappmodule.bean.vo.MediafileVo
|
import cn.flightfeather.thirdappmodule.bean.vo.ProblemlistVo
|
import cn.flightfeather.thirdappmodule.httpservice.ProblemListService
|
import cn.flightfeather.thirdappmodule.module.base.BaseTakePicActivity
|
import cn.flightfeather.thirdappmodule.util.*
|
import cn.flightfeather.thirdappmodule.util.file.FileUtil
|
import cn.flightfeather.thirdappmodule.util.photo.OnUpLoadListener
|
import cn.flightfeather.thirdappmodule.util.photo.PhotoUtil
|
import com.amap.api.maps.CameraUpdateFactory
|
import com.amap.api.maps.model.BitmapDescriptorFactory
|
import com.amap.api.maps.model.LatLng
|
import com.amap.api.maps.model.MarkerOptions
|
import com.ping.greendao.gen.MediafileDao
|
import kotlinx.android.synthetic.main.activity_problem_change_detail.*
|
import okhttp3.ResponseBody
|
import retrofit2.Call
|
import retrofit2.Callback
|
import retrofit2.Response
|
import java.io.File
|
import java.io.IOException
|
import java.io.Serializable
|
import java.util.*
|
|
class ProblemChangeDetailActivity : AppCompatActivity() {
|
//展示的问题对象
|
private var problemlistVo = ProblemlistVo()
|
|
/**
|
* 两种问题类型 [Constant.CHANGED]和[Constant.UNCHANGED]
|
*/
|
private var problemType = ""
|
|
//内部数据类,前一个context传入一些不变的展示数据
|
private var textData = TextData()
|
|
//requestCode
|
private val TAKE_PROBLEM_PHOTO = 13
|
private val TAKE_CHANGE_PHOTO = 14
|
private val PICK_PROBLEM_PHOTO = 15
|
private val PICK_CHANGE_PHOTO = 16
|
private var application: CommonApplication? = null
|
private var mediafileDao: MediafileDao? = null
|
|
//是否已编辑过
|
private var isEdited = false
|
private var unbinder: Unbinder? = null
|
|
//</editor-fold>
|
|
//<editor-fold desc="内部数据类">
|
class TextData : Serializable {
|
//离整改承诺期限剩余或逾期多少天
|
var text_deadline_status = ""
|
var text_remaining_days = ""
|
|
//是否承诺整改
|
var text_promise = ""
|
var text_promise_selected = false
|
|
//截止日期
|
var text_change_deadline = ""
|
var change_deadline_visible = 0
|
|
//超时整改天数
|
var cl_deadline_visible = 0
|
|
constructor() {}
|
constructor(text_deadline_status: String, text_remaining_days: String, text_promise: String, text_promise_selected: Boolean, text_change_deadline: String, change_deadline_visible: Int, cl_deadline_visible: Int) {
|
this.text_deadline_status = text_deadline_status
|
this.text_remaining_days = text_remaining_days
|
this.text_promise = text_promise
|
this.text_promise_selected = text_promise_selected
|
this.text_change_deadline = text_change_deadline
|
this.change_deadline_visible = change_deadline_visible
|
this.cl_deadline_visible = cl_deadline_visible
|
}
|
}
|
|
//</editor-fold>
|
|
//<editor-fold desc="lifecycle">
|
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
|
override fun onCreate(savedInstanceState: Bundle?) {
|
super.onCreate(savedInstanceState)
|
//设置允许使用转场动画
|
window.enterTransition = Explode().setDuration(500)
|
window.exitTransition = Explode().setDuration(300)
|
setContentView(R.layout.activity_problem_change_detail)
|
unbinder = ButterKnife.bind(this)
|
application = getApplication() as CommonApplication
|
initParam()
|
updateMediafile()
|
initView()
|
initProblemPhoto(mediafileListProblem, imageViewProblemList)
|
initChangingPhoto(mediafileListChange, imageViewChangeList)
|
initMap(savedInstanceState)
|
initClickListener()
|
}
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (requestCode == TAKE_CHANGE_PHOTO) {
|
if (resultCode == RESULT_OK && fileCurrent != null && !mediafileListProblem.isEmpty()) {
|
val mediafile = MediafileVo.newMediaFile(fileCurrent, true, mediafileListProblem[0])
|
mediafileListChange.add(mediafile)
|
mediafileDao!!.insert(mediafile)
|
isEdited = true
|
initChangingPhoto(mediafileListChange, imageViewChangeList)
|
}
|
} else if (requestCode == PICK_CHANGE_PHOTO) {
|
if (resultCode == RESULT_OK) {
|
val paths: List<String> = data!!.getStringArrayListExtra(BaseTakePicActivity.EXTRA_SELECT_IMAGES)
|
for (p in paths) {
|
val oldFile = File(p)
|
val newFile = filePath
|
try {
|
FileUtil.copyFile(oldFile, newFile)
|
if (!mediafileListProblem.isEmpty()) {
|
val mediafile = MediafileVo.newMediaFile(newFile, true, mediafileListProblem[0])
|
mediafileListChange.add(mediafile)
|
mediafileDao!!.insert(mediafile)
|
isEdited = true
|
}
|
} catch (e: IOException) {
|
e.printStackTrace()
|
}
|
}
|
initChangingPhoto(mediafileListChange, imageViewChangeList)
|
if (problemlistVo.ischanged) {
|
refreshSaveBtn(3)
|
}
|
}
|
} else if (requestCode == PhotoViewerActivity.CHANGE_PHOTO && resultCode == RESULT_OK) {
|
if (data != null) {
|
val position = data.getIntExtra("position", -1)
|
if (position > -1) {
|
mediafileDao!!.deleteByKey(mediafileListChange[position].guid)
|
mediafileListChange.removeAt(position)
|
//fixme 2020/11/12 由于删除单张图片是直接联网将服务器数据一并删除,因此当图片全部删除后,立即更新问题为未整改状态
|
if (mediafileListChange.isEmpty()) {
|
uploadProblem()
|
}
|
isEdited = !mediafileListChange.isEmpty()
|
initChangingPhoto(mediafileListChange, imageViewChangeList)
|
if (problemlistVo.ischanged) {
|
refreshSaveBtn(3)
|
}
|
}
|
}
|
}
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
if (unbinder != null) {
|
unbinder!!.unbind()
|
}
|
}
|
|
//</editor-fold>
|
|
//<editor-fold desc="初始化图片">
|
|
var imageViewProblemList = mutableListOf<ImageView>()
|
var imageViewChangeList = mutableListOf<ImageView>()
|
|
private fun initParam() {
|
val bundle = intent.extras
|
if (bundle != null) {
|
problemlistVo = bundle.getSerializable(ARG_PROBLEM) as ProblemlistVo
|
problemType = bundle.getString(ARG_PROBLEM_TYPE)
|
textData = bundle.getSerializable(ARG_TEXT_DATA) as TextData
|
}
|
mediafileDao = application!!.daoSession.mediafileDao
|
imageViewProblemList.add(iv_problem_photo1)
|
imageViewProblemList.add(iv_problem_photo2)
|
imageViewProblemList.add(iv_problem_photo3)
|
imageViewChangeList.add(iv_problem_change_photo1)
|
imageViewChangeList.add(iv_problem_change_photo2)
|
imageViewChangeList.add(iv_problem_change_photo3)
|
}
|
|
//</editor-fold>
|
|
//<editor-fold desc="初始化文本">
|
private fun initView() {
|
//问题基本信息
|
tv_problem_type_name!!.text = problemlistVo.typename
|
tv_problem_time!!.text = DateFormatter.dateTimeFormat.format(problemlistVo.time)
|
tv_problem_location!!.text = problemlistVo.location
|
tv_problem_name!!.text = problemlistVo.problemname
|
//逾期天数
|
text_deadline_status!!.text = textData.text_deadline_status
|
text_remaining_days!!.text = textData.text_remaining_days
|
//是否整改
|
image_promise!!.isSelected = textData.text_promise_selected
|
text_promise!!.text = textData.text_promise
|
//整改截止日期
|
text_change_deadline!!.text = textData.text_change_deadline
|
text_change_deadline!!.visibility = textData.change_deadline_visible
|
//整改类型
|
if (problemType == Constant.CHANGED && problemlistVo.changecatalog != null) {
|
text_change_type!!.text = problemlistVo.changecatalog
|
} else {
|
text_change_type!!.visibility = View.INVISIBLE
|
}
|
cl_deadline!!.visibility = textData.cl_deadline_visible
|
|
//整改建议
|
if (problemlistVo.advise != null) {
|
text_change_tips!!.text = problemlistVo.advise
|
}
|
if (problemType == Constant.UNCHANGED) {
|
refreshSaveBtn(1)
|
text_upload_photo!!.setText(R.string.click_to_upload_photo)
|
text_upload_photo!!.setTextColor(resources.getColor(R.color.black))
|
group_change_effect!!.visibility = View.GONE
|
} else {
|
refreshSaveBtn(2)
|
text_upload_photo!!.setText(R.string.changed_photo)
|
group_change_effect!!.visibility = View.VISIBLE
|
}
|
}
|
|
//</editor-fold>
|
private fun refreshSaveBtn(status: Int) {
|
when (status) {
|
1 -> {
|
|
//未整改
|
btn_save!!.setText(R.string.submit)
|
btn_save!!.setBackgroundResource(R.drawable.shape_button_blue)
|
btn_save!!.setOnClickListener { onButtonClick() }
|
}
|
2 -> {
|
|
//已整改
|
btn_save!!.setText(R.string.changed)
|
btn_save!!.setBackgroundResource(R.drawable.shape_button_green)
|
btn_save!!.setOnClickListener(null)
|
}
|
3 -> {
|
|
//提交后修改图片
|
btn_save!!.setText(R.string.submit_again)
|
btn_save!!.setBackgroundResource(R.drawable.shape_button_blue)
|
btn_save!!.setOnClickListener { onButtonClick() }
|
}
|
}
|
}
|
|
//<editor-fold desc="初始化地图定位">
|
private fun initMap(savedInstanceState: Bundle?) {
|
tmv_main!!.onCreate(savedInstanceState)
|
val aMap = tmv_main!!.map
|
var latLng: LatLng? = null
|
val options = MarkerOptions()
|
var bm: Bitmap? = null
|
|
//获取问题定位
|
val latitude = problemlistVo.latitude
|
val longitude = problemlistVo.longitude
|
if (latitude != null && longitude != null
|
&& latitude != 0.0 && longitude != 0.0
|
) {
|
latLng = LatLng(problemlistVo.latitude, problemlistVo.longitude)
|
bm = BitmapFactory.decodeResource(resources, R.drawable.icon_mark_red)
|
val matrix = Matrix()
|
matrix.setScale(0.28f, 0.28f)
|
val bmBig = Bitmap.createBitmap(
|
bm, 0, 0, bm.width,
|
bm.height, matrix, true
|
)
|
options.icon(BitmapDescriptorFactory.fromBitmap(bmBig))
|
options.position(latLng)
|
val marker = aMap.addMarker(options)
|
marker.isInfoWindowEnable = false
|
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14f))
|
//隐藏默认图片
|
tmv_main!!.visibility = View.VISIBLE
|
image_no_map!!.visibility = View.GONE
|
} else {
|
tmv_main!!.visibility = View.GONE
|
image_no_map!!.visibility = View.VISIBLE
|
}
|
|
// double scenseLat = application.getCurrentUser()
|
// double scenseLng = intent.getDoubleExtra("scenseLng",0);
|
// MarkerOptions siteMarkeroptions = new MarkerOptions().position(new LatLng(scenseLat,scenseLng));
|
// aMap.addMarker(siteMarkeroptions);
|
}
|
|
//</editor-fold>
|
|
//<editor-fold desc="获取已有的图片">
|
private val mediafileListChange: MutableList<Mediafile> = ArrayList() //整改图片列表
|
private val mediafileListProblem: MutableList<Mediafile> = ArrayList() //问题图片列表
|
private fun updateMediafile() {
|
val mediafileList = mediafileDao!!.queryBuilder()
|
.where(MediafileDao.Properties.Businessguid.eq(problemlistVo.guid))
|
.where(MediafileDao.Properties.Remark.eq("未上传"))
|
.list()
|
if (problemlistVo.mediafileList != null) {
|
mediafileList.addAll(problemlistVo.mediafileList)
|
}
|
for (m in mediafileList) {
|
if (m.ischanged) {
|
mediafileListChange.add(m)
|
} else {
|
mediafileListProblem.add(m)
|
}
|
}
|
}
|
|
//</editor-fold>
|
|
//<editor-fold desc="初始化问题图片">
|
private fun initProblemPhoto(mediafileList: List<Mediafile>, imageViews: List<ImageView?>) {
|
val photoCount = PhotoUtil.loadImage(mediafileList, imageViews, this) //将图片加载至对应的imageView上
|
val files: MutableList<File> = ArrayList()
|
for (m in mediafileList) {
|
val tempFile = FileUtil.getFileFromMediaFile(m)
|
files.add(tempFile)
|
}
|
for (i in 0 until photoCount) { //添加图片放大点击事件
|
imageViews[i]?.setOnClickListener(
|
PhotoUtil.OnPhotoViewClickListener(
|
this,
|
files,
|
i,
|
mediafileList,
|
PhotoViewerActivity.PROBLEM_PHOTO,
|
false
|
)
|
)
|
}
|
}
|
//</editor-fold>
|
|
//<editor-fold desc="初始化已上传整改的图片">
|
//整改图片
|
/**
|
*
|
* @param mediafileList
|
* @param imageViews
|
*/
|
@Deprecated("20190719 上传完成后,客户需要能够修改上传图片的功能,即也能拍照和删除图片,此函数删去")
|
private fun initChangedPhoto(mediafileList: List<Mediafile>, imageViews: List<ImageView>) {
|
val photoCount = PhotoUtil.loadImage(mediafileList, imageViews, this) //将图片加载至对应的imageView上
|
val files: MutableList<File> = ArrayList()
|
for (m in mediafileList) {
|
val tempFile = FileUtil.getFileFromMediaFile(m)
|
files.add(tempFile)
|
}
|
for (i in 0 until photoCount) { //添加图片放大点击事件
|
imageViews[i]
|
.setOnClickListener(
|
PhotoUtil.OnPhotoViewClickListener(
|
this,
|
files,
|
i,
|
mediafileList,
|
PhotoViewerActivity.CHANGE_PHOTO,
|
true
|
)
|
)
|
}
|
}
|
//</editor-fold>
|
|
//<editor-fold desc="初始化未上传整改的图片">
|
/**
|
*
|
* @param mediafileList 在未整改时,列表[.mediafileListChange]从数据库获取的结果是空,因此此处可直接将该列表对象用于整改时的新图片上传
|
* @param imageViews
|
*/
|
private fun initChangingPhoto(mediafileList: List<Mediafile>, imageViews: List<ImageView?>) {
|
var photoCount = PhotoUtil.loadImage(mediafileList, imageViews, this)
|
val files: MutableList<File> = ArrayList()
|
for (m in mediafileList) {
|
val tempFile = FileUtil.getFileFromMediaFile(m)
|
files.add(tempFile)
|
}
|
for (i in 0 until photoCount) { //添加图片放大点击事件
|
imageViews[i]?.setOnClickListener(
|
PhotoUtil.OnPhotoViewClickListener(
|
this,
|
files,
|
i,
|
mediafileList,
|
PhotoViewerActivity.CHANGE_PHOTO,
|
true
|
)
|
)
|
}
|
if (photoCount >= 3) return
|
val leftCount = if (3 - photoCount > 0) 3 - photoCount else 0
|
val image_addPhoto = imageViews[photoCount]
|
image_addPhoto!!.setImageResource(R.drawable.icon_add_photo)
|
image_addPhoto.setOnClickListener { PhotoUtil.pickPhoto2(this@ProblemChangeDetailActivity, PICK_CHANGE_PHOTO, leftCount) }
|
while (++photoCount < imageViews.size) {
|
imageViews[photoCount]!!.setImageResource(R.drawable.icon_add_photo_blank)
|
}
|
}
|
|
//</editor-fold>
|
|
//<editor-fold desc="获取整改图片路径">
|
private var fileCurrent: File? = null
|
private val filePath: File
|
get() {
|
val mediafile = mediafileListProblem[0]
|
val path = (mediafile.path
|
+ "整改/"
|
+ problemlistVo.problemname
|
+ " "
|
+ problemlistVo.location
|
+ " 整改 "
|
+ UUIDGenerator.generateUUID(4)
|
+ ".jpg")
|
val newFile = File(Environment.getExternalStorageDirectory(), path)
|
if (!newFile.parentFile.exists()) {
|
newFile.parentFile.mkdirs()
|
}
|
fileCurrent = newFile
|
return newFile
|
}
|
|
//</editor-fold>
|
|
//<editor-fold desc="点击事件">
|
private fun refreshProblemStatus() {
|
//整改图片全删除后,问题退回未整改审核通过状态
|
if (mediafileListChange.isEmpty()) {
|
problemlistVo.ischanged = false //设置为未整改
|
problemlistVo.extension3 = Domain.PROBLEM_CHECK_PASS
|
problemlistVo.changedtime = null
|
problemlistVo.changecatalog = null
|
} else {
|
problemlistVo.ischanged = true //设置为已整改
|
problemlistVo.extension3 = Domain.CHANGE_UNCHECKED //设置为整改待审核
|
problemlistVo.changedtime = Date() //设置整改时间为当前时间
|
problemlistVo.mediafileList.clear()
|
problemlistVo.mediafileList.addAll(mediafileListProblem)
|
problemlistVo.mediafileList.addAll(mediafileListChange) //将新增的图片添加进媒体文件列表,用于app数据刷新
|
val calendar = Calendar.getInstance()
|
val today = calendar[Calendar.DAY_OF_YEAR]
|
calendar.time = problemlistVo.time
|
val problemDay = calendar[Calendar.DAY_OF_YEAR]
|
//有承诺整改的日期
|
if (problemlistVo.extension1 != null) {
|
//整改时间已超过监管当天的24点,则认为是承诺整改
|
if (today > problemDay) {
|
problemlistVo.changecatalog = Domain.PROMISE_CHANGE
|
} else {
|
problemlistVo.changecatalog = Domain.LOACAL_CHANGE
|
}
|
} else {
|
problemlistVo.changecatalog = Domain.UNPROMISE_CHANGE
|
}
|
}
|
}
|
|
//提交问题
|
var loadingDialog: Dialog? = null
|
fun onButtonClick() {
|
// if (!problemType.equals(Constant.UNCHANGED)) {
|
// return;
|
// }
|
if (isEdited) {
|
loadingDialog = DialogUtil.createLoadingDialog(this, "")
|
upLoadPic()
|
} else {
|
Toast.makeText(application, R.string.take_one_change_photo_at_least, Toast.LENGTH_SHORT).show()
|
}
|
}
|
|
override fun onBackPressed() {
|
// super.onBackPressed();
|
close()
|
}
|
|
|
private fun initClickListener() {
|
image_close.setOnClickListener { close() }
|
}
|
|
private fun close() {
|
if (isEdited) {
|
val dialog = AlertDialog.Builder(this)
|
dialog.setMessage("整改图片还未提交,是否退出?")
|
dialog.setPositiveButton("确定") { dialogInterface, i -> finish() }
|
dialog.setNegativeButton("取消", null)
|
dialog.show()
|
} else {
|
supportFinishAfterTransition()
|
}
|
}
|
|
private fun uploadProblem() {
|
refreshProblemStatus()
|
val updateProblemList = application!!.retrofit.create(ProblemListService::class.java).updateProblemList(problemlistVo)
|
updateProblemList.enqueue(object : Callback<ResponseBody?> {
|
override fun onResponse(call: Call<ResponseBody?>, response: Response<ResponseBody?>) {
|
if (response.body() != null) {
|
Toast.makeText(application, R.string.submit_success, Toast.LENGTH_LONG).show()
|
if (loadingDialog != null && loadingDialog!!.isShowing) {
|
loadingDialog!!.dismiss()
|
}
|
//返回requestCode
|
val intent = Intent()
|
intent.putExtra(ARG_PROBLEM, problemlistVo)
|
setResult(RESULT_OK, intent)
|
|
//发送数据更新广播
|
val intent1 = Intent("cn.flightfeather.thirdapp.broadcasereceiver.MainReceiver")
|
intent1.action = Constant.REFRESH_DATA
|
intent1.putExtra(ARG_PROBLEM, problemlistVo)
|
sendBroadcast(intent1)
|
finish()
|
} else if (response.errorBody() != null) {
|
Toast.makeText(application, R.string.submit_unsuccess, Toast.LENGTH_LONG).show()
|
loadingDialog!!.dismiss()
|
}
|
}
|
|
override fun onFailure(call: Call<ResponseBody?>, t: Throwable) {
|
Toast.makeText(application, R.string.network_error, Toast.LENGTH_LONG).show()
|
loadingDialog!!.dismiss()
|
}
|
})
|
}
|
|
private fun upLoadPic() {
|
|
//筛选“未上传”的图片
|
val mediafiles: MutableList<Mediafile> = ArrayList()
|
for (m in mediafileListChange) {
|
if (m.remark == "未上传") {
|
mediafiles.add(m)
|
}
|
}
|
val photoUtil = PhotoUtil(mediafiles.size, application, object : OnUpLoadListener {
|
override fun onSuccess() {
|
uploadProblem()
|
}
|
|
override fun onFail() {
|
Toast.makeText(application, "上传失败,请重试", Toast.LENGTH_SHORT).show()
|
loadingDialog!!.dismiss()
|
// AlertDialog.Builder dialog = new AlertDialog.Builder(application);
|
// dialog.setMessage("图片上传失败,请重试");
|
// dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
// @Override
|
// public void onClick(DialogInterface dialogInterface, int i) {
|
// finish();
|
// }
|
// });
|
// dialog.show();
|
}
|
})
|
photoUtil.upLoadPhoto(mediafiles)
|
} //</editor-fold>
|
|
companion object {
|
//<editor-fold desc="全局变量">
|
const val ARG_PROBLEM = "problems"
|
const val ARG_PROBLEM_TYPE = "problemType"
|
const val ARG_TEXT_DATA = "textData"
|
}
|
}
|