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.viewpager2.widget.ViewPager2
|
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.WeekPagerAdapter
|
import java.time.DayOfWeek
|
import java.time.LocalDate
|
import java.time.format.DateTimeFormatter
|
import java.time.temporal.ChronoUnit
|
import java.time.temporal.TemporalAdjusters
|
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 adapter by lazy {
|
WeekPagerAdapter(LocalDate.of(2020, 1, 5), LocalDate.of(2050, 12, 31))
|
}
|
|
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.viewPager.adapter = adapter
|
binding.viewPager.offscreenPageLimit = 1
|
scrollToDate(time)
|
binding.viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
|
override fun onPageScrollStateChanged(state: Int) {
|
super.onPageScrollStateChanged(state)
|
if (state == ViewPager2.SCROLL_STATE_IDLE) {
|
val item = adapter.getItem(binding.viewPager.currentItem)
|
viewModel.yearMonth.value = "${item.year}年${item.monthValue}月"
|
}
|
}
|
})
|
}
|
|
//滚动到指定日期
|
private fun scrollToDate(dateString: String) {
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
val localDate = LocalDate.parse(dateString, formatter)
|
val localSundayDate = localDate.with(TemporalAdjusters.previousOrSame(DayOfWeek.SUNDAY))
|
val between = ChronoUnit.WEEKS.between(adapter.startDate, localSundayDate)
|
if (between < 0) {
|
return
|
}
|
binding.viewPager.setCurrentItem(between.toInt(), false)
|
adapter.selectDate = localDate
|
viewModel.yearMonth.value = "${localDate.year}年${localDate.monthValue}月"
|
}
|
|
//格式化日期
|
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(adapter.startDate))
|
val maxTime = getTime(formateDate(adapter.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.setOnDateItemClickListener {
|
viewModel.selectTime.value = formateDate(it)
|
viewModel.yearMonth.value = "${it.year}年${it.monthValue}月"
|
}
|
}
|
|
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) R.color.gray_3 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) R.color.gray_3 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
|
}
|
}
|
}
|
}
|