Added theme mode picker dialog

Redesigned settings' selector for dark mode a bit
This commit is contained in:
Viktor De Pasquale 2019-10-20 17:28:18 +02:00
parent 27fb0474d5
commit dc09ec7598
8 changed files with 134 additions and 28 deletions

View File

@ -0,0 +1,50 @@
package com.topjohnwu.magisk.model.events.dialog
import android.app.Activity
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.R
import com.topjohnwu.magisk.model.events.ActivityExecutor
import com.topjohnwu.magisk.view.MagiskDialog
import java.lang.ref.WeakReference
class DarkThemeDialog : DialogEvent(), ActivityExecutor {
private var activity: WeakReference<Activity>? = null
override fun invoke(activity: AppCompatActivity) {
this.activity = WeakReference(activity)
}
override fun build(dialog: MagiskDialog) {
dialog.applyTitle(R.string.settings_dark_mode_title)
.applyMessage(R.string.settings_dark_mode_message)
.applyButton(MagiskDialog.ButtonType.POSITIVE) {
titleRes = R.string.settings_dark_mode_light
icon = R.drawable.ic_day
onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_NO) }
}
.applyButton(MagiskDialog.ButtonType.NEUTRAL) {
titleRes = R.string.settings_dark_mode_system
icon = R.drawable.ic_day_night
onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM) }
}
.applyButton(MagiskDialog.ButtonType.NEGATIVE) {
titleRes = R.string.settings_dark_mode_dark
icon = R.drawable.ic_night
onClick { selectTheme(AppCompatDelegate.MODE_NIGHT_YES) }
}
.onDismiss {
activity?.clear()
activity = null
}
}
private fun selectTheme(mode: Int) {
Config.darkThemeExtended = mode
activity?.get()?.recreate()
}
}

View File

@ -4,14 +4,13 @@ import com.topjohnwu.magisk.Config
import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback import com.topjohnwu.magisk.extensions.addOnPropertyChangedCallback
import com.topjohnwu.magisk.extensions.toggle import com.topjohnwu.magisk.extensions.toggle
import com.topjohnwu.magisk.model.events.DieEvent import com.topjohnwu.magisk.model.events.DieEvent
import com.topjohnwu.magisk.model.events.RecreateEvent import com.topjohnwu.magisk.model.events.dialog.DarkThemeDialog
import com.topjohnwu.magisk.redesign.compat.CompatViewModel import com.topjohnwu.magisk.redesign.compat.CompatViewModel
import com.topjohnwu.magisk.utils.KObservableField import com.topjohnwu.magisk.utils.KObservableField
class SettingsViewModel : CompatViewModel() { class SettingsViewModel : CompatViewModel() {
val redesign = KObservableField(Config.redesign) val redesign = KObservableField(Config.redesign)
val darkTheme = KObservableField(Config.darkTheme)
init { init {
//todo make observable preference //todo make observable preference
@ -19,12 +18,9 @@ class SettingsViewModel : CompatViewModel() {
Config.redesign = redesign.value Config.redesign = redesign.value
DieEvent().publish() DieEvent().publish()
} }
darkTheme.addOnPropertyChangedCallback {
Config.darkTheme = darkTheme.value
RecreateEvent().publish()
}
} }
fun toggle(item: KObservableField<Boolean>) = item.toggle() fun toggle(item: KObservableField<Boolean>) = item.toggle()
fun darkModePressed() = DarkThemeDialog().publish()
} }

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?colorOnSurface"
android:pathData="M3.55,18.54L4.96,19.95L6.76,18.16L5.34,16.74M11,22.45C11.32,22.45 13,22.45 13,22.45V19.5H11M12,5.5A6,6 0 0,0 6,11.5A6,6 0 0,0 12,17.5A6,6 0 0,0 18,11.5C18,8.18 15.31,5.5 12,5.5M20,12.5H23V10.5H20M17.24,18.16L19.04,19.95L20.45,18.54L18.66,16.74M20.45,4.46L19.04,3.05L17.24,4.84L18.66,6.26M13,0.55H11V3.5H13M4,10.5H1V12.5H4M6.76,4.84L4.96,3.05L3.55,4.46L5.34,6.26L6.76,4.84Z" />
</vector>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?colorOnSurface"
android:pathData="M7.5,2C5.71,3.15 4.5,5.18 4.5,7.5C4.5,9.82 5.71,11.85 7.53,13C4.46,13 2,10.54 2,7.5A5.5,5.5 0 0,1 7.5,2M19.07,3.5L20.5,4.93L4.93,20.5L3.5,19.07L19.07,3.5M12.89,5.93L11.41,5L9.97,6L10.39,4.3L9,3.24L10.75,3.12L11.33,1.47L12,3.1L13.73,3.13L12.38,4.26L12.89,5.93M9.59,9.54L8.43,8.81L7.31,9.59L7.65,8.27L6.56,7.44L7.92,7.35L8.37,6.06L8.88,7.33L10.24,7.36L9.19,8.23L9.59,9.54M19,13.5A5.5,5.5 0 0,1 13.5,19C12.28,19 11.15,18.6 10.24,17.93L17.93,10.24C18.6,11.15 19,12.28 19,13.5M14.6,20.08L17.37,18.93L17.13,22.28L14.6,20.08M18.93,17.38L20.08,14.61L22.28,17.15L18.93,17.38M20.08,12.42L18.94,9.64L22.28,9.88L20.08,12.42M9.63,18.93L12.4,20.08L9.87,22.27L9.63,18.93Z" />
</vector>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?colorOnSurface"
android:pathData="M17.75,4.09L15.22,6.03L16.13,9.09L13.5,7.28L10.87,9.09L11.78,6.03L9.25,4.09L12.44,4L13.5,1L14.56,4L17.75,4.09M21.25,11L19.61,12.25L20.2,14.23L18.5,13.06L16.8,14.23L17.39,12.25L15.75,11L17.81,10.95L18.5,9L19.19,10.95L21.25,11M18.97,15.95C19.8,15.87 20.69,17.05 20.16,17.8C19.84,18.25 19.5,18.67 19.08,19.07C15.17,23 8.84,23 4.94,19.07C1.03,15.17 1.03,8.83 4.94,4.93C5.34,4.53 5.76,4.17 6.21,3.85C6.96,3.32 8.14,4.21 8.06,5.04C7.79,7.9 8.75,10.87 10.95,13.06C13.14,15.26 16.1,16.22 18.97,15.95M17.33,17.97C14.5,17.81 11.7,16.64 9.53,14.5C7.36,12.31 6.2,9.5 6.04,6.68C3.23,9.82 3.34,14.64 6.35,17.66C9.37,20.67 14.19,20.78 17.33,17.97Z" />
</vector>

View File

@ -18,7 +18,8 @@
android:fillViewport="true" android:fillViewport="true"
android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size + (int) @dimen/l2}" android:paddingTop="@{viewModel.insets.top + (int) @dimen/internal_action_bar_size + (int) @dimen/l2}"
android:paddingBottom="@{viewModel.insets.bottom + (int) @dimen/l2}" android:paddingBottom="@{viewModel.insets.bottom + (int) @dimen/l2}"
tools:layout_marginTop="24dp"> tools:layout_marginTop="24dp"
tools:paddingTop="@dimen/l1">
<androidx.constraintlayout.widget.ConstraintLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
@ -29,34 +30,52 @@
style="?styleCardNormal" style="?styleCardNormal"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="@dimen/l2" android:layout_marginStart="@dimen/l1"
android:layout_marginTop="@dimen/l1" android:onClick="@{() -> viewModel.darkModePressed()}"
android:onClick="@{() -> viewModel.toggle(viewModel.darkTheme)}"
app:layout_constraintEnd_toStartOf="@+id/settings_card_redesign" app:layout_constraintEnd_toStartOf="@+id/settings_card_redesign"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"> app:layout_constraintTop_toTopOf="parent">
<LinearLayout <androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:orientation="vertical">
android:padding="@dimen/l1">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/settings_card_dark_icon"
style="?styleIconNormal"
android:background="@null"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_day_night" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/settings_card_dark_pointer"
style="?styleIconNormal"
android:background="@null"
android:rotation="180"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_back_md2" />
<androidx.appcompat.widget.AppCompatTextView <androidx.appcompat.widget.AppCompatTextView
android:layout_width="match_parent" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:gravity="center" android:background="@null"
android:text="@string/settings_dark_theme_title" android:gravity="start|center_vertical"
android:textAppearance="?appearanceTextTitleNormal" /> android:singleLine="true"
android:text="@string/settings_dark_mode_title"
android:textAppearance="?appearanceTextBodyNormal"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/settings_card_dark_pointer"
app:layout_constraintStart_toEndOf="@+id/settings_card_dark_icon"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.switchmaterial.SwitchMaterial </androidx.constraintlayout.widget.ConstraintLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:checked="@={viewModel.darkTheme}" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView> </com.google.android.material.card.MaterialCardView>
@ -65,10 +84,9 @@
style="?styleCardNormal" style="?styleCardNormal"
android:layout_width="0dp" android:layout_width="0dp"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/l2" android:layout_marginLeft="@dimen/l1"
android:layout_marginTop="@dimen/l1" android:layout_marginRight="@dimen/l1"
android:layout_marginRight="@dimen/l2" android:layout_marginBottom="@dimen/l1"
android:layout_marginBottom="@dimen/l2"
android:onClick="@{() -> viewModel.toggle(viewModel.redesign)}" android:onClick="@{() -> viewModel.toggle(viewModel.redesign)}"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/settings_card_dark" app:layout_constraintStart_toEndOf="@+id/settings_card_dark"

View File

@ -29,4 +29,10 @@
<!--ref. Magisk Hide--> <!--ref. Magisk Hide-->
<string name="magisk_hide_md2">Hide</string> <string name="magisk_hide_md2">Hide</string>
<string name="settings_dark_mode_title">Theme Mode</string>
<string name="settings_dark_mode_message">Select mode which best suits your style!</string>
<string name="settings_dark_mode_light">Always Light</string>
<string name="settings_dark_mode_system">Follow System</string>
<string name="settings_dark_mode_dark">Always Dark</string>
</resources> </resources>

View File

@ -53,6 +53,8 @@ variant. Make sure to use style referenced by attribute defined it attrs.xml.
<style name="WidgetFoundation.Button" parent="Widget.MaterialComponents.Button"> <style name="WidgetFoundation.Button" parent="Widget.MaterialComponents.Button">
<item name="android:textStyle">bold</item> <item name="android:textStyle">bold</item>
<item name="iconGravity">textStart</item>
<item name="iconPadding">@dimen/l1</item>
</style> </style>
<style name="WidgetFoundation.Button.OnPrimary"> <style name="WidgetFoundation.Button.OnPrimary">
@ -64,6 +66,8 @@ variant. Make sure to use style referenced by attribute defined it attrs.xml.
<style name="WidgetFoundation.Button.Flat" parent="Widget.MaterialComponents.Button.UnelevatedButton"> <style name="WidgetFoundation.Button.Flat" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textStyle">bold</item> <item name="android:textStyle">bold</item>
<item name="iconGravity">textStart</item>
<item name="iconPadding">@dimen/l1</item>
</style> </style>
<style name="WidgetFoundation.Button.Flat.OnPrimary"> <style name="WidgetFoundation.Button.Flat.OnPrimary">
@ -75,6 +79,8 @@ variant. Make sure to use style referenced by attribute defined it attrs.xml.
<style name="WidgetFoundation.Button.Text" parent="Widget.MaterialComponents.Button.TextButton"> <style name="WidgetFoundation.Button.Text" parent="Widget.MaterialComponents.Button.TextButton">
<item name="android:textStyle">bold</item> <item name="android:textStyle">bold</item>
<item name="iconGravity">textStart</item>
<item name="iconPadding">@dimen/l1</item>
</style> </style>
<style name="WidgetFoundation.Button.Text.OnPrimary"> <style name="WidgetFoundation.Button.Text.OnPrimary">