File tree Expand file tree Collapse file tree
app/src/main/java/com/omarea/common/ui Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -125,17 +125,30 @@ object BannerNotificationManager {
125125 toast.view = view
126126 // Offset nhỏ để không dính sát mép trên / bị status bar che
127127 toast.setGravity(Gravity .TOP or Gravity .CENTER_HORIZONTAL , 0 , (24 * density).toInt())
128- toast.show()
129128 currentToast = toast
130129
131- val runnable = Runnable {
132- try {
133- toast.cancel()
134- } catch (e: Exception ) {
130+ // Toast chỉ hỗ trợ 2 mốc thời gian cố định (LENGTH_SHORT ~2s / LENGTH_LONG ~3.5s),
131+ // không có API để đặt số ms tùy ý. Để đạt đúng `durationMs` yêu cầu, gọi lại show()
132+ // theo chu kỳ (làm mới thời gian hiển thị) cho tới khi đủ thời lượng mong muốn.
133+ val startTime = android.os.SystemClock .uptimeMillis()
134+ val refreshInterval = 3000L // nhỏ hơn LENGTH_LONG (~3.5s) để không bị chớp tắt giữa 2 lần show
135+ lateinit var keepAliveRunnable: Runnable
136+ keepAliveRunnable = Runnable {
137+ val elapsed = android.os.SystemClock .uptimeMillis() - startTime
138+ if (elapsed < req.durationMs) {
139+ toast.show()
140+ val remaining = req.durationMs - elapsed
141+ mainHandler.postDelayed(keepAliveRunnable, minOf(refreshInterval, remaining))
142+ } else {
143+ try {
144+ toast.cancel()
145+ } catch (e: Exception ) {
146+ }
147+ currentToast = null
148+ showNext()
135149 }
136- currentToast = null
137- showNext()
138150 }
139- mainHandler.postDelayed(runnable, req.durationMs)
151+ toast.show()
152+ mainHandler.postDelayed(keepAliveRunnable, minOf(refreshInterval, req.durationMs))
140153 }
141- }
154+ }
You can’t perform that action at this time.
0 commit comments