Skip to content

Commit 7e02334

Browse files
authored
Merge pull request #18 from patjackson52/feature/generic
make game generic
2 parents 321cd58 + 593b4ac commit 7e02334

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+700
-1941
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GameResultsFragment : Fragment(), CoroutineScope, GameResultsView, MainAct
4949
}
5050

5151
override fun showResults(viewState: GameResultsViewState) {
52-
txt_results.text = viewState.resultsText
52+
txt_questionTitle.text = viewState.resultsText
5353
txt_message.text = viewState.messageText
5454
}
5555

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,8 +190,8 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
190190

191191
private fun setProfileAndFadeIn(viewState: QuestionViewState) {
192192
with(viewState) {
193-
txt_results.text = title
194-
GlideApp.with(this@QuestionFragment).load(profileImageUrl)
193+
txt_question_title.text = title
194+
GlideApp.with(this@QuestionFragment).load(itemImageUrl)
195195
.transition(DrawableTransitionOptions.withCrossFade())
196196
.onComplete {
197197
showButtons()

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
88
import androidx.fragment.app.DialogFragment
9+
import com.willowtreeapps.common.QuestionCategoryId
910
import com.willowtreeapps.common.SettingsViewState
1011
import com.willowtreeapps.common.ui.SettingsPresenter
1112
import com.willowtreeapps.common.ui.SettingsView
@@ -41,6 +42,13 @@ class SettingsDialogFragment: DialogFragment(), SettingsView {
4142
numberPicker.setOnValueChangedListener { _, _, newVal ->
4243
presenter?.numQuestionsChanged(newVal)
4344
}
45+
46+
categoryPicker.displayedValues = QuestionCategoryId.displayNameList.toTypedArray()
47+
categoryPicker.maxValue = 0
48+
categoryPicker.maxValue = QuestionCategoryId.displayNameList.toTypedArray().size - 1
49+
categoryPicker.setOnValueChangedListener { _, _, newVal ->
50+
presenter?.categoryChanged(QuestionCategoryId.fromOrdinal(newVal))
51+
}
4452
btn_ok.setOnClickListener { dismiss() }
4553
}
4654

@@ -56,5 +64,6 @@ class SettingsDialogFragment: DialogFragment(), SettingsView {
5664

5765
override fun showSettings(viewState: SettingsViewState) {
5866
numberPicker.value = viewState.numQuestions
67+
categoryPicker.value = QuestionCategoryId.values().indexOf(viewState.categoryId)
5968
}
6069
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
<TextView
16-
android:id="@+id/txt_results"
16+
android:id="@+id/txt_questionTitle"
1717
android:layout_width="match_parent"
1818
android:layout_height="wrap_content"
1919
android:layout_marginTop="64dp"

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
tools:context="com.willowtreeapps.namegame.MainActivity">
1313

1414
<TextView
15-
android:id="@+id/txt_results"
15+
android:id="@+id/txt_question_title"
1616
android:layout_width="match_parent"
1717
android:layout_height="wrap_content"
1818
android:layout_marginTop="64dp"
@@ -30,7 +30,7 @@
3030
app:layout_constraintEnd_toEndOf="parent"
3131
app:layout_constraintHorizontal_bias="0.5"
3232
app:layout_constraintStart_toStartOf="parent"
33-
app:layout_constraintTop_toBottomOf="@+id/txt_results"
33+
app:layout_constraintTop_toBottomOf="@+id/txt_question_title"
3434
tools:srcCompat="@tools:sample/avatars" />
3535

3636
<Button
@@ -101,7 +101,7 @@
101101
android:layout_width="wrap_content"
102102
android:layout_height="wrap_content"
103103
android:layout_marginBottom="64dp"
104-
android:text="Next Tree"
104+
android:text="Next"
105105
android:visibility="gone"
106106
app:layout_constraintBottom_toBottomOf="parent"
107107
app:layout_constraintEnd_toStartOf="@+id/button2"

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

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
android:layout_width="300dp"
66
android:layout_height="400dp"
77
android:minWidth="300dp"
8-
android:minHeight="400dp"
9-
>
8+
android:minHeight="400dp">
109

1110
<Button
1211
android:id="@+id/btn_ok"
@@ -24,21 +23,40 @@
2423
android:id="@+id/numberPicker"
2524
android:layout_width="wrap_content"
2625
android:layout_height="wrap_content"
27-
android:layout_marginStart="8dp"
28-
android:layout_marginTop="24dp"
29-
android:layout_marginEnd="8dp"
26+
android:layout_marginTop="32dp"
3027
app:layout_constraintEnd_toEndOf="parent"
3128
app:layout_constraintHorizontal_bias="0.5"
29+
app:layout_constraintStart_toEndOf="@+id/categoryPicker"
30+
app:layout_constraintTop_toTopOf="parent" />
31+
32+
<NumberPicker
33+
android:id="@+id/categoryPicker"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:layout_marginTop="32dp"
37+
app:layout_constraintEnd_toStartOf="@+id/numberPicker"
38+
app:layout_constraintHorizontal_bias="0.5"
3239
app:layout_constraintStart_toStartOf="parent"
3340
app:layout_constraintTop_toTopOf="parent" />
3441

3542
<TextView
3643
android:id="@+id/txt_num_questions"
37-
android:layout_width="wrap_content"
44+
android:layout_width="70dp"
3845
android:layout_height="wrap_content"
3946
android:layout_marginTop="8dp"
47+
android:gravity="center"
4048
android:text="Number of Questions"
4149
app:layout_constraintEnd_toEndOf="@+id/numberPicker"
4250
app:layout_constraintStart_toStartOf="@+id/numberPicker"
4351
app:layout_constraintTop_toBottomOf="@+id/numberPicker" />
52+
53+
<TextView
54+
android:id="@+id/txt_num_questions2"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:layout_marginTop="8dp"
58+
android:text="Category"
59+
app:layout_constraintEnd_toEndOf="@+id/categoryPicker"
60+
app:layout_constraintStart_toStartOf="@+id/categoryPicker"
61+
app:layout_constraintTop_toBottomOf="@+id/categoryPicker" />
4462
</androidx.constraintlayout.widget.ConstraintLayout>

app/src/main/res/values/styles.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
1616
<item name="android:statusBarColor" tools:targetApi="lollipop">@android:color/transparent
1717
</item>
18+
<item name="android:buttonStyle">@style/ChoiceButton</item>
1819
</style>
1920

2021
<style name="AppTheme.TransparentTheme">

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

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.willowtreeapps.namegame
22

33
import com.willowtreeapps.common.*
4-
import com.willowtreeapps.common.boundary.displayName
54
import com.willowtreeapps.common.repo.MockRepositoryFactory
5+
import com.willowtreeapps.common.repo.ProfileItemRepository
6+
import kotlinx.coroutines.runBlocking
67
import org.junit.Assert.*
78
import org.junit.Test
89
import java.lang.Exception
@@ -32,34 +33,35 @@ class ReducersTest {
3233

3334
@Test
3435
fun `generate N distinct random rounds`() {
35-
val profiles = MockRepositoryFactory.getValidResponse()
36-
val rounds = generateRounds(profiles, 10)
36+
37+
val itemHolder = runBlocking { ProfileItemRepository(MockRepositoryFactory().success()).fetchItems()}.response
38+
val rounds = generateRounds(itemHolder?.items!!, 10)
3739

3840
assertEquals(10, rounds.size)
39-
assertEquals(10, rounds.distinctBy { it.profileId }.size)
41+
assertEquals(10, rounds.distinctBy { it.itemId }.size)
4042
}
4143

4244
@Test
4345
fun `isLoadingProfiles set true`() {
44-
val final = reducer(generateInitialTestState(), Actions.FetchingProfilesStartedAction())
46+
val final = reducer(generateInitialTestState(), Actions.FetchingItemsStartedAction())
4547

46-
assertTrue(final.isLoadingProfiles)
48+
assertTrue(final.isLoadingItems)
4749
}
4850

4951
@Test
5052
fun `isLoadingProfiles set false on success`() {
51-
val initial = generateInitialTestState().copy(isLoadingProfiles = true)
52-
val final = reducer(initial, Actions.FetchingProfilesSuccessAction(MockRepositoryFactory.getValidResponse()))
53+
val initial = generateInitialTestState().copy(isLoadingItems = true)
54+
val final = reducer(initial, Actions.FetchingItemsSuccessAction(runBlocking { ProfileItemRepository(MockRepositoryFactory().success()).fetchItems()}.response!!))
5355

54-
assertFalse(final.isLoadingProfiles)
56+
assertFalse(final.isLoadingItems)
5557
}
5658

5759
@Test
5860
fun `isLoadingProfiles set false on failure`() {
59-
val initial = generateInitialTestState().copy(isLoadingProfiles = true)
60-
val final = reducer(initial, Actions.FetchingProfilesFailedAction("Test failure"))
61+
val initial = generateInitialTestState().copy(isLoadingItems = true)
62+
val final = reducer(initial, Actions.FetchingItemsFailedAction("Test failure"))
6163

62-
assertFalse(final.isLoadingProfiles)
64+
assertFalse(final.isLoadingItems)
6365
}
6466

6567
@Test
@@ -81,7 +83,7 @@ class ReducersTest {
8183
@Test
8284
fun `mark current round as CORRECT when name matches`() {
8385
val initial = generateInitialTestState()
84-
val answer = initial.currentQuestionProfile().displayName()
86+
val answer = initial.currentQuestionItem().displayName()
8587

8688
val final = reducer(initial, Actions.NamePickedAction(answer))
8789

@@ -136,7 +138,7 @@ class ReducersTest {
136138
}
137139

138140
private fun generateInitialTestState(): AppState {
139-
val initialState = reducer(AppState(), Actions.FetchingProfilesSuccessAction(MockRepositoryFactory.getValidResponse()))
141+
val initialState = reducer(AppState(), Actions.FetchingItemsSuccessAction(runBlocking { ProfileItemRepository(MockRepositoryFactory().success()).fetchItems()}.response!!))
140142
return initialState
141143
}
142144
}

app/src/test/java/com/willowtreeapps/namegame/ExampleUnitTest.kt renamed to app/src/test/java/com/willowtreeapps/namegame/RepositoriesTest.kt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import kotlinx.coroutines.*
77
import kotlinx.serialization.json.Json
88
import kotlinx.serialization.parse
99
import org.junit.Assert.*
10+
import org.junit.Ignore
1011
import org.junit.Test
1112

1213

@@ -15,29 +16,37 @@ import org.junit.Test
1516
*
1617
* See [testing documentation](http://d.android.com/tools/testing).
1718
*/
18-
class ExampleUnitTest {
19+
class RepositoriesTest {
1920
private val repo = KtorProfilesRepository()
2021

22+
@Ignore
2123
@Test
2224
fun fetchProfiles() {
23-
GlobalScope.launch {
24-
async {
25-
val result = repo.profiles()
26-
if (result.isSuccessful) {
27-
assertNotNull(result.response)
28-
assertTrue(result.response?.isNotEmpty() ?: false)
29-
} else {
30-
fail("Failure response from profiles repo: ${result.message}")
31-
}
32-
}.await()
25+
val result = runBlocking { repo.profiles() }
26+
if (result.isSuccessful) {
27+
assertNotNull(result.response)
28+
assertTrue(result.response?.isNotEmpty() ?: false)
29+
} else {
30+
fail("Failure response from cats repo: ${result.message}")
3331
}
34-
Thread.sleep(5000)
3532
}
3633

3734
@Test
3835
fun deserializeProfilesResponse() {
3936
val response = Json.nonstrict.parse(ProfileListHolderSerializer(), MockRepositoryFactory.VALID_RESPONSE_JSON)
4037
assertNotNull(response.profiles)
4138
}
39+
40+
@Ignore
41+
@Test
42+
fun fetchDogs() {
43+
val result = runBlocking { repo.profiles() }
44+
if (result.isSuccessful) {
45+
assertNotNull(result.response)
46+
assertTrue(result.response?.isNotEmpty() ?: false)
47+
} else {
48+
fail("Failure response from cats repo: ${result.message}")
49+
}
50+
}
4251
}
4352

common/src/androidMain/kotlin/com/willowtreeapps/common/PlatformDispatcher.kt

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

88
actual object PlatformDispatcher : CoroutineDispatcher() {
99
override fun dispatch(context: CoroutineContext, block: Runnable) {
10-
Dispatchers.Main.dispatch(context, block)
10+
Dispatchers.IO.dispatch(context, block)
1111
}
1212
}

0 commit comments

Comments
 (0)