From 059e325c42bbbd520a34654217b8062a243aba5c Mon Sep 17 00:00:00 2001 From: David Luhmer Date: Sat, 11 Nov 2023 06:54:54 +0100 Subject: [PATCH] simplify reload of sidebar Signed-off-by: David Luhmer --- .../SubscriptionExpandableListAdapter.java | 162 +++++++++--------- .../NewsReaderListActivity.java | 46 ++--- .../NewsReaderListFragment.java | 14 +- 3 files changed, 109 insertions(+), 113 deletions(-) diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java index afceee739..d1e09fa0e 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/ListView/SubscriptionExpandableListAdapter.java @@ -54,7 +54,6 @@ import de.luhmer.owncloudnewsreader.databinding.SubscriptionListSubItemBinding; import de.luhmer.owncloudnewsreader.helper.FavIconHandler; import de.luhmer.owncloudnewsreader.helper.StopWatch; -import de.luhmer.owncloudnewsreader.helper.ThemeChooser; import de.luhmer.owncloudnewsreader.interfaces.ExpListTextClicked; import de.luhmer.owncloudnewsreader.model.AbstractItem; import de.luhmer.owncloudnewsreader.model.ConcreteFeedItem; @@ -254,7 +253,7 @@ public View getGroupView(final int groupPosition, final boolean isExpanded, View } if (!skipFireEvent) - fireListTextClicked(idFeed, true, ((FolderSubscribtionItem) group).idFolder); + fireListTextClicked(idFeed, true, group.idFolder); }); viewHolder.binding.listItemLayout.setOnLongClickListener(v -> { @@ -264,7 +263,7 @@ public View getGroupView(final int groupPosition, final boolean isExpanded, View if (group instanceof ConcreteFeedItem) { fireListTextLongClicked(idFeed, false, (long) ITEMS_WITHOUT_FOLDER.getValue()); } else { - fireListTextLongClicked(idFeed, true, ((FolderSubscribtionItem) group).idFolder); + fireListTextLongClicked(idFeed, true, group.idFolder); } return true; //consume event }); @@ -361,10 +360,80 @@ public boolean isChildSelectable(int groupPosition, int childPosition) { return true; } - public void NotifyDataSetChangedAsync() { + public void notifyDataSetChangedAsync() { new NotifyDataSetChangedAsyncTask().execute((Void) null); } + /** + * Reload categories and items from the database + */ + public Tuple, SparseArray>> loadCategoriesAndItemsFromDatabase() { + showOnlyUnread = mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false); + + ArrayList mCategories = new ArrayList<>(); + SparseArray> 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())); + + + StopWatch sw = new StopWatch(); + sw.start(); + + List folderList; + //if(showOnlyUnread) { + // folderList = dbConn.getListOfFoldersWithUnreadItems(); + //} else { + folderList = dbConn.getListOfFolders(); + //} + + sw.stop(); + Log.v(TAG, "Time needed (fetch folder list): " + sw); + + + for (Folder folder : folderList) { + mCategories.add(new FolderSubscribtionItem(folder.getLabel(), null, folder.getId())); + } + + for (Feed feed : dbConn.getListOfFeedsWithoutFolders(showOnlyUnread)) { + mCategories.add(new ConcreteFeedItem(feed.getFeedTitle(), (long) ITEMS_WITHOUT_FOLDER.getValue(), feed.getId(), feed.getFaviconUrl(), feed.getId())); + } + + + for (int groupPosition = 0; groupPosition < mCategories.size(); groupPosition++) { + //int parent_id = (int)getGroupId(groupPosition); + int parent_id = (int) mCategories.get(groupPosition).id_database; + mItems.append(parent_id, new ArrayList<>()); + + List feedItemList = null; + + if (parent_id == ALL_UNREAD_ITEMS.getValue()) { + feedItemList = dbConn.getAllFeedsWithUnreadRssItems(); + } else if (parent_id == ALL_STARRED_ITEMS.getValue()) { + feedItemList = dbConn.getAllFeedsWithStarredRssItems(); + } 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()); + mItems.get(parent_id).add(newItem); + } + } + } + + return new Tuple<>(mCategories, mItems); + } + + public void ReloadAdapterAsync() { + new ReloadAdapterAsyncTask().execute((Void) null); + } private class NotifyDataSetChangedAsyncTask extends AsyncTask { SparseArray starredCountFeedsTemp; @@ -386,7 +455,7 @@ protected Void doInBackground(Void... voids) { urlsToFavIconsTemp = dbConn.getUrlsToFavIcons(); stopwatch.stop(); - Log.v(TAG, "Fetched folder/feed counts in " + stopwatch.toString()); + Log.v(TAG, "Fetched folder/feed counts in " + stopwatch); return null; } @@ -428,10 +497,6 @@ protected void onPostExecute(Void aVoid) { } } - public void ReloadAdapterAsync() { - new ReloadAdapterAsyncTask().execute((Void) null); - } - private class ReloadAdapterAsyncTask extends AsyncTask, SparseArray>>> { public ReloadAdapterAsyncTask() { @@ -442,13 +507,9 @@ public ReloadAdapterAsyncTask() { protected Tuple, SparseArray>> doInBackground(Void... voids) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); - - Tuple, SparseArray>> ad = ReloadAdapter(); - //return reloadAdapter(); - + Tuple, SparseArray>> ad = loadCategoriesAndItemsFromDatabase(); stopWatch.stop(); - Log.v(TAG, "Reload Adapter - time taken: " + stopWatch.toString()); - + Log.v(TAG, "Reload Adapter - time taken: " + stopWatch); return ad; } @@ -456,80 +517,13 @@ protected Tuple, SparseArray protected void onPostExecute(Tuple, SparseArray>> arrayListSparseArrayTuple) { mCategoriesArrayList = arrayListSparseArrayTuple.key; mItemsArrayList = arrayListSparseArrayTuple.value; - - notifyDataSetChanged(); - - NotifyDataSetChangedAsync(); - + notifyDataSetChanged(); // inform list view that the data changed + notifyDataSetChangedAsync(); super.onPostExecute(arrayListSparseArrayTuple); } } - public Tuple, SparseArray>> ReloadAdapter() - { - showOnlyUnread = mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false); - - ArrayList 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())); - - - StopWatch sw = new StopWatch(); - sw.start(); - - List 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> 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 feedItemList = null; - - if(parent_id == ALL_UNREAD_ITEMS.getValue()) { - feedItemList = dbConn.getAllFeedsWithUnreadRssItems(); - } else if(parent_id == ALL_STARRED_ITEMS.getValue()) { - feedItemList = dbConn.getAllFeedsWithStarredRssItems(); - } 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 unreadCountFolders, SparseArray unreadCountFeeds, SparseArray starredCountFeeds) { diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java index 562954b18..18b13a89e 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListActivity.java @@ -476,17 +476,30 @@ public void checkNotificationPermissions() { } } + /** + * Updates the unread counts of the data in the sidebar (e.g. when the user marked a few articles as read we just need to reload the unread counts) + */ public void reloadCountNumbersOfSlidingPaneAdapter() { NewsReaderListFragment nlf = getSlidingListFragment(); if (nlf != null) { - nlf.ListViewNotifyDataSetChanged(); + nlf.listViewNotifyDataSetChanged(); + } + } + + /** + * Reload the whole Sidebar (all the categories / items in the sidebar) + */ + public void reloadSidebar() { + NewsReaderListFragment nlf = getSlidingListFragment(); + if (nlf != null) { + nlf.reloadAdapter(); + nlf.bindUserInfoToUI(); } } protected void updateCurrentRssView() { NewsReaderDetailFragment ndf = getNewsReaderDetailFragment(); if (ndf != null) { - //ndf.reloadAdapterFromScratch(); ndf.updateCurrentRssView(); } } @@ -550,11 +563,10 @@ public void onEventMainThread(SyncFinishedEvent event) { * @return true if new items count was greater than 0 */ private boolean syncFinishedHandler() { - NewsReaderListFragment newsReaderListFragment = getSlidingListFragment(); - newsReaderListFragment.reloadAdapter(); UpdateItemList(); updatePodcastView(); updateDetailFragmentTitle(); + reloadSidebar(); if(mApi.getNewsAPI() != null) { getSlidingListFragment().startAsyncTaskGetUserInfo(); @@ -588,11 +600,7 @@ protected void onResume() { mBackOpensDrawer = mPrefs.getBoolean(SettingsActivity.CB_PREF_BACK_OPENS_DRAWER, false); onBackPressedCallback.setEnabled(mBackOpensDrawer); - NewsReaderListFragment newsReaderListFragment = getSlidingListFragment(); - if (newsReaderListFragment != null) { - newsReaderListFragment.reloadAdapter(); - newsReaderListFragment.bindUserInfoToUI(); - } + reloadSidebar(); invalidateOptionsMenu(); super.onResume(); @@ -931,7 +939,7 @@ else if (itemId == R.id.menu_toggleShowOnlyUnread) { boolean newValue = !mPrefs.getBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, false); mPrefs.edit().putBoolean(SettingsActivity.CB_SHOWONLYUNREAD_STRING, newValue).commit(); item.setChecked(newValue); - getSlidingListFragment().reloadAdapter(); + reloadSidebar(); updateCurrentRssView(); } else if (itemId == R.id.menu_StartImageCaching) { @@ -1064,8 +1072,8 @@ protected void onActivityResult(int requestCode, int resultCode, final Intent da super.onActivityResult(requestCode, resultCode, data); if(resultCode == RESULT_OK) { - UpdateListView(); - getSlidingListFragment().ListViewNotifyDataSetChanged(); + updateListView(); + reloadCountNumbersOfSlidingPaneAdapter(); } if (requestCode == RESULT_LOGIN) { @@ -1169,18 +1177,12 @@ public void startLoginActivity() { } private void resetUiAndStartSync() { - NewsReaderListFragment nrlf = getSlidingListFragment(); - if (nrlf != null) { - nrlf.reloadAdapter(); - updateCurrentRssView(); - startSync(); - nrlf.bindUserInfoToUI(); - } else { - Log.e(TAG, "resetUiAndStartSync - NewsReaderListFragment is not available"); - } + reloadSidebar(); + updateCurrentRssView(); + startSync(); } - private void UpdateListView() { + private void updateListView() { getNewsReaderDetailFragment().notifyDataSetChangedOnAdapter(); } diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java index d434b06a8..53a337766 100644 --- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java +++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/NewsReaderListFragment.java @@ -92,20 +92,20 @@ public class NewsReaderListFragment extends Fragment implements OnCreateContextM private Callbacks mCallbacks = null; - protected static final String TAG = "NewsReaderListFragment"; + protected static final String TAG = "NewsReaderListFragment"; - public void ListViewNotifyDataSetChanged() { - lvAdapter.NotifyDataSetChangedAsync(); + public void listViewNotifyDataSetChanged() { + lvAdapter.notifyDataSetChangedAsync(); } public void reloadAdapter() { lvAdapter.ReloadAdapterAsync(); } - public void setRefreshing(boolean isRefreshing) { - if(isRefreshing) { - //headerLogo.setImageResource(R.drawable.ic_launcher_background); - binding.headerLogo.setVisibility(View.INVISIBLE); + public void setRefreshing(boolean isRefreshing) { + if (isRefreshing) { + //headerLogo.setImageResource(R.drawable.ic_launcher_background); + binding.headerLogo.setVisibility(View.INVISIBLE); binding.headerLogoProgress.setVisibility(View.VISIBLE); } else { //headerLogo.setImageResource(R.drawable.ic_launcher);