Browse Source

- moved most of remoterss/RemoteRssActivity code into RssfeedsActivity

- RemoteRssFragment now extends android.support.v4.app.Fragment
- removed swipeRefreshLayout
pull/526/head
Twig N 5 years ago
parent
commit
f92dd04a31
  1. 228
      app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssActivity.java
  2. 83
      app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssFragment.java
  3. 140
      app/src/main/java/org/transdroid/core/gui/rss/RssfeedsActivity.java
  4. 5
      app/src/main/res/layout/fragment_remoterss.xml

228
app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssActivity.java

@ -72,24 +72,24 @@ import java.util.List; @@ -72,24 +72,24 @@ import java.util.List;
* @author Twig Nguyen
*/
@EActivity(R.layout.activity_remoterss)
public class RemoteRssActivity extends AppCompatActivity implements RefreshableActivity {
@NonConfigurationInstance
protected ArrayList<RemoteRssChannel> feeds;
public class RemoteRssActivity extends AppCompatActivity {
// @NonConfigurationInstance
// protected ArrayList<RemoteRssChannel> feeds;
@InstanceState
protected int selectedFilter;
// @InstanceState
// protected int selectedFilter;
@NonConfigurationInstance
protected ArrayList<RemoteRssItem> recentItems;
// @NonConfigurationInstance
// protected ArrayList<RemoteRssItem> recentItems;
// Server connection
@Bean
protected ApplicationSettings applicationSettings;
@Bean
protected Log log;
@Bean
protected ConnectivityHelper connectivityHelper;
private IDaemonAdapter currentConnection;
// @Bean
// protected ConnectivityHelper connectivityHelper;
// private IDaemonAdapter currentConnection;
// Details view components
@ViewById
@ -122,71 +122,71 @@ public class RemoteRssActivity extends AppCompatActivity implements RefreshableA @@ -122,71 +122,71 @@ public class RemoteRssActivity extends AppCompatActivity implements RefreshableA
setSupportActionBar(torrentsToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Connect to the last used server
ServerSetting lastUsed = applicationSettings.getLastUsedServer();
currentConnection = lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this);
// // Connect to the last used server
// ServerSetting lastUsed = applicationSettings.getLastUsedServer();
// currentConnection = lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this);
//
// if (feeds != null) {
// // Called from a configuration change. No need to load anything from server
// showChannelFilters();
// fragmentRemoteRss.updateRemoteItems(
// selectedFilter == 0 ? recentItems : feeds.get(selectedFilter - 1).getItems(),
// false /* allow android to restore scroll position */ );
// } else {
// loadFeeds();
// }
if (feeds != null) {
// Called from a configuration change. No need to load anything from server
showChannelFilters();
fragmentRemoteRss.updateRemoteItems(
selectedFilter == 0 ? recentItems : feeds.get(selectedFilter - 1).getItems(),
false /* allow android to restore scroll position */ );
} else {
loadFeeds();
}
}
@Background
protected void loadFeeds() {
try {
fragmentRemoteRss.setRefreshing(true);
feeds = ((RemoteRssSupplier) (currentConnection)).getRemoteRssChannels(log);
fragmentRemoteRss.setRefreshing(false);
} catch (DaemonException e) {
onCommunicationError(e);
}
recentItems = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
Date oneMonthAgo = calendar.getTime();
for (RemoteRssChannel feed : feeds) {
for (RemoteRssItem item : feed.getItems()) {
if (item.getTimestamp().after(oneMonthAgo)) {
recentItems.add(item);
}
}
}
// Sort by -newest
Collections.sort(recentItems, new Comparator<RemoteRssItem>() {
@Override
public int compare(RemoteRssItem lhs, RemoteRssItem rhs) {
return rhs.getTimestamp().compareTo(lhs.getTimestamp());
}
});
afterLoadFeeds();
}
@UiThread
protected void afterLoadFeeds() {
// We need to wrap these calls in a @UiThread rather than make them each a @UiThread themselves
// because they need to run sequentially in a configuration change scenario in order for Android
// to maintain scroll position on the fragment adapter.
showChannelFilters();
onFeedSelected(selectedFilter);
}
@UiThread
protected void onCommunicationError(DaemonException daemonException) {
//noinspection ThrowableResultOfMethodCallIgnored
log.i(this, daemonException.toString());
String error = getString(LocalTorrent.getResourceForDaemonException(daemonException));
SnackbarManager.show(Snackbar.with(this).text(error).colorResource(R.color.red).type(SnackbarType.MULTI_LINE));
}
// @Background
// protected void loadFeeds() {
// try {
// fragmentRemoteRss.setRefreshing(true);
// feeds = ((RemoteRssSupplier) (currentConnection)).getRemoteRssChannels(log);
// fragmentRemoteRss.setRefreshing(false);
// } catch (DaemonException e) {
// onCommunicationError(e);
// }
//
// recentItems = new ArrayList<>();
// Calendar calendar = Calendar.getInstance();
// calendar.add(Calendar.MONTH, -1);
// Date oneMonthAgo = calendar.getTime();
//
// for (RemoteRssChannel feed : feeds) {
// for (RemoteRssItem item : feed.getItems()) {
// if (item.getTimestamp().after(oneMonthAgo)) {
// recentItems.add(item);
// }
// }
// }
// // Sort by -newest
// Collections.sort(recentItems, new Comparator<RemoteRssItem>() {
// @Override
// public int compare(RemoteRssItem lhs, RemoteRssItem rhs) {
// return rhs.getTimestamp().compareTo(lhs.getTimestamp());
// }
// });
//
// afterLoadFeeds();
// }
// @UiThread
// protected void afterLoadFeeds() {
// // We need to wrap these calls in a @UiThread rather than make them each a @UiThread themselves
// // because they need to run sequentially in a configuration change scenario in order for Android
// // to maintain scroll position on the fragment adapter.
// showChannelFilters();
// onFeedSelected(selectedFilter);
// }
// @UiThread
// protected void onCommunicationError(DaemonException daemonException) {
// //noinspection ThrowableResultOfMethodCallIgnored
// log.i(this, daemonException.toString());
// String error = getString(LocalTorrent.getResourceForDaemonException(daemonException));
// SnackbarManager.show(Snackbar.with(this).text(error).colorResource(R.color.red).type(SnackbarType.MULTI_LINE));
// }
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@ -209,48 +209,48 @@ public class RemoteRssActivity extends AppCompatActivity implements RefreshableA @@ -209,48 +209,48 @@ public class RemoteRssActivity extends AppCompatActivity implements RefreshableA
}
private void showChannelFilters() {
List<RemoteRssChannel> feedLabels = new ArrayList<>(feeds.size() +1);
feedLabels.add(new RemoteRssChannel() {
@Override
public String getName() {
return getString(R.string.remoterss_filter_allrecent);
// List<RemoteRssChannel> feedLabels = new ArrayList<>(feeds.size() +1);
// feedLabels.add(new RemoteRssChannel() {
// @Override
// public String getName() {
// return getString(R.string.remoterss_filter_allrecent);
// }
//
// @Override
// public void writeToParcel(Parcel dest, int flags) {
// }
// });
// feedLabels.addAll(feeds);
//
// drawerList.setAdapter(new SimpleListItemAdapter(this, feedLabels));
}
@Override
public void writeToParcel(Parcel dest, int flags) {
}
});
feedLabels.addAll(feeds);
drawerList.setAdapter(new SimpleListItemAdapter(this, feedLabels));
}
@ItemClick(R.id.drawer_list)
protected void onFeedSelected(int position) {
selectedFilter = position;
fragmentRemoteRss.updateRemoteItems(position == 0 ? recentItems : feeds.get(position - 1).getItems(), true);
RemoteRssChannel channel = (RemoteRssChannel) drawerList.getAdapter().getItem(position);
getSupportActionBar().setSubtitle(channel.getName());
drawerLayout.closeDrawers();
}
public IDaemonAdapter getCurrentConnection() {
return currentConnection;
}
public RemoteRssChannel getChannel(String name) {
for (RemoteRssChannel feed : feeds) {
if (feed.getName().equals(name)) {
return feed;
}
}
return null;
}
@Override
public void refreshScreen() {
loadFeeds();
}
// @ItemClick(R.id.drawer_list)
// protected void onFeedSelected(int position) {
// selectedFilter = position;
// fragmentRemoteRss.updateRemoteItems(position == 0 ? recentItems : feeds.get(position - 1).getItems(), true);
//
// RemoteRssChannel channel = (RemoteRssChannel) drawerList.getAdapter().getItem(position);
// getSupportActionBar().setSubtitle(channel.getName());
//
// drawerLayout.closeDrawers();
// }
// public IDaemonAdapter getCurrentConnection() {
// return currentConnection;
// }
// public RemoteRssChannel getChannel(String name) {
// for (RemoteRssChannel feed : feeds) {
// if (feed.getName().equals(name)) {
// return feed;
// }
// }
// return null;
// }
// @Override
// public void refreshScreen() {
// loadFeeds();
// }
}

83
app/src/main/java/org/transdroid/core/gui/remoterss/RemoteRssFragment.java

@ -17,7 +17,7 @@ @@ -17,7 +17,7 @@
package org.transdroid.core.gui.remoterss;
import android.app.Fragment;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.ActionMenuView;
import android.view.View;
@ -41,6 +41,7 @@ import org.transdroid.core.gui.log.Log; @@ -41,6 +41,7 @@ import org.transdroid.core.gui.log.Log;
import org.transdroid.core.gui.navigation.RefreshableActivity;
import org.transdroid.core.gui.remoterss.data.RemoteRssItem;
import org.transdroid.core.gui.remoterss.data.RemoteRssSupplier;
import org.transdroid.core.gui.rss.RssfeedsActivity;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
@ -66,8 +67,6 @@ public class RemoteRssFragment extends Fragment { @@ -66,8 +67,6 @@ public class RemoteRssFragment extends Fragment {
@ViewById(R.id.contextual_menu)
protected ActionMenuView contextualMenu;
@ViewById
protected SwipeRefreshLayout swipeRefreshLayout;
@ViewById
protected ListView torrentsList;
@ViewById
protected TextView remoterssStatusMessage;
@ -94,16 +93,12 @@ public class RemoteRssFragment extends Fragment { @@ -94,16 +93,12 @@ public class RemoteRssFragment extends Fragment {
adapter = new RemoteRssItemsAdapter(getActivity());
torrentsList.setAdapter(adapter);
torrentsList.setFastScrollEnabled(true);
}
// Allow pulls on the list view to refresh the torrents
if (getActivity() != null && getActivity() instanceof RefreshableActivity) {
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
((RefreshableActivity) getActivity()).refreshScreen();
}
});
}
public void onResume() {
super.onResume();
((RssfeedsActivity) getActivity()).refreshRemoteFeeds();
}
/**
@ -123,12 +118,6 @@ public class RemoteRssFragment extends Fragment { @@ -123,12 +118,6 @@ public class RemoteRssFragment extends Fragment {
remoterssStatusMessage.setVisibility(View.VISIBLE);
remoterssStatusMessage.setText(R.string.remoterss_no_files);
}
swipeRefreshLayout.setRefreshing(false);
}
@UiThread
public void setRefreshing(boolean refreshing) {
swipeRefreshLayout.setRefreshing(refreshing);
}
/**
@ -137,36 +126,38 @@ public class RemoteRssFragment extends Fragment { @@ -137,36 +126,38 @@ public class RemoteRssFragment extends Fragment {
@ItemClick(resName = "torrents_list")
protected void detailsListClicked(int position) {
RemoteRssItem item = (RemoteRssItem) adapter.getItem(position);
downloadRemoteRssItem(item);
}
// downloadRemoteRssItem(item);
/**
* Download the item in a background thread and display success/fail accordingly.
*/
@Background
protected void downloadRemoteRssItem(RemoteRssItem item) {
final RemoteRssActivity activity = (RemoteRssActivity) getActivity();
final RemoteRssSupplier supplier = (RemoteRssSupplier) activity.getCurrentConnection();
try {
supplier.downloadRemoteRssItem(log, item, activity.getChannel(item.getSourceName()));
onTaskSucceeded(null, getString(R.string.result_added, item.getTitle()));
} catch (DaemonException e) {
onTaskFailed(getString(LocalTorrent.getResourceForDaemonException(e)));
}
}
@UiThread
protected void onTaskSucceeded(DaemonTaskSuccessResult result, String successMessage) {
SnackbarManager.show(Snackbar.with(getActivity()).text(successMessage));
((RssfeedsActivity) getActivity()).downloadRemoteRssItem(item);
}
@UiThread
protected void onTaskFailed(String message) {
SnackbarManager.show(Snackbar.with(getActivity())
.text(message)
.colorResource(R.color.red)
.type(SnackbarType.MULTI_LINE)
);
}
// /**
// * Download the item in a background thread and display success/fail accordingly.
// */
// @Background
// protected void downloadRemoteRssItem(RemoteRssItem item) {
// final RemoteRssActivity activity = (RemoteRssActivity) getActivity();
// final RemoteRssSupplier supplier = (RemoteRssSupplier) activity.getCurrentConnection();
//
// try {
// supplier.downloadRemoteRssItem(log, item, activity.getChannel(item.getSourceName()));
// onTaskSucceeded(null, getString(R.string.result_added, item.getTitle()));
// } catch (DaemonException e) {
// onTaskFailed(getString(LocalTorrent.getResourceForDaemonException(e)));
// }
// }
//
// @UiThread
// protected void onTaskSucceeded(DaemonTaskSuccessResult result, String successMessage) {
// SnackbarManager.show(Snackbar.with(getActivity()).text(successMessage));
// }
//
// @UiThread
// protected void onTaskFailed(String message) {
// SnackbarManager.show(Snackbar.with(getActivity())
// .text(message)
// .colorResource(R.color.red)
// .type(SnackbarType.MULTI_LINE)
// );
// }
}

140
app/src/main/java/org/transdroid/core/gui/rss/RssfeedsActivity.java

@ -33,26 +33,43 @@ import android.text.TextUtils; @@ -33,26 +33,43 @@ import android.text.TextUtils;
import com.nispok.snackbar.Snackbar;
import com.nispok.snackbar.SnackbarManager;
import com.nispok.snackbar.enums.SnackbarType;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.FragmentById;
import org.androidannotations.annotations.InstanceState;
import org.androidannotations.annotations.NonConfigurationInstance;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.transdroid.R;
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.SystemSettings_;
import org.transdroid.core.gui.TorrentsActivity_;
import org.transdroid.core.gui.lists.LocalTorrent;
import org.transdroid.core.gui.log.Log;
import org.transdroid.core.gui.navigation.NavigationHelper;
import org.transdroid.core.gui.remoterss.RemoteRssFragment;
import org.transdroid.core.gui.remoterss.RemoteRssFragment_;
import org.transdroid.core.gui.remoterss.data.RemoteRssChannel;
import org.transdroid.core.gui.remoterss.data.RemoteRssItem;
import org.transdroid.core.gui.remoterss.data.RemoteRssSupplier;
import org.transdroid.core.rssparser.Channel;
import org.transdroid.core.rssparser.RssParser;
import org.transdroid.core.service.ConnectivityHelper;
import org.transdroid.daemon.DaemonException;
import org.transdroid.daemon.IDaemonAdapter;
import org.transdroid.daemon.task.DaemonTaskSuccessResult;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
@ -64,10 +81,12 @@ public class RssfeedsActivity extends AppCompatActivity { @@ -64,10 +81,12 @@ public class RssfeedsActivity extends AppCompatActivity {
protected Log log;
@Bean
protected ApplicationSettings applicationSettings;
protected List<RssfeedLoader> loaders;
// Contained feeds and items fragments
protected RssfeedsFragment fragmentFeeds;
protected RemoteRssFragment fragmentRemoteFeeds;
@FragmentById(R.id.rssitems_fragment)
protected RssitemsFragment fragmentItems;
@ViewById
@ -80,6 +99,17 @@ public class RssfeedsActivity extends AppCompatActivity { @@ -80,6 +99,17 @@ public class RssfeedsActivity extends AppCompatActivity {
protected static final int RSS_FEEDS_LOCAL = 0;
protected static final int RSS_FEEDS_REMOTE = 1;
// remote RSS stuff
@NonConfigurationInstance
protected ArrayList<RemoteRssChannel> feeds;
@InstanceState
protected int selectedFilter;
@NonConfigurationInstance
protected ArrayList<RemoteRssItem> recentItems;
@Bean
protected ConnectivityHelper connectivityHelper;
class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm) {
super(fm);
@ -93,7 +123,7 @@ public class RssfeedsActivity extends AppCompatActivity { @@ -93,7 +123,7 @@ public class RssfeedsActivity extends AppCompatActivity {
fragment = fragmentFeeds;
}
else if (position == RSS_FEEDS_REMOTE) {
fragment = null;
fragment = fragmentRemoteFeeds;
}
return fragment;
@ -134,7 +164,7 @@ public class RssfeedsActivity extends AppCompatActivity { @@ -134,7 +164,7 @@ public class RssfeedsActivity extends AppCompatActivity {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
fragmentFeeds = RssfeedsFragment_.builder().build();
fragmentItems = RssitemsFragment_.builder().build();
fragmentRemoteFeeds = RemoteRssFragment_.builder().build();
PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
@ -180,7 +210,6 @@ public class RssfeedsActivity extends AppCompatActivity { @@ -180,7 +210,6 @@ public class RssfeedsActivity extends AppCompatActivity {
handleRssfeedResult(loader, null, true);
log.i(this, "RSS feed " + loader.getSetting().getUrl() + " error: " + e.toString());
}
}
/**
@ -250,7 +279,112 @@ public class RssfeedsActivity extends AppCompatActivity { @@ -250,7 +279,112 @@ public class RssfeedsActivity extends AppCompatActivity {
.requiresExternalAuthentication(loader.getSetting().requiresExternalAuthentication()).start();
}
}
protected IDaemonAdapter getCurrentConnection() {
ServerSetting lastUsed = applicationSettings.getLastUsedServer();
return lastUsed.createServerAdapter(connectivityHelper.getConnectedNetworkName(), this);
}
public void refreshRemoteFeeds() {
// Connect to the last used server
IDaemonAdapter currentConnection = this.getCurrentConnection();
// remote rss not supported for this connection type
if (currentConnection instanceof RemoteRssSupplier == false) {
return;
}
try {
feeds = ((RemoteRssSupplier) (currentConnection)).getRemoteRssChannels(log);
// loadFeeds() in background
recentItems = new ArrayList<>();
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.MONTH, -1);
Date oneMonthAgo = calendar.getTime();
for (RemoteRssChannel feed : feeds) {
for (RemoteRssItem item : feed.getItems()) {
if (item.getTimestamp().after(oneMonthAgo)) {
recentItems.add(item);
}
}
}
// Sort by -newest
Collections.sort(recentItems, new Comparator<RemoteRssItem>() {
@Override
public int compare(RemoteRssItem lhs, RemoteRssItem rhs) {
return rhs.getTimestamp().compareTo(lhs.getTimestamp());
}
});
} catch (DaemonException e) {
onCommunicationError(e);
return;
}
// if (feeds != null) {
// Called from a configuration change. No need to load anything from server
// showChannelFilters();
fragmentRemoteFeeds.updateRemoteItems(
// selectedFilter == 0 ? recentItems : feeds.get(selectedFilter).getItems(),
recentItems,
false /* allow android to restore scroll position */ );
// } else {
// loadFeeds();
// }
}
@UiThread
protected void onCommunicationError(DaemonException daemonException) {
//noinspection ThrowableResultOfMethodCallIgnored
log.i(this, daemonException.toString());
String error = getString(LocalTorrent.getResourceForDaemonException(daemonException));
SnackbarManager.show(Snackbar.with(this).text(error).colorResource(R.color.red).type(SnackbarType.MULTI_LINE));
}
// @ItemClick(R.id.drawer_list)
protected void onFeedSelected(int position) {
selectedFilter = position;
RemoteRssChannel channel = feeds.get(position);
fragmentRemoteFeeds.updateRemoteItems(position == 0 ? recentItems : channel.getItems(), true);
// RemoteRssChannel channel = (RemoteRssChannel) drawerList.getAdapter().getItem(position);
// getSupportActionBar().setSubtitle(channel.getName());
// drawerLayout.closeDrawers();
}
/**
* Download the item in a background thread and display success/fail accordingly.
*/
@Background
public void downloadRemoteRssItem(RemoteRssItem item) {
final RemoteRssSupplier supplier = (RemoteRssSupplier) this.getCurrentConnection();
try {
RemoteRssChannel channel = feeds.get(selectedFilter);
supplier.downloadRemoteRssItem(log, item, channel);
onTaskSucceeded(null, getString(R.string.result_added, item.getTitle()));
} catch (DaemonException e) {
onTaskFailed(getString(LocalTorrent.getResourceForDaemonException(e)));
}
}
@UiThread
protected void onTaskSucceeded(DaemonTaskSuccessResult result, String successMessage) {
SnackbarManager.show(Snackbar.with(this).text(successMessage));
}
@UiThread
protected void onTaskFailed(String message) {
SnackbarManager.show(Snackbar.with(this)
.text(message)
.colorResource(R.color.red)
.type(SnackbarType.MULTI_LINE)
);
}
}

5
app/src/main/res/layout/fragment_remoterss.xml

@ -27,10 +27,6 @@ @@ -27,10 +27,6 @@
android:gravity="center"
android:id="@+id/remoterss_status_message"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/torrents_list"
android:layout_width="match_parent"
@ -38,5 +34,4 @@ @@ -38,5 +34,4 @@
android:clipToPadding="false"
tools:listitem="@layout/list_item_remoterssitem"
tools:visibility="visible"/>
</android.support.v4.widget.SwipeRefreshLayout>
</FrameLayout>

Loading…
Cancel
Save