package com.xianning.driver.utils.view
|
|
import android.graphics.Color
|
import android.view.Gravity
|
import cn.sinata.xldutils.utils.TimeUtils
|
import com.xianning.driver.R
|
import com.xianning.driver.base.BaseDialogFragment
|
import kotlinx.android.synthetic.main.dialog_filter_date.*
|
import java.util.*
|
|
class DateFilterDialog: BaseDialogFragment() {
|
override fun setContentView() = R.layout.dialog_filter_date
|
|
override fun setGravity() = Gravity.BOTTOM
|
|
|
var callback:StringCallback? = null
|
private var startDate:String? = null
|
private var endDate:String? = null
|
var isStart = true
|
|
override fun initView() {
|
startDate = arguments?.getString("start")
|
endDate = arguments?.getString("end")
|
if (startDate.isNullOrEmpty())
|
startDate = TimeUtils.getCurrentDate()
|
if (endDate.isNullOrEmpty())
|
endDate = TimeUtils.getCurrentDate()
|
start.text = startDate
|
end.text = endDate
|
start.setOnClickListener {
|
isStart = true
|
checkBtn()
|
}
|
end.setOnClickListener {
|
isStart = false
|
checkBtn()
|
}
|
initYear()
|
initMonth()
|
initDay(true)
|
wv_1.setOnWheelViewListener { selectedIndex, item ->
|
initDay(false)
|
}
|
wv_2.setOnWheelViewListener { selectedIndex, item ->
|
initDay(false)
|
}
|
wv_3.setOnWheelViewListener { selectedIndex, item ->
|
inputDate()
|
}
|
|
tv_cancel.setOnClickListener { dismissAllowingStateLoss() }
|
tv_sure.setOnClickListener {
|
callback?.onResult(startDate,endDate)
|
}
|
}
|
|
private fun inputDate(){
|
val y = wv_1.seletedItem.substring(0, 4).toInt()
|
val m = wv_2.seletedItem.substring(0, wv_2.seletedItem.length-1)
|
val d = wv_3.seletedItem.substring(0, wv_3.seletedItem.length-1)
|
if (isStart){
|
startDate = "%d/%02d/%02d".format(y,m.toInt(),d.toInt())
|
start.text = startDate
|
}
|
else{
|
endDate = "%d/%02d/%02d".format(y,m.toInt(),d.toInt())
|
end.text = endDate
|
}
|
}
|
|
private fun initDay(isFirst:Boolean) {
|
var selectIndex = 0
|
if (!isFirst){
|
selectIndex = wv_3.seletedIndex
|
}
|
val c = Calendar.getInstance()
|
val day = c.get(Calendar.DAY_OF_MONTH)
|
val year = wv_1.seletedItem.substring(0,4).toInt()
|
val month = wv_2.seletedItem.substring(0,wv_2.seletedItem.length-1).toInt()
|
c.set(Calendar.YEAR,year)
|
c.set(Calendar.MONTH,month-1)
|
val actualMaximum = c.getActualMaximum(Calendar.DAY_OF_MONTH)
|
val list = (1..actualMaximum).map { "${it}日" }
|
wv_3.setItems(list)
|
if (isFirst){
|
wv_3.setSeletion(day-1)
|
}else{
|
// if (selectIndex+1 > actualMaximum){
|
// wv_3.setSeletion(list.lastIndex)
|
// }else{
|
// wv_3.setSeletion(0)
|
// }
|
wv_3.setSeletion(0)
|
}
|
if (!isFirst)
|
inputDate()
|
}
|
|
private fun initMonth() {
|
val list = (1..12).map { "${it}月" }
|
wv_2.setItems(list)
|
wv_2.setSeletion(list.lastIndex)
|
val c = Calendar.getInstance()
|
wv_2.setSeletion(c.get(Calendar.MONTH))
|
}
|
|
private fun initYear() {
|
val c = Calendar.getInstance()
|
val list = (2024..c.get(Calendar.YEAR)).map { "${it}年" }
|
wv_1.setItems(list)
|
wv_1.setSeletion(list.lastIndex)
|
}
|
|
private fun checkBtn(){
|
if (isStart){
|
start.setTextColor(Color.parseColor("#1677FF"))
|
end.setTextColor(Color.parseColor("#333333"))
|
start.setBackgroundResource(R.drawable.bg_blue_line_4)
|
end.setBackgroundResource(R.drawable.bg_gray_line_4)
|
start.setCompoundDrawablesWithIntrinsicBounds(0,0,R.mipmap.ic_date_blue,0)
|
end.setCompoundDrawablesWithIntrinsicBounds(0,0,R.mipmap.ic_date,0)
|
}else{
|
end.setTextColor(Color.parseColor("#1677FF"))
|
start.setTextColor(Color.parseColor("#333333"))
|
end.setBackgroundResource(R.drawable.bg_blue_line_4)
|
start.setBackgroundResource(R.drawable.bg_gray_line_4)
|
end.setCompoundDrawablesWithIntrinsicBounds(0,0,R.mipmap.ic_date_blue,0)
|
start.setCompoundDrawablesWithIntrinsicBounds(0,0,R.mipmap.ic_date,0)
|
}
|
}
|
|
interface StringCallback{
|
fun onResult(start:String?,end:String?)
|
}
|
}
|