-
Notifications
You must be signed in to change notification settings - Fork 211
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Connection service for new API (#1000)
**Background** Device connection service for new API **Changes** - Add new connection service - Start it in application **Test plan** - Install flipper from branch, open int - See it now bottom bar display connection state and synchronization
- Loading branch information
1 parent
2ff30b5
commit d7793ea
Showing
8 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
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,13 @@ | ||
plugins { | ||
id("flipper.multiplatform") | ||
id("flipper.multiplatform-dependencies") | ||
} | ||
|
||
android.namespace = "com.flipperdevices.bridge.connection.service.api" | ||
|
||
commonDependencies { | ||
implementation(projects.components.bridge.connection.orchestrator.api) | ||
implementation(projects.components.bridge.connection.config.api) | ||
|
||
implementation(libs.kotlin.coroutines) | ||
} |
22 changes: 22 additions & 0 deletions
22
.../commonMain/kotlin/com/flipperdevices/bridge/connection/service/api/FConnectionService.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,22 @@ | ||
package com.flipperdevices.bridge.connection.service.api | ||
|
||
/** | ||
* This service handle connection for current flipper device | ||
*/ | ||
interface FConnectionService { | ||
/** | ||
* Should be called once on application start | ||
*/ | ||
fun onApplicationInit() | ||
|
||
/** | ||
* Reconnect to last known device after being force disconnected | ||
*/ | ||
fun forceReconnect() | ||
|
||
/** | ||
* Disconnect current device | ||
* @param force if true, will not reconnect until [forceReconnect] | ||
*/ | ||
fun disconnect(force: Boolean = false) | ||
} |
19 changes: 19 additions & 0 deletions
19
components/bridge/connection/service/impl/build.gradle.kts
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,19 @@ | ||
plugins { | ||
id("flipper.multiplatform") | ||
id("flipper.multiplatform-dependencies") | ||
id("flipper.anvil-multiplatform") | ||
} | ||
|
||
android.namespace = "com.flipperdevices.bridge.connection.service.impl" | ||
|
||
commonDependencies { | ||
implementation(projects.components.core.ktx) | ||
implementation(projects.components.core.log) | ||
implementation(projects.components.core.di) | ||
|
||
implementation(projects.components.bridge.connection.service.api) | ||
implementation(projects.components.bridge.connection.orchestrator.api) | ||
implementation(projects.components.bridge.connection.config.api) | ||
|
||
implementation(libs.kotlin.coroutines) | ||
} |
73 changes: 73 additions & 0 deletions
73
...onMain/kotlin/com/flipperdevices/bridge/connection/service/impl/FConnectionServiceImpl.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,73 @@ | ||
package com.flipperdevices.bridge.connection.service.impl | ||
|
||
import com.flipperdevices.bridge.connection.config.api.FDevicePersistedStorage | ||
import com.flipperdevices.bridge.connection.orchestrator.api.FDeviceOrchestrator | ||
import com.flipperdevices.bridge.connection.service.api.FConnectionService | ||
import com.flipperdevices.core.di.AppGraph | ||
import com.flipperdevices.core.ktx.jre.FlipperDispatchers | ||
import com.flipperdevices.core.log.LogTagProvider | ||
import com.flipperdevices.core.log.warn | ||
import com.flipperdevices.core.ui.lifecycle.DecomposeViewModel | ||
import com.squareup.anvil.annotations.ContributesBinding | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.Job | ||
import kotlinx.coroutines.SupervisorJob | ||
import kotlinx.coroutines.cancelAndJoin | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.combine | ||
import kotlinx.coroutines.flow.filter | ||
import kotlinx.coroutines.flow.launchIn | ||
import kotlinx.coroutines.flow.onEach | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
import kotlinx.coroutines.sync.withLock | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
@ContributesBinding(AppGraph::class, FConnectionService::class) | ||
class FConnectionServiceImpl @Inject constructor( | ||
private val orchestrator: FDeviceOrchestrator, | ||
private val fDevicePersistedStorage: FDevicePersistedStorage | ||
) : FConnectionService, LogTagProvider { | ||
override val TAG: String = "FConnectionService" | ||
|
||
private val scope = CoroutineScope(SupervisorJob() + FlipperDispatchers.workStealingDispatcher) | ||
private val mutex = Mutex() | ||
private val isForceDisconnected = MutableStateFlow(false) | ||
|
||
override fun onApplicationInit() { | ||
scope.launch { | ||
if (mutex.isLocked) { | ||
warn { "#onApplicationInit tried to init connection service again" } | ||
return@launch | ||
} | ||
mutex.withLock { | ||
val connectionJob = combine( | ||
flow = fDevicePersistedStorage.getCurrentDevice(), | ||
flow2 = isForceDisconnected, | ||
transform = { currentDevice, isForceDisconnected -> | ||
when { | ||
isForceDisconnected -> orchestrator.disconnectCurrent() | ||
currentDevice == null -> orchestrator.disconnectCurrent() | ||
else -> orchestrator.connect(currentDevice) | ||
} | ||
} | ||
).launchIn(this) | ||
connectionJob.invokeOnCompletion { warn { "#onApplicationInit connection job cancelled" } } | ||
connectionJob.join() | ||
} | ||
} | ||
} | ||
|
||
override fun forceReconnect() { | ||
scope.launch { isForceDisconnected.emit(false) } | ||
} | ||
|
||
override fun disconnect(force: Boolean) { | ||
scope.launch { | ||
isForceDisconnected.emit(force) | ||
orchestrator.disconnectCurrent() | ||
} | ||
} | ||
} |
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
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