#1341 - Fix UI glitches with widget

This commit is contained in:
moezbhatti
2018-11-18 20:13:48 -05:00
parent e610a373ca
commit 7bc0b2181b
7 changed files with 40 additions and 17 deletions
@@ -18,6 +18,8 @@
*/
package com.moez.QKSMS.manager
import android.appwidget.AppWidgetManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import com.klinker.android.send_message.BroadcastUtils
@@ -29,4 +31,13 @@ class WidgetManagerImpl @Inject constructor(private val context: Context) : Widg
BroadcastUtils.sendExplicitBroadcast(context, Intent(), WidgetManager.ACTION_NOTIFY_DATASET_CHANGED)
}
override fun updateTheme() {
val ids = AppWidgetManager.getInstance(context)
.getAppWidgetIds(ComponentName("com.moez.QKSMS", "com.moez.QKSMS.feature.widget.WidgetProvider"))
val intent = Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, ids)
BroadcastUtils.sendExplicitBroadcast(context, intent, AppWidgetManager.ACTION_APPWIDGET_UPDATE)
}
}
@@ -22,6 +22,7 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import com.moez.QKSMS.manager.WidgetManager
import com.moez.QKSMS.receiver.NightModeReceiver
import java.text.SimpleDateFormat
import java.util.*
@@ -32,7 +33,8 @@ import javax.inject.Singleton
@Singleton
class NightModeManager @Inject constructor(
private val context: Context,
private val prefs: Preferences) {
private val prefs: Preferences,
private val widgetManager: WidgetManager) {
fun updateCurrentTheme() {
// If night mode is not on auto, then there's nothing to do here
@@ -45,6 +47,7 @@ class NightModeManager @Inject constructor(
// If the last nightStart was more recent than the last nightEnd, then it's night time
prefs.night.set(nightStartTime > nightEndTime)
widgetManager.updateTheme()
}
fun updateNightMode(mode: Int) {
@@ -53,6 +56,7 @@ class NightModeManager @Inject constructor(
// If it's not on auto mode, set the appropriate night mode
if (mode != Preferences.NIGHT_MODE_AUTO) {
prefs.night.set(mode == Preferences.NIGHT_MODE_ON)
widgetManager.updateTheme()
}
updateAlarms()
@@ -26,4 +26,6 @@ interface WidgetManager {
fun updateUnreadCount()
fun updateTheme()
}
@@ -151,8 +151,6 @@
<receiver android:name=".feature.widget.WidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<intent-filter>
<action android:name="com.moez.QKSMS.intent.action.ACTION_NOTIFY_DATASET_CHANGED" />
</intent-filter>
@@ -23,6 +23,7 @@ import com.moez.QKSMS.common.Navigator
import com.moez.QKSMS.common.base.QkPresenter
import com.moez.QKSMS.common.util.BillingManager
import com.moez.QKSMS.common.util.Colors
import com.moez.QKSMS.manager.WidgetManager
import com.moez.QKSMS.util.Preferences
import com.uber.autodispose.kotlin.autoDisposable
import io.reactivex.rxkotlin.Observables
@@ -31,11 +32,12 @@ import javax.inject.Inject
import javax.inject.Named
class ThemePickerPresenter @Inject constructor(
@Named("threadId") threadId: Long,
prefs: Preferences,
@Named("threadId") private val threadId: Long,
private val billingManager: BillingManager,
private val colors: Colors,
private val navigator: Navigator
private val navigator: Navigator,
private val widgetManager: WidgetManager
) : QkPresenter<ThemePickerView, ThemePickerState>(ThemePickerState(threadId = threadId)) {
private val theme: Preference<Int> = prefs.theme(threadId)
@@ -50,7 +52,12 @@ class ThemePickerPresenter @Inject constructor(
// Update the theme when a material theme is clicked
view.themeSelected()
.autoDisposable(view.scope())
.subscribe { color -> theme.set(color) }
.subscribe { color ->
theme.set(color)
if (threadId == 0L) {
widgetManager.updateTheme()
}
}
// Update the color of the apply button
view.hsvThemeSelected()
@@ -73,6 +80,9 @@ class ThemePickerPresenter @Inject constructor(
view.showQksmsPlusSnackbar()
} else {
theme.set(color)
if (threadId == 0L) {
widgetManager.updateTheme()
}
}
}
.autoDisposable(view.scope())
@@ -62,21 +62,18 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
private var conversations: List<Conversation> = listOf()
private val appWidgetManager by lazy { AppWidgetManager.getInstance(context) }
// Cache colors, load lazily. This entire class is recreated when the widget is recreated,
// so if the theme changes then these values will be correct in the new instance
private val night by lazy { prefs.night.get() }
private val black by lazy { prefs.black.get() }
private val theme by lazy { colors.theme() }
private val background by lazy {
context.getColorCompat(when {
private val night get() = prefs.night.get()
private val black get() = prefs.black.get()
private val theme get() = colors.theme()
private val background
get() = context.getColorCompat(when {
night && black -> R.color.black
night && !black -> R.color.backgroundDark
else -> R.color.white
})
}
private val textPrimary by lazy { context.getColorCompat(if (night) R.color.textPrimaryDark else R.color.textPrimary) }
private val textSecondary by lazy { context.getColorCompat(if (night) R.color.textSecondaryDark else R.color.textSecondary) }
private val textTertiary by lazy { context.getColorCompat(if (night) R.color.textTertiaryDark else R.color.textTertiary) }
private val textPrimary get() = context.getColorCompat(if (night) R.color.textPrimaryDark else R.color.textPrimary)
private val textSecondary get() = context.getColorCompat(if (night) R.color.textSecondaryDark else R.color.textSecondary)
private val textTertiary get() = context.getColorCompat(if (night) R.color.textTertiaryDark else R.color.textTertiary)
override fun onCreate() {
appComponent.inject(this)
@@ -59,6 +59,7 @@ class WidgetProvider : AppWidgetProvider() {
override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray) {
super.onUpdate(context, appWidgetManager, appWidgetIds)
updateData(context)
for (appWidgetId in appWidgetIds) {
updateWidget(context, appWidgetId, isSmallWidget(appWidgetManager, appWidgetId))
}