Skip to content

Commit 7bd0af5

Browse files
committed
add Settings presenter, view, & fragment
1 parent ba23e4d commit 7bd0af5

File tree

17 files changed

+175
-154
lines changed

17 files changed

+175
-154
lines changed

app/build.gradle

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
repositories {
2-
maven{ url "https://dl.bintray.com/russhwolf/multiplatform-settings" }
3-
}
41
apply plugin: 'com.android.application'
52

63
apply plugin: 'kotlin-android'
@@ -62,7 +59,7 @@ dependencies {
6259
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
6360
implementation project(':common')
6461
implementation 'nl.dionsegijn:konfetti:1.1.2'
65-
implementation "com.russhwolf:multiplatform-settings:0.2"
62+
implementation "com.russhwolf:multiplatform-settings:$multiplatformSettingsVersion"
6663

6764
kapt 'com.github.bumptech.glide:compiler:4.8.0'
6865
testImplementation 'junit:junit:4.12'

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package com.willowtreeapps.namegame
33
import android.app.Activity
44
import android.app.Application
55
import android.os.Bundle
6+
import androidx.appcompat.app.AppCompatActivity
67
import androidx.navigation.findNavController
78
import com.willowtreeapps.common.middleware.Navigator
89
import com.willowtreeapps.common.middleware.Screen
10+
import com.willowtreeapps.namegame.store.SettingsDialogFragment
911

1012
/**
1113
* Android implementation of Navigator interface. This will load the appropriate Activity or Fragment
@@ -15,7 +17,7 @@ import com.willowtreeapps.common.middleware.Screen
1517
class AndroidNavigator : Navigator, Application.ActivityLifecycleCallbacks {
1618

1719

18-
private var currentActivity: Activity? = null
20+
private var currentActivity: AppCompatActivity? = null
1921

2022
//TODO consider using current screen & destination screen to determine routing & animation
2123
override fun goto(screen: Screen) {
@@ -25,6 +27,10 @@ class AndroidNavigator : Navigator, Application.ActivityLifecycleCallbacks {
2527
Screen.GAME_COMPLETE -> navController.navigate(R.id.action_questionScreen_to_resultsFragment)
2628
// Screen.START -> navController.navigate(R.id.startScreen)
2729
Screen.START -> navController.navigate(R.id.action_resultsFragment_to_startScreen)
30+
Screen.SETTINGS -> {
31+
val dialog = SettingsDialogFragment.newInstance()
32+
dialog.show(currentActivity!!.supportFragmentManager, "SettingsFragment")
33+
}
2834
else -> throw IllegalArgumentException("Screen $screen is not handled in AndroidNavigator")
2935
}
3036
}
@@ -36,7 +42,7 @@ class AndroidNavigator : Navigator, Application.ActivityLifecycleCallbacks {
3642
}
3743

3844
override fun onActivityStarted(activity: Activity?) {
39-
currentActivity = activity
45+
currentActivity = activity as AppCompatActivity?
4046
}
4147

4248
override fun onActivityDestroyed(activity: Activity?) {
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.willowtreeapps.namegame.store
2+
3+
import android.app.Dialog
4+
import android.os.Bundle
5+
import android.view.LayoutInflater
6+
import android.view.View
7+
import android.view.ViewGroup
8+
import androidx.fragment.app.DialogFragment
9+
import com.willowtreeapps.common.SettingsViewState
10+
import com.willowtreeapps.common.ui.SettingsPresenter
11+
import com.willowtreeapps.common.ui.SettingsView
12+
import com.willowtreeapps.namegame.NameGameApp
13+
import com.willowtreeapps.namegame.R
14+
import kotlinx.android.synthetic.main.fragment_settings.*
15+
16+
class SettingsDialogFragment: DialogFragment(), SettingsView {
17+
18+
var presenter: SettingsPresenter? = null
19+
20+
companion object {
21+
fun newInstance(): SettingsDialogFragment {
22+
val f = SettingsDialogFragment()
23+
return f
24+
}
25+
}
26+
27+
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
28+
val dialog = super.onCreateDialog(savedInstanceState)
29+
return dialog
30+
}
31+
32+
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
33+
return inflater.inflate(R.layout.fragment_settings, container, false)
34+
}
35+
36+
37+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
38+
super.onViewCreated(view, savedInstanceState)
39+
numberPicker.minValue = 1
40+
numberPicker.maxValue = 20
41+
numberPicker.setOnValueChangedListener { _, _, newVal ->
42+
presenter?.numQuestionsChanged(newVal)
43+
}
44+
btn_ok.setOnClickListener { dismiss() }
45+
}
46+
47+
override fun onResume() {
48+
super.onResume()
49+
presenter = NameGameApp.gameEngine().attachView(this) as SettingsPresenter
50+
}
51+
52+
override fun onPause() {
53+
super.onPause()
54+
NameGameApp.gameEngine().detachView(this)
55+
}
56+
57+
override fun showSettings(viewState: SettingsViewState) {
58+
numberPicker.value = viewState.numQuestions
59+
}
60+
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ class StartFragment : Fragment(), CoroutineScope, StartView {
3030
presenter?.startGame()
3131
}
3232
}
33+
btn_settings.setOnClickListener {
34+
activity?.runOnUiThread {
35+
presenter?.settingsTapped()
36+
}
37+
}
3338
}
3439

3540
override fun onResume() {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="300dp"
6+
android:layout_height="400dp"
7+
android:minWidth="300dp"
8+
android:minHeight="400dp"
9+
>
10+
11+
<Button
12+
android:id="@+id/btn_ok"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content"
15+
android:layout_marginBottom="16dp"
16+
android:text="Ok"
17+
app:layout_constraintBottom_toBottomOf="parent"
18+
app:layout_constraintEnd_toEndOf="parent"
19+
app:layout_constraintHorizontal_bias="0.5"
20+
app:layout_constraintStart_toStartOf="parent"
21+
app:layout_constraintTop_toBottomOf="@+id/txt_num_questions" />
22+
23+
<NumberPicker
24+
android:id="@+id/numberPicker"
25+
android:layout_width="wrap_content"
26+
android:layout_height="wrap_content"
27+
android:layout_marginStart="8dp"
28+
android:layout_marginTop="24dp"
29+
android:layout_marginEnd="8dp"
30+
app:layout_constraintEnd_toEndOf="parent"
31+
app:layout_constraintHorizontal_bias="0.5"
32+
app:layout_constraintStart_toStartOf="parent"
33+
app:layout_constraintTop_toTopOf="parent" />
34+
35+
<TextView
36+
android:id="@+id/txt_num_questions"
37+
android:layout_width="wrap_content"
38+
android:layout_height="wrap_content"
39+
android:layout_marginTop="8dp"
40+
android:text="Number of Questions"
41+
app:layout_constraintEnd_toEndOf="@+id/numberPicker"
42+
app:layout_constraintStart_toStartOf="@+id/numberPicker"
43+
app:layout_constraintTop_toBottomOf="@+id/numberPicker" />
44+
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@
2424
app:layout_constraintHorizontal_bias="0.5"
2525
app:layout_constraintStart_toStartOf="parent" />
2626

27+
<Button
28+
android:id="@+id/btn_settings"
29+
android:layout_width="wrap_content"
30+
android:layout_height="wrap_content"
31+
android:layout_marginStart="8dp"
32+
android:layout_marginTop="16dp"
33+
android:layout_marginEnd="8dp"
34+
android:layout_marginBottom="80dp"
35+
android:text="Settings"
36+
app:layout_constraintBottom_toBottomOf="parent"
37+
app:layout_constraintEnd_toEndOf="parent"
38+
app:layout_constraintHorizontal_bias="0.501"
39+
app:layout_constraintStart_toStartOf="parent"
40+
app:layout_constraintTop_toBottomOf="@+id/btn_start" />
41+
2742
<ImageView
2843
android:id="@+id/imageView2"
2944
android:layout_width="wrap_content"
@@ -57,18 +72,17 @@
5772
app:layout_constraintEnd_toEndOf="parent"
5873
app:layout_constraintHorizontal_bias="0.5"
5974
app:layout_constraintStart_toStartOf="parent"
60-
app:layout_constraintTop_toTopOf="parent"
61-
app:layout_constraintVertical_bias="0.9" />
75+
app:layout_constraintTop_toBottomOf="@+id/btn_settings"
76+
app:layout_constraintVertical_bias="0.39" />
6277

6378
<TextView
6479
android:id="@+id/txt_error"
6580
android:layout_width="wrap_content"
6681
android:layout_height="wrap_content"
67-
android:layout_marginTop="24dp"
82+
android:layout_marginTop="20dp"
6883
android:text="TextView"
6984
app:layout_constraintEnd_toEndOf="parent"
70-
app:layout_constraintHorizontal_bias="0.5"
7185
app:layout_constraintStart_toStartOf="parent"
72-
app:layout_constraintTop_toBottomOf="@+id/btn_start" />
86+
app:layout_constraintTop_toBottomOf="@+id/btn_settings" />
7387

7488
</androidx.constraintlayout.widget.ConstraintLayout>

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ buildscript {
1010
maven { url 'https://dl.bintray.com/jetbrains/kotlin-native-dependencies' }
1111
}
1212
dependencies {
13-
// classpath 'com.android.tools.build:gradle:3.3.2'
1413
classpath 'com.android.tools.build:gradle:3.5.0-alpha10'
1514
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlinVersion"
1615
classpath "org.jetbrains.kotlin:kotlin-serialization:$kotlinVersion"
@@ -49,6 +48,7 @@ ext {
4948
ktorVersion = '1.1.3'
5049
serializationVersion = '0.10.0'
5150
klockVersion = '1.3.1'
51+
multiplatformSettingsVersion = '0.2'
5252

5353
networkDependencies = [
5454
retrofit : "com.squareup.retrofit2:retrofit:${retrofitVersion}",

common/build.gradle

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,6 @@ apply plugin: 'kotlinx-serialization'
88

99
kotlin {
1010

11-
12-
// android {
13-
// compilations.all {
14-
// tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) { task ->
15-
// kotlinOptions {
16-
// freeCompilerArgs = ['-Xuse-experimental=kotlin.Experimental']
17-
// }
18-
// }
19-
// }
20-
// publishLibraryVariants("release")
21-
// }
22-
2311
// iosArm64("ios")
2412
// iosX64("iosSim")
2513
//
@@ -60,7 +48,7 @@ kotlin {
6048
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core-common:$coroutinesVersion"
6149
implementation "org.jetbrains.kotlinx:kotlinx-serialization-runtime-common:$serializationVersion"
6250
implementation "io.ktor:ktor-client-logging:$ktorVersion"
63-
implementation "com.russhwolf:multiplatform-settings:0.2"
51+
implementation "com.russhwolf:multiplatform-settings:$multiplatformSettingsVersion"
6452

6553

6654
implementation "io.ktor:ktor-client-core:$ktorVersion"

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ sealed class Actions : Action {
2222
class DecrementCountDownAction
2323
class TimesUpAction
2424

25+
26+
class SettingsTappedAction
2527
class LoadAllSettingsAction
2628
class ChangeNumQuestionsSettingsAction(val num: Int)
2729

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fun reducer(state: AppState, action: Any): AppState =
3030
}
3131
is NextQuestionAction -> state.copy(waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1)
3232
is GameCompleteAction -> state.copy(waitingForResultsTap = true, waitingForNextQuestion = false, currentQuestionIndex = state.currentQuestionIndex + 1)
33-
is StartOverAction, is ResetGameStateAction -> AppState.INITIAL_STATE
33+
is StartOverAction, is ResetGameStateAction -> AppState.INITIAL_STATE.copy(settings = state.settings)
3434
is StartQuestionTimerAction -> state.copy(questionClock = action.initialValue)
3535
is DecrementCountDownAction -> state.copy(questionClock = state.questionClock - 1)
3636
is TimesUpAction -> {

0 commit comments

Comments
 (0)