Skip to content

Commit 94ef10b

Browse files
committed
clean up
1 parent 6e50462 commit 94ef10b

File tree

14 files changed

+53
-60
lines changed

14 files changed

+53
-60
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
191191
private fun setProfileAndFadeIn(viewState: QuestionViewState) {
192192
with(viewState) {
193193
txt_results.text = title
194-
GlideApp.with(this@QuestionFragment).load(profileImageUrl)
194+
GlideApp.with(this@QuestionFragment).load(itemImageUrl)
195195
.transition(DrawableTransitionOptions.withCrossFade())
196196
.onComplete {
197197
showButtons()

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@
4141

4242
<TextView
4343
android:id="@+id/txt_num_questions"
44-
android:layout_width="wrap_content"
44+
android:layout_width="70dp"
4545
android:layout_height="wrap_content"
4646
android:layout_marginTop="8dp"
47+
android:gravity="center"
4748
android:text="Number of Questions"
4849
app:layout_constraintEnd_toEndOf="@+id/numberPicker"
4950
app:layout_constraintStart_toStartOf="@+id/numberPicker"

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,30 +38,30 @@ class ReducersTest {
3838
val rounds = generateRounds(profiles!!, 10)
3939

4040
assertEquals(10, rounds.size)
41-
assertEquals(10, rounds.distinctBy { it.profileId }.size)
41+
assertEquals(10, rounds.distinctBy { it.itemId }.size)
4242
}
4343

4444
@Test
4545
fun `isLoadingProfiles set true`() {
46-
val final = reducer(generateInitialTestState(), Actions.FetchingProfilesStartedAction())
46+
val final = reducer(generateInitialTestState(), Actions.FetchingItemsStartedAction())
4747

48-
assertTrue(final.isLoadingProfiles)
48+
assertTrue(final.isLoadingItems)
4949
}
5050

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

56-
assertFalse(final.isLoadingProfiles)
56+
assertFalse(final.isLoadingItems)
5757
}
5858

5959
@Test
6060
fun `isLoadingProfiles set false on failure`() {
61-
val initial = generateInitialTestState().copy(isLoadingProfiles = true)
62-
val final = reducer(initial, Actions.FetchingProfilesFailedAction("Test failure"))
61+
val initial = generateInitialTestState().copy(isLoadingItems = true)
62+
val final = reducer(initial, Actions.FetchingItemsFailedAction("Test failure"))
6363

64-
assertFalse(final.isLoadingProfiles)
64+
assertFalse(final.isLoadingItems)
6565
}
6666

6767
@Test
@@ -138,7 +138,7 @@ class ReducersTest {
138138
}
139139

140140
private fun generateInitialTestState(): AppState {
141-
val initialState = reducer(AppState(), Actions.FetchingProfilesSuccessAction(runBlocking { ProfileItemRepository(MockRepositoryFactory().success()).fetchItems()}.response!!))
141+
val initialState = reducer(AppState(), Actions.FetchingItemsSuccessAction(runBlocking { ProfileItemRepository(MockRepositoryFactory().success()).fetchItems()}.response!!))
142142
return initialState
143143
}
144144
}

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

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

66
sealed class Actions : Action {
77

8-
class FetchingProfilesStartedAction
9-
class FetchingProfilesSuccessAction(val profiles: List<Item>)
10-
class FetchingProfilesFailedAction(val message: String)
8+
class FetchingItemsStartedAction
9+
class FetchingItemsSuccessAction(val items: List<Item>)
10+
class FetchingItemsFailedAction(val message: String)
1111

1212
class NamePickedAction(val name: String)
1313

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

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

3-
data class AppState(val isLoadingProfiles: Boolean = false,
3+
data class AppState(val isLoadingItems: Boolean = false,
44
val items: List<Item> = listOf(),
5-
val errorLoadingProfiles: Boolean = false,
5+
val errorLoadingItems: Boolean = false,
66
val errorMsg: String = "",
77
val currentQuestionIndex: Int = 0,
88
val waitingForNextQuestion: Boolean = false,
@@ -23,20 +23,20 @@ data class AppState(val isLoadingProfiles: Boolean = false,
2323
else
2424
null
2525

26-
fun getItem(id: ProfileId?) = items.find { it.id == id }
26+
fun getItem(id: ItemId?) = items.find { it.id == id }
2727

28-
fun currentQuestionItem() = getItem(currentQuestion?.profileId)!!
28+
fun currentQuestionItem() = getItem(currentQuestion?.itemId)!!
2929

3030
fun isGameComplete(): Boolean = currentQuestionIndex >= questions.size || (currentQuestionIndex == questions.size - 1 && questions[currentQuestionIndex].status != Question.Status.UNANSWERED)
3131

3232
val numCorrect: Int
3333
get() = questions.count { it.status == Question.Status.CORRECT }
3434
}
3535

36-
inline class ProfileId(val id: String)
36+
inline class ItemId(val id: String)
3737

38-
data class Question(val profileId: ProfileId,
39-
val choices: List<ProfileId>,
38+
data class Question(val itemId: ItemId,
39+
val choices: List<ItemId>,
4040
val status: Status = Status.UNANSWERED,
4141
val answerName: String? = null) {
4242
enum class Status {
@@ -47,7 +47,7 @@ data class Question(val profileId: ProfileId,
4747
}
4848
}
4949

50-
data class Item(val id: ProfileId,
50+
data class Item(val id: ItemId,
5151
val imageUrl: String,
5252
val firstName: String,
5353
val lastName: String) {

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class NetworkThunks(private val networkContext: CoroutineContext,
2626
val repo = repoForCategory(categoryId)
2727
Logger.d("Fetching StoreInfo and Feed")
2828
launch {
29-
store.dispatch(Actions.FetchingProfilesStartedAction())
29+
store.dispatch(Actions.FetchingItemsStartedAction())
3030
val result = repo.fetchItems()
3131
if (result.isSuccessful) {
3232
Logger.d("Success")
33-
store.dispatch(Actions.FetchingProfilesSuccessAction(result.response!!))
33+
store.dispatch(Actions.FetchingItemsSuccessAction(result.response!!))
3434
} else {
3535
Logger.d("Failure")
36-
store.dispatch(Actions.FetchingProfilesFailedAction(result.message!!))
36+
store.dispatch(Actions.FetchingItemsFailedAction(result.message!!))
3737
}
3838
Logger.d("DONE")
3939
}

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import kotlin.random.Random
1111
*/
1212
fun reducer(state: AppState, action: Any): AppState =
1313
when (action) {
14-
is FetchingProfilesStartedAction -> state.copy(isLoadingProfiles = true)
15-
is FetchingProfilesSuccessAction -> {
16-
val rounds = generateRounds(action.profiles, state.settings.numQuestions)
17-
state.copy(isLoadingProfiles = false, items = action.profiles, questions = rounds)
14+
is FetchingItemsStartedAction -> state.copy(isLoadingItems = true)
15+
is FetchingItemsSuccessAction -> {
16+
val rounds = generateRounds(action.items, state.settings.numQuestions)
17+
state.copy(isLoadingItems = false, items = action.items, questions = rounds)
1818
}
19-
is FetchingProfilesFailedAction -> state.copy(isLoadingProfiles = false, errorLoadingProfiles = true, errorMsg = action.message)
19+
is FetchingItemsFailedAction -> state.copy(isLoadingItems = false, errorLoadingItems = true, errorMsg = action.message)
2020
is NamePickedAction -> {
2121
val status = if (state.currentQuestionItem().matches(action.name)) {
2222
Question.Status.CORRECT
@@ -46,13 +46,13 @@ fun reducer(state: AppState, action: Any): AppState =
4646
else -> throw AssertionError("Action ${action::class.simpleName} not handled")
4747
}
4848

49-
fun generateRounds(profiles: List<Item>, n: Int): List<Question> =
50-
profiles.takeRandomDistinct(n)
51-
.map {
52-
val choiceList = profiles.takeRandomDistinct(3).toMutableList()
53-
choiceList.add(abs(random.nextInt() % 4), it)
49+
fun generateRounds(items: List<Item>, n: Int): List<Question> =
50+
items.takeRandomDistinct(n)
51+
.map { item ->
52+
val choiceList = items.takeRandomDistinct(3).toMutableList()
53+
choiceList.add(abs(random.nextInt() % 4), item)
5454

55-
Question(profileId = it.id, choices = choiceList
55+
Question(itemId = item.id, choices = choiceList
5656
.map { it.id })
5757
}
5858

common/src/commonMain/kotlin/com/willowtreeapps/common/boundary/TransformFunctions.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,24 @@
11
package com.willowtreeapps.common.boundary
22

33
import com.willowtreeapps.common.*
4-
import com.willowtreeapps.common.repo.Profile
54

65
/**
76
* Functions for transforming AppState data into ViewState data to be used by Views.
87
*/
98
fun AppState.toQuestionViewState(): QuestionViewState {
10-
val profile = currentQuestionItem()
11-
val imageUrl = profile.imageUrl
9+
val item = currentQuestionItem()
10+
val imageUrl = item.imageUrl
1211
val choice1 = getItem(currentQuestion?.choices?.get(0))!!.displayName()
1312
val choice2 = getItem(currentQuestion?.choices?.get(1))!!.displayName()
1413
val choice3 = getItem(currentQuestion?.choices?.get(2))!!.displayName()
1514
val choice4 = getItem(currentQuestion?.choices?.get(3))!!.displayName()
16-
val correctBtnNum = currentQuestion?.choices?.indexOfFirst { it == profile.id }!! + 1
15+
val correctBtnNum = currentQuestion?.choices?.indexOfFirst { it == item.id }!! + 1
1716
var selectedBtnNum = currentQuestion?.choices?.indexOfFirst { getItem(it)?.matches(currentQuestion?.answerName ?: "") ?: false}
1817
if (selectedBtnNum != null) {
1918
selectedBtnNum += 1
2019
}
2120
return QuestionViewState(title = "Who is this?",
22-
profileImageUrl = imageUrl,
21+
itemImageUrl = imageUrl,
2322
currentQuestion = (currentQuestionIndex + 1).toString(),
2423
numQuestions = questions.size.toString(),
2524
button1Text = choice1,
@@ -31,9 +30,6 @@ fun AppState.toQuestionViewState(): QuestionViewState {
3130
selectedBtnNum = selectedBtnNum ?: -1)
3231
}
3332

34-
fun Profile.displayName() = "$firstName $lastName"
35-
36-
3733
fun AppState.toGameResultsViewState(): GameResultsViewState {
3834
val percentage = ((numCorrect.toFloat() / questions.size) * 100).toInt()
3935
val messageText = when (percentage) {

common/src/commonMain/kotlin/com/willowtreeapps/common/middleware/NavigationMiddleware.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class NavigationMiddleware(val navigator: Navigator) {
99
fun dispatch(store: Store<AppState>, nextDispatcher: (Any) -> Any, action: Any): Any {
1010
val result = nextDispatcher(action)
1111
when (action) {
12-
is Actions.FetchingProfilesSuccessAction -> navigator.goto(Screen.QUESTION)
12+
is Actions.FetchingItemsSuccessAction -> navigator.goto(Screen.QUESTION)
1313
is Actions.GameCompleteAction -> navigator.goto(Screen.GAME_COMPLETE)
1414
is Actions.StartOverAction -> navigator.goto(Screen.START)
1515
is Actions.SettingsTappedAction -> navigator.goto(Screen.SETTINGS)

common/src/commonMain/kotlin/com/willowtreeapps/common/repo/ItemRepository.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.willowtreeapps.common.repo
22

33
import com.willowtreeapps.common.Item
44
import com.willowtreeapps.common.PlatformDispatcher
5-
import com.willowtreeapps.common.ProfileId
5+
import com.willowtreeapps.common.ItemId
66

77
/**
88
* interface for providing list of items for the game. Items are generic representation.
@@ -16,7 +16,7 @@ class ProfileItemRepository(val repo: ProfilesRepository = KtorProfilesRepositor
1616
override suspend fun fetchItems(): GatewayResponse<List<Item>, GenericError> {
1717
val results = repo.profiles()
1818
return if (results.isSuccessful) {
19-
GatewayResponse.createSuccess(results.response?.map { Item(id = ProfileId(it.id), firstName = it.firstName, lastName = it.lastName, imageUrl = "https:${it.headshot.url}") },
19+
GatewayResponse.createSuccess(results.response?.map { Item(id = ItemId(it.id), firstName = it.firstName, lastName = it.lastName, imageUrl = "https:${it.headshot.url}") },
2020
200, "")
2121
} else {
2222
GatewayResponse.createError(GenericError("Error"), 500, "")
@@ -30,7 +30,7 @@ class DogItemRepository(val repo: KtorDogsRepository = KtorDogsRepository(Platfo
3030
val results = repo.dogs()
3131
return if (results.isSuccessful) {
3232
GatewayResponse.createSuccess(results.response?.map {
33-
Item(id = ProfileId(it.breed + "_" + it.subBreed),
33+
Item(id = ItemId(it.breed + "_" + it.subBreed),
3434
firstName = it.breed, lastName = it.subBreed ?: "",
3535
imageUrl = it.imageUrl)
3636
},
@@ -47,7 +47,7 @@ class CatItemRepository(val repo: KtorCatsRepository = KtorCatsRepository(Platfo
4747
val results = repo.allBreeds()
4848
return if (results.isSuccessful) {
4949
GatewayResponse.createSuccess(results.response?.map {
50-
Item(id = ProfileId(it.id),
50+
Item(id = ItemId(it.id),
5151
firstName = it.breed, lastName = "",
5252
imageUrl = it.imageUrl)
5353
},

0 commit comments

Comments
 (0)