summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlberto Duarte (PWC) <alberto.duarte.delgado@pwc.com>2023-07-10 11:03:54 +0100
committerAlberto Duarte (PWC) <alberto.duarte.delgado@pwc.com>2023-07-10 11:03:54 +0100
commitee5818d91fdd89ab14b982f5ab04eb100a1c3b81 (patch)
treec2d99cf7ecf14280a1b6eb59d07391d0123a30fa
parent2b12e7f68c840214b33e772032aff90b8e47b9e0 (diff)
mejora de la implementacion de room y arquitectura
-rw-r--r--app/src/main/java/com/frannazario/proyectoandroid/data/models/QuestionDTO.kt3
-rw-r--r--app/src/main/java/com/frannazario/proyectoandroid/data/viewmodels/PlayViewModel.kt180
-rw-r--r--app/src/main/java/com/frannazario/proyectoandroid/presentation/screens/PlayScreen.kt170
3 files changed, 105 insertions, 248 deletions
diff --git a/app/src/main/java/com/frannazario/proyectoandroid/data/models/QuestionDTO.kt b/app/src/main/java/com/frannazario/proyectoandroid/data/models/QuestionDTO.kt
index 54b5a4e..bf717e9 100644
--- a/app/src/main/java/com/frannazario/proyectoandroid/data/models/QuestionDTO.kt
+++ b/app/src/main/java/com/frannazario/proyectoandroid/data/models/QuestionDTO.kt
@@ -6,7 +6,8 @@ import androidx.room.PrimaryKey
@Entity
data class Question(
- @PrimaryKey var question: String = "",
+ @PrimaryKey (autoGenerate = true) var id: Int = 0,
+ @ColumnInfo var question: String = "",
@ColumnInfo var response1: String = "",
@ColumnInfo var response2: String = "",
@ColumnInfo var response3: String = "",
diff --git a/app/src/main/java/com/frannazario/proyectoandroid/data/viewmodels/PlayViewModel.kt b/app/src/main/java/com/frannazario/proyectoandroid/data/viewmodels/PlayViewModel.kt
index f9f3af0..1166d8f 100644
--- a/app/src/main/java/com/frannazario/proyectoandroid/data/viewmodels/PlayViewModel.kt
+++ b/app/src/main/java/com/frannazario/proyectoandroid/data/viewmodels/PlayViewModel.kt
@@ -2,80 +2,31 @@ package com.frannazario.proyectoandroid.data.viewmodels
import android.content.Context
import androidx.compose.runtime.MutableState
-import androidx.compose.runtime.mutableStateOf
-import androidx.compose.ui.graphics.Color
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.frannazario.proyectoandroid.data.models.Question
import com.frannazario.proyectoandroid.data.repositories.QuestionsRepository
import com.frannazario.proyectoandroid.data.responses.QuestionsResponse
-import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
-import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
-import kotlinx.coroutines.launch
class PlayViewModel(private val repository: QuestionsRepository = QuestionsRepository()): ViewModel() {
private val mutableState = MutableStateFlow(QuestionsViewState())
val questionsViewState: StateFlow<QuestionsViewState> = mutableState
- private val randomQuestion = mutableStateOf<Question?>(null)
- private val shuffledResponses = mutableStateOf<List<String>?>(null)
- private val score = mutableStateOf(0)
- private val counter = mutableStateOf(0)
- private val isLoading = mutableStateOf(true)
- private val buttonEnabled = mutableStateOf(true)
- private val colorOfBackground = mutableStateOf(Color.White)
-// mutableState.update { QuestionsViewState(isLoading = true) }
-// repository.getQuestions(context).onEach { response ->
-// when (response) {
-// is QuestionsResponse.Success -> mutableState.update { QuestionsViewState(questions = response.questions) }
-// is QuestionsResponse.Error -> mutableState.update { QuestionsViewState(error = response.error) }
-// }
-// }.launchIn(viewModelScope)
-// }
-// private suspend fun getQuestions() {
-// mutableState.update { it.copy(isLoading = true) }
-// val response = repository.getQuestions()
-// when (response) {
-// is QuestionsResponse.Success -> {
-// mutableState.update { it.copy(questions = response.questions, isLoading = false) }
-// randomQuestion.value = getQuestion(response.questions)
-// shuffledResponses.value = listOf(
-// randomQuestion.value!!.response1,
-// randomQuestion.value!!.response2,
-// randomQuestion.value!!.response3
-// ).shuffled()
-// isLoading.value = false
-// startCounter()
-// }
-// is QuestionsResponse.Error -> {
-// mutableState.update { it.copy(error = response.error, isLoading = false) }
-// }
-// }
-// }
-
- fun getQuestions(context: Context) {
+ fun getQuestionList(context: Context) {
mutableState.update { it.copy(isLoading = true) }
val response = repository.getQuestions(context = context)
response.onEach { questionsResponse ->
when (questionsResponse) {
is QuestionsResponse.Success -> {
mutableState.update { it.copy(questions = questionsResponse.questions, isLoading = false) }
- randomQuestion.value = getQuestion(questionsResponse.questions)
- shuffledResponses.value = listOf(
- randomQuestion.value!!.response1,
- randomQuestion.value!!.response2,
- randomQuestion.value!!.response3
- ).shuffled()
- isLoading.value = false
- startCounter(counter)
}
is QuestionsResponse.Error -> {
mutableState.update { it.copy(error = questionsResponse.error, isLoading = false) }
@@ -84,51 +35,23 @@ class PlayViewModel(private val repository: QuestionsRepository = QuestionsRepos
}.flowOn(Dispatchers.IO).launchIn(viewModelScope)
}
+ fun compareAnswer(s: String, response: String?): Boolean {
+ return s === response
+ }
+
fun getQuestion(questionList: List<Question>): Question {
return questionList.randomOrNull() ?: Question()
}
- fun compareAnswer(
- selectedResponse: String,
- score: MutableState<Int>,
- coroutineScope: CoroutineScope,
- randomQuestion: MutableState<Question?>,
- shuffledResponses: MutableState<List<String>?>,
- counter: MutableState<Int>,
- questionList: List<Question>
- ) {
- buttonEnabled.value = false
- val questionModel = this.randomQuestion.value
- if (questionModel != null) {
- if (selectedResponse == questionModel.response1) {
- this.score.value++
- colorOfBackground.value = Color.Green
- } else {
- this.counter.value += 10
- colorOfBackground.value = Color.Red
- }
- viewModelScope.launch {
- delay(2000)
- this@PlayViewModel.randomQuestion.value = getQuestion(questionsViewState.value.questions ?: emptyList())
- this@PlayViewModel.shuffledResponses.value = listOf(
- this@PlayViewModel.randomQuestion.value!!.response1,
- this@PlayViewModel.randomQuestion.value!!.response2,
- this@PlayViewModel.randomQuestion.value!!.response3
- ).shuffled()
- colorOfBackground.value = Color.White
- buttonEnabled.value = true
- }
- }
- }
+ fun shuffle(randomQuestion: MutableState<Question?>): List<String>? = listOf(
+ randomQuestion.value?.response1 ?: "1",
+ randomQuestion.value?.response2 ?: "2",
+ randomQuestion.value?.response3 ?: "3"
+ ).shuffled()
+
+
+
- fun startCounter(counter: MutableState<Int>) {
- viewModelScope.launch {
- while (score.value < 5) {
- delay(1000)
- counter.value++
- }
- }
- }
}
@@ -136,79 +59,4 @@ data class QuestionsViewState(
val questions: List<Question>? = null,
val error: String? = null,
val isLoading: Boolean = false
-)
-
-
-
-//
-//class PlayViewModel(private val repository: QuestionsRepository = QuestionsRepository()) : ViewModel() {
-// private val mutableState = MutableStateFlow(QuestionsViewState())
-// val questionsViewState: StateFlow<QuestionsViewState> = mutableState
-//
-//
-// init {
-// viewModelScope.launch {
-// getQuestions()
-// }
-// }
-//
-// private suspend fun getQuestions() {
-// mutableState.update { it.copy(isLoading = true) }
-// val response = repository.getQuestions()
-// when (response) {
-// is QuestionsResponse.Success -> {
-// mutableState.update { it.copy(questions = response.questions, isLoading = false) }
-// randomQuestion.value = getQuestion(response.questions)
-// shuffledResponses.value = listOf(
-// randomQuestion.value!!.response1,
-// randomQuestion.value!!.response2,
-// randomQuestion.value!!.response3
-// ).shuffled()
-// isLoading.value = false
-// startCounter()
-// }
-// is QuestionsResponse.Error -> {
-// mutableState.update { it.copy(error = response.error, isLoading = false) }
-// }
-// }
-// }
-//
-// private fun getQuestion(questionList: List<Question>): Question {
-// return questionList.randomOrNull() ?: Question()
-// }
-//
-// fun compareAnswer(selectedResponse: String) {
-// buttonEnabled.value = false
-// val questionModel = randomQuestion.value
-// if (questionModel != null) {
-// if (selectedResponse == questionModel.response1) {
-// score.value++
-// colorOfBackground.value = Color.Green
-// } else {
-// counter.value += 10
-// colorOfBackground.value = Color.Red
-// }
-// viewModelScope.launch {
-// delay(2000)
-// randomQuestion.value = getQuestion(questionsViewState.value.questions ?: emptyList())
-// shuffledResponses.value = listOf(
-// randomQuestion.value!!.response1,
-// randomQuestion.value!!.response2,
-// randomQuestion.value!!.response3
-// ).shuffled()
-// colorOfBackground.value = Color.White
-// buttonEnabled.value = true
-// }
-// }
-// }
-//
-// fun startCounter() {
-// viewModelScope.launch {
-// while (score.value < 5) {
-// delay(1000)
-// counter.value++
-// }
-// }
-// }
-//}
-//
+) \ No newline at end of file
diff --git a/app/src/main/java/com/frannazario/proyectoandroid/presentation/screens/PlayScreen.kt b/app/src/main/java/com/frannazario/proyectoandroid/presentation/screens/PlayScreen.kt
index f8e2bce..4b456f4 100644
--- a/app/src/main/java/com/frannazario/proyectoandroid/presentation/screens/PlayScreen.kt
+++ b/app/src/main/java/com/frannazario/proyectoandroid/presentation/screens/PlayScreen.kt
@@ -8,6 +8,7 @@ import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
+import androidx.compose.runtime.MutableState
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
@@ -22,41 +23,37 @@ import androidx.navigation.NavController
import com.frannazario.proyectoandroid.data.models.Question
import com.frannazario.proyectoandroid.data.viewmodels.PlayViewModel
import kotlinx.coroutines.CoroutineScope
-
-val score = mutableStateOf(0)
-var isLoading by mutableStateOf(true) // Initial loading state is true
-var buttonEnabled by mutableStateOf(true) // Initial loading state is true
-var colorOfBackground by mutableStateOf(Color.White) // Initial loading state is true
+import kotlinx.coroutines.delay
+import kotlinx.coroutines.launch
+val viewModel by mutableStateOf(PlayViewModel())
@Composable
fun PlayScreen(navController: NavController){
val context = LocalContext.current
- val coroutineScope: CoroutineScope = rememberCoroutineScope()
val randomQuestion = remember { mutableStateOf<Question?>(null) }
- val counter = remember { mutableStateOf(0) }
val shuffledResponses = remember {mutableStateOf<List<String>?>(null)}
-// val viewModel: PlayViewModel = viewModel()
- val viewModel by remember { mutableStateOf(PlayViewModel()) }
val questionsViewState by viewModel.questionsViewState.collectAsState()
- var questionList = listOf<Question>()
+ var questionList = remember { mutableStateOf(listOf<Question>()) }
+ var isLoading by remember {mutableStateOf(true)} // Initial loading state is true
+ val buttonEnabled = remember {mutableStateOf(true)} // Initial loading state is true
+ val colorOfBackground = remember {mutableStateOf(Color.White)}
+ val score = remember {mutableStateOf(0)}
+ val counter = remember {mutableStateOf(-1)}
+ val counterCoroutine = rememberCoroutineScope()
+ val delayCoroutine = rememberCoroutineScope()
LaunchedEffect(Unit) {
- viewModel.getQuestions(context)
+ viewModel.getQuestionList(context)
}
- LaunchedEffect(questionsViewState.questions){
+ LaunchedEffect(questionsViewState.questions) {
questionsViewState.questions?.let {
- questionList = it
- randomQuestion.value = viewModel.getQuestion(questionList)
- shuffledResponses.value = listOf(
- randomQuestion.value?.response1 ?: "1",
- randomQuestion.value?.response2 ?: "2",
- randomQuestion.value?.response3 ?: "3"
- ).shuffled()
+ questionList.value = it
+ changeQuestion(questionList.value, randomQuestion, shuffledResponses, colorOfBackground, buttonEnabled)
isLoading = false
- viewModel.startCounter(counter)
+ startCounter(counter, score, counterCoroutine)
}
}
@@ -73,7 +70,7 @@ fun PlayScreen(navController: NavController){
Box(
modifier = Modifier
.fillMaxSize()
- .background(color = colorOfBackground),
+ .background(color = colorOfBackground.value),
contentAlignment = Alignment.Center
) {
@@ -85,49 +82,55 @@ fun PlayScreen(navController: NavController){
}
Button(onClick = {
- viewModel.compareAnswer(
+ compareAnswer(
+ buttonEnabled,
shuffledResponses.value?.get(0) ?: "bb",
+ colorOfBackground,
+ counter,
score,
- coroutineScope,
+ questionList.value,
randomQuestion,
shuffledResponses,
- counter,
- questionList
+ delayCoroutine
)
},
- enabled = buttonEnabled
+ enabled = buttonEnabled.value
) {
Text(text = shuffledResponses.value?.get(0) ?: "bb" )
}
Button(onClick = {
- viewModel.compareAnswer(
+ compareAnswer(
+ buttonEnabled,
shuffledResponses.value?.get(1) ?: "bb",
+ colorOfBackground,
+ counter,
score,
- coroutineScope,
+ questionList.value,
randomQuestion,
shuffledResponses,
- counter,
- questionList
+ delayCoroutine
)
},
- enabled = buttonEnabled
+ enabled = buttonEnabled.value
) {
Text(text = shuffledResponses.value?.get(1) ?: "bb" )
}
Button(onClick = {
- viewModel.compareAnswer(
+ compareAnswer(
+ buttonEnabled,
shuffledResponses.value?.get(2) ?: "bb",
+ colorOfBackground,
+ counter,
score,
- coroutineScope,
+ questionList.value,
randomQuestion,
shuffledResponses,
- counter,
- questionList
+ delayCoroutine
)
},
- enabled = buttonEnabled
+ enabled = buttonEnabled.value
) {
Text(text = shuffledResponses.value?.get(2) ?: "bb" )
}
@@ -141,48 +144,53 @@ fun PlayScreen(navController: NavController){
}
-//fun compareAnswer(
-// selectedResponse: String,
-// score: MutableState<Int>,
-// coroutineScope: CoroutineScope,
-// randomQuestion: MutableState<Question?>,
-// shuffledResponses: MutableState<List<String>?>,
-// counter: MutableState<Int>,
-// questionList: List<Question>,
-//) {
-// buttonEnabled = false
-// val questionModel = randomQuestion.value
-// if (questionModel != null) {
-// if (selectedResponse == questionModel.response1) {
-// score.value++
-// colorOfBackground = Color.Green
-// } else {
-// counter.value += 10
-// colorOfBackground = Color.Red
-// }
-// coroutineScope.launch {
-// delay(2000)
-// randomQuestion.value = getQuestion(questionList)
-// shuffledResponses.value = listOf(
-// randomQuestion.value!!.response1,
-// randomQuestion.value!!.response2,
-// randomQuestion.value!!.response3
-// ).shuffled()
-// colorOfBackground = Color.White
-// buttonEnabled = true
-// }
-// }
-//}
-//
-//
-//fun getQuestion(questionList: List<Question>): Question {
-// return questionList.randomOrNull() ?: Question()
-//}
-//
-//
-//suspend fun counterStart(counter: MutableState<Int>) {
-// while(score.value < 5) {
-// counter.value++
-// delay(1000) // Delay for 1 second (1000 milliseconds)
-// }
-//}
+fun changeQuestion(
+ questionList: List<Question>,
+ randomQuestion: MutableState<Question?>,
+ shuffledResponses: MutableState<List<String>?>,
+ colorOfBackground: MutableState<Color>,
+ buttonEnabled: MutableState<Boolean>
+) {
+ colorOfBackground.value = Color.White
+ randomQuestion.value = viewModel.getQuestion(questionList)
+ shuffledResponses.value = viewModel.shuffle(randomQuestion)
+ buttonEnabled.value = true
+}
+
+fun compareAnswer(
+ buttonEnabled: MutableState<Boolean>,
+ s: String,
+ colorOfBackground: MutableState<Color>,
+ counter: MutableState<Int>,
+ score: MutableState<Int>,
+ questionList: List<Question>,
+ randomQuestion: MutableState<Question?>,
+ shuffledResponses: MutableState<List<String>?>,
+ delayCoroutine: CoroutineScope
+) {
+ buttonEnabled.value = false
+ if(viewModel.compareAnswer(s, randomQuestion.value?.response1)){
+ score.value++
+ colorOfBackground.value = Color.Green
+ }else{
+ counter.value += 10
+ colorOfBackground.value = Color.Red
+ }
+ delayCoroutine.launch {
+ delay(2000)
+ changeQuestion(questionList, randomQuestion, shuffledResponses, colorOfBackground, buttonEnabled)
+ }
+}
+
+fun startCounter(
+ counter: MutableState<Int>,
+ score: MutableState<Int>,
+ counterCoroutine: CoroutineScope
+) {
+ counterCoroutine.launch {
+ while (score.value < 5) {
+ counter.value++
+ delay(1000)
+ }
+ }
+} \ No newline at end of file