Skip to content

Commit

Permalink
Keeps uri parts groups state when changing url
Browse files Browse the repository at this point in the history
  • Loading branch information
TrianguloY committed May 29, 2023
1 parent 9547204 commit 491cd17
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import com.trianguloy.urlchecker.url.UrlData;
import com.trianguloy.urlchecker.utilities.AndroidUtils;
import com.trianguloy.urlchecker.utilities.Inflater;
import com.trianguloy.urlchecker.utilities.JavaUtils;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -52,6 +53,7 @@ public AModuleConfig getConfig(ModulesActivity cntx) {
class UriPartsDialog extends AModuleDialog {

private LinearLayout box;
private final List<String> expandedGroups = new ArrayList<>();

public UriPartsDialog(MainDialog dialog) {
super(dialog);
Expand All @@ -77,7 +79,7 @@ public void onDisplayUrl(UrlData urlData) {

// domain elements
if (uri.getAuthority() != null || uri.getScheme() != null) {
var domain = addGroup("Domain", null);
var domain = addGroup("Domain", -1, null);
addPart("scheme", uri.getScheme(), domain, null);
addPart("user info", uri.getUserInfo(), domain, null);
addPart("host", uri.getHost(), domain, null);
Expand All @@ -87,7 +89,7 @@ public void onDisplayUrl(UrlData urlData) {
// paths
var pathSegments = uri.getPathSegments();
if (pathSegments.size() > 0) {
var paths = addGroup("Paths (" + pathSegments.size() + ")", uri.buildUpon().path(null));
var paths = addGroup("Paths", pathSegments.size(), uri.buildUpon().path(null));
for (var i = 0; i < pathSegments.size(); i++) {
var pathSegment = pathSegments.get(i);

Expand All @@ -103,7 +105,7 @@ public void onDisplayUrl(UrlData urlData) {
// queries
var queryParts = getQueryParts(uri);
if (queryParts.size() > 0) {
var queries = addGroup("Queries (" + queryParts.size() + ")", uri.buildUpon().query(null));
var queries = addGroup("Queries", queryParts.size(), uri.buildUpon().query(null));
for (var queryPart : queryParts) {
var builder = uri.buildUpon();
builder.query(null);
Expand All @@ -118,36 +120,39 @@ public void onDisplayUrl(UrlData urlData) {

// fragment
if (uri.getFragment() != null) {
var fragment = addGroup("Fragment", uri.buildUpon().fragment(null));
var fragment = addGroup("Fragment", -1, uri.buildUpon().fragment(null));
addPart("#", uri.getFragment(), fragment, null);
}

setVisibility(box.getChildCount() > 0);
}

/**
* Adds a collapsible group, removes it
* Adds a collapsible group
*/
private LinearLayout addGroup(String name, Uri.Builder onDelete) {
private LinearLayout addGroup(String name, int size, Uri.Builder onDelete) {
var title = Inflater.inflate(R.layout.uri_part, box);
title.findViewById(R.id.key).setVisibility(View.GONE);

var name_view = title.<TextView>findViewById(R.id.value);
name_view.setText(name);
name_view.setText(name + (size <= -1 ? "" : " (" + size + ")"));
AndroidUtils.setAsClickable(name_view);

var delete_view = title.<Button>findViewById(R.id.delete);
if (onDelete == null) delete_view.setVisibility(View.GONE);
else delete_view.setOnClickListener(v -> setUrl(onDelete.build().toString()));

var group = Inflater.<LinearLayout>inflate(R.layout.dialog_parts, box);
group.setVisibility(View.GONE);
group.setVisibility(expandedGroups.contains(name) ? View.VISIBLE : View.GONE);
AndroidUtils.toggleableListener(
title,
v -> group.setVisibility(group.getVisibility() == View.GONE ? View.VISIBLE : View.GONE),
v -> AndroidUtils.setStartDrawables(name_view,
group.getVisibility() != View.GONE ? R.drawable.arrow_down : R.drawable.arrow_right
)
v -> JavaUtils.toggleContains(expandedGroups, name),
v -> {
group.setVisibility(expandedGroups.contains(name) ? View.VISIBLE : View.GONE);
AndroidUtils.setStartDrawables(name_view,
expandedGroups.contains(name) ? R.drawable.arrow_down : R.drawable.arrow_right
);
}
);

return group;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -49,14 +50,14 @@ static int clamp(int min, int value, int max) {
* Applies a filter to both strings to check if all words of keywords are in body.
* The order does not matter.
*/
static boolean containsWords(String body, String keywords){
static boolean containsWords(String body, String keywords) {
JavaUtils.Function<String, String> filter = s -> s.toUpperCase().replaceAll("[\\s-_]+", " ");
// Match all words
String[] words = filter.apply(keywords).split(" ");
body = filter.apply(body);
boolean match = true;
for (String str : words) {
if (!body.contains(str)){
if (!body.contains(str)) {
match = false;
break;
}
Expand All @@ -68,10 +69,19 @@ static boolean containsWords(String body, String keywords){
* Returns the object, or default if null
* java.util.Optional requires api 24
*/
static <T> T valueOrDefault(T value, T defaultValue){
static <T> T valueOrDefault(T value, T defaultValue) {
return value == null ? defaultValue : value;
}

/**
* if the element is present in the list, removes it
* if not, adds it
*/
static <E> void toggleContains(Collection<E> list, E element) {
if (list.contains(element)) list.remove(element);
else list.add(element);
}

/**
* java.util.function.Consumer requires api 24
*/
Expand Down

0 comments on commit 491cd17

Please sign in to comment.