package com.xw.yunwei.ui.user.mine.scheduling
|
|
import android.content.res.ColorStateList
|
import androidx.appcompat.widget.AppCompatTextView
|
import androidx.core.content.ContextCompat
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import com.amap.api.maps.model.LatLng
|
import com.xw.common.R
|
import com.xw.common.TimeDialog
|
import com.xw.common.TimeDialog.Companion.getTime
|
import com.xw.common.utils.BindUtils.getResColor
|
import com.xw.common.utils.GpsHelper.Companion.navigate
|
import com.xw.common.utils.ToastUtilCommon
|
import com.xw.yunwei.BR
|
import com.xw.yunwei.BaseBusinessActivity
|
import com.xw.yunwei.databinding.ActivitySchedulingBinding
|
import com.xw.yunwei.ui.user.mine.scheduling.adapter.WeekDayAdapter
|
import java.time.LocalDate
|
import java.util.Calendar
|
import java.util.Locale
|
|
/**
|
* 排班考勤
|
*/
|
class SchedulingActivity : BaseBusinessActivity<ActivitySchedulingBinding, SchedulingVM>() {
|
override fun getBindingVariable() = BR.viewModel
|
|
val dialogTime: TimeDialog by lazy { TimeDialog(this) }
|
val startDate = LocalDate.of(2020, 1, 1)
|
val endDate = LocalDate.of(2050, 12, 31)
|
|
val adapter by lazy {
|
WeekDayAdapter()
|
}
|
|
override fun init() {
|
super.init()
|
val time = getTime(System.currentTimeMillis())
|
viewModel.today = time
|
viewModel.selectTime.value = time
|
|
val bgColorStateList = ColorStateList(
|
arrayOf(
|
intArrayOf(-android.R.attr.state_enabled), intArrayOf()
|
), intArrayOf(
|
ContextCompat.getColor(this, R.color.blue_3),
|
ContextCompat.getColor(this, R.color.blue_5)
|
)
|
)
|
|
binding.clockIn.apply {
|
backgroundTintList = bgColorStateList
|
setTextColor(ContextCompat.getColor(this@SchedulingActivity, android.R.color.white))
|
}
|
|
binding.clockOut.apply {
|
backgroundTintList = bgColorStateList
|
setTextColor(ContextCompat.getColor(this@SchedulingActivity, android.R.color.white))
|
}
|
binding.rvList.adapter = adapter
|
binding.rvList.layoutManager =
|
LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
|
scrollToDate(time)
|
}
|
|
//滚动到指定日期
|
private fun scrollToDate(dateString: String) {
|
val split = dateString.split("-")
|
Calendar.getInstance().apply {
|
set(Calendar.YEAR, split[0].toInt())
|
set(Calendar.MONTH, split[1].toInt() - 1)
|
set(Calendar.DAY_OF_MONTH, split[2].toInt())
|
}.also { calendar ->
|
adapter.setList((0..29).mapIndexed { index, i ->
|
calendar.add(Calendar.DAY_OF_MONTH, if (index == 0) 0 else 1)
|
"${calendar.get(Calendar.YEAR)}-${
|
String.format(Locale.getDefault(), "%02d", calendar.get(Calendar.MONTH) + 1)
|
}-${
|
String.format(Locale.getDefault(), "%02d", calendar.get(Calendar.DAY_OF_MONTH))
|
}"
|
})
|
adapter.selectDate = dateString
|
viewModel.yearMonth.value =
|
"${split[0]}年${String.format(Locale.getDefault(), "%02d", split[1].toInt())}月"
|
binding.rvList.smoothScrollToPosition(0)
|
}
|
}
|
|
//格式化日期
|
private fun formateDate(localDate: LocalDate): String {
|
return "${localDate.year}-${
|
String.format(Locale.getDefault(), "%02d", localDate.monthValue)
|
}-${
|
String.format(Locale.getDefault(), "%02d", localDate.dayOfMonth)
|
}"
|
}
|
|
override fun setListener() {
|
super.setListener()
|
binding.dateSelect.setOnClickListener {
|
val showTime = getTime(viewModel.selectTime.value)
|
val minTime = getTime(formateDate(startDate))
|
val maxTime = getTime(formateDate(endDate))
|
dialogTime.show("选择日期", showTime, minTime, maxTime) {
|
val time = getTime(it)
|
scrollToDate(time)
|
viewModel.selectTime.value = time
|
}
|
}
|
binding.nav.setOnClickListener {
|
viewModel.clockModel.value?.let {
|
if ((it.xpCheckLocation?.locationLat
|
?: 0.0) != 0.0 && (it.xpCheckLocation?.locationLon ?: 0.0) != 0.0
|
) {
|
navigate(
|
end = LatLng(
|
it.xpCheckLocation?.locationLat!!, it.xpCheckLocation.locationLon!!
|
), context = this
|
)
|
} else {
|
ToastUtilCommon.showError("数据异常")
|
}
|
}
|
}
|
adapter.setOnItemClickListener { _, _, i ->
|
val item = adapter.getItem(i)
|
adapter.selectDate = item
|
viewModel.selectTime.value = item
|
val split = item.split("-")
|
viewModel.yearMonth.value = "${split[0]}年${split[1]}月"
|
}
|
}
|
|
override fun onResume() {
|
super.onResume()
|
if (viewModel.today.isNotEmpty() && viewModel.today == viewModel.selectTime.value) {
|
viewModel.querySchedule(viewModel.today, false)
|
}
|
}
|
|
override fun setObservable() {
|
super.setObservable()
|
viewModel.selectTime.observe(this) {
|
viewModel.querySchedule(it)
|
}
|
val setStatus: (AppCompatTextView, Int, Int, String) -> Unit = { tv, color, bg, text ->
|
tv.setTextColor(color)
|
tv.setBackgroundResource(bg)
|
tv.text = text
|
}
|
viewModel.clockModel.observe(this) {
|
it?.let {
|
val now = Calendar.getInstance()
|
val isToday =
|
viewModel.selectTime.value == null || viewModel.selectTime.value == getTime(now.timeInMillis)
|
// 状态提示控制
|
setStatus(
|
binding.clockIntStatus,
|
getResColor(if (it.xpClockIn?.hadLostWorkCheckedIn() == true) R.color.red_5 else if (it.clockIn == true) R.color.gray_7 else if (!isToday) R.color.gray_8 else R.color.orange_5),
|
if (it.xpClockIn?.hadLostWorkCheckedIn() == true) R.color.red_1 else if (it.clockIn == true) R.color.gray_3 else if (!isToday) com.xw.yunwei.R.drawable.shape_gray_4_bg_4 else R.color.orange_1,
|
if (it.xpClockIn?.hadLostWorkCheckedIn() == true) "缺卡" else if (it.clockIn == true) "已打卡" else if (!isToday) "不在打卡时间范围内" else "未打卡"
|
)
|
setStatus(
|
binding.clockOutStatus,
|
getResColor(if (it.xpClockIn?.hadLostOffWorkCheckedIn() == true) R.color.red_5 else if (it.clockOut == true) R.color.gray_7 else if (!isToday) R.color.gray_8 else R.color.orange_5),
|
if (it.xpClockIn?.hadLostOffWorkCheckedIn() == true) R.color.red_1 else if (it.clockOut == true) R.color.gray_3 else if (!isToday) com.xw.yunwei.R.drawable.shape_gray_4_bg_4 else R.color.orange_1,
|
if (it.xpClockIn?.hadLostOffWorkCheckedIn() == true) "缺卡" else if (it.clockOut == true) "已打卡" else if (!isToday) "不在打卡时间范围内" else "未打卡"
|
)
|
// 未上班打卡或缺卡 可上班打卡
|
binding.clockIn.isEnabled =
|
it.clockIn != true || it.xpClockIn?.hadLostWorkCheckedIn() == true
|
|
// 未下班打卡或缺卡 可下班打卡
|
binding.clockOut.isEnabled =
|
it.clockOut != true || it.xpClockIn?.hadLostOffWorkCheckedIn() == true
|
}
|
}
|
}
|
}
|