public class MainViewModel extends AndroidViewModel implements LifecycleObserver
RecipeRepository
methods and contains various MutableLiveData
fields.Modifier and Type | Class and Description |
---|---|
static class |
MainViewModel.State |
Constructor and Description |
---|
MainViewModel(Application application)
Initializes the MainViewModel and the variables it contains.
|
Modifier and Type | Method and Description |
---|---|
void |
deleteRecipe(Recipe r) |
void |
finish(AssemblyRecipe data)
Posts a completed Recipe and the final status value.
|
void |
generateStrings()
This method is called if
makeItGo(String) completes successfully. |
LiveData<List<Recipe>> |
getAllRecipes() |
LiveData<List<Ingredient>> |
getIngredients() |
LiveData<Recipe> |
getRecipe() |
String |
getSharedUrl() |
LiveData<MainViewModel.State> |
getStatus() |
LiveData<List<Step>> |
getSteps() |
LiveData<List<String>> |
getStringsFromDocument() |
LiveData<Throwable> |
getThrowable() |
void |
loadRecipe(Long id)
On success, posts a retrieved Recipe.
|
void |
makeItGo(String url)
This method is called by the
LoadingFragment
on navigation from the HomeFragment to begin processing. |
void |
processData(String ingredient,
String instruction)
This method is called by the
SelectionFragment
when navigating back to the LoadingFragment. |
void |
resetData()
Returns all
MutableLiveData values (except Status) to default to clear data from prior
activities. |
void |
resetStatus()
Returns Status to default to clear progress from prior activities.
|
void |
saveRecipe(Recipe newRecipe) |
void |
setSharedUrl(String string) |
void |
updateRecipe(Recipe recipe) |
getApplication
public MainViewModel(@NonNull Application application)
application
- this application.public LiveData<List<Recipe>> getAllRecipes()
RecipeRepository.getAll()
.public LiveData<List<Ingredient>> getIngredients()
public LiveData<List<String>> getStringsFromDocument()
public LiveData<MainViewModel.State> getStatus()
public String getSharedUrl()
public void setSharedUrl(String string)
string
- a URL received by an Android Intent.public void resetData()
MutableLiveData
values (except Status) to default to clear data from prior
activities.public void resetStatus()
public void makeItGo(String url)
LoadingFragment
on navigation from the HomeFragment to begin processing. It passes the user's url to RecipeRepository.connect(java.lang.String)
, and notifies the Loading Fragment by posting status
values
as MutableLiveData.
On success, calls generateStrings()
and updates status. On failure, a Throwable is posted instead.url
- is taken from user input on the HomeFragment
.public void generateStrings()
makeItGo(String)
completes successfully. It calls RecipeRepository.generateStrings()
.
On success, post the resulting Strings as MutableLiveData and updates status. On failure, a Throwable is posted instead.
public void processData(String ingredient, String instruction)
SelectionFragment
when navigating back to the LoadingFragment. It passes user-selected Strings to the backend to
help identify what text should be extracted from the HTML.
On success, finish(AssemblyRecipe)
is called and the next status value is posted. On
failure, a Throwable is posted.
ingredient
- user-selected ingredient text.instruction
- user-selected instruction text.public void finish(AssemblyRecipe data)
data
- a completed AssemblyRecipe.public void saveRecipe(Recipe newRecipe)
newRecipe
- is a Recipe to be saved as a new database entity.public void updateRecipe(Recipe recipe)
recipe
- is a Recipe to be updated in the database.public void loadRecipe(Long id)
id
- is a RecipeId passed by either the EditingFragment or RecipeFragment.public void deleteRecipe(Recipe r)