Skip to content

Commit

Permalink
Merge pull request #17 from LeoAndo/release/v1.0.2
Browse files Browse the repository at this point in the history
Release/v1.0.2
  • Loading branch information
LeoAndo authored Jul 31, 2023
2 parents 97fd188 + ac5261a commit a7dcc2d
Show file tree
Hide file tree
Showing 15 changed files with 362 additions and 179 deletions.
Binary file modified .DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "com.leoleo.quizapp"
minSdk 29
targetSdk 34
versionCode 101
versionName "1.0.1"
versionCode 102
versionName "1.0.2"

testInstrumentationRunner "com.leoleo.androidapptemplate.CustomTestRunner"
vectorDrawables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ internal enum class Quiz(
listOf(
QuestionData(
messageResId = R.string.quiz_01_question_01,
answers = listOf("2", "3", "4", "5", "6"),
answerResId = R.array.quiz_01_question_01_answers,
answerIndex = 0
),
QuestionData(
messageResId = R.string.quiz_01_question_02,
answers = listOf("12", "16", "18"),
answerResId = R.array.quiz_01_question_02_answers,
answerIndex = 1
),
QuestionData(
messageResId = R.string.quiz_01_question_03,
answers = listOf("155", "152", "151", "150"),
answerResId = R.array.quiz_01_question_03_answers,
answerIndex = 2
),
)
Expand All @@ -34,17 +34,17 @@ internal enum class Quiz(
listOf(
QuestionData(
messageResId = R.string.quiz_02_question_01,
answers = listOf("2", "8", "1", "0"),
answerResId = R.array.quiz_02_question_01_answers,
answerIndex = 3
),
QuestionData(
messageResId = R.string.quiz_02_question_02,
answers = listOf("2", "0", "1", "8"),
answerResId = R.array.quiz_02_question_02_answers,
answerIndex = 1
),
QuestionData(
messageResId = R.string.quiz_02_question_03,
answers = listOf("-92", "90", "95", "-95"),
answerResId = R.array.quiz_02_question_03_answers,
answerIndex = 3
),
)
Expand All @@ -54,17 +54,17 @@ internal enum class Quiz(
listOf(
QuestionData(
messageResId = R.string.quiz_03_question_01,
answers = listOf("2", "8", "1", "0"),
answerResId = R.array.quiz_03_question_01_answers,
answerIndex = 2
),
QuestionData(
messageResId = R.string.quiz_03_question_02,
answers = listOf("2", "64", "1", "8"),
answerResId = R.array.quiz_03_question_02_answers,
answerIndex = 1
),
QuestionData(
messageResId = R.string.quiz_03_question_03,
answers = listOf("3440", "3442", "3444", "3448"),
answerResId = R.array.quiz_03_question_03_answers,
answerIndex = 2
),
)
Expand Down
54 changes: 54 additions & 0 deletions app/src/arithmetic/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,58 @@
<string name="quiz_03_question_01" translatable="false">1 X 1 = ?</string>
<string name="quiz_03_question_02" translatable="false">8 X 8 = ?</string>
<string name="quiz_03_question_03" translatable="false">28 X 123 = ?</string>
<string-array name="quiz_01_question_01_answers">
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
<string-array name="quiz_01_question_02_answers">
<item>12</item>
<item>16</item>
<item>18</item>
</string-array>
<string-array name="quiz_01_question_03_answers">
<item>155</item>
<item>152</item>
<item>151</item>
<item>150</item>
</string-array>
<string-array name="quiz_02_question_01_answers">
<item>2</item>
<item>8</item>
<item>1</item>
<item>0</item>
</string-array>
<string-array name="quiz_02_question_02_answers">
<item>2</item>
<item>0</item>
<item>1</item>
<item>8</item>
</string-array>
<string-array name="quiz_02_question_03_answers">
<item>-92</item>
<item>90</item>
<item>95</item>
<item>-95</item>
</string-array>
<string-array name="quiz_03_question_01_answers">
<item>2</item>
<item>8</item>
<item>1</item>
<item>0</item>
</string-array>
<string-array name="quiz_03_question_02_answers">
<item>2</item>
<item>64</item>
<item>1</item>
<item>8</item>
</string-array>
<string-array name="quiz_03_question_03_answers">
<item>3440</item>
<item>3442</item>
<item>3444</item>
<item>3448</item>
</string-array>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ internal fun CompactMainScreen(
viewModel.changeUiState(throwable.message ?: "error.")
}

val title = stringResource(id = viewModel.uiState.selectedQuestion.titleResId)

CompactMainScreenStateless(
modifier = modifier,
snackbarHostState = snackbarHostState,
Expand All @@ -60,7 +62,7 @@ internal fun CompactMainScreen(
},
onClickAnswerButton = { index ->
scope.launch(coroutineExceptionHandler) {
viewModel.changeUiState(pagerState.currentPage, index)
viewModel.changeUiState(pagerState.currentPage, index, title)
val msg =
if (viewModel.uiState.selectedQuestion.data[pagerState.currentPage].answerIndex == index) {
"Is the correct answer!"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ package com.leoleo.androidapptemplate.ui.quiz
import android.widget.Toast
import androidx.activity.compose.BackHandler
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
Expand All @@ -17,6 +19,7 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.leoleo.androidapptemplate.ui.component.AppSurface
import com.leoleo.androidapptemplate.ui.component.ErrorContent
import com.leoleo.androidapptemplate.ui.preview.PreviewTabletDevice
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.launch

Expand All @@ -28,72 +31,123 @@ internal fun ExpandedMainScreen(
) {
val drawerState = rememberDrawerState(DrawerValue.Closed)
val pagerState = rememberPagerState()
val pagerScrollState = rememberScrollState()
val scope = rememberCoroutineScope()
val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
viewModel.changeUiState(throwable.message ?: "error.")
}
val context = LocalContext.current
val title = stringResource(id = viewModel.uiState.selectedQuestion.titleResId)
BackHandler(enabled = drawerState.isOpen) { scope.launch { drawerState.close() } }

ExpandedMainScreenStateless(
modifier = modifier,
drawerState = drawerState,
uiState = viewModel.uiState,
pagerState = pagerState,
onClickDrawerItem = { index ->
viewModel.changeUiState(index)
scope.launch {
drawerState.close()
pagerState.scrollToPage(page = 0)
}
},
onClickResetButton = {
scope.launch(coroutineExceptionHandler) {
viewModel.resetUiState()
pagerState.scrollToPage(page = 0)
}
},
onClickAnswerButton = { index ->
scope.launch(coroutineExceptionHandler) {
viewModel.changeUiState(pagerState.currentPage, index, title)
val msg =
if (viewModel.uiState.selectedQuestion.data[pagerState.currentPage].answerIndex == index) {
"Is the correct answer!"
} else {
"Incorrect!"
}
pagerState.scrollToPage(page = pagerState.currentPage + 1)
// TODO: できれば Android ViewのToastを辞めたい
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
}
}
)
}

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun ExpandedMainScreenStateless(
modifier: Modifier = Modifier,
drawerState: DrawerState,
uiState: QuizUiState,
pagerScrollState: ScrollState = rememberScrollState(),
pagerState: PagerState,
onClickDrawerItem: (Int) -> Unit,
onClickResetButton: () -> Unit,
onClickAnswerButton: (Int) -> Unit,
) {
AppSurface(modifier) {
DismissibleNavigationDrawer(drawerState = drawerState, drawerContent = {
DismissibleDrawerSheet {
Spacer(Modifier.height(12.dp))
Quiz.values().forEachIndexed { index, quiz ->
NavigationDrawerItem(
label = { Text(stringResource(id = quiz.titleResId)) },
selected = quiz == viewModel.uiState.selectedQuestion,
onClick = {
viewModel.changeUiState(index)
scope.launch {
drawerState.close()
pagerState.scrollToPage(page = 0)
}
},
selected = quiz == uiState.selectedQuestion,
onClick = { onClickDrawerItem(index) },
modifier = Modifier.padding(horizontal = 12.dp)
)
}
}
}, content = {
if (viewModel.uiState.errorMessage.isNullOrBlank()) {
if (uiState.errorMessage.isNullOrBlank()) {
MainContent(modifier = Modifier
.fillMaxSize()
.padding(8.dp)
.verticalScroll(pagerScrollState),
pagerState = pagerState,
selectedQuestion = viewModel.uiState.selectedQuestion,
isFinishedQuiz = viewModel.uiState.isFinishedQuiz,
collectAnswerCount = viewModel.uiState.collectAnswerCount,
onClickResetButton = {
scope.launch(coroutineExceptionHandler) {
viewModel.resetUiState()
pagerState.scrollToPage(page = 0)
}
},
onClickAnswerButton = { index ->
scope.launch(coroutineExceptionHandler) {
viewModel.changeUiState(pagerState.currentPage, index)
val msg =
if (viewModel.uiState.selectedQuestion.data[pagerState.currentPage].answerIndex == index) {
"Is the correct answer!"
} else {
"Incorrect!"
}
pagerState.scrollToPage(page = pagerState.currentPage + 1)
// TODO: できれば Android ViewのToastを辞めたい
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
}
})
selectedQuestion = uiState.selectedQuestion,
isFinishedQuiz = uiState.isFinishedQuiz,
collectAnswerCount = uiState.collectAnswerCount,
onClickResetButton = { onClickResetButton() },
onClickAnswerButton = { index -> onClickAnswerButton(index) })
} else {
ErrorContent(
modifier = Modifier
.fillMaxSize()
.wrapContentSize()
.padding(8.dp),
errorMessage = viewModel.uiState.errorMessage ?: "error.",
errorMessage = uiState.errorMessage,
)
}
})
}
}

@OptIn(ExperimentalFoundationApi::class)
@PreviewTabletDevice
@Composable
fun Prev_ExpandedMainScreen_Initial() {
ExpandedMainScreenStateless(
drawerState = rememberDrawerState(DrawerValue.Closed),
uiState = QuizUiState(),
pagerState = rememberPagerState(),
onClickDrawerItem = {},
onClickResetButton = { },
onClickAnswerButton = {},
)
}

@OptIn(ExperimentalFoundationApi::class)
@PreviewTabletDevice
@Composable
fun Prev_ExpandedMainScreen_Error() {
ExpandedMainScreenStateless(
drawerState = rememberDrawerState(DrawerValue.Closed),
uiState = QuizUiState(errorMessage = "error..."),
pagerState = rememberPagerState(),
onClickDrawerItem = {},
onClickResetButton = { },
onClickAnswerButton = {},
)
}
Loading

0 comments on commit a7dcc2d

Please sign in to comment.