Last active
June 11, 2017 21:32
-
-
Save Devofure/b2751c5cbad8f32bd73376113c38ca22 to your computer and use it in GitHub Desktop.
A LiveData example when working with firebase database and LiveData, inspired by the examples of Android architecture guide lines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.devofure.tlog.repository.api; | |
import android.support.annotation.NonNull; | |
import com.devofure.tlog.model.Resource; | |
import com.google.firebase.database.DataSnapshot; | |
import com.google.firebase.database.DatabaseError; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.Query; | |
import com.google.firebase.database.ValueEventListener; | |
public abstract class FirebaseItemLiveData<RequestType> | |
extends LiveDataRefresher<Resource<RequestType>> { | |
private final DatabaseReference mRootDatabase; | |
private ValueEventListener mEventItemListener; | |
private Query mDbRef; | |
private OnRefreshDbNodeListener mOnRefreshNode; | |
public FirebaseItemLiveData(DatabaseReference rootDatabase) { | |
mRootDatabase = rootDatabase; | |
} | |
@Override | |
protected void onActive() { | |
super.onActive(); | |
//save the last dbRef to remove listener later on onInactive | |
mDbRef = getCurrentDbNodeRef(mRootDatabase); | |
//save the last listener to remove listener later on onInactive | |
mDbRef.addValueEventListener(mEventItemListener = new ValueEventListener() { | |
@Override | |
public void onDataChange(DataSnapshot dataSnapshot) { | |
setValue(Resource.success(convertTo(dataSnapshot))); | |
} | |
@Override | |
public void onCancelled(DatabaseError databaseError) { | |
setValue(Resource.error(databaseError.getMessage(), null)); | |
} | |
}); | |
} | |
private Query getCurrentDbNodeRef(DatabaseReference rootDatabase) { | |
if(mOnRefreshNode != null){ | |
return mOnRefreshNode.getDbNodeRef(rootDatabase); | |
}else { | |
return getDbNodeRef(rootDatabase); | |
} | |
} | |
protected abstract Query getDbNodeRef(DatabaseReference rootDatabaseRef); | |
@Override | |
void refresh(@NonNull OnRefreshDbNodeListener onRefreshNode) { | |
onInactive(); | |
onActive(); | |
mOnRefreshNode = onRefreshNode; | |
} | |
protected abstract RequestType convertTo(DataSnapshot dataSnapshot); | |
@Override | |
protected void onInactive() { | |
super.onInactive(); | |
mDbRef.removeEventListener(mEventItemListener); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.devofure.tlog.repository.api; | |
import android.arch.lifecycle.LiveData; | |
import android.support.annotation.NonNull; | |
import com.google.firebase.database.DatabaseReference; | |
import com.google.firebase.database.Query; | |
public abstract class LiveDataRefresher<RequestType> extends LiveData<RequestType> { | |
abstract void refresh(@NonNull OnRefreshDbNodeListener onRefreshNode); | |
interface OnRefreshDbNodeListener { | |
Query getDbNodeRef(DatabaseReference rootDatabase); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.devofure.tlog.model; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import static com.devofure.tlog.model.Status.ERROR; | |
import static com.devofure.tlog.model.Status.LOADING; | |
import static com.devofure.tlog.model.Status.SUCCESS; | |
public class Resource<T> { | |
@NonNull | |
public final Status status; | |
@Nullable | |
public final String message; | |
@Nullable | |
public final T data; | |
public Resource(@NonNull Status status, @Nullable T data, @Nullable String message) { | |
this.status = status; | |
this.data = data; | |
this.message = message; | |
} | |
public static <T> Resource<T> success() { | |
return success(null); | |
} | |
public static <T> Resource<T> success(@Nullable T data) { | |
return new Resource<>(SUCCESS, data, null); | |
} | |
public static <T> Resource<T> error(String msg, @Nullable T data) { | |
return new Resource<>(ERROR, data, msg); | |
} | |
public static <T> Resource<T> loading() { | |
return loading(null); | |
} | |
public static <T> Resource<T> loading(@Nullable T data) { | |
return new Resource<>(LOADING, data, null); | |
} | |
@Override | |
public boolean equals(Object o) { | |
if (this == o) { | |
return true; | |
} | |
if (o == null || getClass() != o.getClass()) { | |
return false; | |
} | |
Resource<?> resource = (Resource<?>) o; | |
if (status != resource.status) { | |
return false; | |
} | |
if (message != null ? !message.equals(resource.message) : resource.message != null) { | |
return false; | |
} | |
return data != null ? data.equals(resource.data) : resource.data == null; | |
} | |
@Override | |
public int hashCode() { | |
int result = status.hashCode(); | |
result = 31 * result + (message != null ? message.hashCode() : 0); | |
result = 31 * result + (data != null ? data.hashCode() : 0); | |
return result; | |
} | |
@Override | |
public String toString() { | |
return "Resource{" + | |
"status=" + status + | |
", message='" + message + '\'' + | |
", data=" + data + | |
'}'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment