-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] virtual-device-app: Implement doorlock viewmodel (#30236)
* virtual-device-app: Implement doorlock viewmodel Signed-off-by: Jaehoon You <[email protected]> Signed-off-by: Charles Kim <[email protected]> * virtual-device-app: Apply 'LockState' enum type to align with Doorlock cluster specification Signed-off-by: Jaehoon You <[email protected]> Signed-off-by: Charles Kim <[email protected]> * virtual-device-app: Change variable name more obviously Signed-off-by: Jaehoon You <[email protected]> Signed-off-by: Charles Kim <[email protected]> * virtual-device-app: Fix initial value mismatching Signed-off-by: Jaehoon You <[email protected]> Signed-off-by: Charles Kim <[email protected]> --------- Signed-off-by: Jaehoon You <[email protected]> Signed-off-by: Charles Kim <[email protected]>
- Loading branch information
1 parent
a454ccd
commit fc2a071
Showing
9 changed files
with
130 additions
and
25 deletions.
There are no files selected for viewing
5 changes: 3 additions & 2 deletions
5
...a/com/matter/virtual/device/app/core/data/repository/cluster/DoorLockManagerRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package com.matter.virtual.device.app.core.data.repository.cluster | ||
|
||
import com.matter.virtual.device.app.core.model.matter.LockState | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
interface DoorLockManagerRepository { | ||
fun getLockStateFlow(): StateFlow<Boolean> | ||
fun getLockStateFlow(): StateFlow<LockState> | ||
|
||
suspend fun setLockState(value: Boolean) | ||
suspend fun setLockState(lockState: LockState) | ||
|
||
suspend fun sendLockAlarmEvent() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...virtual/device/app/core/domain/usecase/matter/cluster/doorlock/GetLockStateFlowUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package com.matter.virtual.device.app.core.domain.usecase.matter.cluster.doorlock | ||
|
||
import com.matter.virtual.device.app.core.data.repository.cluster.DoorLockManagerRepository | ||
import com.matter.virtual.device.app.core.model.matter.LockState | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.StateFlow | ||
|
||
class GetLockStateFlowUseCase | ||
@Inject | ||
constructor(private val doorLockManagerRepository: DoorLockManagerRepository) { | ||
|
||
operator fun invoke(): StateFlow<Boolean> = doorLockManagerRepository.getLockStateFlow() | ||
operator fun invoke(): StateFlow<LockState> = doorLockManagerRepository.getLockStateFlow() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...App/core/model/src/main/java/com/matter/virtual/device/app/core/model/matter/LockState.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.matter.virtual.device.app.core.model.matter | ||
|
||
enum class LockState { | ||
NOT_FULLY_LOCKED, | ||
LOCKED, | ||
UNLOCKED, | ||
UNLATCHED, | ||
UNKNOWN_ENUM_VALUE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 85 additions & 2 deletions
87
.../closure/src/main/java/com/matter/virtual/device/app/feature/closure/DoorLockViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,100 @@ | ||
package com.matter.virtual.device.app.feature.closure | ||
|
||
import androidx.lifecycle.LiveData | ||
import androidx.lifecycle.SavedStateHandle | ||
import androidx.lifecycle.asLiveData | ||
import androidx.lifecycle.viewModelScope | ||
import com.matter.virtual.device.app.core.common.successOr | ||
import com.matter.virtual.device.app.core.domain.usecase.matter.IsFabricRemovedUseCase | ||
import com.matter.virtual.device.app.core.domain.usecase.matter.StartMatterAppServiceUseCase | ||
import com.matter.virtual.device.app.core.domain.usecase.matter.cluster.doorlock.GetLockStateFlowUseCase | ||
import com.matter.virtual.device.app.core.domain.usecase.matter.cluster.doorlock.SendLockAlarmEventUseCase | ||
import com.matter.virtual.device.app.core.domain.usecase.matter.cluster.doorlock.SetLockStateUseCase | ||
import com.matter.virtual.device.app.core.domain.usecase.matter.cluster.powersource.GetBatPercentRemainingUseCase | ||
import com.matter.virtual.device.app.core.domain.usecase.matter.cluster.powersource.SetBatPercentRemainingUseCase | ||
import com.matter.virtual.device.app.core.model.matter.LockState | ||
import com.matter.virtual.device.app.core.ui.BaseViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import javax.inject.Inject | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.StateFlow | ||
import kotlinx.coroutines.launch | ||
import timber.log.Timber | ||
|
||
@HiltViewModel | ||
class DoorLockViewModel @Inject constructor(savedStateHandle: SavedStateHandle) : | ||
BaseViewModel(savedStateHandle) { | ||
class DoorLockViewModel | ||
@Inject | ||
constructor( | ||
getLockStateFlowUseCase: GetLockStateFlowUseCase, | ||
getBatPercentRemainingUseCase: GetBatPercentRemainingUseCase, | ||
private val setLockStateUseCase: SetLockStateUseCase, | ||
private val sendLockAlarmEventUseCase: SendLockAlarmEventUseCase, | ||
private val setBatPercentRemainingUseCase: SetBatPercentRemainingUseCase, | ||
private val startMatterAppServiceUseCase: StartMatterAppServiceUseCase, | ||
private val isFabricRemovedUseCase: IsFabricRemovedUseCase, | ||
savedStateHandle: SavedStateHandle | ||
) : BaseViewModel(savedStateHandle) { | ||
|
||
private val _lockState: StateFlow<LockState> = getLockStateFlowUseCase() | ||
val lockState: LiveData<LockState> | ||
get() = _lockState.asLiveData() | ||
|
||
private val _batteryRemainingPercentage: MutableStateFlow<Int> = | ||
getBatPercentRemainingUseCase() as MutableStateFlow<Int> | ||
val batteryRemainingPercentage: LiveData<Int> | ||
get() = _batteryRemainingPercentage.asLiveData() | ||
|
||
init { | ||
viewModelScope.launch { startMatterAppServiceUseCase(matterSettings) } | ||
|
||
viewModelScope.launch { | ||
val isFabricRemoved = isFabricRemovedUseCase().successOr(false) | ||
if (isFabricRemoved) { | ||
Timber.d("Fabric Removed") | ||
onFabricRemoved() | ||
} | ||
} | ||
} | ||
|
||
override fun onCleared() { | ||
Timber.d("onCleared()") | ||
super.onCleared() | ||
} | ||
|
||
fun onClickButton() { | ||
Timber.d("onClickButton()") | ||
viewModelScope.launch { | ||
Timber.d("current lockState value = ${_lockState.value}") | ||
if (_lockState.value == LockState.LOCKED) { | ||
Timber.d("set value = unlocked") | ||
setLockStateUseCase(LockState.UNLOCKED) | ||
} else { | ||
Timber.d("set value = locked") | ||
setLockStateUseCase(LockState.LOCKED) | ||
} | ||
} | ||
} | ||
|
||
fun onClickSendLockAlarmEventButton() { | ||
Timber.d("onClickSendLockAlarmEventButton()") | ||
viewModelScope.launch { | ||
if (_lockState.value == LockState.LOCKED) { | ||
// if lockState == locked, send alarm event and change the lockState to unlocked | ||
sendLockAlarmEventUseCase() | ||
setLockStateUseCase(LockState.UNLOCKED) | ||
} | ||
} | ||
} | ||
|
||
fun updateBatterySeekbarProgress(progress: Int) { | ||
_batteryRemainingPercentage.value = progress | ||
} | ||
|
||
fun updateBatteryStatusToCluster(progress: Int) { | ||
Timber.d("progress:$progress") | ||
viewModelScope.launch { | ||
updateBatterySeekbarProgress(progress) | ||
setBatPercentRemainingUseCase(progress) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters