Skip to content

Commit

Permalink
Merge pull request #1364 from nextcloud/all-downloaded-podcast-refact…
Browse files Browse the repository at this point in the history
…oring

downloaded podcast refactoring
  • Loading branch information
David-Development authored Jan 17, 2024
2 parents 36a1d82 + 007c87d commit aa38bf3
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 187 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class SubscriptionExpandableListAdapter extends BaseExpandableListAdapter
private boolean showOnlyUnread = false;

private SparseArray<String> starredCountFeeds;
private SparseArray<String> downloadedPodcastsCount;
private int downloadedPodcastsCount;
private SparseArray<String> unreadCountFolders;
private SparseArray<String> unreadCountFeeds;

Expand Down Expand Up @@ -117,7 +117,7 @@ public SubscriptionExpandableListAdapter(Context mContext, DatabaseConnectionOrm
unreadCountFeeds = new SparseArray<>();
unreadCountFolders = new SparseArray<>();
starredCountFeeds = new SparseArray<>();
downloadedPodcastsCount = new SparseArray<>();
downloadedPodcastsCount = 0;

mCategoriesArrayList = new ArrayList<>();
mItemsArrayList = new SparseArray<>();
Expand Down Expand Up @@ -153,33 +153,24 @@ public View getChildView(int groupPosition, int childPosition,
}


if(item != null)
{
String headerText = (item.header != null) ? item.header : "";
viewHolder.binding.summary.setText(headerText);

if (item != null) {
String headerText = (item.header != null) ? item.header : "";
viewHolder.binding.summary.setText(headerText);

String unreadCount;
if(item.idFolder == ALL_STARRED_ITEMS.getValue()) {
if (item.idFolder == ALL_STARRED_ITEMS.getValue()) {
unreadCount = starredCountFeeds.get((int) item.id_database);
} else if(item.idFolder == ALL_DOWNLOADED_PODCASTS.getValue()) {
unreadCount = downloadedPodcastsCount.get((int) item.id_database);
} else {
unreadCount = unreadCountFeeds.get((int) item.id_database);
}

if(unreadCount != null)
viewHolder.binding.tvUnreadCount.setText(unreadCount);
else
viewHolder.binding.tvUnreadCount.setText("");
viewHolder.binding.tvUnreadCount.setText(unreadCount != null ? unreadCount : "");

favIconHandler.loadFavIconForFeed(item.favIcon, viewHolder.binding.iVFavicon);
}
else
{
viewHolder.binding.summary.setText(mContext.getString(R.string.login_dialog_text_something_went_wrong));
viewHolder.binding.tvUnreadCount.setText("");
viewHolder.binding.iVFavicon.setImageDrawable(null);
} else {
viewHolder.binding.summary.setText(mContext.getString(R.string.login_dialog_text_something_went_wrong));
viewHolder.binding.tvUnreadCount.setText("");
viewHolder.binding.iVFavicon.setImageDrawable(null);
}

return convertView;
Expand Down Expand Up @@ -285,21 +276,25 @@ public View getGroupView(final int groupPosition, final boolean isExpanded, View
skipGetUnread = true;
}

if(!skipGetUnread) {
if (!skipGetUnread) {
String unreadCount = unreadCountFolders.get((int) group.id_database);
if(unreadCount != null)
if (unreadCount != null) {
viewHolder.binding.tVFeedsCount.setText(unreadCount);
}
}

if (group.id_database == ALL_DOWNLOADED_PODCASTS.getValue()) {
viewHolder.binding.tVFeedsCount.setText(String.valueOf(downloadedPodcastsCount));
}


int rotation = 0;
int contentDescriptionId = R.string.content_desc_none;


if(group.idFolder != null)
{
if (group.idFolder != null) {
viewHolder.binding.imgViewExpandableIndicator.setVisibility(View.GONE);
if(group.idFolder == ITEMS_WITHOUT_FOLDER.getValue())
if (group.idFolder == ITEMS_WITHOUT_FOLDER.getValue())
{
ConcreteFeedItem concreteFeedItem = ((ConcreteFeedItem) group);
favIconHandler.loadFavIconForFeed(concreteFeedItem.favIcon, viewHolder.binding.imgViewFavicon);
Expand Down Expand Up @@ -380,21 +375,14 @@ public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>
showOnlyUnread = mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false);

ArrayList<AbstractItem> mCategories = new ArrayList<>();
SparseArray<ArrayList<ConcreteFeedItem>> mItems = new SparseArray<>();

mCategories.add(new FolderSubscribtionItem(mContext.getString(R.string.allUnreadFeeds), null, ALL_UNREAD_ITEMS.getValue()));
mCategories.add(new FolderSubscribtionItem(mContext.getString(R.string.starredFeeds), null, ALL_STARRED_ITEMS.getValue()));

mCategories.add(new FolderSubscribtionItem(mContext.getString(R.string.downloadedPodcasts), null, ALL_DOWNLOADED_PODCASTS.getValue()));

StopWatch sw = new StopWatch();
sw.start();

List<Folder> folderList;
//if(showOnlyUnread) {
// folderList = dbConn.getListOfFoldersWithUnreadItems();
//} else {
folderList = dbConn.getListOfFolders();
//}
List<Folder> folderList = dbConn.getListOfFolders();

sw.stop();
Log.v(TAG, "Time needed (fetch folder list): " + sw);
Expand All @@ -408,6 +396,7 @@ public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>
mCategories.add(new ConcreteFeedItem(feed.getFeedTitle(), (long) ITEMS_WITHOUT_FOLDER.getValue(), feed.getId(), feed.getFaviconUrl(), feed.getId()));
}

SparseArray<ArrayList<ConcreteFeedItem>> mItems = new SparseArray<>();

for (int groupPosition = 0; groupPosition < mCategories.size(); groupPosition++) {
//int parent_id = (int)getGroupId(groupPosition);
Expand All @@ -420,6 +409,8 @@ public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>
feedItemList = dbConn.getAllFeedsWithUnreadRssItems();
} else if (parent_id == ALL_STARRED_ITEMS.getValue()) {
feedItemList = dbConn.getAllFeedsWithStarredRssItems();
} else if (parent_id == ALL_DOWNLOADED_PODCASTS.getValue()) {
feedItemList = dbConn.getAllFeedsWithDownloadedPodcasts(mContext);
} else {
for (Folder folder : folderList) {//Find the current selected folder
if (folder.getId() == parent_id) {//Current item
Expand All @@ -444,12 +435,60 @@ public void ReloadAdapterAsync() {
new ReloadAdapterAsyncTask().execute((Void) null);
}

@SuppressLint("NewApi") // wrongly reports setSelectionFromTop is only available in lollipop
public void notifyCountDataSetChanged(SparseArray<String> unreadCountFolders, SparseArray<String> unreadCountFeeds, SparseArray<String> starredCountFeeds, int downloadedPodcastsCount) {
this.unreadCountFolders = unreadCountFolders;
this.unreadCountFeeds = unreadCountFeeds;
this.starredCountFeeds = starredCountFeeds;
this.downloadedPodcastsCount = downloadedPodcastsCount;

BlockingExpandableListView bView = (BlockingExpandableListView) listView;

int firstVisPos = bView.getFirstVisiblePosition();
View firstVisView = bView.getChildAt(0);
int top = firstVisView != null ? firstVisView.getTop() : 0;

// Number of items added before the first visible item
int itemsAddedBeforeFirstVisible = 0;

bView.setBlockLayoutChildren(true);
notifyDataSetChanged();
bView.setBlockLayoutChildren(false);

// Call setSelectionFromTop to change the ListView position
if(bView.getCount() >= firstVisPos + itemsAddedBeforeFirstVisible)
bView.setSelectionFromTop(firstVisPos + itemsAddedBeforeFirstVisible, top);
}

private class ReloadAdapterAsyncTask extends AsyncTask<Void, Void, Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>> {

@Override
protected Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> doInBackground(Void... voids) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> ad = loadCategoriesAndItemsFromDatabase();
stopWatch.stop();
Log.v(TAG, "Reload Adapter - time taken: " + stopWatch);
return ad;
}

@Override
protected void onPostExecute(Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> arrayListSparseArrayTuple) {
mCategoriesArrayList = arrayListSparseArrayTuple.key;
mItemsArrayList = arrayListSparseArrayTuple.value;
notifyDataSetChanged(); // inform list view that the data changed
notifyDataSetChangedAsync();
super.onPostExecute(arrayListSparseArrayTuple);
}

}

private class NotifyDataSetChangedAsyncTask extends AsyncTask<Void, Void, Void> {
SparseArray<String> starredCountFeedsTemp;
SparseArray<String> downloadedPodcastsCountFeedsTemp;
SparseArray<String> unreadCountFoldersTemp;
SparseArray<String> unreadCountFeedsTemp;
SparseArray<String> urlsToFavIconsTemp;
int downloadedPodcastsCountTemp;

@Override
protected Void doInBackground(Void... voids) {
Expand All @@ -462,7 +501,7 @@ protected Void doInBackground(Void... voids) {
unreadCountFeedsTemp = temp[1]; // dbConn.getUnreadItemCountForFeed();

starredCountFeedsTemp = dbConn.getStarredItemCount();
downloadedPodcastsCountFeedsTemp = dbConn.getDownloadedPodcastsCount(mContext);
downloadedPodcastsCountTemp = dbConn.getDownloadedPodcastsCount(mContext);
urlsToFavIconsTemp = dbConn.getUrlsToFavIcons();

stopwatch.stop();
Expand All @@ -478,12 +517,16 @@ protected void onPostExecute(Void aVoid) {

if(item instanceof FolderSubscribtionItem &&
unreadCountFoldersTemp.get(((Long) item.id_database).intValue()) == null) {
Log.v(TAG, "Remove folder item!!!");
mCategoriesArrayList.remove(i);
i--;
Log.v(TAG, "Remove folder item: " + item.header);

// we need to keep the ALL_DOWNLOADED_PODCASTS in case at least one article is in there
if (!(item.id_database == ALL_DOWNLOADED_PODCASTS.getValue() && downloadedPodcastsCount > 0)) {
mCategoriesArrayList.remove(i);
i--;
}
} else if(item instanceof ConcreteFeedItem &&
unreadCountFeedsTemp.get(((Long) item.id_database).intValue()) == null) {
Log.v(TAG, "Remove feed item!!!");
Log.v(TAG, "Remove feed item: " + item.header);
mCategoriesArrayList.remove(i);
i--;
} /* else {
Expand All @@ -503,130 +546,11 @@ protected void onPostExecute(Void aVoid) {
}
}

notifyCountDataSetChanged(unreadCountFoldersTemp, unreadCountFeedsTemp, starredCountFeedsTemp, downloadedPodcastsCountFeedsTemp);
notifyCountDataSetChanged(unreadCountFoldersTemp, unreadCountFeedsTemp, starredCountFeedsTemp, downloadedPodcastsCountTemp);
super.onPostExecute(aVoid);
}
}

private class ReloadAdapterAsyncTask extends AsyncTask<Void, Void, Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>>> {

public ReloadAdapterAsyncTask() {

}

@Override
protected Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> doInBackground(Void... voids) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> ad = loadCategoriesAndItemsFromDatabase();
stopWatch.stop();
Log.v(TAG, "Reload Adapter - time taken: " + stopWatch);
return ad;
}

@Override
protected void onPostExecute(Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> arrayListSparseArrayTuple) {
mCategoriesArrayList = arrayListSparseArrayTuple.key;
mItemsArrayList = arrayListSparseArrayTuple.value;
notifyDataSetChanged(); // inform list view that the data changed
notifyDataSetChangedAsync();
super.onPostExecute(arrayListSparseArrayTuple);
}

}

public Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> ReloadAdapter()
{
showOnlyUnread = mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false);

ArrayList<AbstractItem> mCategoriesArrayListAsync = new ArrayList<>();
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.allUnreadFeeds), null, ALL_UNREAD_ITEMS.getValue()));
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.starredFeeds), null, ALL_STARRED_ITEMS.getValue()));
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(mContext.getString(R.string.downloadedPodcasts), null, ALL_DOWNLOADED_PODCASTS.getValue()));


StopWatch sw = new StopWatch();
sw.start();

List<Folder> folderList;
//if(showOnlyUnread) {
// folderList = dbConn.getListOfFoldersWithUnreadItems();
//} else {
folderList = dbConn.getListOfFolders();
//}

sw.stop();
Log.v(TAG, "Time needed (fetch folder list): " + sw.toString());


for(Folder folder : folderList) {
mCategoriesArrayListAsync.add(new FolderSubscribtionItem(folder.getLabel(), null, folder.getId()));
}

for(Feed feed : dbConn.getListOfFeedsWithoutFolders(showOnlyUnread)) {
mCategoriesArrayListAsync.add(new ConcreteFeedItem(feed.getFeedTitle(), (long) ITEMS_WITHOUT_FOLDER.getValue(), feed.getId(), feed.getFaviconUrl(), feed.getId()));
}

SparseArray<ArrayList<ConcreteFeedItem>> mItemsArrayListAsync = new SparseArray<>();

for(int groupPosition = 0; groupPosition < mCategoriesArrayListAsync.size(); groupPosition++) {
//int parent_id = (int)getGroupId(groupPosition);
int parent_id = (int) mCategoriesArrayListAsync.get(groupPosition).id_database;
mItemsArrayListAsync.append(parent_id, new ArrayList<>());

List<Feed> feedItemList = null;

if(parent_id == ALL_UNREAD_ITEMS.getValue()) {
feedItemList = dbConn.getAllFeedsWithUnreadRssItems();
} else if(parent_id == ALL_STARRED_ITEMS.getValue()) {
feedItemList = dbConn.getAllFeedsWithStarredRssItems();
} else if (parent_id == ALL_DOWNLOADED_PODCASTS.getValue()) {
feedItemList = dbConn.getAllFeedsWithDownloadedPodcasts(mContext);
} else {
for(Folder folder : folderList) {//Find the current selected folder
if (folder.getId() == parent_id) {//Current item
feedItemList = dbConn.getAllFeedsWithUnreadRssItemsForFolder(folder.getId());
break;
}
}
}

if(feedItemList != null) {
for (Feed feed : feedItemList) {
ConcreteFeedItem newItem = new ConcreteFeedItem(feed.getFeedTitle(), (long) parent_id, feed.getId(), feed.getFaviconUrl(), feed.getId());
mItemsArrayListAsync.get(parent_id).add(newItem);
}
}
}

return new Tuple<>(mCategoriesArrayListAsync, mItemsArrayListAsync);
}

@SuppressLint("NewApi") // wrongly reports setSelectionFromTop is only available in lollipop
public void notifyCountDataSetChanged(SparseArray<String> unreadCountFolders, SparseArray<String> unreadCountFeeds, SparseArray<String> starredCountFeeds, SparseArray<String> downloadedPodcastsCount) {
this.unreadCountFolders = unreadCountFolders;
this.unreadCountFeeds = unreadCountFeeds;
this.starredCountFeeds = starredCountFeeds;
this.downloadedPodcastsCount = downloadedPodcastsCount;

BlockingExpandableListView bView = (BlockingExpandableListView) listView;

int firstVisPos = bView.getFirstVisiblePosition();
View firstVisView = bView.getChildAt(0);
int top = firstVisView != null ? firstVisView.getTop() : 0;

// Number of items added before the first visible item
int itemsAddedBeforeFirstVisible = 0;

bView.setBlockLayoutChildren(true);
notifyDataSetChanged();
bView.setBlockLayoutChildren(false);

// Call setSelectionFromTop to change the ListView position
if(bView.getCount() >= firstVisPos + itemsAddedBeforeFirstVisible)
bView.setSelectionFromTop(firstVisPos + itemsAddedBeforeFirstVisible, top);
}


public void setHandlerListener(ExpListTextClicked listener)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,21 +329,20 @@ public void removePodcastMedia(final RssItem rssItem, final Consumer<Boolean> ca
}

AlertDialog.Builder alertDialog = new AlertDialog.Builder(this)
.setNegativeButton("Remove", (dialogInterface, i) -> {
.setNegativeButton(getString(R.string.dialog_podcast_remove_confirm), (dialogInterface, i) -> {
boolean success = file.delete() && file.getParentFile().delete(); // remove audio file and parent folder
if (!success) {
Toast.makeText(PodcastFragmentActivity.this, "Failed to remove media for \"" + podcastItem.title + "\"", Toast.LENGTH_SHORT).show();
Toast.makeText(PodcastFragmentActivity.this, getString(R.string.dialog_podcast_status_failed, podcastItem.title), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(PodcastFragmentActivity.this, "Media for \"" + podcastItem.title + "\" has been removed", Toast.LENGTH_SHORT).show();
Toast.makeText(PodcastFragmentActivity.this, getString(R.string.dialog_podcast_status_success, podcastItem.title), Toast.LENGTH_SHORT).show();
}

callback.accept(success);
})
.setNeutralButton("Cancel", (dialogInterface, i) -> {
.setNeutralButton(getString(android.R.string.cancel), (dialogInterface, i) -> {
callback.accept(false);
})
.setTitle("Are you sure?")
.setMessage("Do you want to remove downloaded media for \"" + podcastItem.title + "?\"");
.setTitle(getString(R.string.dialog_podcast_remove_title))
.setMessage(getString(R.string.dialog_podcast_remove_body, podcastItem.title));

alertDialog.show();
}
Expand Down
Loading

0 comments on commit aa38bf3

Please sign in to comment.