Skip to content

Commit

Permalink
Merge download log+running lists (#5134)
Browse files Browse the repository at this point in the history
  • Loading branch information
ByteHamster authored Apr 29, 2021
1 parent 02dba45 commit d08a9c4
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 484 deletions.
132 changes: 101 additions & 31 deletions app/src/main/java/de/danoeh/antennapod/adapter/DownloadLogAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,32 @@

import android.app.Activity;
import android.text.format.DateUtils;
import android.util.Log;
import android.text.format.Formatter;
import android.view.View;
import android.view.ViewGroup;
import android.util.Log;
import android.widget.BaseAdapter;
import android.widget.Toast;

import android.widget.Toast;
import androidx.core.content.ContextCompat;

import androidx.fragment.app.ListFragment;
import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.dialog.DownloadRequestErrorDialogCreator;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.feed.FeedMedia;
import de.danoeh.antennapod.core.service.download.DownloadRequest;
import de.danoeh.antennapod.core.service.download.Downloader;
import de.danoeh.antennapod.core.service.download.DownloadStatus;
import de.danoeh.antennapod.core.storage.DBReader;
import de.danoeh.antennapod.core.storage.DBTasks;
import de.danoeh.antennapod.core.storage.DownloadRequestException;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.view.viewholder.DownloadItemViewHolder;
import de.danoeh.antennapod.model.feed.Feed;
import de.danoeh.antennapod.model.feed.FeedMedia;
import de.danoeh.antennapod.ui.common.ThemeUtils;
import de.danoeh.antennapod.view.viewholder.DownloadLogItemViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
* Displays a list of DownloadStatus entries.
Expand All @@ -29,37 +36,62 @@ public class DownloadLogAdapter extends BaseAdapter {
private static final String TAG = "DownloadLogAdapter";

private final Activity context;
private final ItemAccess itemAccess;
private final ListFragment listFragment;
private List<DownloadStatus> downloadLog = new ArrayList<>();
private List<Downloader> runningDownloads = new ArrayList<>();

public DownloadLogAdapter(Activity context, ItemAccess itemAccess) {
public DownloadLogAdapter(Activity context, ListFragment listFragment) {
super();
this.itemAccess = itemAccess;
this.context = context;
this.listFragment = listFragment;
}

public void setDownloadLog(List<DownloadStatus> downloadLog) {
this.downloadLog = downloadLog;
notifyDataSetChanged();
}

public void setRunningDownloads(List<Downloader> runningDownloads) {
this.runningDownloads = runningDownloads;
notifyDataSetChanged();
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
DownloadItemViewHolder holder;
DownloadLogItemViewHolder holder;
if (convertView == null) {
holder = new DownloadItemViewHolder(context, parent);
holder = new DownloadLogItemViewHolder(context, parent);
holder.itemView.setTag(holder);
} else {
holder = (DownloadItemViewHolder) convertView.getTag();
holder = (DownloadLogItemViewHolder) convertView.getTag();
}

DownloadStatus status = getItem(position);
Object item = getItem(position);
if (item instanceof DownloadStatus) {
bind(holder, (DownloadStatus) item, position);
} else if (item instanceof Downloader) {
bind(holder, (Downloader) item, position);
}
return holder.itemView;
}

private void bind(DownloadLogItemViewHolder holder, DownloadStatus status, int position) {
String statusText = "";
if (status.getFeedfileType() == Feed.FEEDFILETYPE_FEED) {
holder.type.setText(R.string.download_type_feed);
statusText += context.getString(R.string.download_type_feed);
} else if (status.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
holder.type.setText(R.string.download_type_media);
statusText += context.getString(R.string.download_type_media);
}
statusText += " · ";
statusText += DateUtils.getRelativeTimeSpanString(status.getCompletionDate().getTime(),
System.currentTimeMillis(), DateUtils.MINUTE_IN_MILLIS, 0);
holder.status.setText(statusText);

if (status.getTitle() != null) {
holder.title.setText(status.getTitle());
} else {
holder.title.setText(R.string.download_log_title_unknown);
}
holder.date.setText(DateUtils.getRelativeTimeSpanString(status.getCompletionDate().getTime(),
System.currentTimeMillis(), 0, 0));

if (status.isSuccessful()) {
holder.icon.setTextColor(ContextCompat.getColor(context, R.color.download_success_green));
Expand All @@ -76,7 +108,8 @@ public View getView(int position, View convertView, ViewGroup parent) {
holder.reason.setVisibility(View.VISIBLE);
holder.tapForDetails.setVisibility(View.VISIBLE);

if (newerWasSuccessful(position, status.getFeedfileType(), status.getFeedfileId())) {
if (newerWasSuccessful(position - runningDownloads.size(),
status.getFeedfileType(), status.getFeedfileId())) {
holder.secondaryActionButton.setVisibility(View.INVISIBLE);
holder.secondaryActionButton.setOnClickListener(null);
holder.secondaryActionButton.setTag(null);
Expand Down Expand Up @@ -118,13 +151,51 @@ public View getView(int position, View convertView, ViewGroup parent) {
}
}
}
}

return holder.itemView;
private void bind(DownloadLogItemViewHolder holder, Downloader downloader, int position) {
DownloadRequest request = downloader.getDownloadRequest();
holder.title.setText(request.getTitle());
holder.secondaryActionIcon.setImageResource(R.drawable.ic_cancel);
holder.secondaryActionButton.setContentDescription(context.getString(R.string.cancel_download_label));
holder.secondaryActionButton.setVisibility(View.VISIBLE);
holder.secondaryActionButton.setTag(downloader);
holder.secondaryActionButton.setOnClickListener(v ->
listFragment.onListItemClick(null, holder.itemView, position, 0));
holder.reason.setVisibility(View.GONE);
holder.tapForDetails.setVisibility(View.GONE);
holder.icon.setTextColor(ThemeUtils.getColorFromAttr(context, R.attr.colorPrimary));
holder.icon.setText("{fa-arrow-circle-down}");
holder.icon.setContentDescription(context.getString(R.string.status_downloading_label));

boolean percentageWasSet = false;
String status = "";
if (request.getFeedfileType() == Feed.FEEDFILETYPE_FEED) {
status += context.getString(R.string.download_type_feed);
} else if (request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
status += context.getString(R.string.download_type_media);
}
status += " · ";
if (request.getSoFar() <= 0) {
status += context.getString(R.string.download_pending);
} else {
status += Formatter.formatShortFileSize(context, request.getSoFar());
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN) {
status += " / " + Formatter.formatShortFileSize(context, request.getSize());
holder.secondaryActionProgress.setPercentage(
0.01f * Math.max(1, request.getProgressPercent()), request);
percentageWasSet = true;
}
}
if (!percentageWasSet) {
holder.secondaryActionProgress.setPercentage(0, request);
}
holder.status.setText(status);
}

private boolean newerWasSuccessful(int position, int feedTypeId, long id) {
for (int i = 0; i < position; i++) {
DownloadStatus status = getItem(i);
private boolean newerWasSuccessful(int downloadStatusIndex, int feedTypeId, long id) {
for (int i = 0; i < downloadStatusIndex; i++) {
DownloadStatus status = downloadLog.get(i);
if (status.getFeedfileType() == feedTypeId && status.getFeedfileId() == id && status.isSuccessful()) {
return true;
}
Expand All @@ -134,23 +205,22 @@ private boolean newerWasSuccessful(int position, int feedTypeId, long id) {

@Override
public int getCount() {
return itemAccess.getCount();
return downloadLog.size() + runningDownloads.size();
}

@Override
public DownloadStatus getItem(int position) {
return itemAccess.getItem(position);
public Object getItem(int position) {
if (position < runningDownloads.size()) {
return runningDownloads.get(position);
} else if (position - runningDownloads.size() < downloadLog.size()) {
return downloadLog.get(position - runningDownloads.size());
}
return null;
}

@Override
public long getItemId(int position) {
return position;
}

public interface ItemAccess {
int getCount();

DownloadStatus getItem(int position);
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public PendingIntent getNotificationContentIntent(Context context) {
Intent intent = new Intent(context, MainActivity.class);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_TAG, DownloadsFragment.TAG);
Bundle args = new Bundle();
args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_RUNNING);
args.putInt(DownloadsFragment.ARG_SELECTED_TAB, DownloadsFragment.POS_LOG);
intent.putExtra(MainActivity.EXTRA_FRAGMENT_ARGS, args);
return PendingIntent.getActivity(context,
R.id.pending_intent_download_service_notification, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Expand Down
Loading

0 comments on commit d08a9c4

Please sign in to comment.