Skip to content

Commit

Permalink
Add "Finish!" button on the end of the lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed May 31, 2024
1 parent 1da5fd7 commit 275ad9b
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import ivy.model.CourseId
import ivy.model.LessonId
import ui.navigation.Screen
import ui.screen.lesson.composable.LessonContent
import ui.screen.lesson.handler.OnBackClickEventHandler
import ui.screen.lesson.handler.OnContinueClickEventHandler
import ui.screen.lesson.handler.OnSoundClickEventHandler
import ui.screen.lesson.handler.QuestionViewEventHandler
import ui.screen.lesson.handler.*
import ui.screen.lesson.mapper.LessonTreeManager
import ui.screen.lesson.mapper.LessonViewStateMapper

Expand All @@ -28,6 +25,7 @@ class LessonScreen(
register { OnContinueClickEventHandler(Di.get()) }
register { QuestionViewEventHandler(Di.get()) }
register { OnSoundClickEventHandler(Di.get()) }
register { OnFinishClickEventHandler(Di.get(), Di.get()) }
register {
LessonViewModel(
courseId = courseId,
Expand All @@ -40,7 +38,8 @@ class LessonScreen(
Di.get<OnBackClickEventHandler>(),
Di.get<OnContinueClickEventHandler>(),
Di.get<QuestionViewEventHandler>(),
Di.get<OnSoundClickEventHandler>()
Di.get<OnSoundClickEventHandler>(),
Di.get<OnFinishClickEventHandler>(),
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ sealed interface CtaViewState {
val currentItemId: LessonItemIdViewState

data class Continue(override val currentItemId: LessonItemIdViewState) : CtaViewState
data class Finish(override val currentItemId: LessonItemIdViewState) : CtaViewState
}

@Immutable
Expand Down Expand Up @@ -128,6 +129,7 @@ sealed interface LessonViewEvent {
data object OnBackClick : LessonViewEvent
data class OnContinueClick(val currentItemId: LessonItemIdViewState) : LessonViewEvent
data class OnSoundClick(val soundUrl: String) : LessonViewEvent
data class OnFinishClick(val currentItemId: LessonItemIdViewState) : LessonViewEvent
}

sealed interface QuestionViewEvent : LessonViewEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import ui.screen.lesson.LessonItemIdViewState
fun CtaBar(
viewState: CtaViewState,
onContinueClick: (LessonItemIdViewState) -> Unit,
onFinishClick: (LessonItemIdViewState) -> Unit,
modifier: Modifier = Modifier,
) {
Box(
Expand All @@ -29,6 +30,10 @@ fun CtaBar(
is CtaViewState.Continue -> ContinueButton(
onClick = { onContinueClick(viewState.currentItemId) }
)

is CtaViewState.Finish -> FinishButton(
onClick = { onFinishClick(viewState.currentItemId) }
)
}
}
}
Expand All @@ -43,4 +48,16 @@ private fun ContinueButton(
text = "CONTINUE",
onClick = onClick,
)
}

@Composable
private fun FinishButton(
modifier: Modifier = Modifier,
onClick: () -> Unit,
) {
CtaButton(
modifier = modifier,
text = "FINISH!",
onClick = onClick,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ fun LessonContent(
onContinueClick = { itemId ->
onEvent(LessonViewEvent.OnContinueClick(itemId))
},
onFinishClick = { itemId ->
onEvent(LessonViewEvent.OnFinishClick(itemId))
}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ui.screen.lesson.handler

import Platform
import ivy.content.SoundsUrls
import ui.EventHandler
import ui.navigation.Navigation
import ui.screen.lesson.LessonViewEvent
import ui.screen.lesson.LessonViewModel.LocalState
import ui.screen.lesson.LessonVmContext

class OnFinishClickEventHandler(
private val platform: Platform,
private val navigation: Navigation,
) :
EventHandler<LessonViewEvent.OnContinueClick, LocalState> {
override val eventTypes = setOf(LessonViewEvent.OnContinueClick::class)

override suspend fun LessonVmContext.handleEvent(
event: LessonViewEvent.OnContinueClick
) {
navigation.back()
platform.playSound(SoundsUrls.CompleteLesson)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@ class LessonViewStateMapper(
null, is QuestionItem, is OpenQuestionItem,
is ChoiceItem -> null

else -> CtaViewState.Continue(currentItem.id.toViewState())
else -> {
platform.log(LogLevel.Debug, "Current item: $currentItem")
if (currentItem is LinearItem && currentItem.next == null) {
CtaViewState.Finish(currentItem.id.toViewState())
} else {
CtaViewState.Continue(currentItem.id.toViewState())
}
}
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ fun demoLesson() = lessonContent {
text = "Go to Question 1",
onClick = LessonItemId("q1")
)
textItem("end") {
text = "End of demo lesson."
style = TextStyle.Body
}
openQuestion("q2") {
question = "What is the answer to the ultimate question of life, the universe, and everything?"
correctAnswer = "42"
}
textItem("end") {
text = "End of demo lesson."
style = TextStyle.Body
}
}

fun main() {
Expand Down
1 change: 1 addition & 0 deletions shared/src/commonMain/kotlin/ivy/content/SoundsUrls.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ object SoundsUrls {
val Ups = soundUrl("ups.wav")
val Success = soundUrl("success.mp3")
val Complete = soundUrl("complete.wav")
val CompleteLesson = soundUrl("complete-lesson.mp3")

fun soundUrl(fileName: String): String =
"https://github.com/ILIYANGERMANOV/ivy-resources/raw/master/ivy-learn/sounds/$fileName"
Expand Down

0 comments on commit 275ad9b

Please sign in to comment.