Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Material Ui Test #4

Closed
wants to merge 15 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,27 @@
import androidx.core.content.ContextCompat;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceViewHolder;

import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.panda3ds.pandroid.R;
import com.panda3ds.pandroid.utils.Constants;

public class SingleSelectionPreferences extends PreferenceCategory implements Preference.OnPreferenceClickListener {
private final Drawable transparent = new ColorDrawable(Color.TRANSPARENT);
private final Drawable doneDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_done);

private CharSequence[] titles;
private CharSequence[] entryValues;

public SingleSelectionPreferences(@NonNull Context context) {
super(context);
init();
}

public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}

public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
Expand All @@ -38,11 +45,14 @@ public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeS
super(context, attrs, defStyleAttr, defStyleRes);
}

{
private void init() {
try {
titles = getTitles();
entryValues = getEntryValues();
TypedArray color = getContext().obtainStyledAttributes(new int[]{
android.R.attr.textColorSecondary
});
doneDrawable = ContextCompat.getDrawable(getContext(), R.drawable.ic_done);
doneDrawable.setTint(color.getColor(0, Color.RED));
color.recycle();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
Expand All @@ -53,11 +63,24 @@ public SingleSelectionPreferences(@NonNull Context context, @Nullable AttributeS
}
}

@Override
public void onBindViewHolder(@NonNull PreferenceViewHolder holder) {
super.onBindViewHolder(holder);

// Set summary based on selected title
if (titles != null && entryValues != null) {
CharSequence selectedTitle = getTitle();
if (selectedTitle != null) {
setSummary(selectedTitle);
}
}
}

@Override
public void onAttached() {
super.onAttached();

for (int i = 0; i < getPreferenceCount();i++) {
for (int i = 0; i < getPreferenceCount(); i++) {
getPreference(i).setOnPreferenceClickListener(this);
}
}
Expand All @@ -68,19 +91,65 @@ public void setSelectedItem(int index) {

@Override
public boolean onPreferenceClick(@NonNull Preference preference) {
int index = 0;
int index = findPreferenceIndex(preference);

if (index != -1) {
showMaterialDialog(index);
}

return true;
}

private int findPreferenceIndex(@NonNull Preference preference) {
for (int i = 0; i < getPreferenceCount(); i++) {
if (getPreference(i) == preference) {
return i;
}
}
return -1;
}

private void showMaterialDialog(final int selectedIndex) {
final CharSequence[] titlesArray = new CharSequence[getPreferenceCount()];
for (int i = 0; i < getPreferenceCount(); i++) {
titlesArray[i] = getPreference(i).getTitle();
}

new MaterialAlertDialogBuilder(getContext())
.setTitle(getTitle())
.setSingleChoiceItems(titles, selectedIndex, (dialog, which) -> {
updateSelectedPreference(which);
dialog.dismiss();
})
.show();
}

private void updateSelectedPreference(int selectedIndex) {
for (int i = 0; i < getPreferenceCount(); i++) {
Preference item = getPreference(i);
if (item == preference) {
index = i;
item.setIcon(R.drawable.ic_done);
if (i == selectedIndex) {
item.setIcon(doneDrawable);
} else {
item.setIcon(transparent);
}
}

callChangeListener(index);
return false;
callChangeListener(selectedIndex);
}

private CharSequence[] getTitles() {
CharSequence[] titles = new CharSequence[getPreferenceCount()];
for (int i = 0; i < getPreferenceCount(); i++) {
titles[i] = getPreference(i).getTitle();
}
return titles;
}

private CharSequence[] getEntryValues() {
CharSequence[] values = new CharSequence[getPreferenceCount()];
for (int i = 0; i < getPreferenceCount(); i++) {
values[i] = getPreference(i).getKey();
}
return values;
}
}
15 changes: 15 additions & 0 deletions src/pandroid/app/src/main/res/values/array.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="themes">
<item>Use System Default</item>
<item>Light</item>
<item>Dark</item>
<item>Black</item>
</string-array>
<integer-array name="themes_val">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</integer-array>
</resources>
16 changes: 7 additions & 9 deletions src/pandroid/app/src/main/res/xml/appearance_preference.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto">
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<com.panda3ds.pandroid.view.preferences.SingleSelectionPreferences
app:key="theme"
app:title="@string/theme"
app:iconSpaceReserved="false">

<Preference app:title="@string/theme_device"/>
<Preference app:title="@string/light"/>
<Preference app:title="@string/dark"/>
<Preference app:title="@string/black"/>

android:entries="@array/themes"
android:entryValues="@array/themes_val"
app:useSimpleSummaryProvider="true">
</com.panda3ds.pandroid.view.preferences.SingleSelectionPreferences>
</PreferenceScreen>
</PreferenceScreen>
Loading