Skip to content Skip to sidebar Skip to footer

What Is The Clean Way To Check A Select Query Result's Length In Mvvm Architecture?

I am creating an App that will use a SearchView to let user make queries to filter data. I am using RoomDB, and trying to follow Model-View-ViewModel architecture as recommended in

Solution 1:

Here is the way I ended up implementing the logic I had in mind when I made the question. But that doesn't mean this is the clean way to do it.

In order to get the size of the User query, I ended up using an Observer (as Teo said in his comment). I am not sure if using Observer and LiveData for a Database that is merely local in the phone's app (and therefore shall only be modified by the App's user himself) for obtaining query results each time the user hits "Search" button, I am not sure if using Oberser and LiveData for this is overkill or not... and the aberration of using DatabaseClient (the RoomDatabase's singleton) as a Repository? Not anymore... I have created a dedicated Repository Class to handle the DatabaseClient and the DAOs.

That said, the relevant part of my Repository class:

publicclassRepository {
    privatefinal TableRowDao tablerow_dao;

    publicRepository(Application application) {
        AppDatabaseapp_db= DatabaseClient.getInstance(application).getAppDatabase();
        tablerow_dao = app_db.tableRowDao();
    }

    public LiveData<List<TableRow>> getFilteredList(String search) {
        return tablerow_dao.filteredSearch(search);
    }
    // [...]

...here, the ViewModel:

publicclassTableRowsViewModelextendsAndroidViewModel {
private Repository repository;

publicTableRowsViewModel(@NonNull Application application) {
    super(application);
    repository = newRepository(application);
}

public LiveData<List<TableRow>> getUserSearch(String search) {
    return repository.getFilteredList(search);
}

No AsyncTasks were used for this purpose.

In MainActivity, within OnCreate method:

search_view.setOnQueryTextListener(newSearchView.OnQueryTextListener() {
    @OverridepublicbooleanonQueryTextSubmit(String query) {
        my_viewmodel = newTableRowsViewModel(getApplication());
        search_results = my_viewmodel.getUserSearch(query);

        observeSearchResults(search_results);
        returntrue;
    }

    // [...]
});

ObserveSearchResults is a private method that I declared in MainActivity as well:

// Having a observer is good and stuff, but am I overdoing it?privatevoidobserveSearchResults(LiveData<List<TableRow>> search_results) {
    search_results.observe(this, newObserver<List<TableRow>>() {
        @OverridepublicvoidonChanged(List<TableRow> rows) {
            if ( rows.size() > 0 ) {
                // TODO I don't list the results yet, I show the first one right away
                profileFragment = ProfileFragment.newInstance(rows.get(0));
                transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragmentContainer, profileFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            } else {
                transaction = getSupportFragmentManager().beginTransaction();
                transaction.replace(R.id.fragmentContainer, insertFragment);
                transaction.addToBackStack(null);
                transaction.commit();
            }
        }
    });
}

This worked for me, but that doesn't mean that I am doing this in a clean way at all.

Post a Comment for "What Is The Clean Way To Check A Select Query Result's Length In Mvvm Architecture?"