Skip to content

Instantly share code, notes, and snippets.

@PierceZ
Created October 3, 2017 16:35
Show Gist options
  • Save PierceZ/024f20a5ce2072ed682b4c9b31036dc1 to your computer and use it in GitHub Desktop.
Save PierceZ/024f20a5ce2072ed682b4c9b31036dc1 to your computer and use it in GitHub Desktop.
public class ZooRepository {
public static DataSubscription subscribeToZooList(DataObserver<List<Zoo>> observer) {
return ZooDAO.subscribeToZooList(observer);
}
public static DataSubscription subscribeToZoo(DataObserver<Zoo> observer, long id, boolean singleUpdate) {
return ZooDAO.subscribeToZoo(observer, id, singleUpdate);
}
public static void refreshZoo(long id) {
ZooAPI.loadZoo(id, zooResponse -> {
if (zooResponse != null && zooResponse.getStatus() == Response.STATUS_SUCCESS) {
ZooParser parser = new ZooParser(zooResponse.getPayload());
parser.parseZoo();
Zoo zoo = parser.getZoo();
if (zoo != null) {
ZooDAO.insertZoo(zoo);
}
}
});
}
public static void refreshZoos() {
ZooAPI.loadZoos(zoosResponse -> {
if (zoosResponse != null && zoosResponse.getStatus() == Response.STATUS_SUCCESS) {
ZooParser parser = new ZooParser(zoosResponse.getPayload());
parser.parseZooList();
List<Zoo> zoos = parser.getZooList();
if (zoos != null) {
ZooDAO.insertZoos(zoos);
}
}
});
}
public static void addZoo(Zoo newZoo, MutableLiveData<ZooUpdateResponse> liveResponse) {
liveResponse.postValue(new ZooUpdateResponse(Response.STATUS_LOADING));
ZooAPI.addZoo(newZoo, zooResponse -> handleZooResponse(zooResponse, liveResponse));
}
public static void updateZoo(Zoo zoo, MutableLiveData<ZooUpdateResponse> liveResponse) {
liveResponse.postValue(new ZooUpdateResponse(Response.STATUS_LOADING));
ZooAPI.updateZoo(zoo, zooResponse -> handleZooResponse(zooResponse, liveResponse));
}
private static void handleZooResponse(Response zooResponse, MutableLiveData<ZooUpdateResponse> liveResponse) {
if (zooResponse != null) {
if (zooResponse.getStatus() == Response.STATUS_SUCCESS) {
ZooParser parser = new ZooParser(zooResponse.getPayload());
parser.parseZoo();
Zoo zoo = parser.getZoo();
if (zoo != null) {
ZooDAO.insertZoo(zoo);
}
}
liveResponse.postValue(new ZooUpdateResponse(zooResponse.getStatus()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment