Skip to content

Commit

Permalink
Add sort order to tag entry
Browse files Browse the repository at this point in the history
  • Loading branch information
TBog committed Dec 12, 2022
1 parent 3af16cc commit 00c2693
Show file tree
Hide file tree
Showing 17 changed files with 294 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -72,7 +73,7 @@ public void requestResults(String query, ISearcher searcher) {
pojo.setRelevance(pojo.normalizedName, null);

pojo.boostRelevance(19);
searcher.addResult(pojo);
searcher.addResult(Collections.singletonList(pojo));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import androidx.preference.PreferenceManager;

import java.util.Collection;
import java.util.Collections;

import rocks.tbog.tblauncher.Permission;
import rocks.tbog.tblauncher.entry.ContactEntry;
Expand Down Expand Up @@ -108,7 +109,7 @@ public static void checkResults(Collection<ContactEntry> entries, FuzzyScore fuz
boost += 40;
}
entry.boostRelevance(boost);
if (!searcher.addResult(entry))
if (!searcher.addResult(Collections.singletonList(entry)))
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import androidx.annotation.NonNull;

import java.util.Collections;
import java.util.regex.Pattern;

import rocks.tbog.tblauncher.entry.ContactEntry;
Expand Down Expand Up @@ -36,7 +37,7 @@ public DialContactEntry findById(@NonNull String id) {
public void requestResults(String query, ISearcher searcher) {
// Append an item only if query looks like a phone number and device has phone capabilities
if (phonePattern.matcher(query).find()) {
searcher.addResult(getResult(query));
searcher.addResult(Collections.singletonList(getResult(query)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.WorkerThread;

import java.util.Collection;
import java.util.Collections;

import rocks.tbog.tblauncher.entry.EntryItem;
import rocks.tbog.tblauncher.entry.EntryWithTags;
Expand Down Expand Up @@ -74,7 +75,7 @@ public static void tagsCheckResults(Collection<? extends EntryWithTags> entries,

entry.addResultMatch(matchedText, matchedInfo);

if (matchedInfo.match && !searcher.addResult(entry)) {
if (matchedInfo.match && !searcher.addResult(Collections.singletonList(entry))) {
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public SearchEntry findById(@NonNull String id) {

@Override
public void requestResults(String query, ISearcher searcher) {
searcher.addResult(getResults(query).toArray(new SearchEntry[0]));
searcher.addResult(getResults(query));
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.util.List;

import rocks.tbog.tblauncher.db.ModRecord;
import rocks.tbog.tblauncher.entry.TagSortEntry;
import rocks.tbog.tblauncher.entry.TagEntry;
import rocks.tbog.tblauncher.handler.DataHandler;

Expand All @@ -25,13 +26,20 @@ protected DBLoader<TagEntry> newLoadTask() {

@Nullable
public static TagEntry newTagEntryCheckId(String id) {
TagEntry tagEntry = null;
// first check if this is a TagAction
if (TagSortEntry.isTagSort(id)) {
tagEntry = new TagSortEntry(id);
// set default name (based on id)
tagEntry.setName(null);
}
// now check if it's a tag
if (id.startsWith(TagEntry.SCHEME)) {
TagEntry tagEntry = new TagEntry(id);
tagEntry = new TagEntry(id);
String tagName = id.substring(TagEntry.SCHEME.length());
tagEntry.setName(tagName);
return tagEntry;
}
return null;
return tagEntry;
}

@NonNull
Expand Down
25 changes: 15 additions & 10 deletions app/src/main/java/rocks/tbog/tblauncher/entry/StaticEntry.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,27 +52,32 @@ public String getIconCacheId() {

@Override
protected ListPopup buildPopupMenu(Context context, LinearAdapter adapter, View parentView, int flags) {

List<ContentLoadHelper.CategoryItem> categoryTitle = PrefCache.getResultPopupOrder(context);

for (ContentLoadHelper.CategoryItem categoryItem : categoryTitle) {
int titleStringId = categoryItem.textId;
if (titleStringId == R.string.popup_title_customize) {
adapter.add(new LinearAdapter.ItemTitle(context, R.string.popup_title_customize));
adapter.add(new LinearAdapter.Item(context, R.string.menu_action_rename));
adapter.add(new LinearAdapter.Item(context, R.string.menu_custom_icon));
}
int pos = adapter.getCount();
buildPopupMenuCategory(context, adapter, categoryItem.textId);
if (pos != adapter.getCount())
adapter.add(pos, new LinearAdapter.ItemTitle(context, categoryItem.textId));
}

if (Utilities.checkFlag(flags, LAUNCHED_FROM_QUICK_LIST)) {
adapter.add(new LinearAdapter.ItemTitle(context, R.string.menu_popup_title_settings));
adapter.add(new LinearAdapter.Item(context, R.string.menu_quick_list_remove));
adapter.add(new LinearAdapter.Item(context, R.string.menu_popup_quick_list_customize));
buildPopupMenuCategory(context, adapter, R.string.menu_popup_title_settings);
}

return inflatePopupMenu(context, adapter);
}

protected void buildPopupMenuCategory(Context context, @NonNull LinearAdapter adapter, int titleStringId) {
if (titleStringId == R.string.popup_title_customize) {
adapter.add(new LinearAdapter.Item(context, R.string.menu_action_rename));
adapter.add(new LinearAdapter.Item(context, R.string.menu_custom_icon));
} else if (titleStringId == R.string.menu_popup_title_settings) {
adapter.add(new LinearAdapter.Item(context, R.string.menu_quick_list_remove));
adapter.add(new LinearAdapter.Item(context, R.string.menu_popup_quick_list_customize));
}
}

@Override
boolean popupMenuClickHandler(@NonNull View view, @NonNull LinearAdapter.MenuItem item, int stringId, View parentView) {
Context ctx = view.getContext();
Expand Down
130 changes: 130 additions & 0 deletions app/src/main/java/rocks/tbog/tblauncher/entry/TagSortEntry.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package rocks.tbog.tblauncher.entry;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.Collections;

import rocks.tbog.tblauncher.BuildConfig;
import rocks.tbog.tblauncher.R;
import rocks.tbog.tblauncher.TBApplication;
import rocks.tbog.tblauncher.searcher.SortedTagSearcher;
import rocks.tbog.tblauncher.ui.LinearAdapter;

public class TagSortEntry extends TagEntry {
public static final String SORT_AZ = "sort/byName/";
public static final String SORT_ZA = "sort/byNameReversed/";
public static final String HISTORY_REC = "sort/history/recency/";
public static final String HISTORY_FREQ = "sort/history/frequency/";
public static final String HISTORY_FREC = "sort/history/frecency/";
public static final String HISTORY_ADAPTIVE = "sort/history/adaptive/";

public TagSortEntry(@NonNull String id) {
super(id);
if (BuildConfig.DEBUG) {
String action = getTagSortOrder(id);
if (action.isEmpty())
throw new IllegalStateException("Invalid " + TagSortEntry.class.getSimpleName() + " id `" + id + "`");
}
}

@Override
public void setName(String name) {
if (name == null) {
// find the default name from the id
String action = getTagSortOrder(id);
super.setName(id.substring(SCHEME.length() + action.length()));
}
super.setName(name);
}

@Override
public void doLaunch(@NonNull View v, int flags) {
if (TBApplication.activityInvalid(v))
return;
Context ctx = v.getContext();
TBApplication.quickList(ctx).toggleSearch(v, id, SortedTagSearcher.class);
}

@Override
protected void buildPopupMenuCategory(Context context, @NonNull LinearAdapter adapter, int titleStringId) {
if (titleStringId == R.string.popup_title_hist_fav) {
adapter.add(new LinearAdapter.Item(context, R.string.menu_tag_sort_az));
adapter.add(new LinearAdapter.Item(context, R.string.menu_tag_sort_za));
adapter.add(new LinearAdapter.Item(context, R.string.menu_tag_sort_hist_rec));
adapter.add(new LinearAdapter.Item(context, R.string.menu_tag_sort_hist_freq));
adapter.add(new LinearAdapter.Item(context, R.string.menu_tag_sort_hist_frec));
adapter.add(new LinearAdapter.Item(context, R.string.menu_tag_sort_hist_adaptive));
}
}

@Override
boolean popupMenuClickHandler(@NonNull View view, @NonNull LinearAdapter.MenuItem item, int stringId, View parentView) {
if (stringId == R.string.menu_tag_sort_az) {
Toast.makeText(view.getContext(), "WIP: sort AZ", Toast.LENGTH_SHORT).show();
return true;
} else if (stringId == R.string.menu_tag_sort_za) {
Toast.makeText(view.getContext(), "WIP: sort ZA", Toast.LENGTH_SHORT).show();
return true;
}else if (stringId == R.string.menu_tag_sort_hist_rec) {
Toast.makeText(view.getContext(), "WIP: sort hist rec", Toast.LENGTH_SHORT).show();
return true;
}else if (stringId == R.string.menu_tag_sort_hist_freq) {
Toast.makeText(view.getContext(), "WIP: sort hist freq", Toast.LENGTH_SHORT).show();
return true;
}else if (stringId == R.string.menu_tag_sort_hist_frec) {
Toast.makeText(view.getContext(), "WIP: sort hist frec", Toast.LENGTH_SHORT).show();
return true;
}else if (stringId == R.string.menu_tag_sort_hist_adaptive) {
Toast.makeText(view.getContext(), "WIP: sort hist adaptive", Toast.LENGTH_SHORT).show();
return true;
}
return super.popupMenuClickHandler(view, item, stringId, parentView);
}

@NonNull
public static String getTagSortOrder(String id) {
String idWithoutScheme = id.substring(SCHEME.length());
if (idWithoutScheme.startsWith(SORT_AZ))
return SORT_AZ;
if (idWithoutScheme.startsWith(SORT_ZA))
return SORT_ZA;
if (idWithoutScheme.startsWith(HISTORY_REC))
return HISTORY_REC;
if (idWithoutScheme.startsWith(HISTORY_FREQ))
return HISTORY_FREQ;
if (idWithoutScheme.startsWith(HISTORY_FREC))
return HISTORY_FREC;
if (idWithoutScheme.startsWith(HISTORY_ADAPTIVE))
return HISTORY_ADAPTIVE;
return "";
}

public static boolean isTagSort(String id) {
return !getTagSortOrder(id).isEmpty();
}

public static class TagDetails {
public final String name;
public final String order;

public TagDetails(@NonNull String tagSortId) {
order = getTagSortOrder(tagSortId);
name = tagSortId.substring(SCHEME.length() + order.length());
}

public java.util.Comparator<EntryItem> getComparator() {
switch (order) {
case SORT_AZ:
return NAME_COMPARATOR;
case SORT_ZA:
return Collections.reverseOrder(NAME_COMPARATOR);
default:
return RELEVANCE_COMPARATOR;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ public void requestAllRecords(Searcher searcher) {
List<? extends EntryItem> pojos = entry.provider.getPojos();
if (pojos == null)
continue;
boolean accept = searcher.addResult(pojos.toArray(new EntryItem[0]));
boolean accept = searcher.addResult(pojos);
// if searcher will not accept any more results, exit
if (!accept)
break;
Expand Down Expand Up @@ -608,12 +608,12 @@ public static DBHelper.HistoryMode getHistoryMode(String historyMode) {
*
* @param itemCount max number of items to retrieve, total number may be less (search or calls are not returned for instance)
* @param historyMode Recency vs Frecency vs Frequency vs Adaptive
* @param sortHistory Sort history entries alphabetically
* @param sortByName Sort history entries alphabetically
* @param itemsToExcludeById Items to exclude from history by their id
* @return pojos in recent history
*/
public List<EntryItem> getHistory(int itemCount, DBHelper.HistoryMode historyMode,
boolean sortHistory, Set<String> itemsToExcludeById) {
boolean sortByName, Set<String> itemsToExcludeById) {
// Max sure that we get enough items, regardless of how many may be excluded
int extendedItemCount = itemCount + itemsToExcludeById.size();

Expand All @@ -639,7 +639,7 @@ public List<EntryItem> getHistory(int itemCount, DBHelper.HistoryMode historyMod
}

// sort the list if needed
if (sortHistory)
if (sortByName)
Collections.sort(history, (a, b) -> a.getName().compareTo(b.getName()));

// enforce item count after the sort operation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@

import androidx.annotation.WorkerThread;

import java.util.Collection;

import rocks.tbog.tblauncher.entry.EntryItem;

public interface ISearcher {
@WorkerThread
boolean addResult(EntryItem... items);
boolean addResult(Collection<? extends EntryItem> items);

boolean tagsEnabled();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import androidx.annotation.NonNull;
import androidx.annotation.WorkerThread;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;

Expand All @@ -29,7 +30,7 @@ public QuerySearcher(ISearchActivity activity, @NonNull String query) {
}

@Override
public boolean addResult(EntryItem... pojos) {
public boolean addResult(Collection<? extends EntryItem> pojos) {
// Give a boost if item was previously selected for this query
for (EntryItem pojo : pojos) {
int historyRecord = MapCompat.getOrDefault(knownIds, pojo.id, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Collection<T> getEntryItems() {
}

@Override
public boolean addResult(EntryItem... items) {
public boolean addResult(Collection<? extends EntryItem> items) {
boolean result = false;
for (EntryItem item : items)
result |= entryItems.add(typeClass.cast(item));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Collection;
import java.util.PriorityQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -71,10 +71,11 @@ protected int getMaxResultCount(Context context) {

/**
* This is called from the background thread by the providers
* @param pojos
*/
@WorkerThread
@Override
public boolean addResult(EntryItem... pojos) {
public boolean addResult(Collection<? extends EntryItem> pojos) {
if (isCancelled())
return false;

Expand All @@ -83,7 +84,7 @@ public boolean addResult(EntryItem... pojos) {
if (activity == null)
return false;

Collections.addAll(processedPojos, pojos);
processedPojos.addAll(pojos);
while (processedPojos.size() > maxResults)
processedPojos.poll();

Expand Down
Loading

0 comments on commit 00c2693

Please sign in to comment.