Skip to content

Instantly share code, notes, and snippets.

View PierceZ's full-sized avatar

Pierce Zaifman PierceZ

  • Canada
View GitHub Profile
...
ZooFragmentViewModel viewModel = ViewModelProviders.of(this).get(ZooFragmentViewModel.class);
viewModel.getZooUpdateResponse().observe(this, response -> {
if (response != null) {
switch (response.getStatus()) {
case Response.STATUS_LOADING:
showProgressBar(true);
break;
case Response.STATUS_SUCCESS:
public class Response {
public static final int STATUS_LOADING = 0, STATUS_SUCCESS = 1, STATUS_FAIL = 2;
@Retention(SOURCE)
@IntDef({STATUS_LOADING, STATUS_SUCCESS, STATUS_FAIL})
@interface Status {
}
private final int mStatus;
public class ZooDAO {
private static Box<Zoo> getZooBox() {
BoxStore boxStore = App.getBoxStore();
return boxStore.boxFor(Zoo.class);
}
public static DataSubscription subscribeToZooList(DataObserver<List<Zoo>> observer) {
return getZooBox().query().build().subscribe().on(AndroidScheduler.mainThread()).observer(observer);
}
public class ZooParser {
private String mResponse;
private Zoo mZoo;
private List<Zoo> mZooList;
private List<Animal> mAnimalList;
private Gson mGson;
public ZooParser(String response) {
mResponse = response;
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 class ZooListViewModel extends BaseViewModel {
private MutableLiveData<List<Zoo>> mZoosLiveData;
public ZooListViewModel() {
mZoosLiveData = new MutableLiveData<>();
DataSubscription subscription = ZooRepository.subscribeToZooList(this::refreshZoos);
addSubscription(subscription);
}
public abstract class BaseViewModel extends ViewModel {
private final List<DataSubscription> mSubscriptions;
@Override
protected void onCleared() {
super.onCleared();
for (DataSubscription subscription : mSubscriptions) {
if (!subscription.isCanceled()) {
subscription.cancel();
public class MainActivity extends AppCompatActivity {
private ZooListViewModel mViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.activity_main_recyclerview);
@Entity
public class Animal {
@Id
private long id;
private String name;
private String image;
private String group;
@Entity
public class Zoo {
@Id
private long id;
private String name;
@Backlink
public ToMany<Animal> animals;