Skip to content

Commit 6e50462

Browse files
committed
add category selection to settings - android working
1 parent e955d3b commit 6e50462

File tree

12 files changed

+87
-24
lines changed

12 files changed

+87
-24
lines changed

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_settings.xml

Lines changed: 22 additions & 5 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,11 +23,19 @@
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

@@ -41,4 +48,14 @@
4148
app:layout_constraintEnd_toEndOf="@+id/numberPicker"
4249
app:layout_constraintStart_toStartOf="@+id/numberPicker"
4350
app:layout_constraintTop_toBottomOf="@+id/numberPicker" />
51+
52+
<TextView
53+
android:id="@+id/txt_num_questions2"
54+
android:layout_width="wrap_content"
55+
android:layout_height="wrap_content"
56+
android:layout_marginTop="8dp"
57+
android:text="Category"
58+
app:layout_constraintEnd_toEndOf="@+id/categoryPicker"
59+
app:layout_constraintStart_toStartOf="@+id/categoryPicker"
60+
app:layout_constraintTop_toBottomOf="@+id/categoryPicker" />
4461
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ sealed class Actions : Action {
2525

2626
class SettingsTappedAction
2727
class LoadAllSettingsAction
28+
class SettingsLoadedAction(val settings: UserSettings)
2829
class ChangeNumQuestionsSettingsAction(val num: Int)
30+
class ChangeCategorySettingsAction(val categoryId: QuestionCategoryId)
2931

3032
}
3133

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

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

3-
import com.willowtreeapps.common.boundary.displayName
4-
import com.willowtreeapps.common.repo.Profile
5-
63
data class AppState(val isLoadingProfiles: Boolean = false,
74
val items: List<Item> = listOf(),
85
val errorLoadingProfiles: Boolean = false,
@@ -63,9 +60,24 @@ data class Item(val id: ProfileId,
6360

6461
}
6562

66-
data class UserSettings(val numQuestions: Int) {
63+
enum class QuestionCategoryId(val displayName: String) {
64+
WILLOW_TREE("WillowTree"),
65+
DOGS("Dogs"),
66+
CATS("Cats");
67+
68+
companion object {
69+
val displayNameList by lazy {
70+
values().map { it.displayName }
71+
}
72+
73+
fun fromOrdinal(ordinal: Int) = values()[ordinal]
74+
}
75+
}
76+
77+
data class UserSettings(val numQuestions: Int,
78+
val categoryId: QuestionCategoryId) {
6779
companion object {
68-
fun defaults() = UserSettings(3)
80+
fun defaults() = UserSettings(3, categoryId = QuestionCategoryId.CATS)
6981
}
7082
}
7183

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ class NetworkThunks(private val networkContext: CoroutineContext,
1515
override val coroutineContext: CoroutineContext
1616
get() = networkContext + job
1717

18-
private val repo = CatItemRepository()
18+
//TODO cache the repositories
19+
private fun repoForCategory(categoryId: QuestionCategoryId) = when (categoryId) {
20+
QuestionCategoryId.WILLOW_TREE -> ProfileItemRepository()
21+
QuestionCategoryId.CATS -> CatItemRepository()
22+
QuestionCategoryId.DOGS -> DogItemRepository()
23+
}
1924

20-
fun fetchProfiles(): ThunkImpl<AppState> = ThunkFn { dispatcher, state ->
25+
fun fetchItems(categoryId: QuestionCategoryId): ThunkImpl<AppState> = ThunkFn { dispatcher, state ->
26+
val repo = repoForCategory(categoryId)
2127
Logger.d("Fetching StoreInfo and Feed")
2228
launch {
2329
store.dispatch(Actions.FetchingProfilesStartedAction())

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ fun reducer(state: AppState, action: Any): AppState =
4040
}
4141

4242
is ChangeNumQuestionsSettingsAction -> state.copy(settings = state.settings.copy(numQuestions = action.num))
43+
is ChangeCategorySettingsAction -> state.copy(settings = state.settings.copy(categoryId = action.categoryId))
44+
is SettingsLoadedAction -> state.copy(settings = action.settings)
4345

4446
else -> throw AssertionError("Action ${action::class.simpleName} not handled")
4547
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fun AppState.toGameResultsViewState(): GameResultsViewState {
4848
messageText = messageText)
4949
}
5050

51-
fun UserSettings.toViewState(): SettingsViewState = SettingsViewState(this.numQuestions)
51+
fun UserSettings.toViewState(): SettingsViewState = SettingsViewState(this.numQuestions, this.categoryId)
5252

5353
//TODO should this be here?
5454
private fun AppState.roundTotals() = "${currentQuestionIndex + 1} out of ${questions.size}"

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import com.beyondeye.reduks.Store
44
import com.willowtreeapps.common.Actions
55
import com.willowtreeapps.common.Actions.ChangeNumQuestionsSettingsAction
66
import com.willowtreeapps.common.AppState
7+
import com.willowtreeapps.common.UserSettings
78
import com.willowtreeapps.common.repo.LocalStorageSettingsRepository
89
import kotlinx.coroutines.CoroutineScope
910
import kotlinx.coroutines.Job
@@ -22,10 +23,13 @@ class SettingsMiddleware(private val settings: LocalStorageSettingsRepository,
2223
fun dispatch(store: Store<AppState>, nextDispatcher: (Any) -> Any, action: Any): Any {
2324
launch {
2425
when (action) {
25-
is ChangeNumQuestionsSettingsAction -> settings.saveNumRounds(action.num)
26+
is ChangeNumQuestionsSettingsAction -> settings.numRounds = action.num
27+
28+
is Actions.ChangeCategorySettingsAction -> settings.categoryId = action.categoryId
2629

2730
is Actions.LoadAllSettingsAction -> {
28-
store.dispatch(ChangeNumQuestionsSettingsAction(settings.loadNumRounds()))
31+
val settings = UserSettings(numQuestions = settings.numRounds, categoryId = settings.categoryId)
32+
store.dispatch(Actions.SettingsLoadedAction(settings))
2933
}
3034
}
3135
}

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

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,26 @@ package com.willowtreeapps.common.repo
33
import com.russhwolf.settings.Settings
44
import com.russhwolf.settings.get
55
import com.russhwolf.settings.set
6+
import com.willowtreeapps.common.QuestionCategoryId
67

78
expect fun userSettings(context: Any? = null): Settings
89

910
class LocalStorageSettingsRepository(private val settings: Settings) {
1011

11-
fun saveNumRounds(numRounds: Int) {
12-
settings[NUM_ROUNDS] = numRounds
13-
}
12+
var numRounds: Int
13+
get() = settings.getInt(NUM_ROUNDS, 4)
14+
set(numRounds) {
15+
settings[NUM_ROUNDS] = numRounds
16+
}
1417

15-
fun loadNumRounds(): Int = settings.getInt(NUM_ROUNDS, 4)
18+
var categoryId: QuestionCategoryId
19+
get() = QuestionCategoryId.valueOf(settings.getString(CATEGORY_ID, QuestionCategoryId.CATS.toString()))
20+
set(categoryId) {
21+
settings[CATEGORY_ID] = categoryId.toString()
22+
}
1623

1724
companion object {
18-
const val NUM_ROUNDS = "NUM_ROUNDS"
25+
private const val NUM_ROUNDS = "NUM_ROUNDS"
26+
private const val CATEGORY_ID = "CATEGORY_ID"
1927
}
2028
}

common/src/commonMain/kotlin/com/willowtreeapps/common/ui/SettingsPresenter.kt

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

3-
import com.beyondeye.reduks.SelectorSubscriberBuilder
43
import com.beyondeye.reduks.SelectorSubscriberFn
54
import com.beyondeye.reduks.Store
6-
import com.beyondeye.reduks.StoreSubscriber
75
import com.willowtreeapps.common.Actions
86
import com.willowtreeapps.common.AppState
97
import com.willowtreeapps.common.Presenter
8+
import com.willowtreeapps.common.QuestionCategoryId
109
import com.willowtreeapps.common.boundary.toViewState
1110

1211
class SettingsPresenter(val store: Store<AppState>): Presenter<SettingsView>() {
@@ -19,4 +18,7 @@ class SettingsPresenter(val store: Store<AppState>): Presenter<SettingsView>() {
1918
store.dispatch(Actions.ChangeNumQuestionsSettingsAction(numQuestions))
2019
}
2120

21+
fun categoryChanged(categoryId: QuestionCategoryId) {
22+
store.dispatch(Actions.ChangeCategorySettingsAction(categoryId))
23+
}
2224
}

0 commit comments

Comments
 (0)