From e37eba87e650e1de176650962ea2eed97e7304f4 Mon Sep 17 00:00:00 2001 From: DrDisagree Date: Mon, 6 Jan 2025 02:33:22 +0600 Subject: [PATCH] Redesign master switch preference views Signed-off-by: DrDisagree --- .../ui/fragments/xposed/DepthWallpaper.kt | 20 ---- .../ui/preferences/MasterSwitchPreference.kt | 39 ++++++++ .../iconify/ui/widgets/MasterSwitchWidget.kt | 97 +++++++++++++++++++ .../item_master_background_material.xml | 10 ++ .../custom_preference_master_switch.xml | 40 ++++++++ .../fragment_xposed_lockscreen_clock.xml | 3 +- .../res/layout/view_widget_master_switch.xml | 39 ++++++++ app/src/main/res/values/attrs.xml | 5 + .../main/res/xml/xposed_depth_wallpaper.xml | 16 ++- app/src/main/res/xml/xposed_header_clock.xml | 6 +- app/src/main/res/xml/xposed_header_image.xml | 6 +- .../res/xml/xposed_lockscreen_weather.xml | 12 +-- .../main/res/xml/xposed_lockscreen_widget.xml | 6 +- app/src/main/res/xml/xposed_op_qs_header.xml | 6 +- 14 files changed, 251 insertions(+), 54 deletions(-) create mode 100644 app/src/main/java/com/drdisagree/iconify/ui/preferences/MasterSwitchPreference.kt create mode 100644 app/src/main/java/com/drdisagree/iconify/ui/widgets/MasterSwitchWidget.kt create mode 100644 app/src/main/res/drawable-v24/item_master_background_material.xml create mode 100644 app/src/main/res/layout/custom_preference_master_switch.xml create mode 100644 app/src/main/res/layout/view_widget_master_switch.xml diff --git a/app/src/main/java/com/drdisagree/iconify/ui/fragments/xposed/DepthWallpaper.kt b/app/src/main/java/com/drdisagree/iconify/ui/fragments/xposed/DepthWallpaper.kt index 0f9383d20..f1e2bda23 100644 --- a/app/src/main/java/com/drdisagree/iconify/ui/fragments/xposed/DepthWallpaper.kt +++ b/app/src/main/java/com/drdisagree/iconify/ui/fragments/xposed/DepthWallpaper.kt @@ -4,7 +4,6 @@ import android.app.Activity import android.content.Context import android.content.Intent import android.net.Uri -import android.os.Build import android.os.Bundle import android.widget.Toast import androidx.activity.result.ActivityResult @@ -27,7 +26,6 @@ import com.drdisagree.iconify.ui.activities.MainActivity import com.drdisagree.iconify.ui.base.ControlledPreferenceFragmentCompat import com.drdisagree.iconify.ui.preferences.FilePickerPreference import com.drdisagree.iconify.ui.preferences.PreferenceMenu -import com.drdisagree.iconify.ui.preferences.SwitchPreference import com.drdisagree.iconify.utils.AppUtils import com.drdisagree.iconify.utils.FileUtils.getRealPath import com.drdisagree.iconify.utils.FileUtils.launchFilePicker @@ -128,24 +126,6 @@ class DepthWallpaper : ControlledPreferenceFragmentCompat() { override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { super.onCreatePreferences(savedInstanceState, rootKey) - findPreference(DEPTH_WALLPAPER_SWITCH)?.apply { - if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.TIRAMISU) { - setSummary( - getString( - R.string.enable_depth_wallpaper_desc, - getString(R.string.use_custom_lockscreen_clock) - ) - ) - } else { - setSummary( - getString( - R.string.enable_depth_wallpaper_desc, - "" - ).replace("\n", "") // hide args - ) - } - } - checkAiStatus() findPreference("xposed_depthwallpaperbgimagepicker")?.apply { diff --git a/app/src/main/java/com/drdisagree/iconify/ui/preferences/MasterSwitchPreference.kt b/app/src/main/java/com/drdisagree/iconify/ui/preferences/MasterSwitchPreference.kt new file mode 100644 index 000000000..4e6bb4ece --- /dev/null +++ b/app/src/main/java/com/drdisagree/iconify/ui/preferences/MasterSwitchPreference.kt @@ -0,0 +1,39 @@ +package com.drdisagree.iconify.ui.preferences + +import android.content.Context +import android.util.AttributeSet +import androidx.preference.SwitchPreferenceCompat +import com.drdisagree.iconify.R + +class MasterSwitchPreference : SwitchPreferenceCompat { + + constructor( + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int, + defStyleRes: Int + ) : super(context, attrs, defStyleAttr, defStyleRes) { + initResource() + } + + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { + initResource() + } + + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { + initResource() + } + + constructor(context: Context) : super(context) { + initResource() + } + + private fun initResource() { + layoutResource = R.layout.custom_preference_master_switch + widgetLayoutResource = R.layout.preference_material_switch + } +} \ No newline at end of file diff --git a/app/src/main/java/com/drdisagree/iconify/ui/widgets/MasterSwitchWidget.kt b/app/src/main/java/com/drdisagree/iconify/ui/widgets/MasterSwitchWidget.kt new file mode 100644 index 000000000..89381463a --- /dev/null +++ b/app/src/main/java/com/drdisagree/iconify/ui/widgets/MasterSwitchWidget.kt @@ -0,0 +1,97 @@ +package com.drdisagree.iconify.ui.widgets + +import android.content.Context +import android.util.AttributeSet +import android.widget.CompoundButton +import android.widget.LinearLayout +import android.widget.TextView +import com.drdisagree.iconify.R +import com.google.android.material.materialswitch.MaterialSwitch + +class MasterSwitchWidget : LinearLayout { + + private lateinit var container: LinearLayout + private lateinit var titleTextView: TextView + private lateinit var materialSwitch: MaterialSwitch + private var beforeSwitchChangeListener: BeforeSwitchChangeListener? = null + + constructor(context: Context) : super(context) { + init(context, null) + } + + constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) { + init(context, attrs) + } + + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { + init(context, attrs) + } + + private fun init(context: Context, attrs: AttributeSet?) { + inflate(context, R.layout.view_widget_master_switch, this) + + initializeId() + + val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MasterSwitchWidget) + + setTitle(typedArray.getString(R.styleable.MasterSwitchWidget_titleText)) + isSwitchChecked = typedArray.getBoolean(R.styleable.MasterSwitchWidget_isChecked, false) + + typedArray.recycle() + + container.setOnClickListener { + if (materialSwitch.isEnabled) { + beforeSwitchChangeListener?.beforeSwitchChanged() + materialSwitch.toggle() + } + } + } + + fun setTitle(titleResId: Int) { + titleTextView.setText(titleResId) + } + + fun setTitle(title: String?) { + titleTextView.text = title + } + + var isSwitchChecked: Boolean + get() = materialSwitch.isChecked + set(isChecked) { + materialSwitch.setChecked(isChecked) + } + + fun setSwitchChangeListener(listener: CompoundButton.OnCheckedChangeListener?) { + materialSwitch.setOnCheckedChangeListener(listener) + } + + fun setBeforeSwitchChangeListener(listener: BeforeSwitchChangeListener?) { + beforeSwitchChangeListener = listener + } + + override fun setEnabled(enabled: Boolean) { + super.setEnabled(enabled) + + container.setEnabled(enabled) + titleTextView.setEnabled(enabled) + materialSwitch.setEnabled(enabled) + } + + // to avoid listener bug, we need to re-generate unique id for each view + private fun initializeId() { + container = findViewById(R.id.container) + titleTextView = findViewById(R.id.title) + materialSwitch = findViewById(R.id.switch_widget) + container.setId(generateViewId()) + titleTextView.setId(generateViewId()) + materialSwitch.setId(generateViewId()) + } + + fun interface BeforeSwitchChangeListener { + fun beforeSwitchChanged() + } +} diff --git a/app/src/main/res/drawable-v24/item_master_background_material.xml b/app/src/main/res/drawable-v24/item_master_background_material.xml new file mode 100644 index 000000000..af011bd35 --- /dev/null +++ b/app/src/main/res/drawable-v24/item_master_background_material.xml @@ -0,0 +1,10 @@ + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/custom_preference_master_switch.xml b/app/src/main/res/layout/custom_preference_master_switch.xml new file mode 100644 index 000000000..29010be60 --- /dev/null +++ b/app/src/main/res/layout/custom_preference_master_switch.xml @@ -0,0 +1,40 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_xposed_lockscreen_clock.xml b/app/src/main/res/layout/fragment_xposed_lockscreen_clock.xml index 640c81692..acc224375 100644 --- a/app/src/main/res/layout/fragment_xposed_lockscreen_clock.xml +++ b/app/src/main/res/layout/fragment_xposed_lockscreen_clock.xml @@ -22,11 +22,10 @@ android:animateLayoutChanges="true" android:orientation="vertical"> - + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index af4d10bac..b9542aa1a 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -85,6 +85,11 @@ + + + + + diff --git a/app/src/main/res/xml/xposed_depth_wallpaper.xml b/app/src/main/res/xml/xposed_depth_wallpaper.xml index e1c1a6836..4ef03ab59 100644 --- a/app/src/main/res/xml/xposed_depth_wallpaper.xml +++ b/app/src/main/res/xml/xposed_depth_wallpaper.xml @@ -3,28 +3,26 @@ xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:search="http://schemas.android.com/apk/com.drdisagree.iconify.ui.preferences.preferencesearch"> - + android:title="@string/enable_depth_wallpaper_title" /> + app:iconSpaceReserved="false" + app:useSimpleSummaryProvider="true" /> + app:iconSpaceReserved="false" + app:showArrow="false" /> - + android:title="@string/custom_header_clock_title" /> - + android:title="@string/activity_title_header_image" /> - + android:title="@string/lockscreen_weather_title" /> @@ -139,8 +137,8 @@ android:key="weather_custom_location_picker" android:title="@string/weather_custom_location_title" app:fragment="com.drdisagree.iconify.ui.fragments.xposed.LocationBrowse" - app:showArrow="false" - app:iconSpaceReserved="false" /> + app:iconSpaceReserved="false" + app:showArrow="false" /> - + android:title="@string/lockscreen_widgets_enabled_title" /> - + android:title="@string/oneplus_qs_header_title" />