Skip to content

Commit

Permalink
Remotecontrols/add history callback (#941)
Browse files Browse the repository at this point in the history
**Background**

When setup of remotecontrol is in progress, we can't return to previous
setup item

**Changes**

- Add back handler on setup screen

**Test plan**

- Open setup screen, press some buttons
- Press back or arrow at top left to return previous setup entry
  • Loading branch information
makeevrserg authored Sep 10, 2024
1 parent eb5e3e0 commit 40f514a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Attention: don't forget to add the flag for F-Droid before release
- [Feature] Add How to Use dialog into remote-controls
- [Feature] Skip infrared signals on setup screen
- [Feature] Better user-ux when configuring remote control
- [Feature] Navigate to previous setup item on remote controls
- [Feature] Add flipper action dialogs into remote control and move it into bottombar
- [Feature] Add new icons for remote-controls
- [Refactor] Load RemoteControls from flipper, emulating animation
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.flipperdevices.remotecontrols.impl.setup.presentation.decompose.internal

import com.arkivanov.decompose.ComponentContext
import com.arkivanov.essenty.backhandler.BackCallback
import com.arkivanov.essenty.instancekeeper.getOrCreate
import com.flipperdevices.bridge.dao.api.model.FlipperFilePath
import com.flipperdevices.bridge.dao.api.model.FlipperKeyType
Expand Down Expand Up @@ -137,10 +138,11 @@ class SetupComponentImpl @AssistedInject constructor(
override fun tryLoad() {
if (dispatchSignalApi.state.value is DispatchSignalApi.State.Emulating) return
dispatchSignalApi.reset()
val historyData = historyViewModel.data
createCurrentSignalViewModel.load(
successResults = historyViewModel.state.value.successfulSignals,
failedResults = historyViewModel.state.value.failedSignals,
skippedResults = historyViewModel.state.value.skippedSignals
successResults = historyData.successfulSignals,
failedResults = historyData.failedSignals,
skippedResults = historyData.skippedSignals
)
_lastEmulatedSignal.value = null
}
Expand Down Expand Up @@ -202,7 +204,20 @@ class SetupComponentImpl @AssistedInject constructor(
)
}

override fun onBackClick() = onBackClick.invoke()
private val backCallback = BackCallback(true) {
if (historyViewModel.isEmpty) {
onBackClick.invoke()
} else {
historyViewModel.forgetLast()
}
tryLoad()
}

override fun onBackClick() = backCallback.onBack()

init {
backHandler.register(backCallback)
}
}

private val ABSOLUTE_TEMP_FOLDER_PATH = "/${FlipperKeyType.INFRARED.flipperDir}/temp"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,63 @@ package com.flipperdevices.remotecontrols.impl.setup.presentation.viewmodel
import com.flipperdevices.core.ui.lifecycle.DecomposeViewModel
import com.flipperdevices.ifrmvp.backend.model.SignalModel
import com.flipperdevices.ifrmvp.backend.model.SignalRequestModel.SignalResultData
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update
import javax.inject.Inject

class HistoryViewModel @Inject constructor() : DecomposeViewModel() {
private val _state = MutableStateFlow(State())
val state = _state.asStateFlow()
private val flatData = mutableListOf<FlatData>()

val data: State
get() = State(
successfulSignals = flatData
.filter { it.type == FlatData.FlatDataType.SUCCESS }
.map(FlatData::data),
failedSignals = flatData
.filter { it.type == FlatData.FlatDataType.FAILED }
.map(FlatData::data),
skippedSignals = flatData
.filter { it.type == FlatData.FlatDataType.SKIPPED }
.map(FlatData::data),
)
val isEmpty: Boolean
get() = flatData.isEmpty()

fun forgetLast() {
flatData.removeLast()
}

fun rememberSuccessful(signalModel: SignalModel) {
val signalResultData = SignalResultData(
signalId = signalModel.id,
)
_state.update { it.copy(successfulSignals = it.successfulSignals + signalResultData) }
flatData += FlatData(signalResultData, FlatData.FlatDataType.SUCCESS)
}

fun rememberFailed(signalModel: SignalModel) {
val signalResultData = SignalResultData(
signalId = signalModel.id,
)
_state.update { it.copy(failedSignals = it.failedSignals + signalResultData) }
flatData += FlatData(signalResultData, FlatData.FlatDataType.FAILED)
}

fun rememberSkipped(signalModel: SignalModel) {
val signalResultData = SignalResultData(
signalId = signalModel.id,
)
_state.update { it.copy(skippedSignals = it.skippedSignals + signalResultData) }
flatData += FlatData(signalResultData, FlatData.FlatDataType.SKIPPED)
}

data class State(
val successfulSignals: List<SignalResultData> = emptyList(),
val failedSignals: List<SignalResultData> = emptyList(),
val skippedSignals: List<SignalResultData> = emptyList()
)

class FlatData(
val data: SignalResultData,
val type: FlatDataType
) {
enum class FlatDataType {
SUCCESS, FAILED, SKIPPED
}
}
}

0 comments on commit 40f514a

Please sign in to comment.