In the Open Event Android app we had already built the new homescreen but the users only had access to bookmarks in a separate page which could be accessed from the navbar.If the bookmarks section were to be incorporated in the homescreen itself, it would definitely improve its access to the user. In this blog post, I’ll be talking about how this was done in the app.
These 2 images show the homescreen and the bookmarks section respectively.
This was the proposed homescreen page for the app. This would provide easy access to important stuff to the user such as event venue,date,description etc. Also the same homescreen would also have the bookmarks showing at the top if there are any.
The list of bookmarks in the first iteration of design was modeled to be a horizontal list of cards.
Bookmarks Merging Process
These are some variables for reference.
private SessionsListAdapter sessionsListAdapter; private RealmResults<Session> bookmarksResult; private List<Session> mSessions = new ArrayList<>();
The code snippet below highlights the initial setup of the bookmarks recycler view for the horizontal List of cards. All of this is being done in the onCreateView callback of the AboutFragment.java file which is the fragment file for the homescreen.
bookmarksRecyclerView.setVisibility(View.VISIBLE); sessionsListAdapter = new SessionsListAdapter(getContext(), mSessions, bookmarkedSessionList); sessionsListAdapter.setBookmarkView(true); bookmarksRecyclerView.setAdapter(sessionsListAdapter); bookmarksRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL,false));
The SessionListAdapter is an adapter that was built to handle multiple types of displays of the same viewholder i.e SessionViewHolder . This SessionListAdapter is given a static variable as an argument which is just notifies the adapter to switch to the bookmarks mode for the adapter.
private void loadData() { bookmarksResult = realmRepo.getBookMarkedSessions(); bookmarksResult.removeAllChangeListeners(); bookmarksResult.addChangeListener((bookmarked, orderedCollectionChangeSet) -> { mSessions.clear(); mSessions.addAll(bookmarked); sessionsListAdapter.notifyDataSetChanged(); handleVisibility(); }); }
This function loadData() is responsible for extracting the sessions that are bookmarked from the local Realm database. We the update the BookmarkAdapter on the homescreen with the list of the bookmarks obtained. Here we see that a ChangeListener is being attached to our RealmResults. This is being done so that we do our adapter notify only after the data of the bookmarked sessions has been processed from a background thread.
if(bookmarksResult != null) bookmarksResult.removeAllChangeListeners();
And it is good practice to remove any ChangeListeners that we attach during the fragment life cycle in the onStop() method to avoid memory leaks.
So now we have successfully added bookmarks to the homescreen.
Resources
- Realm Change Listener : https://realm.io/docs/java/latest/api/io/realm/RealmChangeListener.html
- Android Activity Lifecycle : https://developer.android.com/guide/components/activities/activity-lifecycle.html
- Blog Post for Homescreen Details : https://blog.fossasia.org/adding-global-search-and-extending-bookmark-views-in-open-event-android/