Skip to content

Commit

Permalink
Merge pull request #1324 from nextcloud/simplify-reload-sidebar
Browse files Browse the repository at this point in the history
simplify reload of sidebar
  • Loading branch information
David-Development authored Nov 22, 2023
2 parents 7126c0e + 059e325 commit 9de0575
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 113 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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
});
Expand Down Expand Up @@ -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<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> loadCategoriesAndItemsFromDatabase() {
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()));


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);


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<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 {
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<Void, Void, Void> {
SparseArray<String> starredCountFeedsTemp;
Expand All @@ -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;
}

Expand Down Expand Up @@ -428,10 +497,6 @@ protected void onPostExecute(Void aVoid) {
}
}

public void ReloadAdapterAsync() {
new ReloadAdapterAsyncTask().execute((Void) null);
}

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

public ReloadAdapterAsyncTask() {
Expand All @@ -442,94 +507,23 @@ public ReloadAdapterAsyncTask() {
protected Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> doInBackground(Void... voids) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();

Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> ad = ReloadAdapter();
//return reloadAdapter();

Tuple<ArrayList<AbstractItem>, SparseArray<ArrayList<ConcreteFeedItem>>> ad = loadCategoriesAndItemsFromDatabase();
stopWatch.stop();
Log.v(TAG, "Reload Adapter - time taken: " + stopWatch.toString());

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();

NotifyDataSetChangedAsync();

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()));


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 {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9de0575

Please sign in to comment.