Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement candidates ranking #44 #45

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.delay
import org.hamcrest.CoreMatchers
import org.hamcrest.Matcher
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue

class Robot(
Expand Down Expand Up @@ -86,4 +87,8 @@ class Robot(
suspend fun backspace() {
uiEventSink.send(Event.KEY_PRESS.withData(Key.Backspace))
}

fun checkCandidatePosition(candidate: String, pos: Int): Robot = apply {
assertEquals(pos, testingHook.candidatesAdapter.findItem(candidate))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,17 @@ class SpecsTest: MainActivityTestsBase(
checkInsertedTextIs("chà ")
}
}

@Test
fun candidateBeenChosenDisplayedFirst(): Unit = runBlocking {
// clear db
with(robot) {
pressSequentially("24236")
checkCandidateDisplayed("chào")
browseTo("chào")
confirm()
pressSequentially("24236")
checkCandidatePosition("chào", 1)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package com.vutrankien.t9vietnamese.android

import android.content.Context
import android.inputmethodservice.KeyboardView
import android.os.PowerManager
import android.view.inputmethod.InputConnection
import androidx.annotation.VisibleForTesting
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.vutrankien.t9vietnamese.lib.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.ReceiveChannel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.launch
Expand All @@ -26,12 +23,12 @@ abstract class AndroidView(
internal abstract val inputConnection: InputConnection
private val preferences by lazy { Preferences(context.applicationContext) }
private lateinit var candidatesView: RecyclerView
protected val wordListAdapter = WordListAdapter()
protected val candidatesAdapter = CandidatesAdapter()

private fun initializeCandidatesView(recyclerView: RecyclerView) {
recyclerView.apply {
layoutManager = LinearLayoutManager(recyclerView.context, RecyclerView.HORIZONTAL, false)
adapter = wordListAdapter
private fun initializeCandidatesView(candidatesView: RecyclerView) {
candidatesView.apply {
layoutManager = LinearLayoutManager(candidatesView.context, RecyclerView.HORIZONTAL, false)
adapter = candidatesAdapter
}
}

Expand Down Expand Up @@ -59,7 +56,7 @@ abstract class AndroidView(
}

private fun updateCandidates(candidates: Collection<String>) {
wordListAdapter.update(candidates)
candidatesAdapter.update(candidates)
}

override fun showCandidates(candidates: Collection<String>) {
Expand All @@ -75,13 +72,13 @@ abstract class AndroidView(
}

private fun selectCandidate(selectedCandidate: Int) {
wordListAdapter.select(selectedCandidate)
candidatesAdapter.select(selectedCandidate)
@Suppress("ConstantConditionIf")
if (preferences.autoScroll) {
//candidatesView.scrollToPosition(wordListAdapter.selectedWord)
// check candidates_view.xml
(candidatesView.layoutManager as LinearLayoutManager).run {
val selectedWord = wordListAdapter.selectedWord
val selectedWord = candidatesAdapter.selectedWord
if (selectedWord !in findFirstCompletelyVisibleItemPosition()..findLastCompletelyVisibleItemPosition()) {
scrollToPositionWithOffset(selectedWord, 20)
}
Expand All @@ -94,7 +91,7 @@ abstract class AndroidView(
}

private fun clearCandidates() {
wordListAdapter.clear()
candidatesAdapter.clear()
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import androidx.cardview.widget.CardView
/**
* Created by user on 2018/03/21.
*/
class WordListAdapter() : RecyclerView.Adapter<WordListAdapter.ViewHolder>() {
class CandidatesAdapter : RecyclerView.Adapter<CandidatesAdapter.ViewHolder>() {

private val words = mutableListOf<String>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class MainActivity : Activity() {
}

interface TestingHook {
val candidatesAdapter: WordListAdapter
val candidatesAdapter: CandidatesAdapter
val eventSink: SendChannel<EventWithData<Event, Key>>

//fun waitNewCandidates()
Expand All @@ -120,9 +120,9 @@ class MainActivity : Activity() {
/** For integration testing. */
@VisibleForTesting
val testingHook = object: TestingHook {
override val candidatesAdapter: WordListAdapter
override val candidatesAdapter: CandidatesAdapter
//get() = [email protected]<RecyclerView>(R.id.candidates_view).adapter as WordListAdapter
get() = (this@MainActivityView).wordListAdapter
get() = (this@MainActivityView).candidatesAdapter
override val eventSink = [email protected]
//override fun waitNewCandidates() {
// this@MainActivity.
Expand Down