mirror of
https://github.com/SimpleMobileTools/Simple-Clock.git
synced 2025-06-05 22:19:17 +02:00
show a toast message about the remaining time till the alarm, when activated
This commit is contained in:
@ -41,7 +41,7 @@ android {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'com.simplemobiletools:commons:3.15.0'
|
implementation 'com.simplemobiletools:commons:3.15.1'
|
||||||
implementation 'com.facebook.stetho:stetho:1.5.0'
|
implementation 'com.facebook.stetho:stetho:1.5.0'
|
||||||
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
|
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import android.support.v4.app.Fragment
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
|
import android.widget.Toast
|
||||||
import com.simplemobiletools.clock.R
|
import com.simplemobiletools.clock.R
|
||||||
import com.simplemobiletools.clock.activities.SimpleActivity
|
import com.simplemobiletools.clock.activities.SimpleActivity
|
||||||
import com.simplemobiletools.clock.adapters.AlarmsAdapter
|
import com.simplemobiletools.clock.adapters.AlarmsAdapter
|
||||||
@ -17,9 +18,11 @@ import com.simplemobiletools.commons.extensions.toast
|
|||||||
import com.simplemobiletools.commons.extensions.updateTextColors
|
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||||
import kotlinx.android.synthetic.main.fragment_alarm.view.*
|
import kotlinx.android.synthetic.main.fragment_alarm.view.*
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.math.pow
|
||||||
|
|
||||||
class AlarmFragment : Fragment(), ToggleAlarmInterface {
|
class AlarmFragment : Fragment(), ToggleAlarmInterface {
|
||||||
private val DEFAULT_ALARM_MINUTES = 480
|
private val DEFAULT_ALARM_MINUTES = 480
|
||||||
|
private val DAY_MINUTES = 1440
|
||||||
|
|
||||||
private var alarms = ArrayList<Alarm>()
|
private var alarms = ArrayList<Alarm>()
|
||||||
lateinit var view: ViewGroup
|
lateinit var view: ViewGroup
|
||||||
@ -80,6 +83,41 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
|
|||||||
private fun getClosestTriggerTimestamp(alarm: Alarm) {
|
private fun getClosestTriggerTimestamp(alarm: Alarm) {
|
||||||
val calendar = Calendar.getInstance()
|
val calendar = Calendar.getInstance()
|
||||||
calendar.firstDayOfWeek = Calendar.MONDAY
|
calendar.firstDayOfWeek = Calendar.MONDAY
|
||||||
val currentDay = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7
|
for (i in 0..7) {
|
||||||
|
val currentDay = (calendar.get(Calendar.DAY_OF_WEEK) + 5) % 7
|
||||||
|
val isCorrectDay = alarm.days and 2.0.pow(currentDay).toInt() != 0
|
||||||
|
val currentTimeInMinutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)
|
||||||
|
if (isCorrectDay && (alarm.timeInMinutes > currentTimeInMinutes || i > 0)) {
|
||||||
|
val triggerInMinutes = alarm.timeInMinutes - currentTimeInMinutes + (i * DAY_MINUTES)
|
||||||
|
showRemainingTimeMessage(triggerInMinutes)
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
calendar.add(Calendar.DAY_OF_MONTH, 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun showRemainingTimeMessage(triggerInMinutes: Int) {
|
||||||
|
val days = triggerInMinutes / DAY_MINUTES
|
||||||
|
val hours = (triggerInMinutes % DAY_MINUTES) / 60
|
||||||
|
val minutes = triggerInMinutes % 60
|
||||||
|
val timesString = StringBuilder()
|
||||||
|
if (days > 0) {
|
||||||
|
val daysString = String.format(activity!!.resources.getQuantityString(R.plurals.days, days, days))
|
||||||
|
timesString.append("$daysString, ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hours > 0) {
|
||||||
|
val hoursString = String.format(activity!!.resources.getQuantityString(R.plurals.hours, hours, hours))
|
||||||
|
timesString.append("$hoursString, ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (minutes > 0) {
|
||||||
|
val minutesString = String.format(activity!!.resources.getQuantityString(R.plurals.minutes, minutes, minutes))
|
||||||
|
timesString.append(minutesString)
|
||||||
|
}
|
||||||
|
|
||||||
|
val fullString = String.format(activity!!.getString(R.string.alarm_goes_off_in), timesString.toString().trim().trimEnd(','))
|
||||||
|
activity!!.toast(fullString, Toast.LENGTH_LONG)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user