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:
Joris Pelgröm 2023-02-17 14:46:32 +01:00 committed by GitHub
parent 7d8bfb2bc6
commit c8cc70ca84
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View file

@ -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 {
try {
serverManager.integrationRepository().scanTag(
hashMapOf("tag_id" to it)
)
val results = serverManager.defaultServers.map { server ->
async {
try {
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)
} catch (e: Exception) {
Log.e(TAG, "Unable to send tag to Home Assistant.", e)
} else {
_nfcResultSnackbar.emit(commonR.string.nfc_event_fired_fail)
}
} ?: _nfcResultSnackbar.emit(commonR.string.nfc_event_fired_fail)

View file

@ -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)
}