Skip to content

Instantly share code, notes, and snippets.

View Elijah-Dangerfield's full-sized avatar

Elijah Dangerfield Elijah-Dangerfield

View GitHub Profile
class QuestionsFragment : Fragment() {
val questionsViewModel: QuestionsViewModel by viewModels()
lateinit var currentQuestion : Question
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary"
tools:context=".ui.QuestionsFragment">
fun populateFakeData() {
val fakeData = arrayListOf(Question(2, arrayListOf("John F. Kennedy","Abraham Lincoln", "William Henery Harrison", "George Washington"), "Which president was in the office for the shortest term?")
,Question(2,arrayListOf("Elon Musk's Tesla", "Sandra Bullock","Voyager 1", "Jupiter"), "What is the furthest man made object from earth?")
,Question(2,arrayListOf("English", "French", "Java", "German"), "Which of the following in not an indo-european language?")
, Question(0,arrayListOf("Kilobyte", "centibyte", "millibyte", "1"),"What is the smallest unit of memory?")
, Question(0,arrayListOf("Mary Curie", "Michell Obama", "Harriet Tubman", "Elenaor Roosevelt"),"Who was the first woman to earn a nobel peace prize?")
,Question(1,arrayListOf("The brain", "The Skin", "The Heart", "The Finger"),"What is the body's largest organ?")
, Question(3,arrayListOf("Tibia", "The Skull", "Mandibles", "Knee Caps"),"Which Bone are babies born withou
class QuestionsViewModel : ViewModel() {
private val repository: QuestionsRepository = QuestionsRepositoryImp()
private var questions = MutableLiveData<List<Question>>()
fun getQuestions(): LiveData<List<Question>> {
if(questions.value.isNullOrEmpty()) {
questions = repository.getQuestions()
}
return questions
class QuestionsViewModel : ViewModel() {
private val repository: QuestionsRepository = QuestionsRepositoryImp()
private var questions = MutableLiveData<List<Question>>()
fun getQuestions(): LiveData<List<Question>> {
questions = repository.getQuestions()
return questions
}
}
class QuestionsFragment : Fragment() {
val questionsViewModel: QuestionsViewModel by viewModels()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_questions, container, false)
class QuestionsViewModel : ViewModel() {
private val repository: QuestionsRepository = QuestionsRepositoryImp()
private val questions = MutableLiveData<List<Question>>()
fun getQuestions(): LiveData<List<Question>> {
return questions
}
}
class QuestionsViewModel : ViewModel() {
private val repository: QuestionsRepository = QuestionsRepositoryImp()
private val questions = MutableLiveData<List<Question>>()
}
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import com.dangerfield.triviaapp.R
import com.dangerfield.triviaapp.api.QuestionsRepositoryImp
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.dangerfield.triviaapp.model.Question
import com.google.firebase.firestore.FirebaseFirestore
class QuestionsRepositoryImp(): QuestionsRepository {
private val db = FirebaseFirestore.getInstance()
override fun getQuestions(): MutableLiveData<List<Question>> {