Move auto night mode logic to NightModeManager

This commit is contained in:
Moez Bhatti
2018-02-07 03:11:05 -05:00
parent f08bd833f8
commit adce8d782c
2 changed files with 31 additions and 30 deletions
@@ -24,6 +24,7 @@ import android.content.Context
import android.content.Intent
import presentation.receiver.NightModeReceiver
import java.util.*
import java.util.concurrent.TimeUnit
import javax.inject.Inject
import javax.inject.Singleton
@@ -33,6 +34,19 @@ class NightModeManager @Inject constructor(
private val dateFormatter: DateFormatter,
private val prefs: Preferences) {
fun updateCurrentTheme() {
// If night mode is not on auto, then there's nothing to do here
if (prefs.nightMode.get() != Preferences.NIGHT_MODE_AUTO) {
return
}
val nightStartTime = getPreviousInstanceOfTime(prefs.nightStart.get())
val nightEndTime = getPreviousInstanceOfTime(prefs.nightEnd.get())
// If the last nightStart was more recent than the last nightEnd, then it's night time
prefs.night.set(nightStartTime > nightEndTime)
}
fun updateNightMode(mode: Int) {
prefs.nightMode.set(mode)
@@ -75,4 +89,18 @@ class NightModeManager @Inject constructor(
}
}
/**
* Returns a Calendar set to the most recent occurrence of this time
*/
private fun getPreviousInstanceOfTime(time: String): Calendar {
val currentTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10)
val calendar = dateFormatter.parseTime(time)
while (calendar.timeInMillis > currentTime) {
calendar.add(Calendar.DAY_OF_YEAR, -1)
}
return calendar
}
}