I’ve view mannequin referred to as KMMPrerequisiteViewModel and I’m making a MutableSharedFlow inside this, when I attempt to replace this movement iOS Facet by calling a confrimPermission() then it isn’t working, however i create a lambda operate and cross it to iOS as a closure and name it’s working wonderful. I’ll connect the pattern code under right here
class KMMPrerequisiteViewModel : PrerequisiteViewModel {
personal val userConfirmedPermissionFlow = MutableSharedFlow(replay = 1)
personal val onRequestCompleted: (Permission, BluetoothStates?) -> Unit = {
permission, bluetoothState ->
when (permission) {
Permission.Bluetooth -> {
bluetoothState?.let {
when(bluetoothState) {
val cameraPermission = permissionViewModel.uiState.worth.permissionStates.first
{ it.permission == Permission.Digital camera }
grantPermission(cameraPermission)
}
}
}
Permission.Digital camera -> {
confirmPermission()
}
Permission.Location -> TODO("Requoired for Android to Implement")
Permission.NearByDevices -> TODO("Requoired for Android to Implement")
}
}
override enjoyable grantPermission(permissionState: PermissionState) {
permissionViewModel.requestPermission(permissionState.permission, onComplete =
onRequestCompleted)
}
}
}
override enjoyable setup() {
val userConfirmedPermissionFlow = userConfirmedPermissionFlow.onStart {emit(true)}
viewModelScope.coroutineScope.launch {
userConfirmedPermissionFlow.accumulate { worth ->
logInfo("KMMPrerequisiteViewModel", "userConfirmedPermissionFlow emitted
worth: $worth")
}
}
mix(
userConfirmedPermissionFlow,
someOtherFlow
) { userConfirmedPermission, someOther ->
val newState = when {
!userConfirmedPermission -> {
}
someOther -> {
}
else -> {
}
logInfo("KMMPrerequisiteViewModel","newstate worth is $newState")
_state.tryEmit(newState)
}.launchIn(viewModelScope.coroutineScope)
}
override enjoyable confirmPermission() {
logInfo("KMMPrerequisiteViewModel", "ViewModelScope is lively
${viewModelScope.coroutineScope.coroutineContext.isActive}")
logInfo("KMMPrerequisiteViewModel", "userConfirmedPermissionFlow
${userConfirmedPermissionFlow.subscriptionCount.worth}")
userConfirmedPermissionFlow.tryEmit(true)
}
}
On iOS Facet
func requestPermission(permission: Permission, onComplete: ((Permission,
BluetoothStates?) -> Void)?) async throws {
change permission {
case .bluetooth:
onComplete?(permission, .power_on)
case .digital camera:
AVCaptureDevice.requestAccess(for: .video, completionHandler: { _ in
onComplete?(permission, nil)
})
default: break
}
}
I’m calling the setUP() on .OnAppear() from iOS when app launches, the place it begins the movement after which im requesting for bluetooth and digital camera entry when i get the digital camera entry and referred to as OnComplete() which we cross from viewmodel, states are getting up to date and subscription depend is >0 however when i referred to as ConfirmPermission() immediately on iOS Facet like this self.preqeusiteViewModel.confirmPermission() subscription depend is 0 and coroutine scope just isn’t lively.
Right here I’m not in a position to perceive why subscription depend is 0 once we immediately name the ConfirmPermission() on iOS aspect however >0 when it referred to as from onRequestCompleted lambda