From 495fcd0388b0bfe6f1b4d1ca919a560f7cad0e25 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Mon, 22 Jul 2024 16:57:15 +0200 Subject: [PATCH] Use M3 Switch Signed-off-by: alperozturk --- .../branding/BrandedSwitchPreference.java | 34 +++++++++++-------- app/src/main/res/layout/preference_switch.xml | 15 ++++++++ app/src/main/res/values/styles.xml | 7 +++- 3 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 app/src/main/res/layout/preference_switch.xml diff --git a/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSwitchPreference.java b/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSwitchPreference.java index 500da3f3..1d15394c 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSwitchPreference.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSwitchPreference.java @@ -6,45 +6,51 @@ */ package it.niedermann.owncloud.notes.branding; -import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; -import android.widget.Switch; import androidx.annotation.ColorInt; +import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.preference.PreferenceViewHolder; -import androidx.preference.SwitchPreference; +import androidx.preference.SwitchPreferenceCompat; -public class BrandedSwitchPreference extends SwitchPreference implements Branded { +import com.google.android.material.materialswitch.MaterialSwitch; + +import it.niedermann.owncloud.notes.R; + +public class BrandedSwitchPreference extends SwitchPreferenceCompat implements Branded { @ColorInt private Integer mainColor = null; - @SuppressLint("UseSwitchCompatOrMaterialCode") @Nullable - private Switch switchView; + private MaterialSwitch switchView; public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); + setWidgetLayoutResource(R.layout.preference_switch); } public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); + setWidgetLayoutResource(R.layout.preference_switch); } public BrandedSwitchPreference(Context context, AttributeSet attrs) { super(context, attrs); + setWidgetLayoutResource(R.layout.preference_switch); } public BrandedSwitchPreference(Context context) { super(context); + setWidgetLayoutResource(R.layout.preference_switch); } @Override - public void onBindViewHolder(PreferenceViewHolder holder) { + public void onBindViewHolder(@NonNull PreferenceViewHolder holder) { super.onBindViewHolder(holder); if (holder.itemView instanceof ViewGroup) { @@ -65,7 +71,7 @@ public class BrandedSwitchPreference extends SwitchPreference implements Branded private void applyBrand() { if (switchView != null) { final var util = BrandingUtil.of(mainColor, getContext()); - util.platform.colorSwitch(switchView); + util.material.colorMaterialSwitch(switchView); } } @@ -76,19 +82,19 @@ public class BrandedSwitchPreference extends SwitchPreference implements Branded * @return A Switch class or null * @see Source */ - private Switch findSwitchWidget(View view) { - if (view instanceof Switch) { - return (Switch) view; + private MaterialSwitch findSwitchWidget(View view) { + if (view instanceof MaterialSwitch) { + return (MaterialSwitch) view; } if (view instanceof ViewGroup viewGroup) { for (int i = 0; i < viewGroup.getChildCount(); i++) { final var child = viewGroup.getChildAt(i); if (child instanceof ViewGroup) { - @SuppressLint("UseSwitchCompatOrMaterialCode") final var result = findSwitchWidget(child); + final var result = findSwitchWidget(child); if (result != null) return result; } - if (child instanceof Switch) { - return (Switch) child; + if (child instanceof MaterialSwitch) { + return (MaterialSwitch) child; } } } diff --git a/app/src/main/res/layout/preference_switch.xml b/app/src/main/res/layout/preference_switch.xml new file mode 100644 index 00000000..b86a792f --- /dev/null +++ b/app/src/main/res/layout/preference_switch.xml @@ -0,0 +1,15 @@ + + + \ No newline at end of file diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 8cde0df4..36c07e91 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -7,7 +7,7 @@ ~ SPDX-FileCopyrightText: 2022 Kévin Cocchi ~ SPDX-License-Identifier: GPL-3.0-or-later --> - + + + \ No newline at end of file