Skip to content

Commit

Permalink
braille-systems#278 change button animation to background animation
Browse files Browse the repository at this point in the history
  • Loading branch information
zuevval committed Oct 3, 2020
1 parent 625437c commit 757eda4
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ import com.github.braillesystems.learnbraille.res.showSymbolPrintRules
import com.github.braillesystems.learnbraille.ui.views.BrailleDotsViewMode
import com.github.braillesystems.learnbraille.utils.*

fun Fragment.showCorrectToast() = toast(getString(R.string.input_correct))

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

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

fun Fragment.announceIncorrect(hint: String = "") =
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.os.Vibrator
import android.view.*
import android.widget.Button
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.databinding.DataBindingUtil
Expand All @@ -19,13 +20,13 @@ 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.announceCorrect
import com.github.braillesystems.learnbraille.ui.announceIncorrect
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.announceIncorrect
import com.github.braillesystems.learnbraille.ui.announceCorrect
import com.github.braillesystems.learnbraille.ui.views.*
import com.github.braillesystems.learnbraille.utils.*
import com.google.android.material.button.MaterialButton
Expand Down Expand Up @@ -123,9 +124,9 @@ class CardFragment : AbstractFragmentWithHelp(R.string.practice_help) {
block = { title = title(viewModel) },
softBlock = {
announceCorrect()
val colorFrom = context?.let { ContextCompat.getColor(it, R.color.colorPrimary) }
val colorTo = context?.let { ContextCompat.getColor(it, R.color.colorGreen) }
animateButton(binding.nextButton, colorFrom, colorTo)
val colorFrom = context?.let { ContextCompat.getColor(it, R.color.colorBackground) }
val colorTo = context?.let { ContextCompat.getColor(it, R.color.lightGreen) }
animateView(binding.constraintLayout, colorFrom, colorTo)
}
)

Expand All @@ -137,9 +138,9 @@ class CardFragment : AbstractFragmentWithHelp(R.string.practice_help) {
?: checkedToast(getString(R.string.input_loading))
title = title(viewModel)

val colorFrom = context?.let { ContextCompat.getColor(it, R.color.colorPrimary) }
val colorTo = context?.let { ContextCompat.getColor(it, R.color.colorRed) }
animateButton(binding.nextButton, colorFrom, colorTo)
val colorFrom = context?.let { ContextCompat.getColor(it, R.color.colorBackground) }
val colorTo = context?.let { ContextCompat.getColor(it, R.color.lightRed) }
animateView(binding.constraintLayout, colorFrom, colorTo)
}

viewModel.observeEventHint(
Expand Down Expand Up @@ -185,13 +186,6 @@ class CardFragment : AbstractFragmentWithHelp(R.string.practice_help) {

}.root

private fun animateButton(button: MaterialButton, colorFrom: Int?, colorTo: Int?){
val duration = 1000
ObjectAnimator.ofObject(button, "backgroundColor", ArgbEvaluator(), colorFrom, colorTo, colorFrom)
.setDuration(duration.toLong())
.start()
}

private fun title(viewModel: CardViewModel? = null): String =
getString(R.string.practice_actionbar_title_template).run {
if (viewModel == null) format(0, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,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.checkedAnnounce
import com.github.braillesystems.learnbraille.utils.navigate
import com.github.braillesystems.learnbraille.utils.title

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package com.github.braillesystems.learnbraille.ui.screens.theory.steps.input

import android.animation.ArgbEvaluator
import android.animation.ObjectAnimator
import android.os.Vibrator
import android.view.View
import android.widget.Button
import androidx.core.content.ContextCompat
import androidx.core.content.getSystemService
import androidx.lifecycle.ViewModelProvider
import com.github.braillesystems.learnbraille.R
import com.github.braillesystems.learnbraille.data.entities.BaseInput
import com.github.braillesystems.learnbraille.data.entities.StepData
import com.github.braillesystems.learnbraille.ui.screens.*
import com.github.braillesystems.learnbraille.ui.screens.theory.steps.AbstractStepFragment
import com.github.braillesystems.learnbraille.ui.screens.theory.toNextStep
import com.github.braillesystems.learnbraille.ui.announceCorrect
import com.github.braillesystems.learnbraille.ui.showCorrectToast
import com.github.braillesystems.learnbraille.ui.showHintToast
import com.github.braillesystems.learnbraille.ui.announceIncorrect
import com.github.braillesystems.learnbraille.ui.showIncorrectToast
import com.github.braillesystems.learnbraille.ui.views.*
import com.github.braillesystems.learnbraille.utils.application
import com.github.braillesystems.learnbraille.utils.checkedBuzz
Expand Down Expand Up @@ -71,12 +66,7 @@ abstract class AbstractInputStepFragment(helpMsgId: HelpMsgId) : AbstractStepFra
viewModel.observeCheckedOnFly(
viewLifecycleOwner, { dotsState }, buzzer,
block = { toNextStep(step, markThisAsPassed = true) },
softBlock = {
announceCorrect()
val colorFrom = context?.let { ContextCompat.getColor(it, R.color.colorPrimary) }
val colorTo = context?.let { ContextCompat.getColor(it, R.color.colorGreen) }
animateButton(binding.rightButton, colorFrom, colorTo)
}
softBlock = { showCorrectToast() }
)

viewModel.observeEventHint(
Expand All @@ -90,14 +80,11 @@ abstract class AbstractInputStepFragment(helpMsgId: HelpMsgId) : AbstractStepFra
viewLifecycleOwner, { dotsState }
) {
fun notify() {
announceIncorrect(data)
toastIncorrect(data)
buzzer.checkedBuzz(
preferenceRepository.incorrectBuzzPattern,
preferenceRepository
)
val colorFrom = context?.let { ContextCompat.getColor(it, R.color.colorPrimary) }
val colorTo = context?.let { ContextCompat.getColor(it, R.color.colorRed) }
animateButton(binding.rightButton, colorFrom, colorTo)
}
if (userTouchedDots) notify()
else toNextStep(step, markThisAsPassed = false) { notify() }
Expand All @@ -114,23 +101,9 @@ abstract class AbstractInputStepFragment(helpMsgId: HelpMsgId) : AbstractStepFra
}
}

protected open fun announceIncorrect(data: StepData) {
announceIncorrect()
protected open fun toastIncorrect(data: StepData) {
showIncorrectToast()
}

protected open fun onPassHint(data: StepData) = Unit

private fun animateButton(button: Button?, colorFrom: Int?, colorTo: Int?) {
val duration = 1000
ObjectAnimator.ofObject(
button,
"backgroundColor",
ArgbEvaluator(),
colorFrom,
colorTo,
colorFrom
)
.setDuration(duration.toLong())
.start()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import com.github.braillesystems.learnbraille.res.captionRules
import com.github.braillesystems.learnbraille.res.inputSymbolPrintRules
import com.github.braillesystems.learnbraille.ui.screens.BrailleDotsInfo
import com.github.braillesystems.learnbraille.ui.screens.theory.steps.StepBinding
import com.github.braillesystems.learnbraille.ui.announceIncorrect
import com.github.braillesystems.learnbraille.ui.showIncorrectToast
import com.github.braillesystems.learnbraille.ui.views.BrailleDotsViewMode
import com.github.braillesystems.learnbraille.utils.checkedAnnounce
import com.github.braillesystems.learnbraille.utils.getValue
Expand Down Expand Up @@ -68,8 +68,8 @@ class InputSymbolFragment : AbstractInputStepFragment(R.string.lessons_help_inpu
return material.data.char
}

override fun announceIncorrect(data: StepData) {
announceIncorrect(inputSymbolPrintRules.getValue(data.char()))
override fun toastIncorrect(data: StepData) {
showIncorrectToast(inputSymbolPrintRules.getValue(data.char()))
}

override fun onPassHint(data: StepData) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,6 @@ 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 {
Expand Down Expand Up @@ -184,14 +178,6 @@ class BrailleDotsView : ConstraintLayout, KoinComponent {
next.accessibilityTraversalAfter = prev.id
}
}

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
private fun setBackgroundMode(mode: BrailleDotsViewMode) {
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 @@ -88,6 +92,14 @@ fun Fragment.updateTitle(title: String) {
this.title = title
}

fun animateView(obj: View?, colorFrom: Int?, colorTo: Int?){
val duration = 1000
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
18 changes: 0 additions & 18 deletions app/src/main/res/drawable/left_border.xml

This file was deleted.

18 changes: 0 additions & 18 deletions app/src/main/res/drawable/right_border.xml

This file was deleted.

3 changes: 1 addition & 2 deletions app/src/main/res/layout/braille_dots_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/constraintLayoutButtons"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:background="@drawable/left_border">
android:layout_height="wrap_content">

<com.github.braillesystems.learnbraille.ui.views.BrailleDotView
android:id="@+id/dotButton1"
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorGreen">#00de7a</color>
<color name="colorRed">#f70000</color>
<color name="lightGreen">#9ff57a</color>
<color name="lightRed">#ff8282</color>
<color name="colorPrimary">#263238</color>
<color name="colorSecondary">#9e3d9a</color>
<color name="colorOnPrimary">#ffffff</color>
Expand Down

0 comments on commit 757eda4

Please sign in to comment.