-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redesign master switch preference views
Signed-off-by: DrDisagree <[email protected]>
- Loading branch information
1 parent
e8e4be3
commit e37eba8
Showing
14 changed files
with
251 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
app/src/main/java/com/drdisagree/iconify/ui/preferences/MasterSwitchPreference.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
app/src/main/java/com/drdisagree/iconify/ui/widgets/MasterSwitchWidget.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
app/src/main/res/drawable-v24/item_master_background_material.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
40
app/src/main/res/layout/custom_preference_master_switch.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.