Skip to content

Commit 0ef0a4e

Browse files
authored
Merge pull request #21 from patjackson52/feature/generic
create BaseNameGameFragment & ViewController
2 parents ee4ed8b + 0986ddc commit 0ef0a4e

22 files changed

+131
-131
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import android.app.Application
55
import android.os.Bundle
66
import androidx.appcompat.app.AppCompatActivity
77
import androidx.navigation.findNavController
8-
import com.willowtreeapps.common.Actions
98
import com.willowtreeapps.common.middleware.Navigator
109
import com.willowtreeapps.common.middleware.Screen
1110
import com.willowtreeapps.namegame.store.SettingsDialogFragment
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.willowtreeapps.namegame.store
2+
3+
import androidx.fragment.app.Fragment
4+
import com.willowtreeapps.common.Presenter
5+
import com.willowtreeapps.common.View
6+
import com.willowtreeapps.common.ui.GameResultsPresenter
7+
import com.willowtreeapps.namegame.NameGameApp
8+
import kotlinx.coroutines.CoroutineScope
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlin.coroutines.CoroutineContext
11+
12+
open class BaseNameGameViewFragment<TPresenter: Presenter<*>>: Fragment(), CoroutineScope, View<TPresenter> {
13+
override val coroutineContext: CoroutineContext
14+
get() = Dispatchers.Main
15+
16+
override lateinit var presenter: TPresenter
17+
18+
override fun onResume() {
19+
super.onResume()
20+
NameGameApp.gameEngine().attachView(this)
21+
}
22+
23+
override fun onPause() {
24+
super.onPause()
25+
NameGameApp.gameEngine().detachView(this)
26+
}
27+
}

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,15 @@ import android.os.Bundle
44
import android.view.LayoutInflater
55
import android.view.View
66
import android.view.ViewGroup
7-
import androidx.fragment.app.Fragment
87
import com.willowtreeapps.common.GameResultsViewState
98
import com.willowtreeapps.common.ui.GameResultsPresenter
109
import com.willowtreeapps.common.ui.GameResultsView
1110
import com.willowtreeapps.namegame.MainActivity
1211
import com.willowtreeapps.namegame.NameGameApp
1312
import com.willowtreeapps.namegame.R
1413
import kotlinx.android.synthetic.main.fragment_game_results.*
15-
import kotlinx.coroutines.CoroutineScope
16-
import kotlinx.coroutines.Dispatchers
17-
import kotlin.coroutines.CoroutineContext
1814

19-
class GameResultsFragment : Fragment(), CoroutineScope, GameResultsView, MainActivity.IOnBackPressed {
20-
21-
override val coroutineContext: CoroutineContext
22-
get() = Dispatchers.Main
23-
24-
private var presenter: GameResultsPresenter? = null
15+
class GameResultsFragment : BaseNameGameViewFragment<GameResultsPresenter>(), GameResultsView, MainActivity.IOnBackPressed {
2516

2617
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2718
return inflater.inflate(R.layout.fragment_game_results, container, false)
@@ -34,27 +25,17 @@ class GameResultsFragment : Fragment(), CoroutineScope, GameResultsView, MainAct
3425
private fun initViews() {
3526
btn_start_over.setOnClickListener {
3627
NameGameApp.gameEngine().detachView(this)
37-
presenter?.startOverTapped()
28+
presenter.startOverTapped()
3829
}
3930
}
4031

41-
override fun onResume() {
42-
super.onResume()
43-
presenter = NameGameApp.gameEngine().attachView(this) as GameResultsPresenter
44-
}
45-
46-
override fun onPause() {
47-
super.onPause()
48-
NameGameApp.gameEngine().detachView(this)
49-
}
50-
5132
override fun showResults(viewState: GameResultsViewState) {
5233
txt_questionTitle.text = viewState.resultsText
5334
txt_message.text = viewState.messageText
5435
}
5536

5637
override fun onBackPressed(): Boolean {
57-
presenter?.onBackPressed()
38+
presenter.onBackPressed()
5839
return false
5940
}
6041

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

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,20 @@ import android.view.LayoutInflater
88
import android.view.View
99
import android.view.ViewGroup
1010
import android.view.animation.BounceInterpolator
11-
import androidx.fragment.app.Fragment
1211
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
1312
import com.willowtreeapps.common.QuestionViewState
1413
import com.willowtreeapps.common.ui.QuestionView
1514
import kotlinx.android.synthetic.main.fragment_question.*
1615
import nl.dionsegijn.konfetti.models.Shape
1716
import nl.dionsegijn.konfetti.models.Size
18-
import kotlin.coroutines.CoroutineContext
1917
import android.widget.Button
2018
import androidx.core.content.res.ResourcesCompat
2119
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
2220
import com.willowtreeapps.common.ui.QuestionPresenter
2321
import com.willowtreeapps.namegame.*
24-
import kotlinx.coroutines.*
2522

2623

27-
class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.IOnBackPressed {
28-
29-
private lateinit var presenter: QuestionPresenter
30-
31-
override val coroutineContext: CoroutineContext
32-
get() = Dispatchers.Main
24+
class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), QuestionView, MainActivity.IOnBackPressed {
3325

3426
private var restoreX: Float? = null
3527
private var restoreY: Float? = null
@@ -40,16 +32,6 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
4032
return inflater.inflate(R.layout.fragment_question, container, false)
4133
}
4234

43-
override fun onResume() {
44-
super.onResume()
45-
presenter = NameGameApp.gameEngine().attachView(this) as QuestionPresenter
46-
}
47-
48-
override fun onPause() {
49-
super.onPause()
50-
NameGameApp.gameEngine().detachView(this)
51-
}
52-
5335
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
5436
initViews()
5537
}
@@ -164,9 +146,11 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
164146
} else {
165147
btn_next
166148
}
167-
btn.visibility = View.VISIBLE
168-
btn.alpha = 0F
169-
btn.animate().alpha(1f)
149+
if (btn != null) {
150+
btn.visibility = View.VISIBLE
151+
btn.alpha = 0F
152+
btn.animate().alpha(1f)
153+
}
170154
}
171155
set.start()
172156
}
@@ -247,7 +231,8 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
247231
txt_timer.animate().alpha(0f)
248232
.withEndAction {
249233
txt_timer.visibility = View.VISIBLE
250-
txt_timer.setTextColor(restoreColor) }
234+
txt_timer.setTextColor(restoreColor)
235+
}
251236
}
252237

253238
}
@@ -273,6 +258,4 @@ class QuestionFragment : Fragment(), CoroutineScope, QuestionView, MainActivity.
273258
4 -> button4
274259
else -> null//throw IllegalStateException("Invalid button index")
275260
}
276-
277-
278261
}

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

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.willowtreeapps.namegame.store
22

3-
import android.app.Dialog
43
import android.os.Bundle
54
import android.view.LayoutInflater
65
import android.view.View
@@ -16,45 +15,38 @@ import kotlinx.android.synthetic.main.fragment_settings.*
1615

1716
class SettingsDialogFragment: DialogFragment(), SettingsView {
1817

19-
var presenter: SettingsPresenter? = null
18+
override lateinit var presenter: SettingsPresenter
2019

2120
companion object {
2221
fun newInstance(): SettingsDialogFragment {
23-
val f = SettingsDialogFragment()
24-
return f
22+
return SettingsDialogFragment()
2523
}
2624
}
2725

28-
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
29-
val dialog = super.onCreateDialog(savedInstanceState)
30-
return dialog
31-
}
32-
3326
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
3427
return inflater.inflate(R.layout.fragment_settings, container, false)
3528
}
3629

37-
3830
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
3931
super.onViewCreated(view, savedInstanceState)
4032
numberPicker.minValue = 1
4133
numberPicker.maxValue = 20
4234
numberPicker.setOnValueChangedListener { _, _, newVal ->
43-
presenter?.numQuestionsChanged(newVal)
35+
presenter.numQuestionsChanged(newVal)
4436
}
4537

4638
categoryPicker.displayedValues = QuestionCategoryId.displayNameList.toTypedArray()
4739
categoryPicker.maxValue = 0
4840
categoryPicker.maxValue = QuestionCategoryId.displayNameList.toTypedArray().size - 1
4941
categoryPicker.setOnValueChangedListener { _, _, newVal ->
50-
presenter?.categoryChanged(QuestionCategoryId.fromOrdinal(newVal))
42+
presenter.categoryChanged(QuestionCategoryId.fromOrdinal(newVal))
5143
}
5244
btn_ok.setOnClickListener { dismiss() }
5345
}
5446

5547
override fun onResume() {
5648
super.onResume()
57-
presenter = NameGameApp.gameEngine().attachView(this) as SettingsPresenter
49+
NameGameApp.gameEngine().attachView(this)
5850
}
5951

6052
override fun onPause() {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class StartFragment : Fragment(), CoroutineScope, StartView {
1818
override val coroutineContext: CoroutineContext
1919
get() = Dispatchers.Main
2020

21-
private var presenter: StartPresenter? = null
21+
override lateinit var presenter: StartPresenter
2222

2323
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
2424
return inflater.inflate(R.layout.fragment_start, container, false)
@@ -39,7 +39,7 @@ class StartFragment : Fragment(), CoroutineScope, StartView {
3939

4040
override fun onResume() {
4141
super.onResume()
42-
presenter = NameGameApp.gameEngine().attachView(this) as StartPresenter
42+
NameGameApp.gameEngine().attachView(this)
4343
}
4444

4545
override fun onPause() {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
app:layout_constraintTop_toTopOf="parent" />
4141

4242
<TextView
43+
style="@style/SettingsLabelText"
4344
android:id="@+id/txt_num_questions"
4445
android:layout_width="70dp"
4546
android:layout_height="wrap_content"
@@ -51,6 +52,7 @@
5152
app:layout_constraintTop_toBottomOf="@+id/numberPicker" />
5253

5354
<TextView
55+
style="@style/SettingsLabelText"
5456
android:id="@+id/txt_num_questions2"
5557
android:layout_width="wrap_content"
5658
android:layout_height="wrap_content"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@
8080
android:layout_width="wrap_content"
8181
android:layout_height="wrap_content"
8282
android:layout_marginTop="20dp"
83-
android:text="TextView"
8483
app:layout_constraintEnd_toEndOf="parent"
8584
app:layout_constraintStart_toStartOf="parent"
86-
app:layout_constraintTop_toBottomOf="@+id/btn_settings" />
85+
app:layout_constraintTop_toBottomOf="@+id/btn_settings"
86+
tools:text="api error message" />
8787

8888
</androidx.constraintlayout.widget.ConstraintLayout>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,10 @@
5353

5454
</style>
5555

56+
<style name="SettingsLabelText">
57+
<item name="android:textStyle">bold</item>
58+
<item name="android:textColor">@color/black</item>
59+
</style>
60+
5661
</resources>
5762

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class GameEngine(navigator: Navigator,
3333
init {
3434
appStore.dispatch(Actions.LoadAllSettingsAction())
3535
}
36-
fun attachView(view: View): Presenter<out View?> = presenterFactory.attachView(view)
36+
fun <T: Presenter<*>>attachView(view: View<T>) = presenterFactory.attachView(view as View<Presenter<*>>)
3737

38-
fun detachView(view: View) = presenterFactory.detachView(view)
38+
fun detachView(view: View<*>) = presenterFactory.detachView(view)
3939
}

0 commit comments

Comments
 (0)