From e3c0af15577a7654c7e02a00bf5ecaa920e76944 Mon Sep 17 00:00:00 2001 From: Eric Kok Date: Fri, 11 Oct 2013 13:09:19 +0200 Subject: [PATCH] Fixed widget preview torrents list UI. --- .../core/widget/WidgetConfigActivity.java | 6 +- .../core/widget/WidgetPreviewAdapter.java | 102 ++++++++++++++++++ 2 files changed, 103 insertions(+), 5 deletions(-) create mode 100644 core/src/org/transdroid/core/widget/WidgetPreviewAdapter.java diff --git a/core/src/org/transdroid/core/widget/WidgetConfigActivity.java b/core/src/org/transdroid/core/widget/WidgetConfigActivity.java index 01d8276f..b87b0ba1 100644 --- a/core/src/org/transdroid/core/widget/WidgetConfigActivity.java +++ b/core/src/org/transdroid/core/widget/WidgetConfigActivity.java @@ -32,7 +32,6 @@ import org.transdroid.core.app.settings.ApplicationSettings; import org.transdroid.core.app.settings.ServerSetting; import org.transdroid.core.gui.lists.SimpleListItemSpinnerAdapter; import org.transdroid.core.gui.lists.SortByListItem; -import org.transdroid.core.gui.lists.TorrentsAdapter; import org.transdroid.core.gui.navigation.StatusType; import org.transdroid.core.gui.navigation.StatusType.StatusTypeFilter; import org.transdroid.core.service.ConnectivityHelper; @@ -76,8 +75,6 @@ public class WidgetConfigActivity extends SherlockActivity { protected TextView filterText, serverText, errorText; @ViewById protected ListView torrentsList; - @Bean - protected TorrentsAdapter previewTorrentsAdapter; private List previewTorrents = null; // Settings and helpers @@ -122,7 +119,6 @@ public class WidgetConfigActivity extends SherlockActivity { sortSpinner.setAdapter(new SimpleListItemSpinnerAdapter(this, 0, sortOrders)); // TODO: Update to AndroidAnnotations 3.0 and use @CheckedChanged reverseorderCheckBox.setOnCheckedChangeListener(reverseorderCheckedChanged); - torrentsList.setAdapter(previewTorrentsAdapter); torrentsList.setEmptyView(errorText); // Set up action bar with a done button @@ -215,7 +211,7 @@ public class WidgetConfigActivity extends SherlockActivity { .sort(filteredTorrents, new TorrentsComparator(serverType, sortBy, reverseorderCheckBox.isChecked())); // Finally update the widget preview with the live, filtered and sorted torrents list - previewTorrentsAdapter.update(filteredTorrents); + torrentsList.setAdapter(new WidgetPreviewAdapter(this, 0, filteredTorrents)); torrentsList.setVisibility(View.VISIBLE); errorText.setVisibility(View.GONE); } diff --git a/core/src/org/transdroid/core/widget/WidgetPreviewAdapter.java b/core/src/org/transdroid/core/widget/WidgetPreviewAdapter.java new file mode 100644 index 00000000..efd9c4d7 --- /dev/null +++ b/core/src/org/transdroid/core/widget/WidgetPreviewAdapter.java @@ -0,0 +1,102 @@ +/* + * Copyright 2010-2013 Eric Kok et al. + * + * Transdroid is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Transdroid is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Transdroid. If not, see . + */ +package org.transdroid.core.widget; + +import java.util.List; + +import org.transdroid.core.R; +import org.transdroid.core.gui.lists.LocalTorrent; +import org.transdroid.daemon.Torrent; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.TextView; + +/** + * A list list item adapter that shows torrents as simplified, widget-style list items; the light theme is always used. + * @author Eric Kok + */ +public class WidgetPreviewAdapter extends ArrayAdapter { + + /** + * Constructs the custom array adapter that shows torrents in a widget list style for preview. + * @param context The widget configuration activity context + * @param foo Ignored parameter; the light theme widget appearance is always used + * @param torrents The already-retrieved, non-null list of torrents to show + */ + public WidgetPreviewAdapter(Context context, int foo, List torrents) { + super(context, R.layout.list_item_widget_light, torrents); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + + // Get the views + ViewHolder holder; + if (convertView == null) { + convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_widget_light, parent, false); + holder = new ViewHolder(); + holder.nameText = (TextView) convertView.findViewById(R.id.name_text); + holder.progressText = (TextView) convertView.findViewById(R.id.progress_text); + holder.ratioText = (TextView) convertView.findViewById(R.id.ratio_text); + holder.statusView = convertView.findViewById(R.id.status_view); + convertView.setTag(holder); + } else { + holder = (ViewHolder) convertView.getTag(); + } + + // Bind the torrent values + Torrent torrent = getItem(position); + LocalTorrent local = LocalTorrent.fromTorrent(torrent); + + int statusColour; + switch (torrent.getStatusCode()) { + case Downloading: + statusColour = R.color.torrent_downloading; + break; + case Paused: + statusColour = R.color.torrent_paused; + break; + case Seeding: + statusColour = R.color.torrent_seeding; + break; + case Error: + statusColour = R.color.torrent_error; + break; + default: // Checking, Waiting, Queued, Unknown + statusColour = R.color.torrent_other; + break; + } + holder.nameText.setText(torrent.getName()); + holder.progressText.setText(local.getProgressSizeText(getContext().getResources(), false)); + holder.ratioText.setText(local.getProgressEtaRatioText(getContext().getResources())); + holder.statusView.setBackgroundColor(getContext().getResources().getColor(statusColour)); + return convertView; + + } + + protected static class ViewHolder { + public TextView nameText; + public TextView progressText; + public TextView ratioText; + public View statusView; + } + +}