Skip to content

Commit

Permalink
Redesign master switch preference views
Browse files Browse the repository at this point in the history
Signed-off-by: DrDisagree <[email protected]>
  • Loading branch information
Mahmud0808 committed Jan 5, 2025
1 parent e8e4be3 commit e37eba8
Show file tree
Hide file tree
Showing 14 changed files with 251 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -128,24 +126,6 @@ class DepthWallpaper : ControlledPreferenceFragmentCompat() {
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
super.onCreatePreferences(savedInstanceState, rootKey)

findPreference<SwitchPreference>(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<FilePickerPreference>("xposed_depthwallpaperbgimagepicker")?.apply {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/drawable-v24/item_master_background_material.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/color_material_ripple">
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimaryContainer" />
<corners android:radius="@dimen/cont_corner_radius" />
</shape>
</item>
</ripple>
40 changes: 40 additions & 0 deletions app/src/main/res/layout/custom_preference_master_switch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginVertical="16dp"
android:background="@drawable/item_master_background_material"
android:clickable="true"
android:clipToOutline="true"
android:gravity="center_vertical"
android:minHeight="?android:listPreferredItemHeight"
android:orientation="horizontal"
android:paddingStart="6dp"
android:paddingEnd="24dp">

<com.google.android.material.textview.MaterialTextView
android:id="@android:id/title"
style="@style/TextAppearance.Material3.TitleMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:layout_marginStart="18dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:lineHeight="28sp"
android:textAlignment="viewStart"
android:textColor="?attr/colorOnPrimaryContainer"
android:textSize="18sp"
android:textStyle="normal" />

<LinearLayout
android:id="@android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp"
android:orientation="vertical" />

</LinearLayout>
3 changes: 1 addition & 2 deletions app/src/main/res/layout/fragment_xposed_lockscreen_clock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,10 @@
android:animateLayoutChanges="true"
android:orientation="vertical">

<com.drdisagree.iconify.ui.widgets.SwitchWidget
<com.drdisagree.iconify.ui.widgets.MasterSwitchWidget
android:id="@+id/ls_clock_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:summaryText="@string/custom_lockscreen_clock_desc"
app:titleText="@string/custom_lockscreen_clock_title" />

<include
Expand Down
39 changes: 39 additions & 0 deletions app/src/main/res/layout/view_widget_master_switch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginVertical="16dp"
android:background="@drawable/item_master_background_material"
android:clickable="true"
android:clipToOutline="true"
android:gravity="center_vertical"
android:minHeight="?android:listPreferredItemHeight"
android:orientation="horizontal"
android:paddingStart="6dp"
android:paddingEnd="24dp">

<com.google.android.material.textview.MaterialTextView
android:id="@+id/title"
style="@style/TextAppearance.Material3.TitleMedium"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginVertical="16dp"
android:layout_marginStart="18dp"
android:layout_marginEnd="10dp"
android:layout_weight="1"
android:fontFamily="sans-serif"
android:lineHeight="28sp"
android:textAlignment="viewStart"
android:textColor="?attr/colorOnPrimaryContainer"
android:textSize="18sp"
android:textStyle="normal" />

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/switch_widget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginVertical="12dp" />

</LinearLayout>
5 changes: 5 additions & 0 deletions app/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
<attr name="isChecked" format="boolean" />
</declare-styleable>

<declare-styleable name="MasterSwitchWidget">
<attr name="titleText" />
<attr name="isChecked" />
</declare-styleable>

<declare-styleable name="SliderWidget">
<attr name="titleText" />
<attr name="summaryText" />
Expand Down
16 changes: 7 additions & 9 deletions app/src/main/res/xml/xposed_depth_wallpaper.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<com.drdisagree.iconify.ui.preferences.SwitchPreference
<com.drdisagree.iconify.ui.preferences.MasterSwitchPreference
android:key="xposed_depthwallpaper"
android:summary="@string/enable_depth_wallpaper_desc"
android:title="@string/enable_depth_wallpaper_title"
app:iconSpaceReserved="false" />
android:title="@string/enable_depth_wallpaper_title" />

<com.drdisagree.iconify.ui.preferences.ListPreference
android:dependency="xposed_depthwallpaper"
android:defaultValue="0"
android:dependency="xposed_depthwallpaper"
android:entries="@array/depth_wallpaper_mode_entries"
android:entryValues="@array/depth_wallpaper_mode_values"
android:key="xposed_depthwallpaper_aimode"
app:useSimpleSummaryProvider="true"
android:title="@string/depth_wallpaper_ai_mode"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false"
app:useSimpleSummaryProvider="true" />

<com.drdisagree.iconify.ui.preferences.PreferenceMenu
android:dependency="xposed_depthwallpaper"
android:key="xposed_depthwallpaper_aistatus"
android:title="@string/depth_wallpaper_ai_status"
app:showArrow="false"
app:iconSpaceReserved="false" />
app:iconSpaceReserved="false"
app:showArrow="false" />

<com.drdisagree.iconify.ui.preferences.SwitchPreference
android:dependency="xposed_depthwallpaper"
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/res/xml/xposed_header_clock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:search="http://schemas.android.com/apk/com.drdisagree.iconify.ui.preferences.preferencesearch">

<com.drdisagree.iconify.ui.preferences.SwitchPreference
<com.drdisagree.iconify.ui.preferences.MasterSwitchPreference
android:key="xposed_headerclock"
android:summary="@string/custom_header_clock_desc"
android:title="@string/custom_header_clock_title"
app:iconSpaceReserved="false" />
android:title="@string/custom_header_clock_title" />

<com.drdisagree.iconify.ui.preferences.RecyclerPreference
android:dependency="xposed_headerclock"
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/res/xml/xposed_header_image.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:search="http://schemas.android.com/apk/com.drdisagree.iconify.ui.preferences.preferencesearch">

<com.drdisagree.iconify.ui.preferences.SwitchPreference
<com.drdisagree.iconify.ui.preferences.MasterSwitchPreference
android:key="xposed_headerimage"
android:summary="@string/activity_desc_header_image"
android:title="@string/activity_title_header_image"
app:iconSpaceReserved="false" />
android:title="@string/activity_title_header_image" />

<com.drdisagree.iconify.ui.preferences.FilePickerPreference
android:dependency="xposed_headerimage"
Expand Down
12 changes: 5 additions & 7 deletions app/src/main/res/xml/xposed_lockscreen_weather.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/weather_config_title">

<com.drdisagree.iconify.ui.preferences.SwitchPreference
<com.drdisagree.iconify.ui.preferences.MasterSwitchPreference
android:defaultValue="false"
android:key="weather_switch"
android:summary="@string/lockscreen_weather_desc"
android:title="@string/lockscreen_weather_title"
app:iconSpaceReserved="false" />
android:title="@string/lockscreen_weather_title" />

<com.drdisagree.iconify.ui.preferences.ListPreference
android:defaultValue="2"
Expand All @@ -22,8 +20,8 @@

<com.drdisagree.iconify.ui.preferences.PreferenceMenu
android:key="update_status"
android:title="@string/last_update_time"
android:summary="@string/not_available"
android:title="@string/last_update_time"
app:iconSpaceReserved="false"
app:showArrow="false" />

Expand Down Expand Up @@ -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" />

<com.drdisagree.iconify.ui.preferences.BottomSheetListPreference
android:dependency="weather_switch"
Expand Down
Loading

0 comments on commit e37eba8

Please sign in to comment.