Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:braille-systems/learn-braille into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
winter-yuki committed Apr 17, 2020
2 parents ddeb759 + ab05a7f commit 586d399
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface MutableDotsChecker : DotsChecker {
}

/**
* Initialize callbacks firstly
* Initialize lateinit callbacks firstly
*/
private class DotsCheckerImpl : MutableDotsChecker {

Expand Down Expand Up @@ -113,7 +113,7 @@ private class DotsCheckerImpl : MutableDotsChecker {
override fun onCheck() = onCheckHandler().side {
if (state == DotsChecker.State.HINT) {
state = DotsChecker.State.INPUT
onPassHist()
onPassHint()
} else {
if (isCorrect) onCorrect()
else onIncorrect()
Expand All @@ -128,13 +128,18 @@ private class DotsCheckerImpl : MutableDotsChecker {
_eventIncorrect.value = true
}

private fun onPassHist() = onPassHintHandler().side {
private fun onPassHint() = onPassHintHandler().side {
_eventPassHint.value = true
}

override fun onHint() = onHintHandler().side {
state = DotsChecker.State.HINT
_eventHint.value = expectedDots
if (state == DotsChecker.State.HINT) {
state = DotsChecker.State.INPUT
onPassHint()
} else {
state = DotsChecker.State.HINT
_eventHint.value = expectedDots
}
}

override fun onCorrectComplete() {
Expand Down Expand Up @@ -194,7 +199,6 @@ fun DotsChecker.getEventHintObserver(
) = Observer<BrailleDots?> { expectedDots ->
if (expectedDots == null) return@Observer
dots.display(expectedDots)
dots.clickable(false)
serial?.trySend(expectedDots)
block(expectedDots)
onHintComplete()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.github.braillesystems.learnbraille.screens.lessons

import android.view.View
import android.widget.Toast
import com.github.braillesystems.learnbraille.R
import com.github.braillesystems.learnbraille.database.LearnBrailleDatabase
import com.github.braillesystems.learnbraille.database.entities.BrailleDots
import com.github.braillesystems.learnbraille.database.entities.Step
import com.github.braillesystems.learnbraille.database.entities.spelling
import com.github.braillesystems.learnbraille.screens.HelpMsgId
import com.github.braillesystems.learnbraille.views.BrailleDotsState
import com.github.braillesystems.learnbraille.views.spelling
import timber.log.Timber

/**
* Set `userTouchedDots` to false in `onCreateView`
*/
abstract class AbstractInputLesson(helpMsgId: HelpMsgId) : AbstractLesson(helpMsgId) {

protected var userTouchedDots: Boolean = false

protected fun getPrevButtonListener(step: Step, userId: Long, database: LearnBrailleDatabase) =
View.OnClickListener {
database.apply {
navigateToPrevStep(
current = step,
userId = userId,
stepDao = stepDao,
lastStepDao = userLastStep
)
}
}

protected fun getToCurrStepListener(userId: Long, database: LearnBrailleDatabase) =
View.OnClickListener {
database.apply {
navigateToCurrentStep(
userId = userId,
stepDao = stepDao,
lastStepDao = userLastStep
)
}
}

protected fun getEventCorrectObserverBlock(
step: Step,
userId: Long,
database: LearnBrailleDatabase
): () -> Unit = {
database.apply {
Timber.i("Handle correct")
Toast.makeText(
context, getString(R.string.msg_correct), Toast.LENGTH_SHORT
).show()
navigateToNextStep(
current = step,
userId = userId,
stepDao = stepDao,
lastStepDao = userLastStep,
upsd = userPassedStepDao
)
}
}

protected fun getEventIncorrectObserverBlock(
step: Step,
userId: Long,
database: LearnBrailleDatabase,
dots: BrailleDotsState
): () -> Unit = {
database.apply {
Timber.i("Handle incorrect: entered = ${dots.spelling}")
if (userTouchedDots) {
Toast.makeText(
context, getString(R.string.msg_incorrect), Toast.LENGTH_SHORT
).show()
} else {
navigateToNextStep(
current = step,
userId = userId,
stepDao = stepDao,
lastStepDao = userLastStep
) {
Toast.makeText(
context, getString(R.string.msg_incorrect), Toast.LENGTH_SHORT
).show()
}
}
}
}

protected fun getEventHintObserverBlock(): (BrailleDots) -> Unit = { expectedDots ->
Timber.i("Handle hint")
val toast = getString(R.string.practice_hint_template)
.format(expectedDots.spelling)
Toast.makeText(context, toast, Toast.LENGTH_LONG).show()
}

protected fun getEventPassHintObserverBlock(): () -> Unit = {
Timber.i("Handle pass hint")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import android.os.Bundle
import android.os.Vibrator
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.core.content.getSystemService
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.ViewModelProvider
Expand All @@ -24,7 +23,7 @@ import com.github.braillesystems.learnbraille.util.updateTitle
import com.github.braillesystems.learnbraille.views.*
import timber.log.Timber

class InputDotsFragment : AbstractLesson(R.string.lessons_help_input_dots) {
class InputDotsFragment : AbstractInputLesson(R.string.lessons_help_input_dots) {

private lateinit var viewModel: InputViewModel
private lateinit var expectedDots: BrailleDots
Expand Down Expand Up @@ -56,9 +55,15 @@ class InputDotsFragment : AbstractLesson(R.string.lessons_help_input_dots) {
brailleDots.dots.display(step.data.dots)

expectedDots = step.data.dots
userTouchedDots = false
dots = brailleDots.dots.apply {
uncheck()
clickable(true)
checkBoxes.forEach { checkBox ->
checkBox.setOnClickListener {
userTouchedDots = true
}
}
}


Expand All @@ -75,74 +80,39 @@ class InputDotsFragment : AbstractLesson(R.string.lessons_help_input_dots) {
lifecycleOwner = this@InputDotsFragment


getDBInstance().apply {

prevButton.setOnClickListener {
navigateToPrevStep(
current = step,
userId = defaultUser,
stepDao = stepDao,
lastStepDao = userLastStep
)
}
val database = getDBInstance()

toCurrStepButton.setOnClickListener {
navigateToCurrentStep(
userId = defaultUser,
stepDao = stepDao,
lastStepDao = userLastStep
)
}
prevButton.setOnClickListener(getPrevButtonListener(step, defaultUser, database))
toCurrStepButton.setOnClickListener(getToCurrStepListener(defaultUser, database))

viewModel.eventCorrect.observe(
viewLifecycleOwner,
viewModel.getEventCorrectObserver(dots, buzzer) {
Timber.i("Handle correct")
Toast.makeText(context, getString(R.string.msg_correct), Toast.LENGTH_SHORT)
.show()
navigateToNextStep(
current = step,
userId = defaultUser,
stepDao = stepDao,
lastStepDao = userLastStep,
upsd = userPassedStepDao
)
}
viewModel.eventCorrect.observe(
viewLifecycleOwner,
viewModel.getEventCorrectObserver(
dots, buzzer,
getEventCorrectObserverBlock(step, defaultUser, database)
)
)

viewModel.eventIncorrect.observe(
viewLifecycleOwner,
viewModel.getEventIncorrectObserver(dots, buzzer) {
Timber.i("Handle incorrect: entered = ${dots.spelling}")
Toast.makeText(
context, getString(R.string.msg_incorrect), Toast.LENGTH_SHORT
).show()
navigateToNextStep(
current = step,
userId = defaultUser,
stepDao = stepDao,
lastStepDao = userLastStep
)
}
viewModel.eventIncorrect.observe(
viewLifecycleOwner,
viewModel.getEventIncorrectObserver(
dots, buzzer,
getEventIncorrectObserverBlock(step, defaultUser, database, dots)
)
)

viewModel.eventHint.observe(
viewLifecycleOwner,
viewModel.getEventHintObserver(dots /*, TODO serial */) { expectedDots ->
Timber.i("Handle hint")
val toast = getString(R.string.practice_hint_template)
.format(expectedDots.spelling)
Toast.makeText(context, toast, Toast.LENGTH_LONG).show()
}
viewModel.eventHint.observe(
viewLifecycleOwner,
viewModel.getEventHintObserver(
dots, null, /*TODO serial */
getEventHintObserverBlock()
)
)

viewModel.eventPassHint.observe(
viewLifecycleOwner,
viewModel.getEventPassHintObserver(dots) {
Timber.i("Handle pass hint")
}
)
}
viewModel.eventPassHint.observe(
viewLifecycleOwner,
viewModel.getEventPassHintObserver(dots, getEventPassHintObserverBlock())
)

}.root
}
Loading

0 comments on commit 586d399

Please sign in to comment.