Skip to content

Commit

Permalink
Add confirm dialog to download asset
Browse files Browse the repository at this point in the history
  • Loading branch information
wingio committed Jun 30, 2023
1 parent bcd9229 commit 704d5c0
Show file tree
Hide file tree
Showing 6 changed files with 149 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.materiiapps.gloom.di.modules

import com.materiiapps.gloom.domain.manager.AuthManager
import com.materiiapps.gloom.domain.manager.DialogManager
import com.materiiapps.gloom.domain.manager.DownloadManager
import com.materiiapps.gloom.domain.manager.PreferenceManager
import org.koin.core.module.dsl.singleOf
Expand All @@ -11,5 +12,6 @@ fun managerModule() = module {
singleOf(::PreferenceManager)
singleOf(::AuthManager)
singleOf(::DownloadManager)
singleOf(::DialogManager)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.materiiapps.gloom.domain.manager

import android.content.Context
import com.materiiapps.gloom.domain.manager.base.BasePreferenceManager

class DialogManager(context: Context): BasePreferenceManager(context.getSharedPreferences("dialogs", Context.MODE_PRIVATE)) {

var downloadAsset by enumPreference("release_asset_download", DialogState.UNKNOWN)

}

enum class DialogState {
UNKNOWN,
CONFIRMED,
DENIED
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.core.net.toUri
import cafe.adriel.voyager.core.model.coroutineScope
import com.materiiapps.gloom.domain.manager.DialogManager
import com.materiiapps.gloom.domain.manager.DialogState
import com.materiiapps.gloom.domain.manager.DownloadManager
import com.materiiapps.gloom.domain.repository.GraphQLRepository
import com.materiiapps.gloom.gql.ReleaseDetailsQuery
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,33 @@ import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.materiiapps.gloom.domain.manager.DialogManager
import com.materiiapps.gloom.domain.manager.DialogState
import com.materiiapps.gloom.ui.widgets.release.dialog.ReleaseAssetDownloadDialog
import com.materiiapps.gloom.utils.getFileSizeString
import org.koin.androidx.compose.get

@Composable
fun ReleaseAsset(
name: String,
size: Int,
onDownloadClick: () -> Unit
) {
val dialogManager: DialogManager = get()
var showConfirmDialog by remember {
mutableStateOf(false)
}
val icon = when (name.split(".").lastOrNull()?.lowercase()) {
"apks",
"apkx",
Expand Down Expand Up @@ -90,16 +102,35 @@ fun ReleaseAsset(
)

Text(
text = getFileSizeString(size, LocalContext.current),
text = getFileSizeString(size),
style = MaterialTheme.typography.labelMedium,
maxLines = 1,
overflow = TextOverflow.Ellipsis,
color = LocalContentColor.current.copy(0.5f)
)
}

if(showConfirmDialog) {
ReleaseAssetDownloadDialog(
fileName = name,
fileSize = size,
onClose = { showConfirmDialog = false },
onConfirm = { dontShowAgain ->
if(dontShowAgain) dialogManager.downloadAsset = DialogState.CONFIRMED
showConfirmDialog = false
onDownloadClick()
}
)
}

IconButton(onClick = onDownloadClick) {
IconButton(
onClick = {
if (dialogManager.downloadAsset == DialogState.UNKNOWN)
showConfirmDialog = true
else
onDownloadClick()
}
) {
Icon(
imageVector = Icons.Filled.Download,
contentDescription = null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package com.materiiapps.gloom.ui.widgets.release.dialog

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Download
import androidx.compose.material.icons.outlined.Download
import androidx.compose.material.icons.rounded.Download
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Checkbox
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.materiiapps.gloom.R
import com.materiiapps.gloom.utils.getFileSizeString

@Composable
fun ReleaseAssetDownloadDialog(
fileName: String,
fileSize: Int,
onClose: () -> Unit,
onConfirm: (dontShowAgain: Boolean) -> Unit
) {
var checked by remember {
mutableStateOf(false)
}

AlertDialog(
onDismissRequest = onClose,
confirmButton = {
FilledTonalButton(onClick = { onConfirm(checked) }) {
Text("Download")
}
},
dismissButton = {
TextButton(onClick = onClose) {
Text("Cancel")
}
},
icon = {
Icon(
imageVector = Icons.Rounded.Download,
contentDescription = null,
modifier = Modifier.size(30.dp)
)
},
title = {
Text(
text = "Confirm download",
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)
},
text = {
Column(
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Text(
text = "You are about to download $fileName (${getFileSizeString(fileSize)}), are you sure you want to do that?",
textAlign = TextAlign.Center,
modifier = Modifier.fillMaxWidth()
)

Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.clickable(role = Role.Checkbox) { checked = !checked }
.fillMaxWidth()
) {
Checkbox(checked = checked, onCheckedChange = { checked = it })
Text("Don't show this again")
}
}
}
)
}
4 changes: 4 additions & 0 deletions app/src/main/java/com/materiiapps/gloom/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.widget.Toast
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import com.materiiapps.gloom.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -74,6 +75,9 @@ fun Context.shareText(text: String) = Intent(Intent.ACTION_SEND).apply {

fun String?.ifNullOrBlank(block: () -> String) = if (isNullOrBlank()) block() else this

@Composable
fun getFileSizeString(size: Int) = getFileSizeString(size, LocalContext.current)

fun getFileSizeString(size: Int, context: Context): String {
return when {
size < Constants.FILE_SIZES.KILO -> context.getString(R.string.file_size_bytes, size)
Expand Down

0 comments on commit 704d5c0

Please sign in to comment.