|
| 1 | +package com.hereliesaz.logkitty.feature |
| 2 | + |
| 3 | +import androidx.compose.runtime.Composable |
| 4 | +import androidx.compose.runtime.DisposableEffect |
| 5 | +import androidx.compose.runtime.getValue |
| 6 | +import androidx.compose.runtime.mutableStateOf |
| 7 | +import androidx.compose.runtime.remember |
| 8 | +import androidx.compose.runtime.setValue |
| 9 | +import androidx.compose.ui.platform.LocalContext |
| 10 | +import com.google.android.play.core.splitcompat.SplitCompat |
| 11 | +import com.google.android.play.core.splitinstall.SplitInstallManagerFactory |
| 12 | +import com.google.android.play.core.splitinstall.SplitInstallRequest |
| 13 | +import com.google.android.play.core.splitinstall.SplitInstallStateUpdatedListener |
| 14 | +import com.google.android.play.core.splitinstall.model.SplitInstallSessionStatus |
| 15 | + |
| 16 | +/** UI-facing status of an on-demand feature module. */ |
| 17 | +sealed interface FeatureInstallStatus { |
| 18 | + data object Installed : FeatureInstallStatus |
| 19 | + data object NotInstalled : FeatureInstallStatus |
| 20 | + /** [progress] is 0f..1f, or -1f when the total size isn't known yet. */ |
| 21 | + data class Installing(val progress: Float) : FeatureInstallStatus |
| 22 | + data class Failed(val message: String) : FeatureInstallStatus |
| 23 | +} |
| 24 | + |
| 25 | +/** Current [status] of a module plus an [install] trigger to request it. */ |
| 26 | +class FeatureInstallHandle( |
| 27 | + val status: FeatureInstallStatus, |
| 28 | + val install: () -> Unit, |
| 29 | +) |
| 30 | + |
| 31 | +/** |
| 32 | + * Observes and drives installation of the dynamic feature module [moduleName] via Play's |
| 33 | + * [com.google.android.play.core.splitinstall.SplitInstallManager], surfacing progress as Compose |
| 34 | + * state. On a build installed outside Play (e.g. the fused GitHub APK) the module is already present, |
| 35 | + * so this reports [FeatureInstallStatus.Installed] immediately. |
| 36 | + */ |
| 37 | +@Composable |
| 38 | +fun rememberFeatureInstall(moduleName: String): FeatureInstallHandle { |
| 39 | + val context = LocalContext.current |
| 40 | + val manager = remember { SplitInstallManagerFactory.create(context.applicationContext) } |
| 41 | + |
| 42 | + var status by remember(moduleName) { |
| 43 | + mutableStateOf<FeatureInstallStatus>( |
| 44 | + if (manager.installedModules.contains(moduleName)) FeatureInstallStatus.Installed |
| 45 | + else FeatureInstallStatus.NotInstalled |
| 46 | + ) |
| 47 | + } |
| 48 | + |
| 49 | + DisposableEffect(moduleName) { |
| 50 | + val listener = SplitInstallStateUpdatedListener { state -> |
| 51 | + if (!state.moduleNames().contains(moduleName)) return@SplitInstallStateUpdatedListener |
| 52 | + status = when (state.status()) { |
| 53 | + SplitInstallSessionStatus.DOWNLOADING -> { |
| 54 | + val total = state.totalBytesToDownload() |
| 55 | + val done = state.bytesDownloaded() |
| 56 | + FeatureInstallStatus.Installing(if (total > 0) done.toFloat() / total else -1f) |
| 57 | + } |
| 58 | + SplitInstallSessionStatus.PENDING, |
| 59 | + SplitInstallSessionStatus.DOWNLOADED, |
| 60 | + SplitInstallSessionStatus.INSTALLING -> FeatureInstallStatus.Installing(-1f) |
| 61 | + SplitInstallSessionStatus.INSTALLED -> { |
| 62 | + SplitCompat.install(context) |
| 63 | + FeatureInstallStatus.Installed |
| 64 | + } |
| 65 | + SplitInstallSessionStatus.FAILED -> |
| 66 | + FeatureInstallStatus.Failed("Install failed (code ${state.errorCode()})") |
| 67 | + else -> status |
| 68 | + } |
| 69 | + } |
| 70 | + manager.registerListener(listener) |
| 71 | + onDispose { manager.unregisterListener(listener) } |
| 72 | + } |
| 73 | + |
| 74 | + return remember(moduleName, status) { |
| 75 | + FeatureInstallHandle(status) { |
| 76 | + if (status is FeatureInstallStatus.Installed || status is FeatureInstallStatus.Installing) { |
| 77 | + return@FeatureInstallHandle |
| 78 | + } |
| 79 | + status = FeatureInstallStatus.Installing(-1f) |
| 80 | + val request = SplitInstallRequest.newBuilder().addModule(moduleName).build() |
| 81 | + manager.startInstall(request) |
| 82 | + .addOnFailureListener { |
| 83 | + status = FeatureInstallStatus.Failed(it.message ?: "Install failed") |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments