Skip to content

Commit

Permalink
Properly setup hilt hierarchy for SttInputDeviceWrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Jul 10, 2024
1 parent db69a8b commit 39b7735
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,16 @@
package org.stypox.dicio.screenshot

import android.content.Context
import androidx.datastore.core.DataStore
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import okhttp3.OkHttpClient
import org.stypox.dicio.di.LocaleManager
import org.stypox.dicio.di.SttInputDeviceWrapper
import org.stypox.dicio.io.input.InputEvent
import org.stypox.dicio.io.input.SttInputDevice
import org.stypox.dicio.settings.datastore.InputDevice
import org.stypox.dicio.settings.datastore.UserSettings
import org.stypox.dicio.ui.home.SttState

class FakeSttInputDeviceWrapper(
@ApplicationContext appContext: Context,
dataStore: DataStore<UserSettings>,
localeManager: LocaleManager,
okHttpClient: OkHttpClient
) : SttInputDeviceWrapper(appContext, dataStore, localeManager, okHttpClient) {
val fakeUiState: MutableStateFlow<SttState> = MutableStateFlow(SttState.NotInitialized)
class FakeSttInputDeviceWrapper : SttInputDeviceWrapper {
override val uiState: MutableStateFlow<SttState> = MutableStateFlow(SttState.NotInitialized)

override fun buildInputDevice(setting: InputDevice): SttInputDevice {
return object : SttInputDevice {
override val uiState: StateFlow<SttState> get() = fakeUiState

override fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) {
}

override fun onClick(eventListener: (InputEvent) -> Unit) {
}
override fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) {
}

override suspend fun destroy() {
}
}
override fun onClick(eventListener: (InputEvent) -> Unit) {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,8 @@ class ScreenshotTakerTest {
class FakeSttInputDeviceWrapperModule {
@Provides
@Singleton
fun provideInputDeviceWrapper(
@ApplicationContext appContext: Context,
dataStore: DataStore<UserSettings>,
localeManager: LocaleManager,
okHttpClient: OkHttpClient,
): SttInputDeviceWrapper {
return FakeSttInputDeviceWrapper(appContext, dataStore, localeManager, okHttpClient)
fun provideInputDeviceWrapper(): SttInputDeviceWrapper {
return FakeSttInputDeviceWrapper()
}
}

Expand Down Expand Up @@ -137,12 +132,12 @@ class ScreenshotTakerTest {

// screenshot 0: home screen with "Here is what I can do" and STT listening
dataStore.updateData { it.copy { theme = Theme.THEME_DARK } }
fakeSttInputDeviceWrapper.fakeUiState.emit(SttState.Listening)
fakeSttInputDeviceWrapper.uiState.emit(SttState.Listening)
composeRule.takeScreenshot("en-US", "0")

// screenshot 1: home screen with interactions with weather, timer and lyrics skills
dataStore.updateData { it.copy { theme = Theme.THEME_LIGHT } }
fakeSttInputDeviceWrapper.fakeUiState.emit(SttState.Loaded)
fakeSttInputDeviceWrapper.uiState.emit(SttState.Loaded)
coilEventListener.resetStartedImages()
fakeSkillEvaluator.state.value = screenshot2InteractionLog
composeRule.onNodeWithTag("interaction_list")
Expand All @@ -152,7 +147,7 @@ class ScreenshotTakerTest {

// screenshot 2: home screen with interactions with calculator, telephone and search skills
dataStore.updateData { it.copy { theme = Theme.THEME_BLACK } }
fakeSttInputDeviceWrapper.fakeUiState.emit(SttState.Loaded)
fakeSttInputDeviceWrapper.uiState.emit(SttState.Loaded)
coilEventListener.resetStartedImages()
fakeSkillEvaluator.state.value = screenshot3InteractionLog
composeRule.onNodeWithTag("interaction_list")
Expand Down
23 changes: 15 additions & 8 deletions app/src/main/kotlin/org/stypox/dicio/di/SttInputDeviceWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,27 @@ import org.stypox.dicio.ui.home.SttState
import org.stypox.dicio.util.distinctUntilChangedBlockingFirst
import javax.inject.Singleton

open class SttInputDeviceWrapper(
interface SttInputDeviceWrapper {
val uiState: StateFlow<SttState?>

fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?)

fun onClick(eventListener: (InputEvent) -> Unit)
}

class SttInputDeviceWrapperImpl(
@ApplicationContext private val appContext: Context,
dataStore: DataStore<UserSettings>,
private val localeManager: LocaleManager,
private val okHttpClient: OkHttpClient,
) {
) : SttInputDeviceWrapper {
private val scope = CoroutineScope(Dispatchers.Default)

private var sttInputDevice: SttInputDevice? = null

// null means that the user has not enabled any STT input device
private val _uiState: MutableStateFlow<SttState?> = MutableStateFlow(null)
val uiState: StateFlow<SttState?> = _uiState
override val uiState: StateFlow<SttState?> = _uiState
private var uiStateJob: Job? = null


Expand All @@ -66,8 +74,7 @@ open class SttInputDeviceWrapper(
}
}

// overridden in tests (TODO is there a better solution?)
protected open fun buildInputDevice(setting: InputDevice): SttInputDevice? {
private fun buildInputDevice(setting: InputDevice): SttInputDevice? {
return when (setting) {
UNRECOGNIZED,
INPUT_DEVICE_UNSET,
Expand All @@ -91,11 +98,11 @@ open class SttInputDeviceWrapper(
}


fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) {
override fun tryLoad(thenStartListeningEventListener: ((InputEvent) -> Unit)?) {
sttInputDevice?.tryLoad(thenStartListeningEventListener)
}

fun onClick(eventListener: (InputEvent) -> Unit) {
override fun onClick(eventListener: (InputEvent) -> Unit) {
sttInputDevice?.onClick(eventListener)
}
}
Expand All @@ -111,6 +118,6 @@ class SttInputDeviceWrapperModule {
localeManager: LocaleManager,
okHttpClient: OkHttpClient,
): SttInputDeviceWrapper {
return SttInputDeviceWrapper(appContext, dataStore, localeManager, okHttpClient)
return SttInputDeviceWrapperImpl(appContext, dataStore, localeManager, okHttpClient)
}
}

0 comments on commit 39b7735

Please sign in to comment.