18034727885
2026-05-23 293183266e941692005262d76f0bcff05d6384d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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")
        }
    }
 
}