Skip to content

Instantly share code, notes, and snippets.

View Elijah-Dangerfield's full-sized avatar

Elijah Dangerfield Elijah-Dangerfield

View GitHub Profile
class QuestionsViewModel : ViewModel() {
private val repository: QuestionsRepository = QuestionsRepositoryImp()
private var questions = MutableLiveData<List<Question>>()
private var answeredState: MutableLiveData<AnsweredState> =
MutableLiveData(AnsweredState.Answering)
var currentQuestionIndex = 0
var questionList: List<Question>? = null
private fun observeQuestions() {
questionsViewModel.getQuestions().observe(viewLifecycleOwner, Observer {
//this code runs every time the data changes.
questionList = it
getNextQuestion()
})
}
class QuestionsFragment : Fragment() {
val questionsViewModel: QuestionsViewModel by viewModels()
lateinit var currentQuestion : Question
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
private fun observeAnsweredState() {
questionsViewModel.getAnsweredState().observe(viewLifecycleOwner, Observer {
Log.d("Elijah", "Answered State: " + it.name)
when(it){
AnsweredState.Answered -> showAnswered()
AnsweredState.Answering -> showAnswering()
}
})
}
class QuestionsFragment : Fragment() {
val questionsViewModel: QuestionsViewModel by viewModels()
lateinit var currentQuestion : Question
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
class QuestionsViewModel : ViewModel() {
private val repository: QuestionsRepository = QuestionsRepositoryImp()
private var questions = MutableLiveData<List<Question>>()
private var answeredState: MutableLiveData<AnsweredState> =
MutableLiveData(AnsweredState.Answering)
fun getAnsweredState(): MutableLiveData<AnsweredState> {
return answeredState
enum class AnsweredState {
Answered, Answering
}
class QuestionsFragment : Fragment() {
val questionsViewModel: QuestionsViewModel by viewModels()
lateinit var currentQuestion : Question
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
private fun getCurrentAnswer(id: Int): String? {
return when (id) {
option_1.id -> option_1.text.toString()
option_2.id -> option_2.text.toString()
option_3.id -> option_3.text.toString()
option_4.id -> option_4.text.toString()
else -> null
}
}
class QuestionsFragment : Fragment() {
val questionsViewModel: QuestionsViewModel by viewModels()
lateinit var currentQuestion : Question
var currentAnswer : String? = null
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?