package com.xw.common
|
|
import android.view.LayoutInflater
|
import androidx.databinding.DataBindingUtil
|
import androidx.fragment.app.FragmentActivity
|
import com.blankj.utilcode.util.TimeUtils
|
import com.google.android.material.bottomsheet.BottomSheetDialog
|
import com.xw.common.custom.loopView.LoopView
|
import com.xw.common.custom.loopView.OnItemSelectedListener
|
import com.xw.common.databinding.DialogTimeBinding
|
import com.xw.common.utils.ToastUtilCommon
|
import kotlin.math.max
|
|
class TimeDialog(activity: FragmentActivity) :
|
BottomSheetDialog(activity, R.style.BottomSheetDialog), OnItemSelectedListener {
|
|
var bind: DialogTimeBinding =
|
DataBindingUtil.inflate(LayoutInflater.from(activity), R.layout.dialog_time, null, false)
|
|
/*时间区间控制*/
|
lateinit var minYear: String
|
lateinit var minMouth: String
|
lateinit var minDay: String
|
|
var maxYear = 2050
|
var maxMouth = 12
|
var maxDay = 31
|
|
var yearDataList: MutableList<String> = ArrayList()
|
var mouthDataList: MutableList<String> = ArrayList()
|
var dayDataList: MutableList<String> = ArrayList()
|
|
var showYear = ""
|
var showMouth = ""
|
var showDay = ""
|
|
var onSure: ((Long) -> Unit)? = null
|
|
init {
|
setContentView(bind.root)
|
setListener()
|
}
|
|
fun setListener() {
|
bind.yearView.tag = "yearView"
|
bind.mouthView.tag = "mouthView"
|
bind.dayView.tag = "dayView"
|
bind.yearView.setListener(this@TimeDialog)
|
bind.mouthView.setListener(this@TimeDialog)
|
bind.dayView.setListener(this@TimeDialog)
|
|
bind.cancel.setOnClickListener { dismiss() }
|
bind.sure.setOnClickListener {
|
if (showYear.isEmpty() || showMouth.isEmpty() || showDay.isEmpty()) {
|
ToastUtilCommon.show("请选择完整时间")
|
return@setOnClickListener
|
}
|
// onSure?.invoke(TimeUtils.string2Millis("$showYear-$showMouth-$showDay 00:00:00"))
|
onSure?.invoke(TimeUtils.string2Millis("$showYear-${if (showMouth < "10") "0$showMouth" else showMouth}-${if (showDay < "10") "0$showDay" else showDay} 00:00:00"))
|
dismiss()
|
}
|
}
|
|
|
/*仅显示年月日*/
|
fun show(title: String, showTime: Long = -1L, minTime: Long = -1L, onSure: (Long) -> Unit) {
|
bind.title.text = title
|
getCurrentData(if (minTime == -1L) System.currentTimeMillis() else minTime)
|
this.onSure = onSure
|
val time = if (showTime == -1L && minTime == -1L) System.currentTimeMillis() else max(showTime, minTime)
|
initSelectTime(TimeUtils.millis2String(time, "yyyy-MM-dd"))
|
super.show()
|
}
|
|
private fun getCurrentData(minTime: Long, maxTime: Long? = null) {
|
val min = TimeUtils.millis2String(minTime)
|
minYear = min.substring(0, 4)
|
minMouth = min.substring(5, 7)
|
minDay = min.substring(8, 10)
|
|
if (maxTime != null) {
|
val max = TimeUtils.millis2String(maxTime)
|
maxYear = max.substring(0, 4).toInt()
|
maxMouth = max.substring(5, 7).toInt()
|
maxDay = max.substring(8, 10).toInt()
|
} else {
|
maxYear = 2050
|
maxMouth = 12
|
maxDay = 31
|
}
|
}
|
|
private fun initSelectTime(time: String) {
|
showYear = time.substring(0, 4)
|
showMouth = time.substring(5, 7)
|
showDay = time.substring(8, 10)
|
|
if (showMouth.startsWith("0")) {
|
showMouth = showMouth.substring(1, showMouth.length)
|
}
|
if (showDay.startsWith("0")) {
|
showDay = showDay.substring(1, showDay.length)
|
}
|
|
initYears()
|
initMonths()
|
initDays(calDays(showYear, showMouth))
|
|
setSelectIndex(bind.yearView, yearDataList.indexOf("${showYear}年"))
|
setSelectIndex(bind.mouthView, mouthDataList.indexOf("${showMouth}月"))
|
setSelectIndex(bind.dayView, dayDataList.indexOf("${showDay}日"))
|
}
|
|
private fun initYears() {
|
yearDataList.clear()
|
for (i in minYear.toInt()..maxYear) {
|
yearDataList.add(i.toString() + "年")
|
}
|
bind.yearView.dataList = yearDataList
|
}
|
|
private fun initMonths() {
|
mouthDataList.clear()
|
val mouthStart = if (minYear == showYear) minMouth.toInt() else 1
|
val mouthEnd = if ("$maxYear" == showYear) maxMouth else 12
|
for (i in mouthStart..mouthEnd) {
|
// mouthDataList.add("${if (i < 10) "0$i" else i}月")
|
mouthDataList.add("${i}月")
|
}
|
bind.mouthView.dataList = mouthDataList
|
}
|
|
private fun initDays(days: Int) {
|
dayDataList.clear()
|
val dayStart = if (minYear == showYear && minMouth == showMouth) minDay.toInt() else 1
|
val dayEnd = if ("$maxYear" == showYear && "$maxMouth" == showMouth) maxDay else days
|
for (i in dayStart..dayEnd) {
|
// dayDataList.add("${if (i < 10) "0$i" else i}日")
|
dayDataList.add("${i}日")
|
}
|
bind.dayView.dataList = dayDataList
|
}
|
|
/**
|
* 计算每月多少天
|
*/
|
private fun calDays(year: String, month: String): Int {
|
val leapYear = year.toInt() % 4 == 0 && year.toInt() % 100 != 0
|
return when (month.toInt()) {
|
1, 3, 5, 7, 8, 10, 12 -> 31
|
2 -> if (leapYear) 29 else 28
|
4, 6, 9, 11 -> 30
|
else -> 0
|
}
|
|
}
|
|
private fun setSelectIndex(loopView: LoopView, index: Int) {
|
loopView.preCurrentIndex = -1 // dialog 多类型共用一个弹窗 需要重置保持正确显示
|
loopView.setInitPosition(index)
|
loopView.setCurrentPosition(index)
|
}
|
|
override fun onItemSelected(loopview: LoopView, index: Int) {
|
if(index == - 1) return
|
when (loopview.tag.toString()) {
|
"yearView" -> {
|
showYear = yearDataList[index].replace("年", "")
|
resetMouth()
|
}
|
|
"mouthView" -> {
|
showMouth = mouthDataList[index].replace("月", "")
|
resetDay()
|
}
|
|
"dayView" -> {
|
showDay = dayDataList[index].replace("日", "")
|
}
|
}
|
}
|
|
private fun resetMouth() {
|
initMonths()
|
var index = mouthDataList.indexOf("${showMouth}月")
|
index = if (index == -1) 0 else index
|
setSelectIndex(bind.mouthView, index)
|
resetDay()
|
}
|
|
private fun resetDay() {
|
initDays(calDays(showYear, showMouth))
|
var index = dayDataList.indexOf("${showDay}日")
|
index = if (index == -1) 0 else index
|
setSelectIndex(bind.dayView, index)
|
}
|
|
companion object {
|
fun getTime(time: String?): Long {
|
return if (time.isNullOrEmpty()) System.currentTimeMillis()
|
else TimeUtils.string2Millis(time, "yyyy-MM-dd")
|
}
|
|
fun getTime(time: Long): String {
|
return TimeUtils.millis2String(time, "yyyy-MM-dd")
|
}
|
}
|
|
}
|