properly format the next alarm time

This commit is contained in:
tibbi 2018-03-13 16:22:24 +01:00
parent cb1156fe01
commit b4a1d6fce8
2 changed files with 43 additions and 9 deletions

View file

@ -183,10 +183,7 @@ fun Context.getFormattedTime(passedSeconds: Int, showSeconds: Boolean, makeAmPmS
val seconds = passedSeconds % 60
return if (!use24HourFormat) {
val appendable = getString(if (hours >= 12) R.string.p_m else R.string.a_m)
val newHours = if (hours == 0 || hours == 12) 12 else hours % 12
val formattedTime = "${formatTime(showSeconds, use24HourFormat, newHours, minutes, seconds)} $appendable"
val formattedTime = formatTo12HourFormat(showSeconds, hours, minutes, seconds)
val spannableTime = SpannableString(formattedTime)
val amPmMultiplier = if (makeAmPmSmaller) 0.4f else 1f
spannableTime.setSpan(RelativeSizeSpan(amPmMultiplier), spannableTime.length - 5, spannableTime.length, 0)
@ -196,3 +193,9 @@ fun Context.getFormattedTime(passedSeconds: Int, showSeconds: Boolean, makeAmPmS
SpannableString(formattedTime)
}
}
fun Context.formatTo12HourFormat(showSeconds: Boolean, hours: Int, minutes: Int, seconds: Int): String {
val appendable = getString(if (hours >= 12) R.string.p_m else R.string.a_m)
val newHours = if (hours == 0 || hours == 12) 12 else hours % 12
return "${formatTime(showSeconds, false, newHours, minutes, seconds)} $appendable"
}

View file

@ -10,10 +10,7 @@ import android.provider.Settings
import android.widget.RemoteViews
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SplashActivity
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.extensions.getFormattedDate
import com.simplemobiletools.clock.extensions.getFormattedTime
import com.simplemobiletools.clock.extensions.scheduleNextWidgetUpdate
import com.simplemobiletools.clock.extensions.*
import com.simplemobiletools.commons.extensions.getColoredBitmap
import com.simplemobiletools.commons.extensions.setBackgroundColor
import com.simplemobiletools.commons.extensions.setText
@ -61,7 +58,7 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
setText(R.id.widget_date, context.getFormattedDate(calendar))
setVisibleIf(R.id.widget_time_am_pm, !use24HourFormat)
val nextAlarm = Settings.System.getString(context.contentResolver, Settings.System.NEXT_ALARM_FORMATTED)
val nextAlarm = getFormattedNextAlarm(context)
setVisibleIf(R.id.widget_next_alarm, nextAlarm.isNotEmpty())
setText(R.id.widget_next_alarm, nextAlarm)
}
@ -91,4 +88,38 @@ class MyWidgetDateTimeProvider : AppWidgetProvider() {
views.setOnClickPendingIntent(R.id.widget_date_time_holder, pendingIntent)
}
}
private fun getFormattedNextAlarm(context: Context): String {
val nextAlarm = Settings.System.getString(context.contentResolver, Settings.System.NEXT_ALARM_FORMATTED)
if (nextAlarm.isEmpty()) {
return ""
}
val isIn24HoursFormat = !nextAlarm.endsWith(".")
return when {
context.config.use24HourFormat && !isIn24HoursFormat -> {
val dayTime = nextAlarm.split(" ")
val times = dayTime[1].split(":")
val hours = times[0].toInt()
val minutes = times[1].toInt()
val seconds = 0
val isAM = dayTime[2].startsWith("a", true)
val newHours = when {
hours == 12 && isAM -> 0
hours == 12 && !isAM -> 12
!isAM -> hours + 12
else -> hours
}
formatTime(false, true, newHours, minutes, seconds)
}
!context.config.use24HourFormat && isIn24HoursFormat -> {
val times = nextAlarm.split(" ")[1].split(":")
val hours = times[0].toInt()
val minutes = times[1].toInt()
val seconds = 0
context.formatTo12HourFormat(false, hours, minutes, seconds)
}
else -> nextAlarm
}
}
}