@@ -15,21 +15,21 @@ import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
1515import com.willowtreeapps.common.QuestionViewState
1616import com.willowtreeapps.common.ui.QuestionView
1717import kotlinx.android.synthetic.main.fragment_question.*
18- import kotlinx.coroutines.CoroutineScope
19- import kotlinx.coroutines.Dispatchers
2018import nl.dionsegijn.konfetti.models.Shape
2119import nl.dionsegijn.konfetti.models.Size
22- import java.lang.IllegalStateException
2320import kotlin.coroutines.CoroutineContext
2421import android.widget.Button
2522import androidx.annotation.ColorRes
23+ import androidx.core.content.res.ResourcesCompat
24+ import androidx.interpolator.view.animation.FastOutSlowInInterpolator
2625import com.willowtreeapps.common.ui.QuestionPresenter
2726import com.willowtreeapps.namegame.*
27+ import kotlinx.coroutines.*
2828
2929
3030class QuestionFragment : Fragment (), CoroutineScope, QuestionView, MainActivity.IOnBackPressed {
3131
32- private var presenter: QuestionPresenter ? = null
32+ private lateinit var presenter: QuestionPresenter
3333
3434 override val coroutineContext: CoroutineContext
3535 get() = Dispatchers .Main
@@ -51,26 +51,26 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
5151
5252 override fun onPause () {
5353 super .onPause()
54- NameGameApp .gameEngine().detachView(presenter !! )
54+ NameGameApp .gameEngine().detachView(this )
5555 }
5656
5757 override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
5858 initViews()
5959 }
6060
6161 private fun initViews () {
62- button1.setOnClickListener { presenter? .namePicked(button1.text.toString()) }
63- button2.setOnClickListener { presenter? .namePicked(button2.text.toString()) }
64- button3.setOnClickListener { presenter? .namePicked(button3.text.toString()) }
65- button4.setOnClickListener { presenter? .namePicked(button4.text.toString()) }
66- btn_next.setOnClickListener { presenter? .nextTapped() }
67- btn_end_game.setOnClickListener { presenter? .endGameTapped() }
62+ button1.setOnClickListener { presenter.namePicked(button1.text.toString()) }
63+ button2.setOnClickListener { presenter.namePicked(button2.text.toString()) }
64+ button3.setOnClickListener { presenter.namePicked(button3.text.toString()) }
65+ button4.setOnClickListener { presenter.namePicked(button4.text.toString()) }
66+ btn_next.setOnClickListener { presenter.nextTapped() }
67+ btn_end_game.setOnClickListener { presenter.endGameTapped() }
6868 }
6969
7070
7171 override fun onBackPressed (): Boolean {
72- NameGameApp .gameEngine().detachView(presenter !! )
73- presenter? .onBackPressed()
72+ NameGameApp .gameEngine().detachView(this )
73+ presenter.onBackPressed()
7474 return false
7575 }
7676
@@ -82,25 +82,16 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
8282 setProfileAndFadeIn(viewState)
8383 }
8484 }
85- }
86-
87-
88- override fun showCorrectAnswer (viewState : QuestionViewState ) {
89- hideButtonsShowNext(viewState, false )
90- celebrate()
91- }
9285
93- override fun showWrongAnswer (viewState : QuestionViewState ) {
94- wrongShakeAnimation(viewState) { hideButtonsShowNext(viewState, false ) }
9586 }
9687
97- override fun showCorrectAnswerEndGame (viewState : QuestionViewState ) {
98- hideButtonsShowNext(viewState, true )
88+ override fun showCorrectAnswer (viewState : QuestionViewState , isEndGame : Boolean ) {
89+ hideButtonsShowNext(viewState, isEndGame )
9990 celebrate()
10091 }
10192
102- override fun showWrongAnswerEndGame (viewState : QuestionViewState ) {
103- wrongShakeAnimation(viewState) { hideButtonsShowNext(viewState, true ) }
93+ override fun showWrongAnswer (viewState : QuestionViewState , isEndGame : Boolean ) {
94+ wrongShakeAnimation(viewState) { hideButtonsShowNext(viewState, isEndGame ) }
10495 }
10596
10697 private val showButtonsAnimatorSet by lazy {
@@ -115,19 +106,23 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
115106
116107 private fun wrongShakeAnimation (viewState : QuestionViewState , after : () -> Unit ) {
117108 val selectedBtn = getBtnByNum(viewState.selectedBtnNum)
118- selectedBtn.isSelected = true
119- val animScaleX = ObjectAnimator .ofFloat(selectedBtn, View .SCALE_X , 3F , 0.5F , 1F )
120- val animScaleY = ObjectAnimator .ofFloat(selectedBtn, View .SCALE_Y , 3F , 0.5F , 1F )
121- val upSet = AnimatorSet ()
122- upSet.playTogether(animScaleX, animScaleY)
123- upSet.interpolator = BounceInterpolator ()
124- upSet.duration = 500
125- upSet.addListener(object : AnimatorListenerAdapter () {
126- override fun onAnimationEnd (animation : Animator ? ) {
127- after()
128- }
129- })
130- upSet.start()
109+ if (selectedBtn != null ) {
110+ selectedBtn.isSelected = true
111+ val animScaleX = ObjectAnimator .ofFloat(selectedBtn, View .SCALE_X , 3F , 0.5F , 1F )
112+ val animScaleY = ObjectAnimator .ofFloat(selectedBtn, View .SCALE_Y , 3F , 0.5F , 1F )
113+ val upSet = AnimatorSet ()
114+ upSet.playTogether(animScaleX, animScaleY)
115+ upSet.interpolator = BounceInterpolator ()
116+ upSet.duration = 500
117+ upSet.addListener(object : AnimatorListenerAdapter () {
118+ override fun onAnimationEnd (animation : Animator ? ) {
119+ after()
120+ }
121+ })
122+ upSet.start()
123+ } else {
124+ after()
125+ }
131126 }
132127
133128 /* *
@@ -157,8 +152,8 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
157152 set
158153 }
159154 }
160- restoreX = correctBtn.x
161- restoreY = correctBtn.y
155+ restoreX = correctBtn? .x
156+ restoreY = correctBtn? .y
162157 lastCorrectBtn = correctBtn
163158 lastSelectedBtn = selectedBtn
164159
@@ -170,15 +165,15 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
170165 val set = AnimatorSet ()
171166 set.playTogether(anim1, anim2, anim3, anim4)
172167 set.onComplete {
173- val btn = if (isEndGame) {
174- btn_end_game
175- } else {
176- btn_next
177- }
178- btn.visibility = View .VISIBLE
179- btn.alpha = 0F
180- btn.animate().alpha(1f )
168+ val btn = if (isEndGame) {
169+ btn_end_game
170+ } else {
171+ btn_next
181172 }
173+ btn.visibility = View .VISIBLE
174+ btn.alpha = 0F
175+ btn.animate().alpha(1f )
176+ }
182177 set.start()
183178 }
184179
@@ -217,6 +212,47 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
217212 }
218213 }
219214
215+ override fun setTimerText (viewState : QuestionViewState ) {
216+ activity?.runOnUiThread {
217+ txt_timer.scaleX = 0f
218+ txt_timer.scaleY = 0f
219+ txt_timer.alpha = 1f
220+ txt_timer.text = viewState.questionTime.toString()
221+ txt_timer.animate()
222+ .scaleX(1f )
223+ .scaleY(1f )
224+ .setInterpolator(FastOutSlowInInterpolator ())
225+ .setDuration(500 )
226+ .withEndAction {
227+ if (txt_timer != null ) {
228+ txt_timer.animate()
229+ .scaleX(0f )
230+ .scaleY(0f )
231+ .duration = 500
232+ }
233+ }
234+ }
235+ }
236+
237+ override fun showTimesUp (viewState : QuestionViewState , isEndGame : Boolean ) {
238+ activity?.runOnUiThread {
239+ txt_timer.scaleX = 0f
240+ txt_timer.scaleY = 0f
241+ txt_timer.text = " TIMES UP!!"
242+ txt_timer.setTextColor(ResourcesCompat .getColor(context?.resources!! , R .color.red, null ))
243+ txt_timer.animate()
244+ .scaleX(1f )
245+ .scaleY(1f )
246+ .setInterpolator(FastOutSlowInInterpolator ())
247+ .setDuration(500 )
248+ .withEndAction {
249+ showWrongAnswer(viewState, isEndGame)
250+ txt_timer.animate().alpha(0f )
251+ }
252+
253+ }
254+ }
255+
220256 private fun celebrate () {
221257 view_konfetti.build()
222258 .addColors(Color .YELLOW , Color .GREEN , Color .MAGENTA )
@@ -230,12 +266,12 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
230266 .burst(200 )
231267 }
232268
233- private fun getBtnByNum (num : Int ): Button = when (num) {
269+ private fun getBtnByNum (num : Int ): Button ? = when (num) {
234270 1 -> button1
235271 2 -> button2
236272 3 -> button3
237273 4 -> button4
238- else -> throw IllegalStateException (" Invalid correct button index" )
274+ else -> null // throw IllegalStateException("Invalid button index")
239275 }
240276
241277
0 commit comments