0
0
mirror of https://github.com/TrianguloY/UrlChecker.git synced 2024-09-19 20:02:16 +02:00

replace flag buttons with icons

This commit is contained in:
TrianguloY 2023-04-28 20:48:06 +02:00
parent 06797e1b7f
commit 76f6a814b4
10 changed files with 178 additions and 83 deletions

View File

@ -8,12 +8,9 @@ import android.text.Editable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
@ -27,11 +24,12 @@ import com.trianguloy.urlchecker.modules.companions.Flags;
import com.trianguloy.urlchecker.url.UrlData;
import com.trianguloy.urlchecker.utilities.AndroidUtils;
import com.trianguloy.urlchecker.utilities.DefaultTextWatcher;
import com.trianguloy.urlchecker.utilities.Enums;
import com.trianguloy.urlchecker.utilities.GenericPref;
import com.trianguloy.urlchecker.utilities.Inflater;
import com.trianguloy.urlchecker.utilities.InternalFile;
import com.trianguloy.urlchecker.utilities.JavaUtils;
import com.trianguloy.urlchecker.utilities.TranslatableEnum;
import com.trianguloy.urlchecker.views.CycleImageButton;
import org.json.JSONException;
import org.json.JSONObject;
@ -185,7 +183,7 @@ class FlagsDialog extends AModuleDialog {
// Get state preference of flag from json and then store it in a map
flagsStatePref = new HashMap<>();
if (groupPref != null) {
Map<Integer, FlagsConfig.FlagState> flagsStateMap = TranslatableEnum.toEnumMap(FlagsConfig.FlagState.class);
Map<Integer, FlagsConfig.FlagState> flagsStateMap = Enums.toEnumMap(FlagsConfig.FlagState.class);
for (Iterator<String> it = groupPref.keys(); it.hasNext(); ) {
String flag = it.next();
try {
@ -242,23 +240,11 @@ class FlagsDialog extends AModuleDialog {
private void fillWithFlags(Set<String> flags, ViewGroup vg) {
vg.removeAllViews();
// Checkbox listener
CompoundButton.OnCheckedChangeListener l = (v, isChecked) -> {
// Store flag
String flag = (String) v.getTag(R.id.text);
currentFlags.setFlag(flag, isChecked);
// Update global
Flags.setGlobalFlags(currentFlags, this);
// To update debug module view of GlobalData
setUrl(new UrlData(getUrl()).dontTriggerOwn().asMinorUpdate());
};
for (String flag : flags) {
var checkbox_text = Inflater.inflate(R.layout.dialog_flags_entry, vg, getActivity());
// Checkbox
CheckBox checkBox = checkbox_text.findViewById(R.id.checkbox);
var checkBox = checkbox_text.<ImageView>findViewById(R.id.state);
boolean bool;
switch (valueOrDefault(flagsStatePref.get(flag), FlagsConfig.FlagState.AUTO)) {
case ON:
@ -271,11 +257,21 @@ class FlagsDialog extends AModuleDialog {
default:
bool = defaultFlags.isSet(flag);
}
checkBox.setChecked(bool);
currentFlags.setFlag(flag, bool);
checkBox.setTag(R.id.text, flag);
checkBox.setOnCheckedChangeListener(l);
AndroidUtils.toggleableListener(checkBox,
v -> {
currentFlags.setFlag(flag, !currentFlags.isSet(flag));
// Update global
Flags.setGlobalFlags(currentFlags, this);
// To update debug module view of GlobalData
setUrl(new UrlData(getUrl()).dontTriggerOwn().asMinorUpdate());
},
v -> checkBox.setImageResource(currentFlags.isSet(flag) ? R.drawable.flag_on : R.drawable.flag_off)
);
// Text
((TextView) checkbox_text.findViewById(R.id.text)).setText(flag);
@ -316,7 +312,6 @@ class FlagsDialog extends AModuleDialog {
class FlagsConfig extends AModuleConfig {
protected static final String CONF_FILE = "flags_editor_settings";
private Map<Integer, Integer> stateToIndex;
public FlagsConfig(ModulesActivity activity) {
super(activity);
@ -343,15 +338,17 @@ class FlagsConfig extends AModuleConfig {
// Save the settings
storePreferences(box, file, FlagsModule.DEFAULT_GROUP);
})
.setNegativeButton(views.getContext().getText(android.R.string.cancel), (dialog, which) -> {
// Reset current group flags (does not save)
resetFlags(box);
})
.setNegativeButton(views.getContext().getText(android.R.string.cancel), null)
.setNeutralButton(views.getContext().getText(R.string.reset), null)
.show();
alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL).setOnClickListener(listener -> {
// Reset current group flags (does not save)
resetFlags(box);
});
// Search
((EditText) flagsDialogLayout.findViewById(R.id.search)).addTextChangedListener(new DefaultTextWatcher() {
@Override
@ -372,23 +369,6 @@ class FlagsConfig extends AModuleConfig {
}
private void fillBoxViewGroup(ViewGroup vg, InternalFile file, String group) {
// Set spinner items
FlagState[] spinnerItems = FlagState.class.getEnumConstants();
List<String> spinnerItemsList = new ArrayList<>(spinnerItems.length);
stateToIndex = new HashMap<>();
for (int i = 0; i < spinnerItems.length; i++) {
spinnerItemsList.add(vg.getContext().getString(spinnerItems[i].getStringResource()));
// Map state to index
stateToIndex.put(spinnerItems[i].getId(), i);
}
ArrayAdapter<String> adapter = new ArrayAdapter<>(
vg.getContext(),
android.R.layout.simple_spinner_item,
spinnerItemsList
);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Store order info in vg
vg.setTag(spinnerItems);
String prefString = file.get();
JSONObject oldPref = null; // Null if there is no file or fails to parse
@ -401,12 +381,12 @@ class FlagsConfig extends AModuleConfig {
// Fill the box
for (String flag : Flags.getCompatibleFlags().keySet()) {
var text_spinner_checkbox = Inflater.inflate(R.layout.flags_editor_entry, vg, getActivity());
TextView textView = text_spinner_checkbox.findViewById(R.id.text);
textView.setText(flag);
Spinner spinner = text_spinner_checkbox.findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setTag(spinnerItems);
var flagState = text_spinner_checkbox.<CycleImageButton<FlagState>>findViewById(R.id.state);
flagState.setStates(List.of(FlagState.values()));
// Load preferences from settings
if (oldPref != null) {
@ -415,10 +395,14 @@ class FlagsConfig extends AModuleConfig {
flagPref = oldPref.getJSONObject(flag);
// select current option
spinner.setSelection(valueOrDefault(stateToIndex.get(flagPref.getInt("state")),
FlagState.AUTO.getId()));
((CheckBox) text_spinner_checkbox.findViewById(R.id.checkbox)).setChecked(flagPref.getBoolean("show"));
flagState.setCurrentState(valueOrDefault(Enums.toEnum(FlagState.class, flagPref.getInt("state")),
FlagState.AUTO));
var show = text_spinner_checkbox.<ImageButton>findViewById(R.id.show);
show.setTag(flagPref.getBoolean("show"));
AndroidUtils.toggleableListener(show,
v -> v.setTag(v.getTag() == Boolean.FALSE),
v -> v.setImageResource(v.getTag() == Boolean.TRUE ? R.drawable.show : R.drawable.hide)
);
} catch (JSONException ignored) {
}
}
@ -437,8 +421,6 @@ class FlagsConfig extends AModuleConfig {
// If the json fails to parse then we will create a new file
}
}
// Retrieve order of spinner
FlagState[] spinnerItems = (FlagState[]) vg.getTag();
try {
// Collect all the settings of the vg
@ -446,8 +428,8 @@ class FlagsConfig extends AModuleConfig {
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
FlagState state = spinnerItems[((Spinner) v.findViewById(R.id.spinner)).getSelectedItemPosition()];
boolean show = ((CheckBox) v.findViewById(R.id.checkbox)).isChecked();
FlagState state = v.<CycleImageButton<FlagState>>findViewById(R.id.state).getCurrentState();
boolean show = v.findViewById(R.id.show).getTag() == Boolean.TRUE;
newSettings.put(((TextView) v.findViewById(R.id.text)).getText().toString(),
new JSONObject()
.put("state", state.getId())
@ -467,22 +449,13 @@ class FlagsConfig extends AModuleConfig {
}
private void resetFlags(ViewGroup vg) {
// Retrieve order of spinner
FlagState[] spinnerItems = (FlagState[]) vg.getTag();
// Index of default
int def;
for (def = 0; def < spinnerItems.length; def++) {
if (spinnerItems[def] == FlagState.AUTO) {
break;
}
}
// Set everything to default values
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
((Spinner) v.findViewById(R.id.spinner)).setSelection(def);
((CheckBox) v.findViewById(R.id.checkbox)).setChecked(false);
v.<CycleImageButton<FlagState>>findViewById(R.id.state).setCurrentState(FlagState.AUTO);
var visible = v.<ImageButton>findViewById(R.id.show);
visible.setImageResource(R.drawable.hide);
visible.setTag(Boolean.FALSE);
}
}

View File

@ -0,0 +1,61 @@
package com.trianguloy.urlchecker.views;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.ImageButton;
import com.trianguloy.urlchecker.utilities.Enums;
import java.util.List;
public class CycleImageButton<T extends Enums.ImageEnum> extends ImageButton {
private List<T> states;
private int currentState;
public CycleImageButton(Context context) {
super(context);
}
public CycleImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CycleImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public void setStates(List<T> states) {
this.states = states;
updateImageResource(0);
}
public void setCurrentState(T currentState) {
updateImageResource(states.indexOf(currentState));
}
public T getCurrentState() {
return states == null || states.isEmpty() ? null : states.get(currentState);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public boolean performClick() {
updateImageResource(currentState + 1);
return super.performClick();
}
private void updateImageResource(int newState) {
if (states == null || states.isEmpty()) {
setImageDrawable(null);
} else {
currentState = newState >= 0 ? newState % states.size() : 0;
setImageResource(states.get(currentState).getImageResource());
}
}
}

View File

@ -0,0 +1,13 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="24"
android:viewportHeight="24">
<!-- <path-->
<!-- android:fillColor="@color/app"-->
<!-- android:pathData="M5 4 5 6 7 6 18 15 18 17 20 17 20 6 14 6 13 4M5 7 5 9 7 9 7 7M5 13 5 15 7 15 7 13M5 16 5 18 7 18 7 16M5 19 5 21 7 21 7 19M8 13 8 15 10 15 10 13M11 13 11 15 12 15 13 17 14 17 14 15 13 13M15 15 15 17 17 17 17 15M5 10 5 12 7 12 7 10" />-->
<path
android:fillColor="@color/app"
android:pathData="M5 4 5 15 7 15 7 13 18 8 20 8 20 6 14 6 13 4M5 16 5 18 7 18 7 16M5 19 5 21 7 21 7 19M8 13 8 15 10 15 10 13M11 13 11 15 12 15 13 17 14 17 14 15 13 13M15 15 15 17 17 17 17 15M18 15 18 17 20 17 20 15M18 12 18 14 20 14 20 12M18 9 18 11 20 11 20 9" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/app"
android:pathData="M5 4 5 6 7 6 7 4M5 7 5 9 7 9 7 7M5 13 5 15 7 15 7 13M5 16 5 18 7 18 7 16M5 19 5 21 7 21 7 19M8 13 8 15 10 15 10 13M11 13 11 15 12 15 13 17 14 17 14 15 13 13M15 15 15 17 17 17 17 15M18 15 18 17 20 17 20 15M18 12 18 14 20 14 20 12M18 6 18 8 20 8 20 6M15 6 15 8 17 8 17 6M11 4 11 6 12 6 13 8 14 8 14 6 13 4M8 4 8 6 10 6 10 4M5 10 5 12 7 12 7 10M18 9 18 11 20 11 20 9" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/app"
android:pathData="M14 6 13 4H5v17h2v-7h5l1 2h7V6z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/app"
android:pathData="M12 17C6 17 1 8 1 12c1.73 4.39 6 7.5 11 7.5s9.27-3.11 11-7.5c0-4-5 5-11 5z" />
</vector>

View File

@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:autoMirrored="true"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="@color/app"
android:pathData="M12,4.5C7,4.5 2.73,7.61 1,12c1.73,4.39 6,7.5 11,7.5s9.27,-3.11 11,-7.5c-1.73,-4.39 -6,-7.5 -11,-7.5zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5zM12,9c-1.66,0 -3,1.34 -3,3s1.34,3 3,3 3,-1.34 3,-3 -1.34,-3 -3,-3z" />
</vector>

View File

@ -4,11 +4,13 @@
android:layout_height="wrap_content"
android:gravity="center">
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0" />
<ImageButton
android:id="@+id/state"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="@dimen/square_button"
android:layout_height="@dimen/square_button"
android:layout_weight="0"
android:src="@drawable/flag_auto" />
<TextView
android:id="@+id/defaultIndicator"

View File

@ -2,9 +2,18 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:gravity="center_vertical">
<view
android:id="@+id/state"
style="?android:attr/buttonBarButtonStyle"
class="com.trianguloy.urlchecker.views.CycleImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp"
android:src="@drawable/flag_auto" />
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
@ -12,17 +21,13 @@
android:layout_weight="1"
android:gravity="left|center_vertical" />
<Spinner
android:id="@+id/spinner"
<ImageButton
android:id="@+id/show"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right|center_vertical" />
android:minWidth="0dp"
android:minHeight="0dp"
android:src="@drawable/show" />
<CheckBox
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="center" />
</LinearLayout>

View File

@ -2,4 +2,5 @@
<resources>
<dimen name="padding">25dp</dimen>
<dimen name="smallPadding">10dp</dimen>
<dimen name="square_button">35dp</dimen>
</resources>