Skip to content

Commit

Permalink
Add an uninstall button for features
Browse files Browse the repository at this point in the history
Make big box feature removable
  • Loading branch information
dellisd committed Feb 2, 2024
1 parent af41bae commit 9604bc6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
package app.cash.boxapp.install

internal enum class Module(val id: String) {
BigBox("bigboxfeature"),
ExtraBigBox("extrabigboxfeature"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import android.content.Context
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import com.google.android.play.core.ktx.requestDeferredUninstall
import com.google.android.play.core.ktx.requestInstall
import com.google.android.play.core.ktx.status
import com.google.android.play.core.splitinstall.SplitInstallManager
Expand Down Expand Up @@ -62,4 +63,8 @@ internal class SplitInstallHelper(
suspend fun requestInstall(module: Module) {
splitInstallManager.requestInstall(modules = listOf(module.id))
}

suspend fun requestUninstall(module: Module) {
splitInstallManager.requestDeferredUninstall(moduleNames = listOf(module.id))
}
}
105 changes: 77 additions & 28 deletions sample/app/src/main/kotlin/app/cash/boxapp/ui/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ package app.cash.boxapp.ui
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material.Button
Expand All @@ -37,6 +38,7 @@ import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
Expand All @@ -63,8 +65,8 @@ internal fun HomeScreen() {

Column(
modifier = Modifier
.padding(10.dp)
.background(color = Color(196, 255, 233)),
.background(color = Color(196, 255, 233))
.padding(10.dp),
) {
// The "My Boxes" tab.
Row(modifier = Modifier.weight(1f)) {
Expand All @@ -78,35 +80,82 @@ internal fun HomeScreen() {
}
}

Box(modifier = Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
Button(
onClick = {
scope.launch { installHelper.requestInstall(Module.ExtraBigBox) }
Column(modifier = Modifier.fillMaxWidth(), horizontalAlignment = Alignment.CenterHorizontally) {
InstallButton(
text = buildAnnotatedString {
append("Install ")
withStyle(SpanStyle(fontWeight = FontWeight.Black)) {
append("EXTRA")
}
append(" Big Box")
},
colors = ButtonDefaults.buttonColors(Color(0, 222, 133)),
isLoading = isSplitInstalling,
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Text(
text = buildAnnotatedString {
append("Install ")
withStyle(SpanStyle(fontWeight = FontWeight.Black)) {
append("EXTRA")
}
append(" Big Box")
},
)
AnimatedVisibility(visible = isSplitInstalling) {
CircularProgressIndicator(
modifier = Modifier.size(16.dp),
color = MaterialTheme.colors.onBackground,
strokeWidth = 2.dp,
)
scope.launch { installHelper.requestInstall(Module.ExtraBigBox) }
}
UninstallButton(
onClick = {
scope.launch { installHelper.requestUninstall(Module.ExtraBigBox) }
},
text = buildAnnotatedString {
append("Uninstall ")
withStyle(SpanStyle(fontWeight = FontWeight.Black)) {
append("EXTRA")
}
}
append(" Big Box")
},
)

Spacer(modifier = Modifier.height(16.dp))

InstallButton(
text = buildAnnotatedString {
append("Install Big Box")
},
isLoading = isSplitInstalling,
) {
scope.launch { installHelper.requestInstall(Module.BigBox) }
}
UninstallButton(
onClick = {
scope.launch { installHelper.requestUninstall(Module.BigBox) }
},
text = buildAnnotatedString {
append("Uninstall Big Box")
},
)
}
}
}

@Composable
private fun InstallButton(text: AnnotatedString, isLoading: Boolean, onClick: () -> Unit) {
Button(
onClick = onClick,
colors = ButtonDefaults.buttonColors(Color(0, 222, 133)),
) {
Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
Text(text = text)
AnimatedVisibility(visible = isLoading) {
CircularProgressIndicator(
modifier = Modifier.size(16.dp),
color = MaterialTheme.colors.onBackground,
strokeWidth = 2.dp,
)
}
}
}
}

@Composable
private fun UninstallButton(text: AnnotatedString, onClick: () -> Unit) {
Button(
onClick = onClick,
colors = ButtonDefaults.buttonColors(Color(234, 67, 67)),
) {
Text(text = text)
}
}
2 changes: 1 addition & 1 deletion sample/bigboxfeature/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
<dist:delivery>
<dist:install-time />
</dist:delivery>
<dist:fusing dist:include="true" />
<dist:removable dist:value="true" />
</dist:module>
</manifest>

0 comments on commit 9604bc6

Please sign in to comment.