Skip to content

Commit 0b15e91

Browse files
committed
Review comments: split up/document into smaller functions
1 parent 32ba770 commit 0b15e91

1 file changed

Lines changed: 62 additions & 36 deletions

File tree

app/src/main/kotlin/io/homeassistant/companion/android/webview/WebViewPresenterImpl.kt

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -507,42 +507,7 @@ class WebViewPresenterImpl @Inject constructor(
507507
mainScope.launch {
508508
if (tlv != null && borderAgentId != null) {
509509
Timber.d("Matter commissioning payload has preferred Thread dataset, comparing")
510-
val isPreferred = threadUseCase.isPreferredDatasetByDevice(context, tlv)
511-
512-
if (isPreferred) {
513-
Timber.d("Device prefers core preferred dataset, starting commissioning")
514-
startMatterCommissioningFlow(context)
515-
} else if (!threadUseCase.hasSyncedPreferredDataset(serverId)) {
516-
// We only sync if the server has never synced, as a full sync can be slow
517-
Timber.d("Device doesn't prefer core preferred dataset, starting first sync")
518-
val deviceThreadIntent = try {
519-
val result = threadUseCase.syncPreferredDataset(
520-
context = context,
521-
serverId = serverId,
522-
exportOnly = false,
523-
scope = CoroutineScope(mainScope.coroutineContext + SupervisorJob()),
524-
)
525-
when (result) {
526-
is ThreadManager.SyncResult.OnlyOnDevice -> result.exportIntent
527-
is ThreadManager.SyncResult.AllHaveCredentials -> result.exportIntent
528-
else -> null
529-
}
530-
} catch (e: Exception) {
531-
Timber.w(e, "Unable to sync preferred Thread dataset, continuing")
532-
null
533-
}
534-
if (deviceThreadIntent != null) {
535-
matterThreadIntentSender = deviceThreadIntent
536-
mutableMatterThreadStep.tryEmit(MatterThreadStep.THREAD_EXPORT_TO_SERVER_MATTER)
537-
} else {
538-
startMatterCommissioningFlow(context)
539-
}
540-
} else {
541-
Timber.d(
542-
"Device doesn't prefer core preferred dataset but has previously synced, starting commissioning",
543-
)
544-
startMatterCommissioningFlow(context)
545-
}
510+
compareThreadCredentialsForCommissioning(context, tlv)
546511
} else {
547512
Timber.d("Matter commissioning payload doesn't have Thread, starting commissioning")
548513
startMatterCommissioningFlow(context)
@@ -551,6 +516,67 @@ class WebViewPresenterImpl @Inject constructor(
551516
} // else already waiting for a result, don't send another request
552517
}
553518

519+
/**
520+
* Compare Thread credentials supplied for Matter commissioning, and take action depending on the device state:
521+
* - Device prefers credentials: start (external) Matter commissioning flow
522+
* - Device doesn't prefer credentials:
523+
* - Has never synced Thread for the server: do a full Thread credentials sync (this will usually result in the
524+
* credentials becoming preferred)
525+
* - Has previously synced Thread for the server: start (external) Matter commissioning flow (the app is
526+
* expected to be unable to change the preferred state by syncing)
527+
*/
528+
private suspend fun compareThreadCredentialsForCommissioning(context: Context, tlv: String) {
529+
val isPreferred = threadUseCase.isPreferredDatasetByDevice(context, tlv)
530+
531+
if (isPreferred) {
532+
Timber.d("Device prefers core preferred dataset, starting commissioning")
533+
startMatterCommissioningFlow(context)
534+
} else if (!threadUseCase.hasSyncedPreferredDataset(serverId)) {
535+
// We only sync if the server has never synced, as a full sync can be slow
536+
Timber.d("Device doesn't prefer core preferred dataset, starting first sync")
537+
val syncExportIntent = syncThreadDataset(context)
538+
539+
if (syncExportIntent != null) {
540+
matterThreadIntentSender = syncExportIntent
541+
mutableMatterThreadStep.tryEmit(MatterThreadStep.THREAD_EXPORT_TO_SERVER_MATTER)
542+
} else {
543+
startMatterCommissioningFlow(context)
544+
}
545+
} else {
546+
Timber.d(
547+
"Device doesn't prefer core preferred dataset, has previously synced, starting commissioning",
548+
)
549+
startMatterCommissioningFlow(context)
550+
}
551+
}
552+
553+
/**
554+
* Try to sync the preferred Thread dataset between the server and the device.
555+
* @return [IntentSender] if syncing requires starting an intent to complete (export to server), otherwise `null`
556+
*/
557+
private suspend fun syncThreadDataset(context: Context): IntentSender? {
558+
return try {
559+
val result = threadUseCase.syncPreferredDataset(
560+
context = context,
561+
serverId = serverId,
562+
exportOnly = false,
563+
scope = CoroutineScope(mainScope.coroutineContext + SupervisorJob()),
564+
)
565+
when (result) {
566+
is ThreadManager.SyncResult.OnlyOnDevice -> result.exportIntent
567+
is ThreadManager.SyncResult.AllHaveCredentials -> result.exportIntent
568+
else -> null
569+
}
570+
} catch (e: Exception) {
571+
Timber.w(e, "Unable to sync preferred Thread dataset, continuing")
572+
null
573+
}
574+
}
575+
576+
/**
577+
* Start the (external) Matter commissioning flow. Depending on the result, this will emit a new step with the
578+
* intent to open the activity, or a new step for an error.
579+
* */
554580
private fun startMatterCommissioningFlow(context: Context) {
555581
matterUseCase.startNewCommissioningFlow(
556582
context,

0 commit comments

Comments
 (0)