Skip to content

Commit 28199d0

Browse files
committed
upgrade to kotlin 1.3.3 & multiplatform settings 0.3
1 parent f5a3f10 commit 28199d0

File tree

35 files changed

+444
-488
lines changed

35 files changed

+444
-488
lines changed

android/build.gradle

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ dependencies {
5555
implementation "io.ktor:ktor-client-json-jvm:$ktorVersion"
5656
implementation group: 'org.slf4j', name: 'slf4j-android', version: '1.7.26'
5757

58-
implementation networkDependencies.values()
59-
6058
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutinesVersion"
6159
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:$coroutinesVersion"
6260
implementation project(':common')

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ class GameResultsFragment : BaseNameGameViewFragment<GameResultsPresenter>(), Ga
3030
}
3131

3232
override fun showResults(viewState: GameResultsViewState) {
33-
txt_questionTitle.text = viewState.resultsText
34-
txt_message.text = viewState.messageText
33+
activity?.runOnUiThread {
34+
txt_questionTitle.text = viewState.resultsText
35+
txt_message.text = viewState.messageText
36+
}
3537
}
3638

3739
override fun onBackPressed(): Boolean {

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

Lines changed: 30 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import java.util.*
2626

2727

2828
class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), QuestionView, MainActivity.IOnBackPressed {
29-
private val speechRecognizer by lazy { SpeechRecognizer.createSpeechRecognizer(activity!!) }
29+
private lateinit var speechRecognizer: SpeechRecognizer
3030
private val speechRecognizerIntent by lazy {
3131
val speechRecIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
3232
speechRecIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL,
@@ -37,7 +37,6 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
3737
}
3838

3939
override fun openMic() {
40-
4140
speechRecognizer.setRecognitionListener(object : RecognitionListener {
4241
override fun onReadyForSpeech(params: Bundle?) {
4342
}
@@ -83,12 +82,17 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
8382

8483
}
8584

85+
override fun closeMic() {
86+
speechRecognizer.stopListening()
87+
}
88+
8689
private var restoreX: Float? = null
8790
private var restoreY: Float? = null
8891
private var lastCorrectBtn: Button? = null
8992
private var lastSelectedBtn: Button? = null
9093

9194
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
95+
speechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity!!)
9296
return inflater.inflate(R.layout.fragment_question, container, false)
9397
}
9498

@@ -173,12 +177,10 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
173177
}
174178

175179
override fun showProfile(viewState: QuestionViewState) {
176-
activity?.runOnUiThread {
177-
if (btn_next.visibility == View.VISIBLE) {
178-
fadeNextButton { setProfileAndFadeIn(viewState) }
179-
} else {
180-
setProfileAndFadeIn(viewState)
181-
}
180+
if (btn_next.visibility == View.VISIBLE) {
181+
fadeNextButton { setProfileAndFadeIn(viewState) }
182+
} else {
183+
setProfileAndFadeIn(viewState)
182184
}
183185
}
184186

@@ -188,9 +190,7 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
188190
}
189191

190192
override fun showWrongAnswer(viewState: QuestionViewState, isEndGame: Boolean) {
191-
activity?.runOnUiThread {
192-
wrongShakeAnimation(viewState) { hideButtonsShowNext(viewState, isEndGame) }
193-
}
193+
wrongShakeAnimation(viewState) { hideButtonsShowNext(viewState, isEndGame) }
194194
}
195195

196196
private val showButtonsAnimatorSet by lazy {
@@ -337,7 +337,7 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
337337
}
338338

339339
override fun setTimerText(viewState: QuestionViewState) {
340-
activity?.runOnUiThread {
340+
if (view != null) {
341341
txt_timer.scaleX = 0f
342342
txt_timer.scaleY = 0f
343343
txt_timer.alpha = 1f
@@ -359,27 +359,24 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
359359
}
360360

361361
override fun showTimesUp(viewState: QuestionViewState, isEndGame: Boolean) {
362-
activity?.runOnUiThread {
363-
txt_timer.scaleX = 0f
364-
txt_timer.scaleY = 0f
365-
txt_timer.text = viewState.timerText
366-
val restoreColor = txt_timer.currentTextColor
367-
txt_timer.setTextColor(ResourcesCompat.getColor(context?.resources!!, R.color.red, activity?.theme))
368-
txt_timer.animate()
369-
.scaleX(1f)
370-
.scaleY(1f)
371-
.setInterpolator(FastOutSlowInInterpolator())
372-
.setDuration(500)
373-
.withEndAction {
374-
showWrongAnswer(viewState, isEndGame)
375-
txt_timer?.animate()?.alpha(0f)
376-
?.withEndAction {
377-
txt_timer?.visibility = View.VISIBLE
378-
txt_timer?.setTextColor(restoreColor)
379-
}
380-
}
381-
382-
}
362+
txt_timer.scaleX = 0f
363+
txt_timer.scaleY = 0f
364+
txt_timer.text = viewState.timerText
365+
val restoreColor = txt_timer.currentTextColor
366+
txt_timer.setTextColor(ResourcesCompat.getColor(context?.resources!!, R.color.red, activity?.theme))
367+
txt_timer.animate()
368+
.scaleX(1f)
369+
.scaleY(1f)
370+
.setInterpolator(FastOutSlowInInterpolator())
371+
.setDuration(500)
372+
.withEndAction {
373+
showWrongAnswer(viewState, isEndGame)
374+
txt_timer?.animate()?.alpha(0f)
375+
?.withEndAction {
376+
txt_timer?.visibility = View.VISIBLE
377+
txt_timer?.setTextColor(restoreColor)
378+
}
379+
}
383380
}
384381

385382
private fun celebrate() {
@@ -402,53 +399,4 @@ class QuestionFragment : BaseNameGameViewFragment<QuestionPresenter>(), Question
402399
4 -> button4
403400
else -> null//throw IllegalStateException("Invalid button index")
404401
}
405-
406-
407-
private fun startSpeechToText() {
408-
409-
val speechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity!!)
410-
val speechRecognizerIntent = Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH)
411-
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM)
412-
speechRecognizerIntent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault())
413-
414-
speechRecognizer.setRecognitionListener(object : RecognitionListener {
415-
override fun onReadyForSpeech(bundle: Bundle) {}
416-
417-
override fun onBeginningOfSpeech() {}
418-
419-
override fun onRmsChanged(v: Float) {}
420-
421-
override fun onBufferReceived(bytes: ByteArray) {}
422-
423-
override fun onEndOfSpeech() {}
424-
425-
override fun onError(i: Int) {}
426-
427-
override fun onResults(bundle: Bundle) {
428-
val matches = bundle.getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION)//getting all the matches
429-
//displaying the first match
430-
// if (matches != null)
431-
}
432-
433-
override fun onPartialResults(bundle: Bundle) {}
434-
435-
override fun onEvent(i: Int, bundle: Bundle) {}
436-
})
437-
438-
btn_end_game.setOnTouchListener { view, motionEvent ->
439-
when (motionEvent.action) {
440-
MotionEvent.ACTION_UP -> {
441-
speechRecognizer.stopListening()
442-
// editText.hint = getString(R.string.text_hint)
443-
}
444-
445-
MotionEvent.ACTION_DOWN -> {
446-
speechRecognizer.startListening(speechRecognizerIntent)
447-
// editText.setText("")
448-
// editText.hint = "Listening..."
449-
}
450-
}
451-
false
452-
}
453-
}
454402
}

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class SettingsDialogFragment : DialogFragment(), SettingsView {
6565
override fun showSettings(viewState: SettingsViewState) {
6666
numberPicker.value = viewState.numQuestions
6767
categoryPicker.value = QuestionCategoryId.values().indexOf(viewState.categoryId)
68+
switch_mic.isChecked = viewState.isMicModeEnabled
6869
}
6970

7071
override fun askForMicPermissions() {
@@ -77,16 +78,14 @@ class SettingsDialogFragment : DialogFragment(), SettingsView {
7778

7879
if (permission != PackageManager.PERMISSION_GRANTED) {
7980
Logger.d("Permission to record denied")
80-
makeRequest()
81+
requestPermissions(
82+
arrayOf(Manifest.permission.RECORD_AUDIO),
83+
MainActivity.RECORD_REQUEST_CODE)
84+
} else {
85+
presenter.microphonePermissionGranted()
8186
}
8287
}
8388

84-
private fun makeRequest() {
85-
requestPermissions(
86-
arrayOf(Manifest.permission.RECORD_AUDIO),
87-
MainActivity.RECORD_REQUEST_CODE)
88-
}
89-
9089
override fun onRequestPermissionsResult(requestCode: Int,
9190
permissions: Array<String>, grantResults: IntArray) {
9291
when (requestCode) {
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.willowtreeapps.namegame
2+
3+
import com.willowtreeapps.common.util.NO_MATCH
4+
import com.willowtreeapps.common.util.match
5+
import org.junit.Assert.assertEquals
6+
import org.junit.Test
7+
8+
class MatcherTest {
9+
val presidents2Georges = listOf("Thomas Jefferson", "Abraham Lincoln", "George Washington", "George Bush")
10+
val presidentsUnique = listOf("Thomas Jefferson", "Abraham Lincoln", "Alexander Hamilton", "George Bush")
11+
val cats = listOf("Russian Blue", "Persian Cat", "Scottish Fold", "Siamese Cat")
12+
13+
@Test
14+
fun `choose exact match`() = testMatch("George Washington",
15+
presidents2Georges,
16+
2)
17+
18+
@Test
19+
fun `choose exact match case insensitive`() = testMatch("george washington",
20+
presidents2Georges,
21+
2)
22+
23+
@Test
24+
fun `choose partial match`() = testMatch("George Castansa",
25+
presidentsUnique,
26+
3)
27+
28+
29+
@Test
30+
fun `choose similar partial match`() = testMatch("Geoge Castansa",
31+
presidentsUnique,
32+
3)
33+
34+
@Test
35+
fun `returns -1 is no match`() = testMatch("bob",
36+
presidents2Georges,
37+
NO_MATCH)
38+
39+
@Test
40+
fun `match when only one name`() = testMatch("Persian",
41+
cats,
42+
1)
43+
44+
@Test
45+
fun `match when only one name and misspelled`() = testMatch("Persain",
46+
cats,
47+
1)
48+
49+
@Test
50+
fun `match when only one name and misspelled badly`() = testMatch("Pur sin",
51+
cats,
52+
1)
53+
54+
@Test
55+
fun `NO_MATCH when ambiguous`() = testMatch("cat",
56+
cats,
57+
NO_MATCH)
58+
59+
@Test
60+
fun `NO_MATCH similar partial match ambiguous`() = testMatch("Geoge Castansa",
61+
presidents2Georges,
62+
NO_MATCH)
63+
64+
65+
fun testMatch(query: String,
66+
choices: List<String>,
67+
expectedIndex: Int) {
68+
val index = match(query, choices)
69+
70+
assertEquals(expectedIndex, index)
71+
}
72+
}

0 commit comments

Comments
 (0)