#1143 - Fix crash when phone uses 24 hour time format

This commit is contained in:
Moez Bhatti
2018-05-14 15:10:45 -04:00
parent bf8cab3c8b
commit 72b4bce1d8
4 changed files with 40 additions and 27 deletions
@@ -71,18 +71,4 @@ class DateFormatter @Inject constructor(val context: Context) {
}.format(date)
}
fun formatTime(hour: Int, minute: Int): String {
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY, hour)
calendar.set(Calendar.MINUTE, minute)
val simpleDateFormat = getFormatter("h:mm a")
return simpleDateFormat.format(calendar.time)
}
fun parseTime(time: String): Calendar {
val simpleDateFormat = if (time.contains(" ")) getFormatter("h:mm a") else SimpleDateFormat("H:mm", Locale.getDefault())
return Calendar.getInstance().apply { this.time = simpleDateFormat.parse(time) }
}
}
@@ -24,6 +24,8 @@ import android.content.Context
import android.content.Intent
import receiver.NightModeReceiver
import util.Preferences
import util.tryOrNull
import java.text.SimpleDateFormat
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject
@@ -32,7 +34,6 @@ import javax.inject.Singleton
@Singleton
class NightModeManager @Inject constructor(
private val context: Context,
private val dateFormatter: DateFormatter,
private val prefs: Preferences) {
fun updateCurrentTheme() {
@@ -59,7 +60,17 @@ class NightModeManager @Inject constructor(
updateAlarms()
}
fun updateAlarms() {
fun setNightStart(hour: Int, minute: Int) {
prefs.nightStart.set("$hour:$minute")
updateAlarms()
}
fun setNightEnd(hour: Int, minute: Int) {
prefs.nightEnd.set("$hour:$minute")
updateAlarms()
}
private fun updateAlarms() {
val dayCalendar = createCalendar(prefs.nightEnd.get())
val day = Intent(context, NightModeReceiver::class.java)
val dayIntent = PendingIntent.getBroadcast(context, 0, day, 0)
@@ -82,7 +93,7 @@ class NightModeManager @Inject constructor(
}
private fun createCalendar(time: String): Calendar {
val calendar = dateFormatter.parseTime(time)
val calendar = parseTime(time)
return Calendar.getInstance().apply {
set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY))
@@ -90,6 +101,20 @@ class NightModeManager @Inject constructor(
}
}
/**
* Parses the hour and minute out of the [time], which should be formatted h:mm
*/
fun parseTime(time: String): Calendar {
return tryOrNull {
val parsedTime = SimpleDateFormat("H:mm", Locale.US).parse(time)
Calendar.getInstance().apply { this.time = parsedTime }
} ?: tryOrNull {
// Parse the legacy timestamp format (<=3.1.3)
val parsedTime = SimpleDateFormat("h:mm a", Locale.US).parse(time)
Calendar.getInstance().apply { this.time = parsedTime }
} ?: Calendar.getInstance().apply { set(Calendar.HOUR_OF_DAY, 18) }
}
/**
* Returns a Calendar set to the most recent occurrence of this time
*/
@@ -59,9 +59,15 @@ class SettingsViewModel @Inject constructor(
}
disposables += prefs.nightStart.asObservable()
.map { time -> nightModeManager.parseTime(time) }
.map { calendar -> calendar.timeInMillis }
.map { millis -> dateFormatter.getTimestamp(millis) }
.subscribe { nightStart -> newState { it.copy(nightStart = nightStart) } }
disposables += prefs.nightEnd.asObservable()
.map { time -> nightModeManager.parseTime(time) }
.map { calendar -> calendar.timeInMillis }
.map { millis -> dateFormatter.getTimestamp(millis) }
.subscribe { nightEnd -> newState { it.copy(nightEnd = nightEnd) } }
disposables += prefs.black.asObservable()
@@ -117,12 +123,12 @@ class SettingsViewModel @Inject constructor(
R.id.night -> view.showNightModeDialog()
R.id.nightStart -> {
val date = dateFormatter.parseTime(prefs.nightStart.get())
val date = nightModeManager.parseTime(prefs.nightStart.get())
view.showStartTimePicker(date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE))
}
R.id.nightEnd -> {
val date = dateFormatter.parseTime(prefs.nightEnd.get())
val date = nightModeManager.parseTime(prefs.nightEnd.get())
view.showEndTimePicker(date.get(Calendar.HOUR_OF_DAY), date.get(Calendar.MINUTE))
}
@@ -173,16 +179,12 @@ class SettingsViewModel @Inject constructor(
.subscribe { navigator.showQksmsPlusActivity() }
view.startTimeSelectedIntent
.doOnNext { nightModeManager.updateAlarms() }
.map { dateFormatter.formatTime(it.first, it.second) }
.autoDisposable(view.scope())
.subscribe { prefs.nightStart.set(it) }
.subscribe { nightModeManager.setNightStart(it.first, it.second) }
view.endTimeSelectedIntent
.doOnNext { nightModeManager.updateAlarms() }
.map { dateFormatter.formatTime(it.first, it.second) }
.autoDisposable(view.scope())
.subscribe { prefs.nightEnd.set(it) }
.subscribe { nightModeManager.setNightEnd(it.first, it.second) }
view.textSizeSelectedIntent
.autoDisposable(view.scope())