From 43ed12b0834e7ac9ad949427c5b7793c7e603693 Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Tue, 9 Jan 2018 09:56:37 +0200 Subject: [PATCH 1/2] Add Sort-By Action to Search Activity --- .../app/settings/ApplicationSettings.java | 39 ++++++++++++++----- .../core/gui/search/SearchActivity.java | 33 +++++++++++++++- .../gui/search/SearchResultsFragment.java | 8 ++-- app/src/main/res/menu/activity_search.xml | 18 +++++++++ app/src/main/res/values/strings.xml | 1 + 5 files changed, 83 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java b/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java index 7e88e1c9..1ab34c3e 100644 --- a/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java +++ b/app/src/main/java/org/transdroid/core/app/settings/ApplicationSettings.java @@ -16,11 +16,11 @@ */ package org.transdroid.core.app.settings; -import java.security.InvalidParameterException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; -import java.util.List; +import android.content.Context; +import android.content.SharedPreferences; +import android.content.SharedPreferences.Editor; +import android.preference.PreferenceManager; +import android.text.TextUtils; import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EBean; @@ -29,6 +29,7 @@ import org.androidannotations.annotations.RootContext; import org.json.JSONArray; import org.json.JSONException; import org.transdroid.core.app.search.SearchHelper; +import org.transdroid.core.app.search.SearchHelper.SearchSortOrder; import org.transdroid.core.app.search.SearchSite; import org.transdroid.core.gui.navigation.NavigationFilter; import org.transdroid.core.gui.navigation.StatusType; @@ -39,11 +40,11 @@ import org.transdroid.daemon.Daemon; import org.transdroid.daemon.OS; import org.transdroid.daemon.TorrentsSortBy; -import android.content.Context; -import android.content.SharedPreferences; -import android.content.SharedPreferences.Editor; -import android.preference.PreferenceManager; -import android.text.TextUtils; +import java.security.InvalidParameterException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; /** * Singleton object to access all application settings, including stored servers, web search sites and RSS feeds. @@ -558,6 +559,24 @@ public class ApplicationSettings { .getStatus(prefs.getInt("system_lastusedsortorder", TorrentsSortBy.Alphanumeric.getCode())); } + /** + * Registers the search list sort order as being last used by the user + * @param currentSortOrder The sort order property the user selected last + */ + public void setLastUsedSearchSortOrder(SearchSortOrder currentSortOrder) { + Editor edit = prefs.edit(); + edit.putInt("system_lastusedsearchsortorder", currentSortOrder.ordinal()); + edit.apply(); + } + + /** + * Returns the search sort order property that the user last used. + * @return The last used sort order enumeration value + */ + public SearchSortOrder getLastUsedSearchSortOrder() { + return SearchSortOrder.values()[(prefs.getInt("system_lastusedsearchsortorder", SearchSortOrder.BySeeders.ordinal()))]; + } + /** * Returns the sort order direction that the user last used. Use together with {@link #getLastUsedSortOrder()} to * get the full last used sort settings. diff --git a/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java b/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java index ad10d9af..5b8188a9 100644 --- a/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java @@ -25,7 +25,6 @@ import android.os.Bundle; import android.provider.SearchRecentSuggestions; import android.support.v4.view.MenuItemCompat; import android.support.v7.app.AppCompatActivity; -import android.support.v7.view.ContextThemeWrapper; import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; @@ -46,6 +45,7 @@ import org.androidannotations.annotations.SystemService; import org.androidannotations.annotations.ViewById; import org.transdroid.R; import org.transdroid.core.app.search.SearchHelper; +import org.transdroid.core.app.search.SearchHelper.SearchSortOrder; import org.transdroid.core.app.search.SearchSite; import org.transdroid.core.app.settings.ApplicationSettings; import org.transdroid.core.app.settings.SystemSettings_; @@ -85,6 +85,8 @@ public class SearchActivity extends AppCompatActivity { private List searchSites; private SearchSetting lastUsedSite; private String lastUsedQuery; + private MenuItem sortByAdded; + private MenuItem sortBySeeders; @Override public void onCreate(Bundle savedInstanceState) { @@ -177,6 +179,14 @@ public class SearchActivity extends AppCompatActivity { searchView.setIconifiedByDefault(false); MenuItemCompat.setActionView(item, searchView); searchMenu = item; + sortBySeeders = menu.findItem(R.id.action_sort_seeders); + sortByAdded = menu.findItem(R.id.action_sort_added); + final SearchSortOrder sortOrder = applicationSettings.getLastUsedSearchSortOrder(); + if (sortOrder == SearchSortOrder.BySeeders) { + sortBySeeders.setChecked(true); + } else { + sortByAdded.setChecked(true); + } return true; } @@ -304,7 +314,7 @@ public class SearchActivity extends AppCompatActivity { getSupportActionBar() .setTitle(NavigationHelper.buildCondensedFontString(getString(R.string.search_queryonsite, lastUsedQuery, lastUsedSite.getName()))); // Ask the results fragment to start a search for the specified query - fragmentResults.startSearch(lastUsedQuery, (SearchSite) lastUsedSite); + fragmentResults.startSearch(lastUsedQuery, (SearchSite) lastUsedSite, applicationSettings.getLastUsedSearchSortOrder()); } } @@ -314,4 +324,23 @@ public class SearchActivity extends AppCompatActivity { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.transdroid.org/latest-search"))); } + @OptionsItem(R.id.action_sort_added) + protected void sortByDateAdded() { + if (sortByAdded.isChecked()) { + return; + } + sortByAdded.setChecked(true); + applicationSettings.setLastUsedSearchSortOrder(SearchSortOrder.Combined); + refreshSearch(); + } + + @OptionsItem(R.id.action_sort_seeders) + protected void sortBySeeders() { + if (sortBySeeders.isChecked()) { + return; + } + sortBySeeders.setChecked(true); + applicationSettings.setLastUsedSearchSortOrder(SearchSortOrder.BySeeders); + refreshSearch(); + } } diff --git a/app/src/main/java/org/transdroid/core/gui/search/SearchResultsFragment.java b/app/src/main/java/org/transdroid/core/gui/search/SearchResultsFragment.java index 6ecbbea9..2aa5c3bd 100644 --- a/app/src/main/java/org/transdroid/core/gui/search/SearchResultsFragment.java +++ b/app/src/main/java/org/transdroid/core/gui/search/SearchResultsFragment.java @@ -102,16 +102,16 @@ public class SearchResultsFragment extends Fragment { } - public void startSearch(String query, SearchSite site) { + public void startSearch(String query, SearchSite site, SearchSortOrder sortBy) { loadingProgress.setVisibility(View.VISIBLE); resultsList.setVisibility(View.GONE); emptyText.setVisibility(View.GONE); - performSearch(query, site); + performSearch(query, site, sortBy); } @Background - protected void performSearch(String query, SearchSite site) { - results = searchHelper.search(query, site, SearchSortOrder.BySeeders); + protected void performSearch(String query, SearchSite site, SearchSortOrder sortBy) { + results = searchHelper.search(query, site, sortBy); resultsSource = site.isPrivate() ? site.getKey() : null; showResults(); } diff --git a/app/src/main/res/menu/activity_search.xml b/app/src/main/res/menu/activity_search.xml index 1d81b7fd..1af4a3f9 100644 --- a/app/src/main/res/menu/activity_search.xml +++ b/app/src/main/res/menu/activity_search.xml @@ -28,6 +28,24 @@ android:icon="@drawable/ic_action_refresh" android:title="@string/action_refresh" app:showAsAction="ifRoom" /> + + + + + + + + + Status Date done Date added + Seeders Percent downloaded Download speed Upload speed From 61ddff9da4055c2f9828d06817ea3a16724dce5e Mon Sep 17 00:00:00 2001 From: Alon Albert Date: Thu, 11 Jan 2018 17:26:30 +0200 Subject: [PATCH 2/2] Remove menuitem fields --- .../transdroid/core/gui/search/SearchActivity.java | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java b/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java index 5b8188a9..d65f66ca 100644 --- a/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java +++ b/app/src/main/java/org/transdroid/core/gui/search/SearchActivity.java @@ -85,8 +85,6 @@ public class SearchActivity extends AppCompatActivity { private List searchSites; private SearchSetting lastUsedSite; private String lastUsedQuery; - private MenuItem sortByAdded; - private MenuItem sortBySeeders; @Override public void onCreate(Bundle savedInstanceState) { @@ -179,8 +177,8 @@ public class SearchActivity extends AppCompatActivity { searchView.setIconifiedByDefault(false); MenuItemCompat.setActionView(item, searchView); searchMenu = item; - sortBySeeders = menu.findItem(R.id.action_sort_seeders); - sortByAdded = menu.findItem(R.id.action_sort_added); + final MenuItem sortBySeeders = menu.findItem(R.id.action_sort_seeders); + final MenuItem sortByAdded = menu.findItem(R.id.action_sort_added); final SearchSortOrder sortOrder = applicationSettings.getLastUsedSearchSortOrder(); if (sortOrder == SearchSortOrder.BySeeders) { sortBySeeders.setChecked(true); @@ -326,20 +324,20 @@ public class SearchActivity extends AppCompatActivity { @OptionsItem(R.id.action_sort_added) protected void sortByDateAdded() { - if (sortByAdded.isChecked()) { + if (applicationSettings.getLastUsedSearchSortOrder() == SearchSortOrder.Combined) { return; } - sortByAdded.setChecked(true); + invalidateOptionsMenu(); applicationSettings.setLastUsedSearchSortOrder(SearchSortOrder.Combined); refreshSearch(); } @OptionsItem(R.id.action_sort_seeders) protected void sortBySeeders() { - if (sortBySeeders.isChecked()) { + if (applicationSettings.getLastUsedSearchSortOrder() == SearchSortOrder.BySeeders) { return; } - sortBySeeders.setChecked(true); + invalidateOptionsMenu(); applicationSettings.setLastUsedSearchSortOrder(SearchSortOrder.BySeeders); refreshSearch(); }