Class QuestionViewModel
- java.lang.Object
-
- androidx.lifecycle.ViewModel
-
- androidx.lifecycle.AndroidViewModel
-
- edu.cnm.deepdive.interviewprep.viewmodel.QuestionViewModel
-
- All Implemented Interfaces:
DefaultLifecycleObserver
,LifecycleObserver
public class QuestionViewModel extends AndroidViewModel implements DefaultLifecycleObserver
Implements the business logic behind the application. Interacts with the QuestionRepository to perform CRUD operations on the server.
-
-
Constructor Summary
Constructors Constructor Description QuestionViewModel(Application application)
Class constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
createQuestion(Question question)
creates a question and sends a question to the questionRepository for creation on the server.void
deleteQuestion(UUID questionId)
deletes a question from the questionRepository which subsequently deletes a question from the server.LiveData<Question>
getQuestion()
Returns the local question variable.LiveData<List<Question>>
getQuestions()
Returns the local questions variable.void
onStop(LifecycleOwner owner)
void
refreshQuestion(UUID questionId)
gets a question from the questionRepository which subsequently gets a question from the server.void
refreshQuestions()
gets a list of all questions from the question repository.void
updateQuestion(Question question)
updates a question and sends a question to the questionRepository for updating on the server.-
Methods inherited from class androidx.lifecycle.AndroidViewModel
getApplication
-
-
-
-
Constructor Detail
-
QuestionViewModel
public QuestionViewModel(@NonNull Application application)
Class constructor. Instantiates local class variables. Additionally, gets all the questions from the server.- Parameters:
application
- an application.
-
-
Method Detail
-
refreshQuestions
public void refreshQuestions()
gets a list of all questions from the question repository. Additionally, subscribes to the result and puts the result in the local variable questions.
-
updateQuestion
public void updateQuestion(Question question)
updates a question and sends a question to the questionRepository for updating on the server. Additionally, subscribes to the result and puts the result in the local variable question. Then reinitializes the questions field so that it will refresh on the screen.- Parameters:
question
- An updatedQuestion
object.
-
createQuestion
public void createQuestion(Question question)
creates a question and sends a question to the questionRepository for creation on the server. Additionally, subscribes to the result and puts the result in the local variable question. Then reinitializes the questions field so that it will refresh on the screen.- Parameters:
question
- A newQuestion
object.
-
refreshQuestion
public void refreshQuestion(UUID questionId)
gets a question from the questionRepository which subsequently gets a question from the server. Additionally, subscribes to the result and puts the result in the local variable question. Then reinitializes the questions field so that it will refresh on the screen.- Parameters:
questionId
- A questionId in the form of a universally unique identifier.
-
deleteQuestion
public void deleteQuestion(UUID questionId)
deletes a question from the questionRepository which subsequently deletes a question from the server.- Parameters:
questionId
- A questionId in the form of a universally unique identifier.
-
onStop
public void onStop(@NonNull LifecycleOwner owner)
- Specified by:
onStop
in interfaceDefaultLifecycleObserver
-
-