Skip to content

Commit

Permalink
[Settings] Replace “Remove all rules” with a button
Browse files Browse the repository at this point in the history
Signed-off-by: Muntashir Al-Islam <[email protected]>
  • Loading branch information
MuntashirAkon committed Apr 1, 2024
1 parent 5b167b2 commit a7fbcff
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
((Preference) Objects.requireNonNull(findPreference("remove_all_rules"))).setOnPreferenceClickListener(preference -> {
new MaterialAlertDialogBuilder(mActivity)
.setTitle(R.string.pref_remove_all_rules)
.setMessage(R.string.are_you_sure)
.setMessage(getString(R.string.are_you_sure) + " " + getString(R.string.pref_remove_all_rules_msg))
.setPositiveButton(R.string.yes, (dialog, which) -> {
mActivity.progressIndicator.show();
model.removeAllRules();
Expand Down
5 changes: 2 additions & 3 deletions app/src/main/res/xml/preferences_rules.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@
android:fragment="io.github.muntashirakon.AppManager.settings.ImportExportRulesPreferences"
app:iconSpaceReserved="false" />

<Preference
<io.github.muntashirakon.preference.PrimaryButtonPreference
app:key="remove_all_rules"
app:title="@string/pref_remove_all_rules"
app:summary="@string/pref_remove_all_rules_msg"
app:iconSpaceReserved="false" />
app:icon="@drawable/ic_restore"/>

</PreferenceScreen>
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// SPDX-License-Identifier: GPL-3.0-or-later

package io.github.muntashirakon.preference;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.preference.Preference;
import androidx.preference.PreferenceViewHolder;

import com.google.android.material.button.MaterialButton;

import io.github.muntashirakon.ui.R;

public class PrimaryButtonPreference extends Preference {
public PrimaryButtonPreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

public PrimaryButtonPreference(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, R.style.Preference_M3_ButtonPreference_Primary);
}

public PrimaryButtonPreference(@NonNull Context context, @Nullable AttributeSet attrs) {
this(context, attrs, R.attr.primaryButtonPreferenceStyle);
}

public PrimaryButtonPreference(@NonNull Context context) {
this(context, null);
}

@Override
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
super.onBindViewHolder(holder);
View itemView = holder.itemView;
MaterialButton button = (MaterialButton) holder.findViewById(android.R.id.button1);
button.setText(getTitle());
button.setIcon(getIcon());
if (itemView.hasOnClickListeners()) {
// Proxy listeners
button.setOnClickListener(v -> itemView.callOnClick());
}
// Selectable
boolean isSelectable = isSelectable();
itemView.setClickable(false);
itemView.setFocusable(false);
button.setClickable(isSelectable);
button.setFocusable(isSelectable);
}
}
28 changes: 28 additions & 0 deletions libcore/ui/src/main/res/layout/m3_preference_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!-- SPDX-License-Identifier: Apache-2.0 AND GPL-3.0-or-later -->
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:layout_marginHorizontal="?attr/listItemMarginHorizontal"
android:paddingVertical="16dp"
android:clipToPadding="false"
android:baselineAligned="false"
android:orientation="vertical">

<com.google.android.material.button.MaterialButton
android:id="@android:id/button1"
style="@style/Widget.AppTheme.Button.FilledButton.Dense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|center_vertical"
android:textAppearance="?attr/textAppearanceTitleMedium"
app:iconSize="18dp"
android:paddingHorizontal="16dp"
tools:text="Button" />

</androidx.appcompat.widget.LinearLayoutCompat>
1 change: 1 addition & 0 deletions libcore/ui/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<attr name="topSwitchPreferenceStyle" format="reference" />
<attr name="alertPreferenceStyle" format="reference" />
<attr name="primaryButtonPreferenceStyle" format="reference" />

<declare-styleable name="SearchView">
<!-- Tint to apply to the SearchView's close icon. -->
Expand Down
15 changes: 15 additions & 0 deletions libcore/ui/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<item name="alertPreferenceStyle">@style/Preference.M3.Alert</item>
<item name="checkBoxPreferenceStyle">@style/Preference.M3.CheckBoxPreference</item>
<item name="dialogPreferenceStyle">@style/Preference.M3.DialogPreference</item>
<item name="primaryButtonPreferenceStyle">@style/Preference.M3.ButtonPreference.Primary</item>
<item name="topSwitchPreferenceStyle">@style/Preference.M3.TopSwitchPreference</item>

<item name="dropdownPreferenceStyle">@style/Preference.DropDown.Material</item>
Expand Down Expand Up @@ -432,6 +433,10 @@
<item name="android:layout">@layout/m3_preference</item>
</style>

<style name="Preference.M3.ButtonPreference.Primary" parent="Preference.Material">
<item name="android:layout">@layout/m3_preference_button</item>
</style>

<style name="Preference.M3.SwitchPreference" parent="Preference.SwitchPreference.Material">
<item name="android:layout">@layout/m3_preference</item>
</style>
Expand Down Expand Up @@ -515,8 +520,18 @@

<style name="Widget.AppTheme.Button.FilledButton" parent="Widget.Material3.Button" />

<style name="Widget.AppTheme.Button.FilledButton.Dense" parent="Widget.AppTheme.Button.FilledButton">
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>
</style>

<style name="Widget.AppTheme.Button.FilledTonalButton" parent="Widget.Material3.Button.TonalButton" />

<style name="Widget.AppTheme.Button.FilledTonalButton.Dense" parent="Widget.AppTheme.Button.FilledTonalButton">
<item name="android:minWidth">0dp</item>
<item name="android:minHeight">0dp</item>
</style>

<style name="Widget.AppTheme.Button.OutlinedButton" parent="Widget.Material3.Button.OutlinedButton" />

<style name="Widget.AppTheme.Button.OutlinedButton.Dense" parent="Widget.AppTheme.Button.OutlinedButton">
Expand Down

0 comments on commit a7fbcff

Please sign in to comment.