I'm trying to implement ViewModel in a app which get some data from Firestore using the FirestorepagingAdapter, and displays it in a recyclerview. I'm already getting all data and displayig it, but still not using ViewModel, it's all on the MainActivity
I'm trying to move the code that creates Firestorepagingoptions to the the view model, and in my MainActivity, just create the adapter and set to the recyclerview. But the FirestorePagingOptions.Builder needs to set a LifecycleOwner and I don't know how to get it in my ViewModel, or maybe i should't be doing it on the ViewModel at all, I'm pretty lost yet with these ViewModel, so if anyone has any suggestion I appreciate. Thanks a lot.
Here's my original code on the MainActivity (not changed yet to get the data from the Viewmodel)
public class ListaEmpresasActivity extends AppCompatActivity {
private FirebaseFirestore firebaseDB;
private RecyclerView recyclerViewListaEmpresas;
private FirestorePagingAdapter adapter;
private boolean viewChanged;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista_empresas);
recyclerViewListaEmpresas = findViewById(R.id.activity_main_lista_empresas);
firebaseDB = FirebaseFirestore.getInstance();
FirestorePagingOptions<EmpresaLista> options = configuraPaginacaoInicial();
adapter = new ListaEmpresasAdapter(options);
configuraRecyclerView(adapter);
}
private FirestorePagingOptions<EmpresaLista> configuraPaginacaoInicial() {
Query query = firebaseDB.collection("Lista_Empresas").orderBy("id");
return configuraOpcoesPaginacao(query);
}
private FirestorePagingOptions<EmpresaLista> configuraOpcoesPaginacao(Query query) {
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setPageSize(20)
.build();
return new FirestorePagingOptions.Builder<EmpresaLista>()
.setLifecycleOwner(this)
.setQuery(query, config, EmpresaLista.class)
.build();
}
private void configuraRecyclerView(FirestorePagingAdapter adapter) {
recyclerViewListaEmpresas.setHasFixedSize(true);
recyclerViewListaEmpresas.setLayoutManager(new LinearLayoutManager(this));
recyclerViewListaEmpresas.setAdapter(this.adapter);
}
And the code on my ViewModel
public class ListaEmpresasViewModel extends ViewModel {
private MutableLiveData<FirestorePagingOptions<EmpresaLista>> options;
private FirebaseFirestore firebaseDB;
public LiveData<FirestorePagingOptions<EmpresaLista>> getOptions() {
firebaseDB = FirebaseFirestore.getInstance();
options = configuraPaginacaoInicial();
return options;
}
private MutableLiveData<FirestorePagingOptions<EmpresaLista>> configuraPaginacaoInicial() {
Query query = firebaseDB.collection("Lista_Empresas").orderBy("id");
return configuraOpcoesPaginacao(query);
}
private MutableLiveData<FirestorePagingOptions<EmpresaLista>> configuraOpcoesPaginacao(Query query) {
PagedList.Config config = new PagedList.Config.Builder()
.setEnablePlaceholders(false)
.setPrefetchDistance(10)
.setPageSize(20)
.build();
return new FirestorePagingOptions.Builder<EmpresaLista>()
.setLifecycleOwner()
.setQuery(query, config, EmpresaLista.class)
.build();
}
}
Instead of setting the lifecycle owner in the options start and stop listening manually.
Start/stop listening The FirestorePagingAdapter listens for scrolling events and loads additional pages from the database only when needed.