mirror of
https://github.com/home-assistant/android
synced 2024-10-15 12:32:54 +00:00
Multiserver NFC tag fixes (#3336)
- Fix scanning tag not completing due to activity finishing early - Fire events to all servers from settings to match normal scanning
This commit is contained in:
parent
7d8bfb2bc6
commit
c8cc70ca84
|
@ -11,6 +11,8 @@ import androidx.lifecycle.viewModelScope
|
|||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import io.homeassistant.companion.android.common.data.servers.ServerManager
|
||||
import io.homeassistant.companion.android.util.Navigator
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.flow.MutableSharedFlow
|
||||
import kotlinx.coroutines.flow.asSharedFlow
|
||||
import kotlinx.coroutines.launch
|
||||
|
@ -109,13 +111,20 @@ class NfcViewModel @Inject constructor(
|
|||
fun fireNfcTagEvent() {
|
||||
viewModelScope.launch {
|
||||
nfcTagIdentifier?.let {
|
||||
val results = serverManager.defaultServers.map { server ->
|
||||
async {
|
||||
try {
|
||||
serverManager.integrationRepository().scanTag(
|
||||
hashMapOf("tag_id" to it)
|
||||
)
|
||||
_nfcResultSnackbar.emit(commonR.string.nfc_event_fired_success)
|
||||
serverManager.integrationRepository(server.id).scanTag(hashMapOf("tag_id" to it))
|
||||
true
|
||||
} catch (e: Exception) {
|
||||
Log.e(TAG, "Unable to send tag to Home Assistant.", e)
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
if (results.awaitAll().any { it }) {
|
||||
_nfcResultSnackbar.emit(commonR.string.nfc_event_fired_success)
|
||||
} else {
|
||||
_nfcResultSnackbar.emit(commonR.string.nfc_event_fired_fail)
|
||||
}
|
||||
} ?: _nfcResultSnackbar.emit(commonR.string.nfc_event_fired_fail)
|
||||
|
|
|
@ -14,6 +14,8 @@ import io.homeassistant.companion.android.BaseActivity
|
|||
import io.homeassistant.companion.android.common.data.servers.ServerManager
|
||||
import io.homeassistant.companion.android.nfc.views.TagReaderView
|
||||
import io.homeassistant.companion.android.util.UrlHandler
|
||||
import kotlinx.coroutines.async
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
import io.homeassistant.companion.android.common.R as commonR
|
||||
|
@ -61,8 +63,8 @@ class TagReaderActivity : BaseActivity() {
|
|||
val nfcTagId = UrlHandler.splitNfcTagId(url)
|
||||
Log.d(TAG, "Tag ID: $nfcTagId")
|
||||
if (nfcTagId != null && serverManager.isRegistered()) {
|
||||
serverManager.defaultServers.forEach {
|
||||
lifecycleScope.launch {
|
||||
serverManager.defaultServers.map {
|
||||
lifecycleScope.async {
|
||||
try {
|
||||
serverManager.integrationRepository(it.id).scanTag(hashMapOf("tag_id" to nfcTagId))
|
||||
Log.d(TAG, "Tag scanned to HA successfully")
|
||||
|
@ -70,7 +72,7 @@ class TagReaderActivity : BaseActivity() {
|
|||
Log.e(TAG, "Tag not scanned to HA", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.awaitAll()
|
||||
} else {
|
||||
showProcessingError(isNfcTag)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue