Skip to content
Merged
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 @@ -2,8 +2,6 @@ package com.simprints.matcher.screen

import android.animation.ObjectAnimator
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.view.View
import androidx.core.view.isGone
import androidx.core.view.isVisible
Expand Down Expand Up @@ -34,7 +32,9 @@ internal class MatchFragment : Fragment(R.layout.fragment_matcher) {
super.onViewCreated(view, savedInstanceState)

observeViewModel()
viewModel.setupMatch(args.params)
if(!viewModel.isInitialized) {
viewModel.setupMatch(args.params)
}
}

private fun setIdentificationProgress(progress: Int) = requireActivity().runOnUiThread {
Expand All @@ -46,11 +46,7 @@ internal class MatchFragment : Fragment(R.layout.fragment_matcher) {

private fun observeViewModel() {
viewModel.matchResponse.observe(viewLifecycleOwner, LiveDataEventWithContentObserver {
// wait a bit for the user to see the results
Handler(Looper.getMainLooper()).postDelayed(
{ findNavController().finishWithResult(this, it) },
MatchViewModel.matchingEndWaitTimeInMillis
)
findNavController().finishWithResult(this, it)
})
viewModel.matchState.observe(viewLifecycleOwner) { matchState ->
when (matchState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import androidx.lifecycle.viewModelScope
import com.simprints.core.livedata.LiveDataEventWithContent
import com.simprints.core.livedata.send
import com.simprints.core.tools.time.TimeHelper
import com.simprints.infra.logging.Simber
import com.simprints.matcher.FaceMatchResult
import com.simprints.matcher.FingerprintMatchResult
import com.simprints.matcher.MatchParams
import com.simprints.matcher.MatchResultItem
import com.simprints.matcher.usecases.FaceMatcherUseCase
import com.simprints.matcher.usecases.FingerprintMatcherUseCase
import com.simprints.matcher.usecases.SaveMatchEventUseCase
import com.simprints.infra.logging.Simber
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import java.io.Serializable
import javax.inject.Inject
Expand All @@ -29,6 +30,8 @@ internal class MatchViewModel @Inject constructor(
private val timeHelper: TimeHelper,
) : ViewModel() {

var isInitialized = false
private set
val matchState: LiveData<MatchState>
get() = _matchState
private val _matchState = MutableLiveData<MatchState>(MatchState.NotStarted)
Expand All @@ -38,6 +41,7 @@ internal class MatchViewModel @Inject constructor(
private val _matchResponse = MutableLiveData<LiveDataEventWithContent<Serializable>>()

fun setupMatch(params: MatchParams) = viewModelScope.launch {
isInitialized = true
val startTime = timeHelper.now()

val isFaceMatch = params.isFaceMatch()
Expand Down Expand Up @@ -67,6 +71,9 @@ internal class MatchViewModel @Inject constructor(

setMatchState(totalCandidates, sortedResults)

// wait a bit for the user to see the results
delay(matchingEndWaitTimeInMillis)

_matchResponse.send(when {
isFaceMatch -> FaceMatchResult(sortedResults)
else -> FingerprintMatchResult(sortedResults)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ import com.simprints.matcher.usecases.FingerprintMatcherUseCase
import com.simprints.matcher.usecases.SaveMatchEventUseCase
import com.simprints.testtools.common.coroutines.TestCoroutineRule
import com.simprints.testtools.common.livedata.getOrAwaitValue
import io.mockk.*
import io.mockk.CapturingSlot
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.coJustRun
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Rule
Expand Down Expand Up @@ -65,6 +73,29 @@ internal class MatchViewModelTest {
timeHelper
)
}
@Test
fun `when setup is called, then view model becomes initialized`() = runTest {
val responseItems = listOf(
FaceMatchResult.Item("1", 90f),
)

coEvery { faceMatcherUseCase.invoke(any(), capture(cb1)) } answers {
cb1.captured.invoke("tag1")
responseItems to responseItems.size
}
coJustRun { saveMatchEvent.invoke(any(), any(), any(), any(), any(), any()) }

assertThat(viewModel.isInitialized).isFalse()

viewModel.matchState.test()
viewModel.setupMatch(MatchParams(
probeFaceSamples = listOf(getFaceSample()),
flowType = FlowType.ENROL,
queryForCandidates = mockk {}
))

assertThat(viewModel.isInitialized).isTrue()
}

@Test
fun `Handle face match request correctly`() = runTest {
Expand All @@ -90,6 +121,8 @@ internal class MatchViewModelTest {
flowType = FlowType.ENROL,
queryForCandidates = mockk {}
))
// Waiting for the ::delay in viewModel::setupMatch
advanceUntilIdle()

assertThat(states.valueHistory()).isEqualTo(
listOf(
Expand Down Expand Up @@ -130,6 +163,8 @@ internal class MatchViewModelTest {
flowType = FlowType.ENROL,
queryForCandidates = mockk {}
))
// Waiting for the ::delay in viewModel::setupMatch
advanceUntilIdle()

assertThat(states.valueHistory()).isEqualTo(
listOf(
Expand Down