Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/eliminate toasts #279

Draft
wants to merge 10 commits into
base: dev
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import com.github.braillesystems.learnbraille.data.entities.MarkerSymbol
import com.github.braillesystems.learnbraille.data.entities.Material
import com.github.braillesystems.learnbraille.databinding.FragmentMarkerViewBinding
import com.github.braillesystems.learnbraille.res.showMarkerPrintRules
import com.github.braillesystems.learnbraille.ui.dotsMode
import com.github.braillesystems.learnbraille.ui.screens.AbstractFragmentWithHelp
import com.github.braillesystems.learnbraille.ui.screens.BrailleDotsInfo
import com.github.braillesystems.learnbraille.ui.screens.FragmentBinding
Expand Down Expand Up @@ -47,12 +46,9 @@ class MarkerViewFragment : AbstractFragmentWithHelp(R.string.browser_marker_view
val text = showMarkerPrintRules.getValue(m.data.type)
infoTextView.text = text
checkedAnnounce(text)

brailleDots.dotsState.display(m.data.brailleDots)
checkedToast(dotsMode(brailleDots.mode))
flipButton.setOnClickListener {
brailleDots.reflect().display(m.data.brailleDots)
checkedToast(dotsMode(brailleDots.mode))
}

}.root
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.github.braillesystems.learnbraille.ui.screens.browser

import android.animation.ObjectAnimator
import android.animation.TypeEvaluator
import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
Expand All @@ -10,15 +12,17 @@ import com.github.braillesystems.learnbraille.data.entities.Material
import com.github.braillesystems.learnbraille.data.entities.Symbol
import com.github.braillesystems.learnbraille.databinding.FragmentSymbolViewBinding
import com.github.braillesystems.learnbraille.res.captionRules
import com.github.braillesystems.learnbraille.ui.dotsMode
import com.github.braillesystems.learnbraille.ui.screens.AbstractFragmentWithHelp
import com.github.braillesystems.learnbraille.ui.screens.BrailleDotsInfo
import com.github.braillesystems.learnbraille.ui.screens.FragmentBinding
import com.github.braillesystems.learnbraille.ui.showPrint
import com.github.braillesystems.learnbraille.ui.views.BrailleDotsViewMode
import com.github.braillesystems.learnbraille.ui.views.display
import com.github.braillesystems.learnbraille.ui.views.dotsState
import com.github.braillesystems.learnbraille.utils.*
import com.github.braillesystems.learnbraille.utils.checkedAnnounce
import com.github.braillesystems.learnbraille.utils.getFragmentStringArg
import com.github.braillesystems.learnbraille.utils.getValue
import com.github.braillesystems.learnbraille.utils.parse

class SymbolViewFragment : AbstractFragmentWithHelp(R.string.browser_symbol_view_help) {

Expand Down Expand Up @@ -48,10 +52,8 @@ class SymbolViewFragment : AbstractFragmentWithHelp(R.string.browser_symbol_view
checkedAnnounce(showPrint(m.data))

brailleDots.dotsState.display(m.data.brailleDots)
checkedToast(dotsMode(brailleDots.mode))
flipButton.setOnClickListener {
brailleDots.reflect().display(m.data.brailleDots)
checkedToast(dotsMode(brailleDots.mode))
}

}.root
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package com.github.braillesystems.learnbraille.ui.screens.practice

import android.graphics.drawable.TransitionDrawable
import android.os.Bundle
import android.os.Vibrator
import android.view.*
import android.widget.Button
import android.widget.TextView
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
Expand All @@ -16,16 +18,18 @@ import com.github.braillesystems.learnbraille.databinding.FragmentCardBinding
import com.github.braillesystems.learnbraille.res.captionRules
import com.github.braillesystems.learnbraille.res.deckTagToName
import com.github.braillesystems.learnbraille.res.inputMarkerPrintRules
import com.github.braillesystems.learnbraille.ui.*
import com.github.braillesystems.learnbraille.ui.brailletrainer.BrailleTrainer
import com.github.braillesystems.learnbraille.ui.brailletrainer.BrailleTrainerSignalHandler
import com.github.braillesystems.learnbraille.ui.inputPrint
import com.github.braillesystems.learnbraille.ui.screens.*
import com.github.braillesystems.learnbraille.ui.showHintToast
import com.github.braillesystems.learnbraille.ui.views.*
import com.github.braillesystems.learnbraille.utils.*
import org.koin.android.ext.android.inject
import org.koin.core.parameter.parametersOf
import timber.log.Timber


class CardFragment : AbstractFragmentWithHelp(R.string.practice_help) {

// This value can change during ViewModel lifetime (ViewModelProvider does not call
Expand Down Expand Up @@ -86,7 +90,6 @@ class CardFragment : AbstractFragmentWithHelp(R.string.practice_help) {
binding.flipButton.setOnClickListener {
dotsState = binding.brailleDots.reflect().apply {
dotsState.subscribe(viewModel)
checkedToast(dotsMode(binding.brailleDots.mode))
if (viewModel.state == DotsChecker.State.HINT) {
viewModel.expectedDots?.let { display(it) }
}
Expand Down Expand Up @@ -114,16 +117,27 @@ class CardFragment : AbstractFragmentWithHelp(R.string.practice_help) {
viewModel.observeCheckedOnFly(
viewLifecycleOwner, { dotsState }, buzzer,
block = { title = title(viewModel) },
softBlock = ::showCorrectToast
softBlock = {
announceCorrect()
binding.constraintLayout.background = context?.let { ContextCompat.getDrawable(it, R.drawable.background_animation_green) }
val transition = binding.constraintLayout.background as TransitionDrawable
transition.startTransition(500)
transition.reverseTransition(500)
}
)

viewModel.observeEventIncorrect(
viewLifecycleOwner, { dotsState }, buzzer
) {
viewModel.symbol.value
?.let { showIncorrectToast(inputPrint(it)) }
?.let { announceIncorrect(inputPrint(it)) }
?: checkedToast(getString(R.string.input_loading))
title = title(viewModel)

binding.constraintLayout.background = context?.let { ContextCompat.getDrawable(it, R.drawable.background_animation_red) }
val transition = binding.constraintLayout.background as TransitionDrawable
transition.startTransition(500)
transition.reverseTransition(500)
}

viewModel.observeEventHint(
Expand Down Expand Up @@ -158,10 +172,7 @@ class CardFragment : AbstractFragmentWithHelp(R.string.practice_help) {
getString(R.string.practice_deck_name_disabled_template)
}
toast(
template.format(
deckTagToName.getValue(tag),
dotsMode(binding.brailleDots.mode)
)
template.format(deckTagToName.getValue(tag))
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import android.widget.TextView
import com.github.braillesystems.learnbraille.COURSE
import com.github.braillesystems.learnbraille.R
import com.github.braillesystems.learnbraille.data.entities.Step
import com.github.braillesystems.learnbraille.ui.dotsMode
import com.github.braillesystems.learnbraille.ui.screens.AbstractFragmentWithHelp
import com.github.braillesystems.learnbraille.ui.screens.BrailleDotsInfo
import com.github.braillesystems.learnbraille.ui.screens.FragmentBinding
Expand All @@ -18,7 +17,6 @@ import com.github.braillesystems.learnbraille.ui.screens.theory.getStepArg
import com.github.braillesystems.learnbraille.ui.screens.theory.toCurrentStep
import com.github.braillesystems.learnbraille.ui.screens.theory.toNextStep
import com.github.braillesystems.learnbraille.ui.screens.theory.toPrevStep
import com.github.braillesystems.learnbraille.utils.checkedToast
import com.github.braillesystems.learnbraille.utils.navigate
import com.github.braillesystems.learnbraille.utils.title

Expand Down Expand Up @@ -83,12 +81,6 @@ abstract class AbstractStepFragment(helpMsgId: HelpMsgId) : AbstractFragmentWith

protected open fun iniStepHelper() = Unit

protected fun toastDotsMode() {
binding.brailleDotsInfo?.view?.mode?.let {
checkedToast(dotsMode(it))
}
}

override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
inflater.inflate(
if (preferenceRepository.extendedAccessibilityEnabled) R.menu.steps_menu_hide
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ abstract class AbstractInputStepFragment(helpMsgId: HelpMsgId) : AbstractStepFra

val buzzer: Vibrator? = activity?.getSystemService()

toastDotsMode()
stepBinding.flipButton?.setOnClickListener {
dotsState = stepBinding.brailleDotsInfo!!.view.reflect().apply {
subscribe(View.OnClickListener {
viewModel.onSoftCheck()
userTouchedDots = true
})
toastDotsMode()
if (viewModel.state == DotsChecker.State.HINT) {
display(expectedDots)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ abstract class AbstractShowStepFragment(helpMsgId: HelpMsgId) : AbstractStepFrag
require(data is BaseShow)
stepBinding.brailleDotsInfo?.view?.dotsState?.display(data.brailleDots)
?: error("Show step should have braille dots")
toastDotsMode()
stepBinding.flipButton?.setOnClickListener {
stepBinding.brailleDotsInfo?.view?.reflect()?.display(data.brailleDots)
toastDotsMode()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import com.github.braillesystems.learnbraille.data.entities.spelling
import com.github.braillesystems.learnbraille.data.repository.PreferenceRepository
import com.github.braillesystems.learnbraille.ui.views.BrailleDotsViewMode.Reading
import com.github.braillesystems.learnbraille.ui.views.BrailleDotsViewMode.Writing
import com.github.braillesystems.learnbraille.utils.chainify
import com.github.braillesystems.learnbraille.utils.forEach
import com.github.braillesystems.learnbraille.utils.unreachable
import com.github.braillesystems.learnbraille.utils.*
import kotlinx.android.synthetic.main.braille_dots_view.view.*
import org.koin.core.KoinComponent
import org.koin.core.inject
Expand Down Expand Up @@ -96,18 +94,19 @@ class BrailleDotsView : ConstraintLayout, KoinComponent {
reflectChecks()
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
setBackgroundMode(mode)
} else {
Timber.w("Unable to set braille dots background due to low API level")
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
setTraversalMode(mode)
} else {
Timber.w("API level < 22, unable co control accessibility traversal order")
}

context.announce(
when (mode) {
Writing -> context.getString(R.string.braille_dots_mode_writing)
Reading -> context.getString(R.string.braille_dots_mode_reading)
}
)

this.mode = mode
}

Expand All @@ -117,27 +116,27 @@ class BrailleDotsView : ConstraintLayout, KoinComponent {
}

private fun setDescriptionMode(mode: BrailleDotsViewMode) {
when (mode) {
Writing -> forEach(
dotButton4 to R.string.braille_dot_1,
dotButton5 to R.string.braille_dot_2,
dotButton6 to R.string.braille_dot_3,
dotButton1 to R.string.braille_dot_4,
dotButton2 to R.string.braille_dot_5,
dotButton3 to R.string.braille_dot_6
) { (dotButton, id) ->
dotButton.contentDescription = context.getString(id)
}
Reading -> forEach(
dotButton1 to R.string.braille_dot_1,
dotButton2 to R.string.braille_dot_2,
dotButton3 to R.string.braille_dot_3,
dotButton4 to R.string.braille_dot_4,
dotButton5 to R.string.braille_dot_5,
dotButton6 to R.string.braille_dot_6
) { (dotButton, id) ->
dotButton.contentDescription = context.getString(id)
}
val dotsMapping = when (mode) {
Writing -> listOf(
Triple(dotButton4, R.string.braille_dot_1, R.string.braille_dot_1_text),
Triple(dotButton5, R.string.braille_dot_2, R.string.braille_dot_2_text),
Triple(dotButton6, R.string.braille_dot_3, R.string.braille_dot_3_text),
Triple(dotButton1, R.string.braille_dot_4, R.string.braille_dot_4_text),
Triple(dotButton2, R.string.braille_dot_5, R.string.braille_dot_5_text),
Triple(dotButton3, R.string.braille_dot_6, R.string.braille_dot_6_text)
)
Reading -> listOf(
Triple(dotButton1, R.string.braille_dot_1, R.string.braille_dot_1_text),
Triple(dotButton2, R.string.braille_dot_2, R.string.braille_dot_2_text),
Triple(dotButton3, R.string.braille_dot_3, R.string.braille_dot_3_text),
Triple(dotButton4, R.string.braille_dot_4, R.string.braille_dot_4_text),
Triple(dotButton5, R.string.braille_dot_5, R.string.braille_dot_5_text),
Triple(dotButton6, R.string.braille_dot_6, R.string.braille_dot_6_text)
)
}
dotsMapping.forEach { (dotButton, desc_id, caption_id) ->
dotButton.contentDescription = context.getString(desc_id)
dotButton.text = context.getString(caption_id)
}
}

Expand Down Expand Up @@ -179,14 +178,6 @@ class BrailleDotsView : ConstraintLayout, KoinComponent {
next.accessibilityTraversalAfter = prev.id
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun setBackgroundMode(mode: BrailleDotsViewMode) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This thing looked good to me. I think it is still useful

background = when (mode) {
Writing -> context.getDrawable(R.drawable.right_border)
Reading -> context.getDrawable(R.drawable.left_border)
}
}
}

val BrailleDotsView.dotsState: BrailleDotsState
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package com.github.braillesystems.learnbraille.utils

import android.animation.ArgbEvaluator
import android.animation.ObjectAnimator
import android.content.Context
import android.os.Vibrator
import android.view.View
import android.view.accessibility.AccessibilityEvent
import android.widget.Toast
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.fragment.app.Fragment
import com.github.braillesystems.learnbraille.LearnBrailleApplication
import com.github.braillesystems.learnbraille.R
Expand Down Expand Up @@ -68,6 +72,10 @@ fun Fragment.checkedAnnounce(
announce(announcement)
}

fun Fragment.announceCorrect() = announce(getString(R.string.input_correct))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is Messages.kt file for such kind of things


fun Fragment.announceIncorrect(hint: String = "") =
announce("${getString(R.string.input_incorrect)} $hint")

val Fragment.actionBar: ActionBar?
get() = (activity as AppCompatActivity).supportActionBar
Expand All @@ -88,6 +96,14 @@ fun Fragment.updateTitle(title: String) {
this.title = title
}

fun animateView(obj: View?, colorFrom: Int?, colorTo: Int?) {
val duration = 1000
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rathe be const val DURATION = 1000L

ObjectAnimator
.ofObject(obj, "backgroundColor", ArgbEvaluator(), colorFrom, colorTo, colorFrom)
.setDuration(duration.toLong())
.start()
}


fun <T> stringify(s: SerializationStrategy<T>, obj: T) = Json.stringify(s, obj)
fun <T> parse(d: DeserializationStrategy<T>, s: String) = Json.parse(d, s)
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/background_animation_green.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_blank" />
<item android:drawable="@drawable/background_gradient_green" />
</transition>
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/background_animation_red.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/background_blank" />
<item android:drawable="@drawable/background_gradient_red" />
</transition>
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/background_blank.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="@color/colorBackground" />
</shape>
</item>
</selector>
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/background_gradient_green.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="@color/colorBackground"
android:endColor="@color/lightGreen"
android:angle="90"/>
</shape>
</item>
</selector>
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/background_gradient_red.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="@color/colorBackground"
android:centerColor="@color/colorBackground"
android:endColor="@color/lightRed"
android:angle="90"/>
</shape>
</item>
</selector>
Loading