diff --git a/core/src/org/transdroid/core/app/settings/ApplicationSettings.java b/core/src/org/transdroid/core/app/settings/ApplicationSettings.java index f8ab4c15..d1a8eab1 100644 --- a/core/src/org/transdroid/core/app/settings/ApplicationSettings.java +++ b/core/src/org/transdroid/core/app/settings/ApplicationSettings.java @@ -8,6 +8,7 @@ import org.androidannotations.annotations.EBean.Scope; import org.androidannotations.annotations.RootContext; import org.transdroid.daemon.Daemon; import org.transdroid.daemon.OS; +import org.transdroid.daemon.TorrentsSortBy; import android.content.Context; import android.content.SharedPreferences; @@ -320,4 +321,33 @@ public class ApplicationSettings { } + /** + * Registers the torrents list sort order as being last used by the user + * @param currentSortOrder The sort order property the user selected last + * @param currentSortAscending The sort order direction that was last used + */ + public void setLastUsedSortOrder(TorrentsSortBy currentSortOrder, boolean currentSortAscending) { + prefs.edit().putInt("system_lastusedsortorder", currentSortOrder.getCode()).commit(); + prefs.edit().putBoolean("system_lastusedsortdirection", currentSortAscending).commit(); + } + + /** + * Returns the sort order property that the user last used. Use together with {@link #getLastUsedSortDescending()} to + * get the full last used sort settings. + * @return The last used sort order enumeration value + */ + public TorrentsSortBy getLastUsedSortOrder() { + return TorrentsSortBy.getStatus(prefs.getInt("system_lastusedsortorder", TorrentsSortBy.Alphanumeric.getCode())); + } + + /** + * Returns the sort order direction that the user last used. Use together with {@link #getLastUsedSortOrder()} to + * get the full last used sort settings. + * @return True if the last used sort direction was descending, false otherwise (i.e. the default ascending + * direction) + */ + public boolean getLastUsedSortDescending() { + return prefs.getBoolean("system_lastusedsortdirection", false); + } + } diff --git a/core/src/org/transdroid/core/gui/TorrentsActivity.java b/core/src/org/transdroid/core/gui/TorrentsActivity.java index c2c0536f..9d5ffb7c 100644 --- a/core/src/org/transdroid/core/gui/TorrentsActivity.java +++ b/core/src/org/transdroid/core/gui/TorrentsActivity.java @@ -24,25 +24,18 @@ import org.transdroid.core.app.settings.ApplicationSettings; import org.transdroid.core.app.settings.ServerSetting; import org.transdroid.core.gui.lists.LocalTorrent; import org.transdroid.core.gui.lists.SimpleListItem; -import org.transdroid.core.gui.log.Log; -import org.transdroid.core.gui.log.Log_; -import org.transdroid.core.gui.navigation.FilterListAdapter; -import org.transdroid.core.gui.navigation.FilterListAdapter_; -import org.transdroid.core.gui.navigation.FilterListDropDownAdapter; -import org.transdroid.core.gui.navigation.FilterListDropDownAdapter_; -import org.transdroid.core.gui.navigation.Label; -import org.transdroid.core.gui.navigation.NavigationFilter; -import org.transdroid.core.gui.navigation.NavigationHelper; -import org.transdroid.core.gui.navigation.StatusType; +import org.transdroid.core.gui.log.*; +import org.transdroid.core.gui.navigation.*; import org.transdroid.core.gui.search.BarcodeHelper; import org.transdroid.core.gui.search.FilePickerHelper; import org.transdroid.core.gui.search.UrlEntryDialog; -import org.transdroid.core.gui.settings.MainSettingsActivity_; +import org.transdroid.core.gui.settings.*; import org.transdroid.daemon.Daemon; import org.transdroid.daemon.IDaemonAdapter; import org.transdroid.daemon.Torrent; import org.transdroid.daemon.TorrentDetails; import org.transdroid.daemon.TorrentFile; +import org.transdroid.daemon.TorrentsSortBy; import org.transdroid.daemon.task.AddByFileTask; import org.transdroid.daemon.task.AddByMagnetUrlTask; import org.transdroid.daemon.task.AddByUrlTask; @@ -219,7 +212,8 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi menu.findItem(R.id.action_filter).setVisible(false); menu.findItem(R.id.action_settings).setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS); menu.findItem(R.id.action_help).setVisible(true); - fragmentTorrents.updateConnectionStatus(false); + if (fragmentTorrents != null) + fragmentTorrents.updateConnectionStatus(false); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); return true; } @@ -236,7 +230,8 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi menu.findItem(R.id.action_filter).setVisible(true); menu.findItem(R.id.action_settings).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER); menu.findItem(R.id.action_help).setVisible(false); - fragmentTorrents.updateConnectionStatus(true); + if (fragmentTorrents != null) + fragmentTorrents.updateConnectionStatus(true); getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); return true; @@ -463,6 +458,36 @@ public class TorrentsActivity extends SherlockFragmentActivity implements OnNavi startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.transdroid.org/download/"))); } + @OptionsItem(resName = "action_sort_byname") + protected void sortByName() { + fragmentTorrents.sortBy(TorrentsSortBy.Alphanumeric); + } + + @OptionsItem(resName = "action_sort_status") + protected void sortByStatus() { + fragmentTorrents.sortBy(TorrentsSortBy.Status); + } + + @OptionsItem(resName = "action_sort_done") + protected void sortByDateDone() { + fragmentTorrents.sortBy(TorrentsSortBy.DateDone); + } + + @OptionsItem(resName = "action_sort_added") + protected void sortByDateAdded() { + fragmentTorrents.sortBy(TorrentsSortBy.DateAdded); + } + + @OptionsItem(resName = "action_sort_upspeed") + protected void sortByUpspeed() { + fragmentTorrents.sortBy(TorrentsSortBy.UploadSpeed); + } + + @OptionsItem(resName = "action_sort_ratio") + protected void sortByRatio() { + fragmentTorrents.sortBy(TorrentsSortBy.Ratio); + } + /** * Shows the a details fragment for the given torrent, either in the dedicated details fragment pane, in the same * pane as the torrent list was displayed or by starting a details activity. diff --git a/core/src/org/transdroid/core/gui/TorrentsFragment.java b/core/src/org/transdroid/core/gui/TorrentsFragment.java index 99622395..bc9df79f 100644 --- a/core/src/org/transdroid/core/gui/TorrentsFragment.java +++ b/core/src/org/transdroid/core/gui/TorrentsFragment.java @@ -1,18 +1,24 @@ package org.transdroid.core.gui; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import org.androidannotations.annotations.AfterViews; +import org.androidannotations.annotations.Bean; import org.androidannotations.annotations.EFragment; import org.androidannotations.annotations.InstanceState; import org.androidannotations.annotations.ItemClick; import org.androidannotations.annotations.ViewById; import org.transdroid.core.R; +import org.transdroid.core.app.settings.ApplicationSettings; import org.transdroid.core.gui.lists.TorrentsAdapter; import org.transdroid.core.gui.lists.TorrentsAdapter_; import org.transdroid.core.gui.navigation.NavigationFilter; +import org.transdroid.daemon.Daemon; import org.transdroid.daemon.Torrent; +import org.transdroid.daemon.TorrentsComparator; +import org.transdroid.daemon.TorrentsSortBy; import android.view.View; import android.widget.ProgressBar; @@ -29,11 +35,17 @@ import com.actionbarsherlock.view.SherlockListView.MultiChoiceModeListenerCompat public class TorrentsFragment extends SherlockFragment { // Local data + @Bean + protected ApplicationSettings applicationSettings; @InstanceState protected ArrayList torrents = null; @InstanceState protected NavigationFilter currentFilter = null; @InstanceState + protected TorrentsSortBy currentSortOrder = TorrentsSortBy.Alphanumeric; + @InstanceState + protected boolean currentSortDescending = false; + @InstanceState protected boolean hasAConnection = false; @InstanceState protected boolean isLoading = true; @@ -54,6 +66,8 @@ public class TorrentsFragment extends SherlockFragment { @AfterViews protected void init() { + this.currentSortOrder = applicationSettings.getLastUsedSortOrder(); + this.currentSortDescending = applicationSettings.getLastUsedSortDescending(); torrentsList.setAdapter(TorrentsAdapter_.getInstance_(getActivity())); torrentsList.setMultiChoiceModeListener(onTorrentsSelected); if (torrents != null) @@ -70,24 +84,47 @@ public class TorrentsFragment extends SherlockFragment { } /** - * Clear currently visible list of torrents + * Clears the currently visible list of torrents. */ public void clear() { this.connectionErrorMessage = null; updateTorrents(null); } + /** + * Stores the new sort order (for future refreshes) and sorts the current visible list. If the given new sort + * property equals the existing property, the list sort order is reversed instead. + * @param newSortOrder The sort order that the user selected. + */ + public void sortBy(TorrentsSortBy newSortOrder) { + // Update the sort order property and direction and store this last used setting + if (this.currentSortOrder == newSortOrder) { + this.currentSortDescending = !this.currentSortDescending; + } else { + this.currentSortOrder = newSortOrder; + this.currentSortDescending = false; + } + applicationSettings.setLastUsedSortOrder(this.currentSortOrder, this.currentSortDescending); + // Get the server daemon type directly form the local list of torrents, if it's not empty + Daemon serverType = (this.torrents != null && this.torrents.size() > 0 ? this.torrents.get(0).getDaemon() + : Daemon.Transmission); + Collections.sort(this.torrents, new TorrentsComparator(serverType, this.currentSortOrder, + this.currentSortDescending)); + // Show the new resorted list + applyFilter(this.currentFilter); + } + /** * Apply a filter on the current list of all torrents, showing the appropriate sublist of torrents only - * @param currentFilter + * @param newFilter The new filter to apply to the local list of torrents */ - public void applyFilter(NavigationFilter currentFilter) { - this.currentFilter = currentFilter; - if (torrents != null && currentFilter != null) { + public void applyFilter(NavigationFilter newFilter) { + this.currentFilter = newFilter; + if (torrents != null && newFilter != null) { // Build a local list of torrents that match the selected navigation filter ArrayList filteredTorrents = new ArrayList(); for (Torrent torrent : torrents) { - if (currentFilter.matches(torrent)) + if (newFilter.matches(torrent)) filteredTorrents.add(torrent); } ((TorrentsAdapter) torrentsList.getAdapter()).update(filteredTorrents); diff --git a/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java index 7cdfbe41..818851b4 100644 --- a/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/MainSettingsActivity.java @@ -13,7 +13,7 @@ import org.transdroid.core.app.settings.ApplicationSettings; import org.transdroid.core.app.settings.RssfeedSetting; import org.transdroid.core.app.settings.ServerSetting; import org.transdroid.core.app.settings.WebsearchSetting; -import org.transdroid.core.gui.TorrentsActivity_; +import org.transdroid.core.gui.*; import org.transdroid.core.gui.settings.RssfeedPreference.OnRssfeedClickedListener; import org.transdroid.core.gui.settings.ServerPreference.OnServerClickedListener; import org.transdroid.core.gui.settings.WebsearchPreference.OnWebsearchClickedListener; diff --git a/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java index 8828fc3e..5d1d7a56 100644 --- a/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/RssfeedSettingsActivity.java @@ -4,7 +4,7 @@ import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.transdroid.core.R; -import org.transdroid.core.app.settings.ApplicationSettings_; +import org.transdroid.core.app.settings.*; import android.annotation.TargetApi; import android.content.Intent; diff --git a/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java index a24c59ee..772975d9 100644 --- a/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/ServerSettingsActivity.java @@ -4,7 +4,7 @@ import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.transdroid.core.R; -import org.transdroid.core.app.settings.ApplicationSettings_; +import org.transdroid.core.app.settings.*; import org.transdroid.daemon.Daemon; import android.annotation.TargetApi; diff --git a/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java b/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java index c47f458e..261cd531 100644 --- a/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java +++ b/core/src/org/transdroid/core/gui/settings/WebsearchSettingsActivity.java @@ -4,7 +4,7 @@ import org.androidannotations.annotations.EActivity; import org.androidannotations.annotations.OptionsItem; import org.androidannotations.annotations.OptionsMenu; import org.transdroid.core.R; -import org.transdroid.core.app.settings.ApplicationSettings_; +import org.transdroid.core.app.settings.*; import android.annotation.TargetApi; import android.content.Intent; diff --git a/lib/src/org/transdroid/daemon/TorrentsComparator.java b/lib/src/org/transdroid/daemon/TorrentsComparator.java index 8cb93f9f..43a30884 100644 --- a/lib/src/org/transdroid/daemon/TorrentsComparator.java +++ b/lib/src/org/transdroid/daemon/TorrentsComparator.java @@ -32,24 +32,22 @@ public class TorrentsComparator implements Comparator { boolean reversed; /** - * Instantiate a torrents comparator. The daemon object is used to check support for comparing - * on the set properties. If the daemon does not support the property, ascending Alphanumeric - * sorting will be used even if sorting is requested on the unsupported property. - * @param daemon The loaded server daemon, which exposes what features and properties it supports + * Instantiate a torrents comparator. The daemon type is used to check support for comparing on the set property. If + * the daemon does not support the property, Alphanumeric sorting will be used even if sorting is requested on the + * unsupported property. + * @param daemonType The currently loaded server daemon's type, which exposes what features and properties it supports * @param sortBy The requested sorting property (Alphanumeric is used for unsupported properties that are requested) * @param reversed If the sorting should be in reverse order */ - public TorrentsComparator(IDaemonAdapter daemon, TorrentsSortBy sortBy, boolean reversed) { + public TorrentsComparator(Daemon daemonType, TorrentsSortBy sortBy, boolean reversed) { this.sortBy = sortBy; this.reversed = reversed; - switch (sortBy) { - case DateAdded: - if (daemon != null && !Daemon.supportsDateAdded(daemon.getType())) { + if (sortBy == TorrentsSortBy.DateAdded) { + if (daemonType != null && !Daemon.supportsDateAdded(daemonType)) { // Reset the sorting to simple Alphanumeric this.sortBy = TorrentsSortBy.Alphanumeric; this.reversed = false; } - break; } }