Skip to content

Commit

Permalink
fix(Feature Flags): Use ListView instead of RecyclerView for a more r…
Browse files Browse the repository at this point in the history
…eliable list.
  • Loading branch information
crimera committed Aug 18, 2024
1 parent 5583258 commit 7136241
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 213 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package app.revanced.integrations.twitter.settings.featureflags;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
import app.revanced.integrations.shared.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class FeatureFlagAdapter extends BaseAdapter {
private final LayoutInflater inflater;
private final List<FeatureFlag> list;

OnItemClickListener itemClickListener;
OnItemCheckedChangeListener itemCheckedChangeListener;

FeatureFlagAdapter(Context context, ArrayList<FeatureFlag> list) {
inflater = LayoutInflater.from(context);

this.list = list;
}

private class ViewHolder {
TextView textView;
Switch enabled;
}

public void setItemClickListener(OnItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}

public void setItemCheckedChangeListener(OnItemCheckedChangeListener itemCheckedChangeListener) {
this.itemCheckedChangeListener = itemCheckedChangeListener;
}

@Override
public int getCount() {
return list.size();
}

@Override
public FeatureFlag getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(Utils.getResourceIdentifier("item_row", "layout"), null);

holder.textView = convertView.findViewById(Utils.getResourceIdentifier("textView", "id"));
holder.enabled = convertView.findViewById(Utils.getResourceIdentifier("enabled", "id"));

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.textView.setText(list.get(position).getName());
holder.textView.setOnClickListener(view -> {
itemClickListener.onClick(position);
});

holder.enabled.setChecked(list.get(position).getEnabled());
holder.enabled.setOnCheckedChangeListener((compoundButton, b) -> {
if (itemCheckedChangeListener!=null) itemCheckedChangeListener.onCheck(b, position);
});

return convertView;
}

public void filter(String charText) {
List<FeatureFlag> original = new ArrayList<>(list);

charText = charText.toLowerCase(Locale.getDefault());
list.clear();
if (charText.isEmpty()) {
list.addAll(original);
} else {
for (FeatureFlag item : original) {
if (item.getName().toLowerCase(Locale.getDefault()).contains(charText)) {
list.add(item);
}
}
}
notifyDataSetChanged();
}
}
interface OnItemClickListener {
void onClick(int position);
}

interface OnItemCheckedChangeListener {
void onCheck(boolean checked, int position);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import app.revanced.integrations.shared.Utils;
import app.revanced.integrations.twitter.patches.FeatureSwitchPatch;
import app.revanced.integrations.twitter.settings.ActivityHook;
Expand Down Expand Up @@ -49,7 +47,7 @@ private void saveFlags() {
app.revanced.integrations.twitter.Utils.setStringPref(Settings.MISC_FEATURE_FLAGS.key, FeatureFlag.toStringPref(flags));
}

public void modifyFlag(CustomAdapter adapter, int position) {
public void modifyFlag(FeatureFlagAdapter adapter, int position) {
FeatureFlag flag = flags.get(position);

AlertDialog.Builder dia = new AlertDialog.Builder(getContext());
Expand All @@ -67,14 +65,14 @@ public void modifyFlag(CustomAdapter adapter, int position) {
String editTextValue = flagEditText.getText().toString();
if (!editTextValue.equals(flag.getName())) {
flags.set(position, new FeatureFlag(flagEditText.getText().toString(), flag.getEnabled()));
adapter.notifyItemChanged(position);
adapter.notifyDataSetChanged();
saveFlags();
}
});

dia.setNeutralButton(Utils.getResourceString("remove"), ((dialogInterface, i) -> {
flags.remove(position);
adapter.c.f(position, 1);
adapter.notifyDataSetChanged();
saveFlags();
}));

Expand All @@ -85,7 +83,7 @@ public void modifyFlag(CustomAdapter adapter, int position) {
dia.create().show();
}

private void searchFlagsDialog(CustomAdapter parentAdapter) {
private void searchFlagsDialog(FeatureFlagAdapter parentAdapter) {
AlertDialog.Builder dia = new AlertDialog.Builder(getContext());
dia.setTitle(Utils.getResourceString("piko_pref_add_flag_title"));

Expand Down Expand Up @@ -119,14 +117,15 @@ public void afterTextChanged(Editable editable) {

listView.setOnItemClickListener((adapterView, view1, i, l) -> {
flags.add(new FeatureFlag(adapter.getItem(i), true));
parentAdapter.notifyItemChanged(flags.size());
parentAdapter.notifyDataSetChanged();
saveFlags();
dialog.dismiss();
});

dialog.show();
}

public void addFlag(CustomAdapter adapter) {
public void addFlag(FeatureFlagAdapter adapter) {
AlertDialog.Builder dia = new AlertDialog.Builder(getContext());
dia.setTitle(Utils.getResourceString("piko_pref_add_flag_title"));

Expand All @@ -142,7 +141,7 @@ public void addFlag(CustomAdapter adapter) {
dia.setPositiveButton(Utils.getResourceString("save"), (dialogInterface, i) -> {
String editTextValue = flagEditText.getText().toString();
flags.add(new FeatureFlag(editTextValue, true));
adapter.notifyItemChanged(flags.size());
adapter.notifyDataSetChanged();
saveFlags();
});

Expand All @@ -163,12 +162,15 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
View view = inflater.inflate(Utils.getResourceIdentifier("feature_flags_view", "layout"), container, false);
FloatingActionButton floatingActionButton = view.findViewById(Utils.getResourceIdentifier("add_flag", "id"));

RecyclerView rc = view.findViewById(Utils.getResourceIdentifier("list", "id"));
rc.setLayoutManager(new LinearLayoutManager(1));
ListView rc = view.findViewById(Utils.getResourceIdentifier("list", "id"));
rc.setClipToPadding(false);
rc.setPadding(0, 0, 0, 200);

CustomAdapter adapter = new CustomAdapter(flags);
FeatureFlagAdapter adapter = new FeatureFlagAdapter(getContext(), flags);

rc.setOnItemClickListener((adapterView, view1, i, l) -> {
app.revanced.integrations.twitter.Utils.toast(adapter.getItem(i).getName());
});

floatingActionButton.setOnClickListener(view1 -> addFlag(adapter));

Expand Down

This file was deleted.

Loading

0 comments on commit 7136241

Please sign in to comment.