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
... | |
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: |
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
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; |
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
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); | |
} |
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
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; |
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
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); | |
} |
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
public class ZooListViewModel extends BaseViewModel { | |
private MutableLiveData<List<Zoo>> mZoosLiveData; | |
public ZooListViewModel() { | |
mZoosLiveData = new MutableLiveData<>(); | |
DataSubscription subscription = ZooRepository.subscribeToZooList(this::refreshZoos); | |
addSubscription(subscription); | |
} |
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
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(); |
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
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); |
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
@Entity | |
public class Animal { | |
@Id | |
private long id; | |
private String name; | |
private String image; | |
private String group; |
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
@Entity | |
public class Zoo { | |
@Id | |
private long id; | |
private String name; | |
@Backlink | |
public ToMany<Animal> animals; |
NewerOlder