show a toast message about the remaining time till the alarm, when activated
This commit is contained in:
parent
bbdf015823
commit
0cda6a133e
|
@ -41,7 +41,7 @@ android {
|
|||
}
|
||||
|
||||
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.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.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import com.simplemobiletools.clock.R
|
||||
import com.simplemobiletools.clock.activities.SimpleActivity
|
||||
import com.simplemobiletools.clock.adapters.AlarmsAdapter
|
||||
|
@ -17,9 +18,11 @@ import com.simplemobiletools.commons.extensions.toast
|
|||
import com.simplemobiletools.commons.extensions.updateTextColors
|
||||
import kotlinx.android.synthetic.main.fragment_alarm.view.*
|
||||
import java.util.*
|
||||
import kotlin.math.pow
|
||||
|
||||
class AlarmFragment : Fragment(), ToggleAlarmInterface {
|
||||
private val DEFAULT_ALARM_MINUTES = 480
|
||||
private val DAY_MINUTES = 1440
|
||||
|
||||
private var alarms = ArrayList<Alarm>()
|
||||
lateinit var view: ViewGroup
|
||||
|
@ -80,6 +83,41 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface {
|
|||
private fun getClosestTriggerTimestamp(alarm: Alarm) {
|
||||
val calendar = Calendar.getInstance()
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue