Skip to content

Commit

Permalink
Fix/infrared share (#945)
Browse files Browse the repository at this point in the history
**Background**

When renaming infrared key you can't share it until you re-open screen

**Changes**

- Get keys as provider instead of value

**Test plan**

- Open infrared key
- Press share and see you can share it
- Rename key
- When infrared screen opened again press share
- See it also can be shared
  • Loading branch information
makeevrserg authored Sep 10, 2024
1 parent e6f2aff commit e0bf5ec
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Attention: don't forget to add the flag for F-Droid before release
- [FIX] Fix flaky test
- [FIX] Infinite dispatch after screen close on remote-control screens
- [FIX] Bad bottom sheet animation on infrared setup screen
- [FIX] Share infrared remote after rename
- [CI] Fix merge-queue files diff
- [CI] Add https://github.com/LionZXY/detekt-decompose-rule
- [CI] Enabling detekt module for android and kmp modules
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class InfraredViewDecomposeComponentImpl @AssistedInject constructor(
}

shareBottomUiApi.ComposableShareBottomSheet(
viewModel.keyPath,
provideFlipperKeyPath = viewModel::getKeyPath,
onSheetStateVisible = { isShown, onClose ->
val isBackPressHandled by isBackPressHandledFlow.collectAsState()
backCallback.isEnabled = isShown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.flipperdevices.core.ui.lifecycle.DecomposeViewModel
import com.flipperdevices.infrared.api.InfraredConnectionApi
import com.flipperdevices.infrared.api.InfraredConnectionApi.InfraredEmulateState
import com.flipperdevices.keyscreen.api.KeyStateHelperApi
import com.flipperdevices.keyscreen.model.KeyScreenState
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
Expand All @@ -19,16 +20,23 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class InfraredViewModel @AssistedInject constructor(
@Assisted val keyPath: FlipperKeyPath, // For get value to bottom sheet
@Assisted private val paramKeyPath: FlipperKeyPath, // For get value to bottom sheet
keyStateHelperApi: KeyStateHelperApi.Builder,
serviceProvider: FlipperServiceProvider,
private val infraredConnectionApi: InfraredConnectionApi
) : DecomposeViewModel(), FlipperBleServiceConsumer, LogTagProvider {
override val TAG: String = "InfraredViewModel"

private val keyStateHelper = keyStateHelperApi.build(keyPath, viewModelScope)
private val keyStateHelper = keyStateHelperApi.build(paramKeyPath, viewModelScope)
fun getState() = keyStateHelper.getKeyScreenState()

fun getKeyPath(): FlipperKeyPath {
return (keyStateHelper.getKeyScreenState().value as? KeyScreenState.Ready)
?.flipperKey
?.getKeyPath()
?: paramKeyPath
}

private val emulateStateFlow = MutableStateFlow<InfraredEmulateState?>(null)
fun getEmulateState() = emulateStateFlow.asStateFlow()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class KeyScreenViewDecomposeComponentImpl @AssistedInject constructor(
keyScreenViewModelFactory(keyPath)
}
shareBottomApi.ComposableShareBottomSheet(
flipperKeyPath = viewModel.keyPath,
provideFlipperKeyPath = viewModel::getKeyPath,
onSheetStateVisible = { isShown, onClose ->
val isBackPressHandled by isBackPressHandledFlow.collectAsState()
backCallback.isEnabled = isShown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,20 @@ import kotlinx.coroutines.flow.StateFlow

@Suppress("LongParameterList")
class KeyScreenViewModel @AssistedInject constructor(
@Assisted val keyPath: FlipperKeyPath, // For get value to bottom sheet
@Assisted val paramKeyPath: FlipperKeyPath, // For get value to bottom sheet
keyStateHelperApi: KeyStateHelperApi.Builder,
private val metricApi: MetricApi
) : DecomposeViewModel(), LogTagProvider {
override val TAG = "KeyScreenViewModel"

private val keyStateHelper = keyStateHelperApi.build(keyPath, viewModelScope)
private val keyStateHelper = keyStateHelperApi.build(paramKeyPath, viewModelScope)

fun getKeyPath(): FlipperKeyPath {
return (keyStateHelper.getKeyScreenState().value as? KeyScreenState.Ready)
?.flipperKey
?.getKeyPath()
?: paramKeyPath
}

fun getKeyScreenState(): StateFlow<KeyScreenState> = keyStateHelper.getKeyScreenState()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class LocalGridScreenDecomposeComponentImpl @AssistedInject constructor(
@Composable
override fun Render() {
shareBottomUiApi.ComposableShareBottomSheet(
keyPath,
provideFlipperKeyPath = { keyPath },
onSheetStateVisible = { isShown, onClose ->
val isBackPressHandled by isBackPressHandledFlow.collectAsState()
backCallback.isEnabled = isShown
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.flipperdevices.bridge.dao.api.model.FlipperKeyPath
interface ShareBottomUIApi {
@Composable
fun ComposableShareBottomSheet(
flipperKeyPath: FlipperKeyPath,
provideFlipperKeyPath: () -> FlipperKeyPath,
componentContext: ComponentContext,
onSheetStateVisible: @Composable (isVisible: Boolean, onClose: () -> Unit) -> Unit,
screenContent: @Composable (() -> Unit) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class ShareBottomUIImpl @Inject constructor(
@Composable
@Suppress("NonSkippableComposable")
override fun ComposableShareBottomSheet(
flipperKeyPath: FlipperKeyPath,
provideFlipperKeyPath: () -> FlipperKeyPath,
componentContext: ComponentContext,
onSheetStateVisible: @Composable (isVisible: Boolean, onClose: () -> Unit) -> Unit,
screenContent: @Composable (() -> Unit) -> Unit,
) {
val viewModel = componentContext.viewModelWithFactory(flipperKeyPath) {
uploaderViewModelFactory(flipperKeyPath)
val viewModel = componentContext.viewModelWithFactory(provideFlipperKeyPath.invoke()) {
uploaderViewModelFactory(provideFlipperKeyPath)
}

val scrimColor = if (MaterialTheme.colors.isLight) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class UploaderViewModel @AssistedInject constructor(
private val cryptoStorageApi: CryptoStorageApi,
private val simpleKeyApi: SimpleKeyApi,
private val metricApi: MetricApi,
@Assisted private val flipperKeyPath: FlipperKeyPath
@Assisted private val provideFlipperKeyPath: () -> FlipperKeyPath
) : DecomposeViewModel(), LogTagProvider {
override val TAG: String = "UploaderViewModel"

private val _state = MutableStateFlow<ShareState>(ShareState.Initial)
fun getState() = _state.asStateFlow()
fun getFlipperKeyName() = flipperKeyPath.path.nameWithExtension
fun getFlipperKeyName() = provideFlipperKeyPath.invoke().path.nameWithExtension

fun invalidate() {
viewModelScope.launch {
Expand All @@ -52,7 +52,7 @@ class UploaderViewModel @AssistedInject constructor(
}

private suspend fun parseFlipperKeyPath() {
simpleKeyApi.getKeyAsFlow(flipperKeyPath).collectLatest { flipperKey ->
simpleKeyApi.getKeyAsFlow(provideFlipperKeyPath.invoke()).collectLatest { flipperKey ->
if (flipperKey == null) {
_state.emit(ShareState.Error(ShareError.OTHER))
return@collectLatest
Expand Down Expand Up @@ -88,7 +88,7 @@ class UploaderViewModel @AssistedInject constructor(
)
_state.emit(ShareState.Completed)
}.onFailure { exception ->
error(exception) { "Error on share $flipperKeyPath by file" }
error(exception) { "Error on share ${provideFlipperKeyPath.invoke()} by file" }
_state.emit(ShareState.Error(ShareError.OTHER))
}
}
Expand Down Expand Up @@ -157,7 +157,7 @@ class UploaderViewModel @AssistedInject constructor(
@AssistedFactory
fun interface Factory {
operator fun invoke(
flipperKeyPath: FlipperKeyPath
provideFlipperKeyPath: () -> FlipperKeyPath
): UploaderViewModel
}
}

0 comments on commit e0bf5ec

Please sign in to comment.