Skip to content

Commit 416fef9

Browse files
committed
improved Cat & dog api. broken iOS build
1 parent 94ef10b commit 416fef9

File tree

21 files changed

+144
-1845
lines changed

21 files changed

+144
-1845
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
190190

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

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/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">

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.willowtreeapps.common
22

33
import com.beyondeye.reduks.Action
4+
import com.willowtreeapps.common.repo.ItemsHolder
45
import com.willowtreeapps.common.repo.Profile
56

67
sealed class Actions : Action {
78

89
class FetchingItemsStartedAction
9-
class FetchingItemsSuccessAction(val items: List<Item>)
10+
class FetchingItemsSuccessAction(val itemsHolder: ItemsHolder)
1011
class FetchingItemsFailedAction(val message: String)
1112

1213
class NamePickedAction(val name: String)

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,19 @@ data class AppState(val isLoadingItems: Boolean = false,
88
val waitingForNextQuestion: Boolean = false,
99
val waitingForResultsTap: Boolean = false,
1010
val questionClock: Int = -1,
11+
val questionTitle: String = "",
1112
val questions: List<Question> = listOf(),
1213
val settings: UserSettings = UserSettings.defaults()) {
1314
companion object {
1415
val INITIAL_STATE = AppState()
1516
}
1617

1718
val timerText: String
18-
get() = if (questionClock < 0) "" else if (questionClock >= 0) questionClock.toString() else "TIME'S UP!!"
19+
get() = when {
20+
questionClock < 0 -> ""
21+
questionClock > 0 -> questionClock.toString()
22+
else -> "TIME'S UP!!"
23+
}
1924

2025
val currentQuestion: Question?
2126
get() = if (questions.size > currentQuestionIndex)

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ fun reducer(state: AppState, action: Any): AppState =
1313
when (action) {
1414
is FetchingItemsStartedAction -> state.copy(isLoadingItems = true)
1515
is FetchingItemsSuccessAction -> {
16-
val rounds = generateRounds(action.items, state.settings.numQuestions)
17-
state.copy(isLoadingItems = false, items = action.items, questions = rounds)
16+
val rounds = generateRounds(action.itemsHolder.items, state.settings.numQuestions)
17+
state.copy(isLoadingItems = false,
18+
items = action.itemsHolder.items,
19+
questionTitle = action.itemsHolder.questionTitle,
20+
questions = rounds)
1821
}
1922
is FetchingItemsFailedAction -> state.copy(isLoadingItems = false, errorLoadingItems = true, errorMsg = action.message)
2023
is NamePickedAction -> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ fun AppState.toQuestionViewState(): QuestionViewState {
1717
if (selectedBtnNum != null) {
1818
selectedBtnNum += 1
1919
}
20-
return QuestionViewState(title = "Who is this?",
20+
return QuestionViewState(title = questionTitle,
2121
itemImageUrl = imageUrl,
2222
currentQuestion = (currentQuestionIndex + 1).toString(),
2323
numQuestions = questions.size.toString(),

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

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,19 @@ import com.willowtreeapps.common.ItemId
99
* This may be used for any data source to generate questions.
1010
*/
1111
interface ItemRepository {
12-
suspend fun fetchItems(): GatewayResponse<List<Item>, GenericError>
12+
suspend fun fetchItems(): GatewayResponse<ItemsHolder, GenericError>
1313
}
1414

15+
data class ItemsHolder(val questionTitle: String,
16+
val items: List<Item>)
17+
1518
class ProfileItemRepository(val repo: ProfilesRepository = KtorProfilesRepository()) : ItemRepository {
16-
override suspend fun fetchItems(): GatewayResponse<List<Item>, GenericError> {
19+
override suspend fun fetchItems(): GatewayResponse<ItemsHolder, GenericError> {
1720
val results = repo.profiles()
1821
return if (results.isSuccessful) {
19-
GatewayResponse.createSuccess(results.response?.map { Item(id = ItemId(it.id), firstName = it.firstName, lastName = it.lastName, imageUrl = "https:${it.headshot.url}") },
20-
200, "")
22+
val itemHolder = ItemsHolder(questionTitle = "Who is this?",
23+
items = results.response?.map { Item(id = ItemId(it.id), firstName = it.firstName, lastName = it.lastName, imageUrl = "https:${it.headshot.url}") }!!)
24+
GatewayResponse.createSuccess(itemHolder, 200, "")
2125
} else {
2226
GatewayResponse.createError(GenericError("Error"), 500, "")
2327
}
@@ -26,15 +30,16 @@ class ProfileItemRepository(val repo: ProfilesRepository = KtorProfilesRepositor
2630
}
2731

2832
class DogItemRepository(val repo: KtorDogsRepository = KtorDogsRepository(PlatformDispatcher)) : ItemRepository {
29-
override suspend fun fetchItems(): GatewayResponse<List<Item>, GenericError> {
33+
override suspend fun fetchItems(): GatewayResponse<ItemsHolder, GenericError> {
3034
val results = repo.dogs()
3135
return if (results.isSuccessful) {
32-
GatewayResponse.createSuccess(results.response?.map {
36+
val itemsHolder = ItemsHolder(questionTitle = "Name the breed",
37+
items = results.response?.map {
3338
Item(id = ItemId(it.breed + "_" + it.subBreed),
3439
firstName = it.breed, lastName = it.subBreed ?: "",
3540
imageUrl = it.imageUrl)
36-
},
37-
200, "")
41+
}!!)
42+
GatewayResponse.createSuccess(itemsHolder, 200, "")
3843
} else {
3944
GatewayResponse.createError(GenericError("Error"), 500, "")
4045
}
@@ -43,19 +48,19 @@ class DogItemRepository(val repo: KtorDogsRepository = KtorDogsRepository(Platfo
4348
}
4449

4550
class CatItemRepository(val repo: KtorCatsRepository = KtorCatsRepository(PlatformDispatcher)) : ItemRepository {
46-
override suspend fun fetchItems(): GatewayResponse<List<Item>, GenericError> {
51+
override suspend fun fetchItems(): GatewayResponse<ItemsHolder, GenericError> {
4752
val results = repo.allBreeds()
48-
return if (results.isSuccessful) {
49-
GatewayResponse.createSuccess(results.response?.map {
53+
val itemsHolder = ItemsHolder(questionTitle = "Name the breed",
54+
items = results.response?.map {
5055
Item(id = ItemId(it.id),
5156
firstName = it.breed, lastName = "",
5257
imageUrl = it.imageUrl)
53-
},
54-
200, "")
58+
}!!)
59+
return if (results.isSuccessful) {
60+
GatewayResponse.createSuccess(itemsHolder, 200, "")
5561
} else {
5662
GatewayResponse.createError(GenericError("Error"), 500, "")
5763
}
5864
}
59-
6065
}
6166

0 commit comments

Comments
 (0)