package com.xw.yunwei.ui.user.mine.scheduling
|
|
import android.annotation.SuppressLint
|
import android.app.Application
|
import android.view.View
|
import androidx.core.os.bundleOf
|
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.map
|
import com.xw.common.base.BaseViewModel
|
import com.xw.common.utils.ToastUtilCommon
|
import com.xw.yunwei.R
|
import com.xw.yunwei.model.ClockInStatusModel
|
import com.xw.yunwei.net.UserRepository
|
import com.xw.yunwei.ui.user.mine.checkin.CheckInActivity
|
import org.koin.core.component.inject
|
import java.time.LocalDate
|
import java.time.format.DateTimeFormatter
|
import java.util.Locale
|
import kotlin.getValue
|
|
@SuppressLint("SimpleDateFormat")
|
class SchedulingVM(application: Application) : BaseViewModel(application) {
|
val userRepository: UserRepository by inject()
|
|
var today = ""
|
val selectTime = MutableLiveData<String>()
|
val yearMonth = selectTime.map {
|
try {
|
val inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
val date = LocalDate.parse(it, inputFormatter)
|
val outputFormatter = DateTimeFormatter.ofPattern("yyyy年M月", Locale.CHINESE)
|
date.format(outputFormatter)
|
} catch (e: Exception) {
|
""
|
}
|
}
|
// 一周的中文以及格式化时间
|
val showWeekDay: MutableLiveData<List<String>> = MutableLiveData()
|
val showWeekDate: MutableLiveData<List<String>> = MutableLiveData()
|
|
// 对应currentTime的打卡信息
|
val clockModel: MutableLiveData<ClockInStatusModel?> = MutableLiveData()
|
val clockTime = clockModel.map {
|
// "${it?.xpShift?.startTime ?: ""} - ${it?.xpShift?.restStartTime ?: ""} ${it?.xpShift?.restEndTime ?: ""} - ${it?.xpShift?.endTime ?: ""}"
|
"${selectTime.value?:""} ${it?.xpShift?.startTime ?: ""}~${it?.xpShift?.endTime ?: ""}"
|
}
|
|
val onClick = View.OnClickListener {
|
when (it.id) {
|
// R.id.goCheck -> startActivity(
|
// CheckInActivity::class.java,
|
// bundleOf("clockModel" to clockModel.value)
|
// )
|
R.id.date1 -> selectTime.value = showWeekDate.value?.getOrNull(0) ?: ""
|
R.id.date2 -> selectTime.value = showWeekDate.value?.getOrNull(1) ?: ""
|
R.id.date3 -> selectTime.value = showWeekDate.value?.getOrNull(2) ?: ""
|
R.id.date4 -> selectTime.value = showWeekDate.value?.getOrNull(3) ?: ""
|
R.id.date5 -> selectTime.value = showWeekDate.value?.getOrNull(4) ?: ""
|
R.id.date6 -> selectTime.value = showWeekDate.value?.getOrNull(5) ?: ""
|
R.id.date7 -> selectTime.value = showWeekDate.value?.getOrNull(6) ?: ""
|
R.id.clockIn -> startActivity(
|
CheckInActivity::class.java,
|
bundleOf("clockModel" to clockModel.value, "enterType" to 1)
|
)
|
|
R.id.clockOut -> startActivity(
|
CheckInActivity::class.java,
|
bundleOf("clockModel" to clockModel.value, "enterType" to 2)
|
)
|
}
|
}
|
|
fun querySchedule(time: String, showLoading: Boolean = true) {
|
launchRequest(showLoading = showLoading) {
|
userRepository.querySchedule(mapOf("schedulingDate" to time)).let {
|
clockModel.postValue(it)
|
}
|
}
|
}
|
|
/**
|
* 生成从输入日期开始后7天的星期名称
|
* @param dateString 日期字符串
|
*/
|
fun createNext7DaysWeekNames(dateString: String) {
|
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd")
|
val startDate = LocalDate.parse(dateString, formatter)
|
|
// 中文星期简写映射
|
val weekDayMap = mapOf(
|
java.time.DayOfWeek.MONDAY to "一",
|
java.time.DayOfWeek.TUESDAY to "二",
|
java.time.DayOfWeek.WEDNESDAY to "三",
|
java.time.DayOfWeek.THURSDAY to "四",
|
java.time.DayOfWeek.FRIDAY to "五",
|
java.time.DayOfWeek.SATURDAY to "六",
|
java.time.DayOfWeek.SUNDAY to "日"
|
)
|
|
val days = mutableListOf<String>()
|
val weeks = mutableListOf<String>()
|
|
(0 until 7).map { dayOffset ->
|
val date = startDate.plusDays(dayOffset.toLong())
|
days.add(date.format(formatter)) // 几号:24, 25, 26...
|
weeks.add(weekDayMap[date.dayOfWeek] ?: "")
|
}
|
showWeekDate.value = days
|
showWeekDay.value = weeks
|
}
|
}
|