https://github.com/harshshah6/firebase-pagination-offlinecapability
An example project of pagination with offline capability using firebase in android application, written in java
https://github.com/harshshah6/firebase-pagination-offlinecapability
firebase firebase-database firebase-database-pagination firebase-offline java pagination
Last synced: 2 months ago
JSON representation
An example project of pagination with offline capability using firebase in android application, written in java
- Host: GitHub
- URL: https://github.com/harshshah6/firebase-pagination-offlinecapability
- Owner: Harshshah6
- Created: 2024-09-16T12:47:28.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-19T14:19:00.000Z (almost 2 years ago)
- Last Synced: 2025-05-15T16:51:38.788Z (about 1 year ago)
- Topics: firebase, firebase-database, firebase-database-pagination, firebase-offline, java, pagination
- Language: Java
- Homepage:
- Size: 113 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Firebase-Pagination-OfflineCapability
An example project of pagination with offline capability using firebase in android application, written in java
## Dependencies Used (external dependencies)
Firebase core / realtime database dependency are not listed here, please include the core and db dependency before using below libraries.
``` gradle
implementation 'com.firebaseui:firebase-ui-database:8.0.2'
implementation 'androidx.paging:paging-runtime:3.3.2'
```
## Instructions
### Declaring and Initializing variables
Firebase related variables :-
```java
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("dummy");
```
Paging related variables :-
```java
PagingConfig config = new PagingConfig(
10, // Item Load Limits
2, // Prefetch items limit before next load/scroll
false // Placeholders (Please refer official doc for more info)
);
```
```java
//Initialize Firebase Paging Options
DatabasePagingOptions options = new DatabasePagingOptions.Builder() // ResponseModel = Model class of your response data
.setLifecycleOwner(this) //this = Context
.setQuery(myRef, config, ResponseModel.class) //myRef = DatabaseReference
.build();
```
Adapter for RecyclerView
```java
//ResponseModel = Model class
//ViewHolder = ViewHolder class explicitly written by you
//options = DatabasePagingOptions<>()
FirebaseRecyclerPagingAdapter mAdapter = new FirebaseRecyclerPagingAdapter(options) {
@NonNull
@Override
public ViewHolder onCreateViewHolder (@NonNull ViewGroup parent,int viewType){
...
}
@Override
protected void onBindViewHolder (@NonNull ViewHolder holder,int position,@NonNull ResponseModel model){
...
}
};
recyclerView.setAdapter(mAdapter); //setting adapter to RecyclerView
```
Listening to the scroll and states
```java
mAdapter.addLoadStateListener(states -> {
LoadState refresh = states.getRefresh();
LoadState append = states.getAppend();
if (refresh instanceof LoadState.Error || append instanceof LoadState.Error) {
// There might be some error
// Request to database error
// OR
// No Network Available
}
if (append instanceof LoadState.Loading) {
//Next Items Loading....
}
if (append instanceof LoadState.NotLoading) {
LoadState.NotLoading notLoading = (LoadState.NotLoading) append;
if (notLoading.getEndOfPaginationReached()) {
// This indicates that the user has scrolled
// until the end of the data set.
return null;
}
if (refresh instanceof LoadState.NotLoading) {
// This indicates the most recent load
// has finished.
return null;
}
}
return null;
});
```
## To Enable Offline Capabality
In the example app this code was added in MyApplication.java class extending Application
```java
//enable firebase offline capabilities
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
```