Use Of DBFlowChangeListener for Automatic Updation of FAQ List in the Open Event Orga App
In the Open Event Orga App, whenever a faq is created in via the CreatefaqFragment, the new faqs aren’t updated in the FAQ’s list. The user has to swipe down and refresh to see the newly created FAQ. This seems to be a major problem and hence to solve this issue a DBFlowChangeListener class has been created.
Following blog post shows the implementation of the ChangeListener class.
Steps of Implementation
In the FaqListPresenter constructor pass an argument of the type DatabaseChangeListener<Faq> and assign it to local variable faqChangeListener.
@Inject public FaqListPresenter(FaqRepository faqRepository, DatabaseChangeListener<Faq> faqChangeListener) { this.faqRepository = faqRepository; this.faqChangeListener = faqChangeListener; } |
A method listenChanges( ) is made in the Presenter that will initiate the listening of database. Following code shows the listenChanges( ) . startListening( ) is a method in the DBFlowDatabaseChangeListener which is accessed with the help of DatabaseChangeListener interface.
private void listenChanges() { faqChangeListener.startListening(); faqChangeListener.getNotifier() .compose(dispose(getDisposable())) .map(DbFlowDatabaseChangeListener.ModelChange::getAction) .filter(action -> action.equals(BaseModel.Action.INSERT) || action.equals(BaseModel.Action.DELETE)) .subscribeOn(Schedulers.io()) .subscribe(faqModelChange -> loadFaqs(false), Logger::logError); } |
In the startListening( ) in DatabaseChangeListener, DirectModelNotifier is used to get notified of database changes. It is ultimately registered for any kind of model change as shown in the following code snippet. ReplaySubject is one of the 4 kinds of subjects ( Publish Subject, Replay Subject, Behavior Subject, Async Subject) that RxJava provides. It emits all the items of the source Observable, regardless of when the subscriber subscribes.
public void startListening() { if (disposable == null || disposable.isDisposed()) replaySubject = ReplaySubject.create(); modelModelChangedListener = new DirectModelNotifier.ModelChangedListener<T>() { @Override public void onTableChanged(@Nullable Class<?> aClass, @NonNull BaseModel.Action action) { replaySubject.onNext(new ModelChange<>(null, action)); } @Override public void onModelChanged(@NonNull T model, @NonNull BaseModel.Action action) { replaySubject.onNext(new ModelChange<>(model, action)); } }; DirectModelNotifier.get().registerForModelChanges(classType, modelModelChangedListener); } |
When the faqChangeListener is subscribed it calls the loadFaqs( ) , it ultimately loads the updated values from the database. Hence all the updated values are now displayed in the FaqListFragment.
public void loadFaqs(boolean forceReload) { getFaqSource(forceReload) .compose(dispose(getDisposable())) .compose(progressiveErroneousRefresh(getView(), forceReload)) .toList() .compose(emptiable(getView(), faqs)) .subscribe(Logger::logSuccess, Logger::logError); } |
Hence the use of DBFlowListener makes it possible to communicate between 2 fragments. Its usability has also been exploited in Tracks, Tickets, Sponsors fragments as well.
References:
- RaizLab’s DBFLow: https://github.com/Raizlabs/DBFlow
- Docs related to DBFlow: https://agrosner.gitbooks.io/dbflow/content/Observability.html
- Subjects used in RxJavahttps://blog.mindorks.com/understanding-rxjava-subject-publish-replay-behavior-and-async-subject-224d663d452f
You must be logged in to post a comment.