package com.example.smart.ui.sign import android.content.pm.PackageManager import android.os.Handler import android.os.Looper import android.text.TextUtils import android.view.LayoutInflater import android.view.View import androidx.activity.viewModels import androidx.core.app.ActivityCompat import androidx.core.view.isVisible import com.bumptech.glide.Glide import com.example.basecore.BaseBindingActivity import com.example.basecore.amap.ILocationCallBack import com.example.basecore.util.DateUtils import com.example.basecore.amap.LocationProvider import com.example.basecore.util.showCenterToast import com.example.basecore.util.showOnlyToast import com.example.basecore.util.sysbar.StatusBarUtils import com.example.basecore.util.toast import com.example.smart.R import com.example.smart.bean.CommonOptionsBean import com.example.smart.bean.EntityRef import com.example.smart.bean.ImageRef import com.example.smart.databinding.ActivitySignBinding import com.example.smart.image.ImageProvider import com.example.smart.oss.ImageRemotePathProvider import com.example.smart.ui.sign.bean.SignOptionsBean import com.example.smart.ui.sign.bean.SignRequestBean import com.example.smart.util.openImageViewerScheme class SignActivity : BaseBindingActivity() { private val binding by lazy { ActivitySignBinding.inflate(LayoutInflater.from(mContext)) } private val viewModel: SignViewModel by viewModels() private var signed = false private var type = "DEFAULT" /** * 本地路径 */ private var imgLocalPath = "" /** * 上传后路径 */ private var imgAccessPath = "" private var fileId = 0 // private val loadingDialog: LoadingDialog by lazy { LoadingDialog(this) } private val mLocationProvider by lazy { LocationProvider(mContext) } private lateinit var imageProvider: ImageProvider companion object { const val TIME_UPDATE = 1 } override fun getRootView() = binding.root override fun initView() { StatusBarUtils.transparentMode(this, true, isFullScreen = true) initOptionsView() requestOptions() updateCurrentTime() imageProvider = ImageProvider(this, ImageCallBack()) binding.apply { timeTv.text = DateUtils.getCurrentTime() signLayout.setOnClickListener { signWork() } //查看详情 signImg.setOnClickListener { val paths = ArrayList() paths.add(imgLocalPath) openImageViewerScheme(this@SignActivity, paths) } //清除 clearImg.setOnClickListener { signPicLayout.isVisible = false } //选择照片 takePhotoLayout.setOnClickListener { imageProvider.showImageSelect(1) } } locationPermission() } private inner class ImageCallBack : ImageProvider.IImageCallback { override fun onUploadSuccess(entityRef: List) { entityRef.forEach { if (it.name.isNotEmpty()) { imgAccessPath = it.name binding.signPicLayout.visibility = View.VISIBLE fileId = it.id.toInt() Glide.with(mContext) .load(ImageRemotePathProvider.IMAGE_BASE_URL + imgAccessPath) .into(binding.signImg) } } } override fun onUploadFailed() { mContext.showCenterToast("图片上传失败,请稍后再试...") } } private fun requestOptions() { type = intent.data?.getQueryParameter("type") ?: "DEFAULT" if (type == "PROJECT") { binding.titleLayout.setTitle("项目签到") } } private fun locationPermission() { val granted = PackageManager.PERMISSION_GRANTED val fineLocation = android.Manifest.permission.ACCESS_FINE_LOCATION val coarseLocation = android.Manifest.permission.ACCESS_COARSE_LOCATION if (ActivityCompat.checkSelfPermission(this, fineLocation) != granted && ActivityCompat.checkSelfPermission(this, coarseLocation) != granted ) { val permissionList = listOf(fineLocation, coarseLocation) requestPermission(permissionList) } else { onGenerateLocation() } } private var lat: Double = 0.0 private var lon: Double = 0.0 private fun onGenerateLocation() { mLocationProvider.getLocation(object : ILocationCallBack { override fun onLocationSuccess(address: String, lat: Double, lon: Double) { binding.locationTv.text = address this@SignActivity.lat = lat this@SignActivity.lon = lon } override fun onLocationFailed(reason: String) { binding.locationTv.text = reason } }) } override fun bindDataObserver() { viewModel.signResult.observe(this) { toast("签到成功") finish() } } private fun initOptionsView() { val signTypeOptions = mutableListOf() val timePeriodOptions = mutableListOf() signTypeOptions.add(CommonOptionsBean("OUT", "外勤")) signTypeOptions.add(CommonOptionsBean("IN", "内勤")) timePeriodOptions.add(CommonOptionsBean("ALL_DAY", "全天")) timePeriodOptions.add(CommonOptionsBean("MORNING", "上午")) timePeriodOptions.add(CommonOptionsBean("AFTERNOON", "下午")) val data = SignOptionsBean(signTypeOptions, timePeriodOptions) binding.apply { if (signed) { signTv.text = "已签到" signLocationBgView.setBackgroundResource(R.drawable.shape_signed_location_bg) } dateWidget.bindData(data.timePeriodOptions) typeWidget.bindData(data.signTypeOptions) } } private val mHandler = Handler(Looper.getMainLooper()) { if (it.what == TIME_UPDATE) { binding.timeTv.text = DateUtils.getCurrentTime() updateCurrentTime() } true } /** * 签到 */ private fun signWork() { if (type != "DEFAULT") { toast("暂不支持项目签到") return } if (signed) { toast("请勿重复签到") return } val bean = buildRequestData() ?: return viewModel.signForWork(bean) { toast(it) } } private fun buildRequestData(): SignRequestBean? { val location = binding.locationTv.text.trim().toString() if (TextUtils.isEmpty(location)) { toast("请获取位置信息") return null } val photo = imgAccessPath if (TextUtils.isEmpty(photo)) { toast("请上传图片") return null } val signTime = binding.dateWidget.getContainerId() if (TextUtils.isEmpty(signTime)) { toast("请选择签到时间") return null } val signType = binding.typeWidget.getContainerId() if (TextUtils.isEmpty(signType)) { toast("请选择签到类型") return null } val content = binding.contentEdt.text.trim().toString() if (TextUtils.isEmpty(location)) { toast("请输入工作内容") return null } val gpsList = arrayListOf(lat, lon) val ref = EntityRef(fileId) return SignRequestBean(location, signType, signTime, content, listOf(ref), gpsList) } /** * 更新当前时间 */ private fun updateCurrentTime() { mHandler.sendEmptyMessageDelayed(TIME_UPDATE, 1000) } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array, grantResults: IntArray ) { super.onRequestPermissionsResult(requestCode, permissions, grantResults) if (requestCode == PERMISSION_CODE) { for (res in grantResults) { if (res != PackageManager.PERMISSION_GRANTED) { showOnlyToast("请授予权限,否则部分功能无法正常使用") return } } onGenerateLocation() } } }