Skip to content

Commit 7471d82

Browse files
committed
clean up and add tests
1 parent 5b59db0 commit 7471d82

File tree

17 files changed

+105
-107
lines changed

17 files changed

+105
-107
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<activity
1616
android:name="com.willowtreeapps.namegame.MainActivity"
1717
android:label="@string/app_name"
18+
android:screenOrientation="portrait"
1819
android:theme="@style/AppTheme">
1920
<intent-filter>
2021
<action android:name="android.intent.action.MAIN" />

app/src/main/java/com/willowtreeapps/namegame/Utils.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ import android.animation.Animator
44
import android.animation.AnimatorListenerAdapter
55
import android.animation.AnimatorSet
66
import android.content.res.Resources
7+
import android.graphics.drawable.Drawable
8+
import com.bumptech.glide.load.DataSource
9+
import com.bumptech.glide.load.engine.GlideException
10+
import com.bumptech.glide.request.RequestListener
11+
import com.bumptech.glide.request.target.Target
712

813
val Int.dp: Int
914
get() = (this / Resources.getSystem().displayMetrics.density).toInt()
@@ -19,4 +24,20 @@ fun AnimatorSet.onComplete(after: () -> Unit) {
1924
after()
2025
}
2126
})
22-
}
27+
}
28+
29+
/*
30+
* Convenience function for executing lambda after the image is displayed
31+
*/
32+
fun <T: Any> GlideRequest<T>.onComplete(after: () -> Unit): GlideRequest<T> {
33+
return this.listener(object : RequestListener<T> {
34+
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<T>?, isFirstResource: Boolean): Boolean {
35+
return true
36+
}
37+
override fun onResourceReady(resource: T?, model: Any?, target: Target<T>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
38+
after()
39+
return false
40+
}
41+
42+
})
43+
}

app/src/main/java/com/willowtreeapps/namegame/store/QuestionFragment.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.willowtreeapps.namegame.store
33
import android.animation.AnimatorSet
44
import android.animation.ObjectAnimator
55
import android.graphics.Color
6-
import android.graphics.drawable.Drawable
76
import android.os.Bundle
87
import android.view.LayoutInflater
98
import android.view.View
@@ -18,13 +17,8 @@ import nl.dionsegijn.konfetti.models.Shape
1817
import nl.dionsegijn.konfetti.models.Size
1918
import kotlin.coroutines.CoroutineContext
2019
import android.widget.Button
21-
import androidx.annotation.ColorRes
2220
import androidx.core.content.res.ResourcesCompat
2321
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
24-
import com.bumptech.glide.load.DataSource
25-
import com.bumptech.glide.load.engine.GlideException
26-
import com.bumptech.glide.request.RequestListener
27-
import com.bumptech.glide.request.target.Target
2822
import com.willowtreeapps.common.ui.QuestionPresenter
2923
import com.willowtreeapps.namegame.*
3024
import kotlinx.coroutines.*
@@ -39,7 +33,7 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
3933

4034
private var restoreX: Float? = null
4135
private var restoreY: Float? = null
42-
var lastCorrectBtn: Button? = null
36+
private var lastCorrectBtn: Button? = null
4337
private var lastSelectedBtn: Button? = null
4438

4539
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
@@ -198,15 +192,11 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
198192
txt_results.text = title
199193
GlideApp.with(this@QuestionFragment).load(profileImageUrl)
200194
.transition(DrawableTransitionOptions.withCrossFade())
201-
.listener(object : RequestListener<Drawable> {
202-
override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
203-
return true
204-
}
205-
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
206-
showButtons()
207-
return false
208-
}
209-
})
195+
.onComplete {
196+
showButtons()
197+
txt_timer.visibility = View.VISIBLE
198+
presenter.profileImageIsVisible()
199+
}
210200
.into(imageView)
211201
button1.text = button1Text
212202
button2.text = button2Text
@@ -220,7 +210,7 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
220210
txt_timer.scaleX = 0f
221211
txt_timer.scaleY = 0f
222212
txt_timer.alpha = 1f
223-
txt_timer.text = viewState.timerText.toString()
213+
txt_timer.text = viewState.timerText
224214
txt_timer.animate()
225215
.scaleX(1f)
226216
.scaleY(1f)
@@ -252,7 +242,9 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
252242
.withEndAction {
253243
showWrongAnswer(viewState, isEndGame)
254244
txt_timer.animate().alpha(0f)
255-
.withEndAction { txt_timer.setTextColor(restoreColor) }
245+
.withEndAction {
246+
txt_timer.visibility = View.VISIBLE
247+
txt_timer.setTextColor(restoreColor) }
256248
}
257249

258250
}

app/src/main/res/layout/fragment_start.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
app:layout_constraintHorizontal_bias="0.5"
5959
app:layout_constraintStart_toStartOf="parent"
6060
app:layout_constraintTop_toTopOf="parent"
61-
app:layout_constraintVertical_bias="0.86" />
61+
app:layout_constraintVertical_bias="0.9" />
6262

6363
<TextView
6464
android:id="@+id/txt_error"

app/src/test/java/com/willowtreeapps/namegame/ReducersTest.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ class ReducersTest {
9898
assertEquals(Question.Status.INCORRECT, final.currentQuestion?.status)
9999
}
100100

101+
@Test
102+
fun `mark current round as TIMES when time is up`() {
103+
val initial = generateInitialTestState()
104+
105+
val final = reducer(initial, Actions.TimesUpAction())
106+
107+
assertEquals(Question.Status.TIMES_UP, final.currentQuestion?.status)
108+
}
109+
110+
@Test
111+
fun `decrement timer`() {
112+
val initial = generateInitialTestState()
113+
114+
val final = reducer(initial, Actions.DecrementCountDownAction())
115+
116+
assertEquals(initial.questionClock - 1, final.questionClock)
117+
}
118+
119+
@Test
120+
fun `start question timer with initial value`() {
121+
val initial = generateInitialTestState()
122+
123+
val final = reducer(initial, Actions.StartQuestionTimerAction(10))
124+
125+
assertEquals(10, final.questionClock)
126+
}
101127

102128
private fun generateInitialTestState(): AppState {
103129
val initialState = reducer(AppState(), Actions.FetchingProfilesSuccessAction(MockRepositoryFactory.getValidResponse()))

common/src/commonMain/kotlin/com/willowtreeapps/common/Actions.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import com.willowtreeapps.common.repo.Profile
55

66
sealed class Actions : Action {
77

8-
class GameStartedAction()
9-
108
class FetchingProfilesStartedAction
119
class FetchingProfilesSuccessAction(val profiles: List<Profile>)
1210
class FetchingProfilesFailedAction(val message: String)
@@ -21,14 +19,6 @@ sealed class Actions : Action {
2119
class ResetGameStateAction
2220

2321
class StartQuestionTimerAction(val initialValue: Int)
24-
25-
class StartTimerAction(val name: String,
26-
val intervalMs: Long,
27-
val initialValue: Int = 5,
28-
val work: () -> Unit)
29-
30-
class StopTimerAction
31-
3222
class DecrementCountDownAction
3323
class TimesUpAction
3424

common/src/commonMain/kotlin/com/willowtreeapps/common/GameEngine.kt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.beyondeye.reduks.middlewares.applyMiddleware
55
import com.beyondeye.reduks.middlewares.thunkMiddleware
66
import com.willowtreeapps.common.middleware.NavigationMiddleware
77
import com.willowtreeapps.common.middleware.Navigator
8-
import com.willowtreeapps.common.middleware.TimerMiddleware
98
import com.willowtreeapps.common.middleware.ViewEffectsMiddleware
109
import com.willowtreeapps.common.util.VibrateUtil
1110
import kotlin.coroutines.CoroutineContext
@@ -15,16 +14,14 @@ class GameEngine(navigator: Navigator, application: Any = Any(),
1514
uiContext: CoroutineContext) {
1615
private val navigationMiddleware = NavigationMiddleware(navigator)
1716
private val viewEffectsMiddleware = ViewEffectsMiddleware()
18-
private val timerMiddleware = TimerMiddleware(uiContext)
1917
private val presenterFactory by lazy { PresenterFactory(this, networkContext) }
2018
val vibrateUtil = VibrateUtil(application)
2119

2220
val appStore by lazy {
2321
SimpleStore(AppState.INITIAL_STATE, reducer)
2422
.applyMiddleware(::thunkMiddleware,
2523
viewEffectsMiddleware::dispatch,
26-
navigationMiddleware::dispatch,
27-
timerMiddleware::dispatch)
24+
navigationMiddleware::dispatch)
2825
}
2926

3027
fun attachView(view: View): Presenter<out View?> = presenterFactory.attachView(view)

common/src/commonMain/kotlin/com/willowtreeapps/common/NetworkThunks.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ class NetworkThunks(private val networkContext: CoroutineContext,
2626
if (result.isSuccessful) {
2727
Logger.d("Success")
2828
store.dispatch(Actions.FetchingProfilesSuccessAction(result.response!!))
29-
store.dispatch(timerThunks.startCountDownTimer(5))
3029
} else {
3130
Logger.d("Failure")
3231
store.dispatch(Actions.FetchingProfilesFailedAction(result.message!!))

common/src/commonMain/kotlin/com/willowtreeapps/common/Reducers.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ val reducer = ReducerFn<AppState> { state, action ->
3333
is NextQuestionAction -> state.copy(waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1)
3434
is GameCompleteAction -> state.copy(waitingForResultsTap = true, waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1)
3535
is StartOverAction, is ResetGameStateAction -> AppState.INITIAL_STATE
36-
is StartTimerAction -> state.copy(questionClock = action.initialValue)
3736
is StartQuestionTimerAction -> state.copy(questionClock = action.initialValue)
3837
is DecrementCountDownAction -> state.copy(questionClock = state.questionClock - 1)
3938
is TimesUpAction -> {

common/src/commonMain/kotlin/com/willowtreeapps/common/TimerThunks.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import kotlin.coroutines.CoroutineContext
88

99
class TimerThunks(private val networkContext: CoroutineContext, val store: Store<AppState>) : CoroutineScope {
1010
override val coroutineContext = networkContext + Job()
11-
var timerJob: Job? = null
11+
private var timerJob: Job? = null
1212

1313
/**
1414
* Starts a countdown timer that executes every X ms as specified by the action.

0 commit comments

Comments
 (0)