From 6ac5c6a0b4b7dac8edd4454b0efad1b80ff088af Mon Sep 17 00:00:00 2001 From: Mateusz Idziejczak <53345435+PanTajemnic@users.noreply.github.com> Date: Sun, 10 May 2020 12:00:21 +0200 Subject: [PATCH] Add widget system theme option (#759) --- .../LuckyNumberWidgetConfigureActivity.kt | 8 ++++- .../LuckyNumberWidgetConfigurePresenter.kt | 2 +- .../LuckyNumberWidgetProvider.kt | 34 +++++++++++++------ .../TimetableWidgetConfigureActivity.kt | 8 ++++- .../timetablewidget/TimetableWidgetFactory.kt | 28 ++++++++------- .../TimetableWidgetProvider.kt | 22 ++++++++++-- .../res/layout/activity_widget_configure.xml | 2 -- .../main/res/layout/item_widget_timetable.xml | 22 ++++++------ .../res/layout/item_widget_timetable_dark.xml | 33 +++++------------- .../layout/item_widget_timetable_small.xml | 5 +-- app/src/main/res/layout/widget_timetable.xml | 7 ---- .../main/res/layout/widget_timetable_dark.xml | 7 ---- app/src/main/res/values-de/strings.xml | 1 + app/src/main/res/values-pl/strings.xml | 1 + app/src/main/res/values-ru/strings.xml | 1 + app/src/main/res/values-uk/strings.xml | 1 + app/src/main/res/values/api_hosts.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 18 files changed, 99 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt index d7d5c4ff1..961ac6338 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigureActivity.kt @@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS import android.content.Intent +import android.os.Build import android.os.Bundle import android.widget.Toast import androidx.appcompat.app.AlertDialog @@ -14,6 +15,7 @@ import io.github.wulkanowy.databinding.ActivityWidgetConfigureBinding import io.github.wulkanowy.ui.base.BaseActivity import io.github.wulkanowy.ui.base.WidgetConfigureAdapter import io.github.wulkanowy.ui.modules.login.LoginActivity +import io.github.wulkanowy.utils.AppInfo import javax.inject.Inject class LuckyNumberWidgetConfigureActivity : @@ -26,6 +28,9 @@ class LuckyNumberWidgetConfigureActivity : @Inject override lateinit var presenter: LuckyNumberWidgetConfigurePresenter + @Inject + lateinit var appInfo: AppInfo + private var dialog: AlertDialog? = null override fun onCreate(savedInstanceState: Bundle?) { @@ -48,10 +53,11 @@ class LuckyNumberWidgetConfigureActivity : } override fun showThemeDialog() { - val items = arrayOf( + var items = arrayOf( getString(R.string.widget_timetable_theme_light), getString(R.string.widget_timetable_theme_dark) ) + if (appInfo.systemVersion >= Build.VERSION_CODES.Q) items += (getString(R.string.widget_timetable_theme_system)) dialog = AlertDialog.Builder(this, R.style.WulkanowyTheme_WidgetAccountSwitcher) .setTitle(R.string.widget_timetable_theme_title) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt index 1bb7447a2..468f9b575 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetConfigurePresenter.kt @@ -40,7 +40,7 @@ class LuckyNumberWidgetConfigurePresenter @Inject constructor( registerStudent(selectedStudent) } - fun onDismissThemeView(){ + fun onDismissThemeView() { view?.finishView() } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt index e4b1072b6..55a048b32 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/luckynumberwidget/LuckyNumberWidgetProvider.kt @@ -8,6 +8,7 @@ import android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH import android.appwidget.AppWidgetProvider import android.content.Context import android.content.Intent +import android.content.res.Configuration import android.os.Bundle import android.view.View.GONE import android.view.View.VISIBLE @@ -62,14 +63,12 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { override fun onUpdate(context: Context, appWidgetManager: AppWidgetManager, appWidgetIds: IntArray?) { super.onUpdate(context, appWidgetManager, appWidgetIds) appWidgetIds?.forEach { appWidgetId -> - val savedTheme = sharedPref.getLong(getThemeWidgetKey(appWidgetId), 0) - val layoutId = if (savedTheme == 0L) R.layout.widget_luckynumber else R.layout.widget_luckynumber_dark val luckyNumber = getLuckyNumber(sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0), appWidgetId) val appIntent = PendingIntent.getActivity(context, MainView.Section.LUCKY_NUMBER.id, MainActivity.getStartIntent(context, MainView.Section.LUCKY_NUMBER, true), FLAG_UPDATE_CURRENT) - val remoteView = RemoteViews(context.packageName, layoutId).apply { + val remoteView = RemoteViews(context.packageName, getCorrectLayoutId(appWidgetId, context)).apply { setTextViewText(R.id.luckyNumberWidgetNumber, luckyNumber?.luckyNumber?.toString() ?: "#") setOnClickPendingIntent(R.id.luckyNumberWidgetContainer, appIntent) } @@ -82,17 +81,19 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { override fun onDeleted(context: Context?, appWidgetIds: IntArray?) { super.onDeleted(context, appWidgetIds) appWidgetIds?.forEach { appWidgetId -> - if (appWidgetId != 0) sharedPref.delete(getStudentWidgetKey(appWidgetId)) + with(sharedPref) { + delete(getHeightWidgetKey(appWidgetId)) + delete(getStudentWidgetKey(appWidgetId)) + delete(getThemeWidgetKey(appWidgetId)) + delete(getWidthWidgetKey(appWidgetId)) + } } } override fun onAppWidgetOptionsChanged(context: Context, appWidgetManager: AppWidgetManager, appWidgetId: Int, newOptions: Bundle?) { super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions) - val savedTheme = sharedPref.getLong(getThemeWidgetKey(appWidgetId), 0) - val layoutId = if (savedTheme == 0L) R.layout.widget_luckynumber else R.layout.widget_luckynumber_dark - - val remoteView = RemoteViews(context.packageName, layoutId) + val remoteView = RemoteViews(context.packageName, getCorrectLayoutId(appWidgetId, context)) setStyles(remoteView, appWidgetId, newOptions) appWidgetManager.updateAppWidget(appWidgetId, remoteView) @@ -102,8 +103,10 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { val width = options?.getInt(OPTION_APPWIDGET_MIN_WIDTH) ?: sharedPref.getLong(getWidthWidgetKey(appWidgetId), 74).toInt() val height = options?.getInt(OPTION_APPWIDGET_MAX_HEIGHT) ?: sharedPref.getLong(getHeightWidgetKey(appWidgetId), 74).toInt() - sharedPref.putLong(getWidthWidgetKey(appWidgetId), width.toLong()) - sharedPref.putLong(getHeightWidgetKey(appWidgetId), height.toLong()) + with(sharedPref) { + putLong(getWidthWidgetKey(appWidgetId), width.toLong()) + putLong(getHeightWidgetKey(appWidgetId), height.toLong()) + } val rows = getCellsForSize(height) val cols = getCellsForSize(width) @@ -162,4 +165,15 @@ class LuckyNumberWidgetProvider : AppWidgetProvider() { null } } + + private fun getCorrectLayoutId(appWidgetId: Int, context: Context): Int { + val savedTheme = sharedPref.getLong(getThemeWidgetKey(appWidgetId), 0) + val isSystemDarkMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES + + return if (savedTheme == 1L || (savedTheme == 2L && isSystemDarkMode)) { + R.layout.widget_luckynumber_dark + } else { + R.layout.widget_luckynumber + } + } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt index 75548c9cc..3dc7a3a8c 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetConfigureActivity.kt @@ -4,6 +4,7 @@ import android.appwidget.AppWidgetManager.ACTION_APPWIDGET_UPDATE import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_ID import android.appwidget.AppWidgetManager.EXTRA_APPWIDGET_IDS import android.content.Intent +import android.os.Build import android.os.Bundle import android.widget.Toast import android.widget.Toast.LENGTH_LONG @@ -16,6 +17,7 @@ import io.github.wulkanowy.ui.base.BaseActivity import io.github.wulkanowy.ui.base.WidgetConfigureAdapter import io.github.wulkanowy.ui.modules.login.LoginActivity import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.EXTRA_FROM_PROVIDER +import io.github.wulkanowy.utils.AppInfo import javax.inject.Inject class TimetableWidgetConfigureActivity : @@ -28,6 +30,9 @@ class TimetableWidgetConfigureActivity : @Inject override lateinit var presenter: TimetableWidgetConfigurePresenter + @Inject + lateinit var appInfo: AppInfo + private var dialog: AlertDialog? = null override fun onCreate(savedInstanceState: Bundle?) { @@ -50,10 +55,11 @@ class TimetableWidgetConfigureActivity : } override fun showThemeDialog() { - val items = arrayOf( + var items = arrayOf( getString(R.string.widget_timetable_theme_light), getString(R.string.widget_timetable_theme_dark) ) + if (appInfo.systemVersion >= Build.VERSION_CODES.Q) items += getString(R.string.widget_timetable_theme_system) dialog = AlertDialog.Builder(this, R.style.WulkanowyTheme_WidgetAccountSwitcher) .setTitle(R.string.widget_timetable_theme_title) diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt index fb8c59586..dd223ad8b 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetFactory.kt @@ -18,9 +18,9 @@ import io.github.wulkanowy.data.repositories.preferences.PreferencesRepository import io.github.wulkanowy.data.repositories.semester.SemesterRepository import io.github.wulkanowy.data.repositories.student.StudentRepository import io.github.wulkanowy.data.repositories.timetable.TimetableRepository +import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getCurrentThemeWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getDateWidgetKey import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getStudentWidgetKey -import io.github.wulkanowy.ui.modules.timetablewidget.TimetableWidgetProvider.Companion.getThemeWidgetKey import io.github.wulkanowy.utils.SchedulersProvider import io.github.wulkanowy.utils.getCompatColor import io.github.wulkanowy.utils.toFormattedString @@ -41,9 +41,7 @@ class TimetableWidgetFactory( private var lessons = emptyList() - private var savedTheme: Long? = null - - private var layoutId: Int? = null + private var savedCurrentTheme: Long? = null private var primaryColor: Int? = null @@ -71,28 +69,32 @@ class TimetableWidgetFactory( val studentId = sharedPref.getLong(getStudentWidgetKey(appWidgetId), 0) updateTheme(appWidgetId) - updateLessons(date, studentId) } } private fun updateTheme(appWidgetId: Int) { - savedTheme = sharedPref.getLong(getThemeWidgetKey(appWidgetId), 0) - layoutId = if (savedTheme == 0L) R.layout.item_widget_timetable else R.layout.item_widget_timetable_dark + savedCurrentTheme = sharedPref.getLong(getCurrentThemeWidgetKey(appWidgetId), 0) - primaryColor = if (savedTheme == 0L) R.color.colorPrimary else R.color.colorPrimaryLight - textColor = if (savedTheme == 0L) android.R.color.black else android.R.color.white - timetableChangeColor = if (savedTheme == 0L) R.color.timetable_change_dark else R.color.timetable_change_light + if (savedCurrentTheme == 0L) { + primaryColor = R.color.colorPrimary + textColor = android.R.color.black + timetableChangeColor = R.color.timetable_change_dark + } else { + primaryColor = R.color.colorPrimaryLight + textColor = android.R.color.white + timetableChangeColor = R.color.timetable_change_light + } } private fun getItemLayout(lesson: Timetable): Int { return when { prefRepository.showWholeClassPlan == "small" && !lesson.isStudentPlan -> { - if (savedTheme == 0L) R.layout.item_widget_timetable_small + if (savedCurrentTheme == 0L) R.layout.item_widget_timetable_small else R.layout.item_widget_timetable_small_dark } - savedTheme == 0L -> R.layout.item_widget_timetable - else -> R.layout.item_widget_timetable_dark + savedCurrentTheme == 1L -> R.layout.item_widget_timetable_dark + else -> R.layout.item_widget_timetable } } diff --git a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt index 62192a1b0..79888f83d 100644 --- a/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt +++ b/app/src/main/java/io/github/wulkanowy/ui/modules/timetablewidget/TimetableWidgetProvider.kt @@ -13,6 +13,7 @@ import android.content.Context import android.content.Intent import android.content.Intent.FLAG_ACTIVITY_CLEAR_TASK import android.content.Intent.FLAG_ACTIVITY_NEW_TASK +import android.content.res.Configuration import android.widget.RemoteViews import dagger.android.AndroidInjection import io.github.wulkanowy.R @@ -71,6 +72,8 @@ class TimetableWidgetProvider : BroadcastReceiver() { fun getStudentWidgetKey(appWidgetId: Int) = "timetable_widget_student_$appWidgetId" fun getThemeWidgetKey(appWidgetId: Int) = "timetable_widget_theme_$appWidgetId" + + fun getCurrentThemeWidgetKey(appWidgetId: Int) = "timetable_widget_current_theme_$appWidgetId" } override fun onReceive(context: Context, intent: Intent) { @@ -110,14 +113,23 @@ class TimetableWidgetProvider : BroadcastReceiver() { with(sharedPref) { delete(getStudentWidgetKey(appWidgetId)) delete(getDateWidgetKey(appWidgetId)) + delete(getThemeWidgetKey(appWidgetId)) + delete(getCurrentThemeWidgetKey(appWidgetId)) } } } @SuppressLint("DefaultLocale") private fun updateWidget(context: Context, appWidgetId: Int, date: LocalDate, student: Student?) { - val savedTheme = sharedPref.getLong(getThemeWidgetKey(appWidgetId), 0) - val layoutId = if (savedTheme == 0L) R.layout.widget_timetable else R.layout.widget_timetable_dark + val savedConfigureTheme = sharedPref.getLong(getThemeWidgetKey(appWidgetId), 0) + val isSystemDarkMode = context.resources.configuration.uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES + var currentTheme = 0L + var layoutId = R.layout.widget_timetable + + if (savedConfigureTheme == 1L || (savedConfigureTheme == 2L && isSystemDarkMode)) { + currentTheme = 1L + layoutId = R.layout.widget_timetable_dark + } val nextNavIntent = createNavIntent(context, appWidgetId, appWidgetId, BUTTON_NEXT) val prevNavIntent = createNavIntent(context, -appWidgetId, appWidgetId, BUTTON_PREV) @@ -150,7 +162,11 @@ class TimetableWidgetProvider : BroadcastReceiver() { setPendingIntentTemplate(R.id.timetableWidgetList, appIntent) } - sharedPref.putLong(getDateWidgetKey(appWidgetId), date.toEpochDay(), true) + with(sharedPref) { + putLong(getCurrentThemeWidgetKey(appWidgetId), currentTheme) + putLong(getDateWidgetKey(appWidgetId), date.toEpochDay(), true) + } + with(appWidgetManager) { notifyAppWidgetViewDataChanged(appWidgetId, R.id.timetableWidgetList) updateAppWidget(appWidgetId, remoteView) diff --git a/app/src/main/res/layout/activity_widget_configure.xml b/app/src/main/res/layout/activity_widget_configure.xml index 548b86041..9a463284e 100644 --- a/app/src/main/res/layout/activity_widget_configure.xml +++ b/app/src/main/res/layout/activity_widget_configure.xml @@ -10,9 +10,7 @@ android:layout_width="match_parent" android:layout_height="64dp" android:paddingStart="24dp" - android:paddingLeft="24dp" android:paddingEnd="24dp" - android:paddingRight="24dp" android:text="@string/account_title" android:textSize="20sp" android:textStyle="bold" diff --git a/app/src/main/res/layout/item_widget_timetable.xml b/app/src/main/res/layout/item_widget_timetable.xml index c69bade8a..33f686dac 100644 --- a/app/src/main/res/layout/item_widget_timetable.xml +++ b/app/src/main/res/layout/item_widget_timetable.xml @@ -12,12 +12,12 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" - android:paddingTop="6dp" - android:paddingBottom="6dp" - android:paddingLeft="6dp" android:paddingStart="6dp" + android:paddingLeft="6dp" + android:paddingTop="6dp" android:paddingEnd="12dp" - android:paddingRight="12dp"> + android:paddingRight="12dp" + android:paddingBottom="6dp"> + tools:visibility="gone" /> + android:paddingBottom="6dp"> + tools:visibility="gone" /> + @@ -60,7 +55,6 @@ android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="10dp" - android:layout_marginLeft="10dp" android:backgroundTint="@color/colorPrimaryDark" android:contentDescription="@string/all_prev" android:src="@drawable/ic_widget_chevron" @@ -72,7 +66,6 @@ android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@id/timetableWidgetPrev" - android:layout_toRightOf="@id/timetableWidgetPrev" android:backgroundTint="@color/colorPrimaryDark" android:contentDescription="@string/all_next" android:rotation="180" diff --git a/app/src/main/res/layout/widget_timetable_dark.xml b/app/src/main/res/layout/widget_timetable_dark.xml index 3a301eb94..5533eaeee 100644 --- a/app/src/main/res/layout/widget_timetable_dark.xml +++ b/app/src/main/res/layout/widget_timetable_dark.xml @@ -16,9 +16,7 @@ android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toStartOf="@id/timetableWidgetAccount" - android:layout_toLeftOf="@id/timetableWidgetAccount" android:layout_toEndOf="@id/timetableWidgetNext" - android:layout_toRightOf="@id/timetableWidgetNext" android:orientation="vertical"> @@ -60,7 +55,6 @@ android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_marginStart="10dp" - android:layout_marginLeft="10dp" android:backgroundTint="@color/colorWidgetNavButton" android:contentDescription="@string/all_prev" android:src="@drawable/ic_widget_chevron" @@ -72,7 +66,6 @@ android:layout_height="wrap_content" android:layout_centerVertical="true" android:layout_toEndOf="@id/timetableWidgetPrev" - android:layout_toRightOf="@id/timetableWidgetPrev" android:backgroundTint="@color/colorWidgetNavButton" android:contentDescription="@string/all_next" android:rotation="180" diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index e294593ca..8af2ed6f0 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -283,6 +283,7 @@ Thema wählen Licht Dunkel + Systemthema Erscheinungsbild Standard Ansicht diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 30d80acdb..82aa81180 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -303,6 +303,7 @@ Wybierz motyw Jasny Ciemny + Motyw systemu Wygląd Domyślny widok diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index a4666d730..426bfe241 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -303,6 +303,7 @@ Выбрать тему Светлая Тёмная + Системная тема Вид Окно по умолчанию diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index e1117a884..0a5ac733b 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -303,6 +303,7 @@ Увібрати тему Яскрава Темна + Тема системи Вигляд Вікно за замовчуванням diff --git a/app/src/main/res/values/api_hosts.xml b/app/src/main/res/values/api_hosts.xml index 5ce26b1d6..f661b88c5 100644 --- a/app/src/main/res/values/api_hosts.xml +++ b/app/src/main/res/values/api_hosts.xml @@ -36,7 +36,7 @@ https://vulcan.net.pl/ https://vulcan.net.pl/ https://vulcan.net.pl/ - http://fakelog.cf/?standard + http://fakelog.tk/?standard Default diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 429cd7404..c2cbeada2 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -336,6 +336,7 @@ Choose theme Light Dark + System Theme