forked from upstream-mirrors/quik
Move auto night mode logic to NightModeManager
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
}
|
||||
@@ -22,45 +22,18 @@ import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import common.di.appComponent
|
||||
import common.util.DateFormatter
|
||||
import common.util.Preferences
|
||||
import java.util.*
|
||||
import java.util.concurrent.TimeUnit
|
||||
import common.util.NightModeManager
|
||||
import javax.inject.Inject
|
||||
|
||||
class NightModeReceiver : BroadcastReceiver() {
|
||||
|
||||
@Inject lateinit var dateFormatter: DateFormatter
|
||||
@Inject lateinit var prefs: Preferences
|
||||
@Inject lateinit var nightModeManager: NightModeManager
|
||||
|
||||
init {
|
||||
appComponent.inject(this)
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
// 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)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Calendar set to the most recent occurrence of this time
|
||||
*/
|
||||
private fun getPreviousInstanceOfTime(time: String): Calendar {
|
||||
val calendar = dateFormatter.parseTime(time)
|
||||
val currentTime = System.currentTimeMillis() + TimeUnit.MINUTES.toMillis(10)
|
||||
|
||||
while (calendar.timeInMillis > currentTime) {
|
||||
calendar.add(Calendar.DAY_OF_YEAR, -1)
|
||||
}
|
||||
|
||||
return calendar
|
||||
nightModeManager.updateCurrentTheme()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user