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
enum class ABTestVariation(val navGraphRes: Int) { | |
BASELINE(R.navigation.baseline_navigation), | |
VARIATION1(R.navigation.variant1_navigation), | |
VARIATION2(R.navigation.variant2_navigation), | |
} | |
private val abTestVariation: ABTestVariation by lazy { | |
intent?.getSerializableExtra(KEY) as? ABTestVariation ?: ABTestVariation.BASELINE | |
} |
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
private void getRecipesFromWeb(){ | |
Timber.d("getRecipesFromWeb"); | |
recipeApi.getRecipesFromWeb().enqueue(new Callback<List<Recipe>>() { | |
@Override | |
public void onResponse(Call<List<Recipe>> call, Response<List<Recipe>> response) { | |
if (response.isSuccessful()) { | |
setRecipesListObservableStatus(Status.SUCCESS,null); | |
addRecipesToDB(response.body()); | |
} else { | |
// error case |
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
private void loadAllRecipesFromDB() { | |
Timber.d("loadAllRecipesFromDB"); | |
new AsyncTask<Void, Void, List<Recipe>>() { | |
@Override | |
protected List<Recipe> doInBackground(Void...a) { | |
return recipesDAO.getAllEntries(); | |
} | |
@Override | |
protected void onPostExecute(List<Recipe> results) { |
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 void fetchData(){ | |
List<Recipe> loadingList = null; | |
if (recipesListObservable.getValue()!=null){ | |
loadingList=recipesListObservable.getValue().data; | |
} | |
recipesListObservable.setValue(Resource.loading(loadingList)); | |
loadAllRecipesFromDB(); | |
getRecipesFromWeb(); | |
} |
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 void getData(){ | |
mRepository.fetchData(); | |
} |
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
//couple the activity with the view model | |
viewModel = ViewModelProviders.of(this).get(MainActivityViewModel.class); | |
//subscribe to LiveData | |
viewModel.getRecipesListObservable().observe(MainActivity.this, new Observer<Resource<List<Recipe>>>() { | |
@Override | |
public void onChanged(@Nullable Resource<List<Recipe>> recipes) { | |
Timber.d(recipes.status.toString()); | |
} | |
}); | |
//ask the viewmodel for data (maybe an event happened?or onCreate?) |
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
/* | |
* Copyright (C) 2017 The Android Open Source Project | |
*/ | |
//a generic wrapper class that describes a data with a status | |
public class Resource<T> { | |
@NonNull | |
public final Status status; | |
@Nullable | |
public final T data; | |
@Nullable public final String message; |
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.example.android.menu; | |
import android.os.Bundle; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { |