Use M3 Switch

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-07-22 16:57:15 +02:00 committed by Tobias Kaminsky
parent dbfaffa445
commit 58a0e291db
3 changed files with 41 additions and 15 deletions

View file

@ -6,45 +6,51 @@
*/ */
package it.niedermann.owncloud.notes.branding; package it.niedermann.owncloud.notes.branding;
import android.annotation.SuppressLint;
import android.content.Context; import android.content.Context;
import android.util.AttributeSet; import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Switch;
import androidx.annotation.ColorInt; import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.preference.PreferenceViewHolder; 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 @ColorInt
private Integer mainColor = null; private Integer mainColor = null;
@SuppressLint("UseSwitchCompatOrMaterialCode")
@Nullable @Nullable
private Switch switchView; private MaterialSwitch switchView;
public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes); super(context, attrs, defStyleAttr, defStyleRes);
setWidgetLayoutResource(R.layout.preference_switch);
} }
public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { public BrandedSwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
setWidgetLayoutResource(R.layout.preference_switch);
} }
public BrandedSwitchPreference(Context context, AttributeSet attrs) { public BrandedSwitchPreference(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
setWidgetLayoutResource(R.layout.preference_switch);
} }
public BrandedSwitchPreference(Context context) { public BrandedSwitchPreference(Context context) {
super(context); super(context);
setWidgetLayoutResource(R.layout.preference_switch);
} }
@Override @Override
public void onBindViewHolder(PreferenceViewHolder holder) { public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
super.onBindViewHolder(holder); super.onBindViewHolder(holder);
if (holder.itemView instanceof ViewGroup) { if (holder.itemView instanceof ViewGroup) {
@ -65,7 +71,7 @@ public class BrandedSwitchPreference extends SwitchPreference implements Branded
private void applyBrand() { private void applyBrand() {
if (switchView != null) { if (switchView != null) {
final var util = BrandingUtil.of(mainColor, getContext()); 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 * @return A Switch class or null
* @see <a href="https://gist.github.com/marchold/45e22839eb94aa14dfb5">Source</a> * @see <a href="https://gist.github.com/marchold/45e22839eb94aa14dfb5">Source</a>
*/ */
private Switch findSwitchWidget(View view) { private MaterialSwitch findSwitchWidget(View view) {
if (view instanceof Switch) { if (view instanceof MaterialSwitch) {
return (Switch) view; return (MaterialSwitch) view;
} }
if (view instanceof ViewGroup viewGroup) { if (view instanceof ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) { for (int i = 0; i < viewGroup.getChildCount(); i++) {
final var child = viewGroup.getChildAt(i); final var child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) { if (child instanceof ViewGroup) {
@SuppressLint("UseSwitchCompatOrMaterialCode") final var result = findSwitchWidget(child); final var result = findSwitchWidget(child);
if (result != null) return result; if (result != null) return result;
} }
if (child instanceof Switch) { if (child instanceof MaterialSwitch) {
return (Switch) child; return (MaterialSwitch) child;
} }
} }
} }

View file

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Nextcloud - Android Client
~
~ SPDX-FileCopyrightText: 2024 Alper Ozturk <alper.ozturk@nextcloud.com>
~ SPDX-License-Identifier: AGPL-3.0-or-later
-->
<com.google.android.material.materialswitch.MaterialSwitch
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:clickable="false"
android:background="@null" />

View file

@ -7,7 +7,7 @@
~ SPDX-FileCopyrightText: 2022 Kévin Cocchi <kevin.cocchi@gmail.com> ~ SPDX-FileCopyrightText: 2022 Kévin Cocchi <kevin.cocchi@gmail.com>
~ SPDX-License-Identifier: GPL-3.0-or-later ~ SPDX-License-Identifier: GPL-3.0-or-later
--> -->
<resources xmlns:android="http://schemas.android.com/apk/res/android"> <resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<style name="BaseTheme" parent="Theme.Material3.DayNight.NoActionBar"> <style name="BaseTheme" parent="Theme.Material3.DayNight.NoActionBar">
<item name="colorPrimary">@color/primary</item> <item name="colorPrimary">@color/primary</item>
@ -99,4 +99,9 @@
<item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item> <item name="windowSplashScreenAnimatedIcon">@drawable/ic_launcher_foreground</item>
<item name="postSplashScreenTheme">@style/AppTheme</item> <item name="postSplashScreenTheme">@style/AppTheme</item>
</style> </style>
<style name="Preference.SwitchPreferenceCompat" parent="@style/Preference.SwitchPreferenceCompat.Material"
tools:ignore="ResourceCycle">
<item name="widgetLayout">@layout/preference_switch</item>
</style>
</resources> </resources>