Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add module installation confirmation #33

Merged
merged 5 commits into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ android {
applicationId = "com.chouten.app"
minSdk = 23
targetSdk = 34
versionCode = 7
versionName = "0.1.0"
versionCode = 8
versionName = "0.1.1"
buildConfigField("String", "WEBHOOK_URL", "\"${properties.getProperty("bug_webhook")}\"")

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
Expand Down
62 changes: 59 additions & 3 deletions app/src/main/java/com/chouten/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,23 @@ package com.chouten.app
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Warning
import androidx.lifecycle.ViewModelProvider
import com.chouten.app.common.findActivity
import com.chouten.app.domain.model.AlertDialogModel
import com.chouten.app.domain.use_case.module_use_cases.ModuleInstallEvent
import com.chouten.app.presentation.ui.ChoutenApp
import com.chouten.app.presentation.ui.ChoutenAppViewModel
import com.chouten.app.presentation.ui.components.common.AppState
import com.chouten.app.presentation.ui.components.common.rememberAppState
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking

@AndroidEntryPoint
class MainActivity : ComponentActivity() {
Expand Down Expand Up @@ -66,9 +73,58 @@ class MainActivity : ComponentActivity() {
if (intent?.action in supportedActions) {
data?.let { uri ->
appState.viewModel.runAsync {
appState.viewModel.installModule(
uri, appState::showSnackbar
)
try {
appState.viewModel.installModule(
uri = uri
) {
if (it is ModuleInstallEvent.PARSED) {
// Can the app install the module?
var hasPermission = false
// Has the user dismissed/accepted the permission?
var hasUserPermission = false

appState.showAlertDialog(
AlertDialogModel(
icon = Icons.Default.Warning,
title = getString(
R.string.install_module_dialog_title,
it.module.name
),
message = getString(
R.string.module_install_dialog_description,
it.module.name
).trimIndent(),
positiveButton = Pair(getString(R.string.install)) {
hasUserPermission = true
hasPermission = true
},
negativeButton = Pair(getString(R.string.cancel)) {
hasUserPermission = true
hasPermission = false
}
)
)
// We want to suspend until the user has accepted or declined the alert
while (!hasUserPermission) {
runBlocking {
delay(100)
}
}
!hasPermission
} else false
}
} catch (e: InterruptedException) {
Log.d("MainActivity", "User cancelled module installation")
} catch (e: Exception) {
e.printStackTrace()
appState.showSnackbar(
com.chouten.app.domain.model.SnackbarModel(
message = e.message ?: "Unknown error",
actionLabel = "Dismiss",
isError = true
)
)
}
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/chouten/app/domain/model/AlertDialogModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.chouten.app.domain.model

import androidx.compose.ui.graphics.vector.ImageVector

data class AlertDialogModel(
val icon: ImageVector? = null,
val title: String,
val message: String,
val positiveButton: Pair<String, () -> Unit>,
val negativeButton: Pair<String, () -> Unit>? = null,
)
Loading
Loading