|
| 1 | +package me.kavishdevar.librepods.composables |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.layout.Arrangement |
| 5 | +import androidx.compose.foundation.layout.Box |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.height |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.foundation.layout.width |
| 13 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 14 | +import androidx.compose.material3.HorizontalDivider |
| 15 | +import androidx.compose.material3.Text |
| 16 | +import androidx.compose.runtime.Composable |
| 17 | +import androidx.compose.runtime.MutableState |
| 18 | +import androidx.compose.runtime.getValue |
| 19 | +import androidx.compose.runtime.mutableStateOf |
| 20 | +import androidx.compose.runtime.remember |
| 21 | +import androidx.compose.runtime.setValue |
| 22 | +import androidx.compose.ui.Alignment |
| 23 | +import androidx.compose.ui.Modifier |
| 24 | +import androidx.compose.ui.draw.clip |
| 25 | +import androidx.compose.ui.graphics.Color |
| 26 | +import androidx.compose.ui.input.pointer.PointerEventType |
| 27 | +import androidx.compose.ui.input.pointer.pointerInput |
| 28 | +import androidx.compose.ui.platform.LocalContext |
| 29 | +import androidx.compose.ui.text.TextStyle |
| 30 | +import androidx.compose.ui.text.font.Font |
| 31 | +import androidx.compose.ui.text.font.FontFamily |
| 32 | +import androidx.compose.ui.text.font.FontWeight |
| 33 | +import androidx.compose.ui.text.style.TextAlign |
| 34 | +import androidx.compose.ui.unit.dp |
| 35 | +import androidx.compose.ui.unit.sp |
| 36 | +import androidx.compose.ui.window.Dialog |
| 37 | +import dev.chrisbanes.haze.HazeState |
| 38 | +import dev.chrisbanes.haze.hazeEffect |
| 39 | +import dev.chrisbanes.haze.materials.CupertinoMaterials |
| 40 | +import dev.chrisbanes.haze.materials.ExperimentalHazeMaterialsApi |
| 41 | +import me.kavishdevar.librepods.R |
| 42 | + |
| 43 | +@ExperimentalHazeMaterialsApi |
| 44 | +@Composable |
| 45 | +fun ConfirmationDialog( |
| 46 | + showDialog: MutableState<Boolean>, |
| 47 | + title: String, |
| 48 | + message: String, |
| 49 | + confirmText: String = "Enable", |
| 50 | + dismissText: String = "Cancel", |
| 51 | + onConfirm: () -> Unit, |
| 52 | + onDismiss: () -> Unit = { showDialog.value = false }, |
| 53 | + hazeState: HazeState, |
| 54 | + isDarkTheme: Boolean, |
| 55 | + textColor: Color, |
| 56 | + activeTrackColor: Color |
| 57 | +) { |
| 58 | + if (showDialog.value) { |
| 59 | + Dialog(onDismissRequest = { showDialog.value = false }) { |
| 60 | + Box( |
| 61 | + modifier = Modifier |
| 62 | + .fillMaxWidth() |
| 63 | + .background(if (isDarkTheme) Color(0xFF1C1C1E).copy(alpha = 0.95f) else Color.White.copy(alpha = 0.95f), RoundedCornerShape(14.dp)) |
| 64 | + .clip(RoundedCornerShape(14.dp)) |
| 65 | + .hazeEffect(hazeState, CupertinoMaterials.regular()) |
| 66 | + ) { |
| 67 | + Column(horizontalAlignment = Alignment.CenterHorizontally) { |
| 68 | + androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(24.dp)) |
| 69 | + Text( |
| 70 | + title, |
| 71 | + style = TextStyle( |
| 72 | + fontSize = 16.sp, |
| 73 | + fontWeight = FontWeight.Bold, |
| 74 | + color = textColor, |
| 75 | + fontFamily = FontFamily(Font(R.font.sf_pro)) |
| 76 | + ), |
| 77 | + textAlign = TextAlign.Center, |
| 78 | + modifier = Modifier.padding(horizontal = 16.dp) |
| 79 | + ) |
| 80 | + androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(12.dp)) |
| 81 | + Text( |
| 82 | + message, |
| 83 | + style = TextStyle( |
| 84 | + fontSize = 14.sp, |
| 85 | + color = textColor.copy(alpha = 0.8f), |
| 86 | + fontFamily = FontFamily(Font(R.font.sf_pro)) |
| 87 | + ), |
| 88 | + textAlign = TextAlign.Center, |
| 89 | + modifier = Modifier.padding(horizontal = 16.dp) |
| 90 | + ) |
| 91 | + androidx.compose.foundation.layout.Spacer(modifier = Modifier.height(16.dp)) |
| 92 | + HorizontalDivider( |
| 93 | + thickness = 1.dp, |
| 94 | + color = Color(0x40888888), |
| 95 | + modifier = Modifier.fillMaxWidth() |
| 96 | + ) |
| 97 | + var leftPressed by remember { mutableStateOf(false) } |
| 98 | + var rightPressed by remember { mutableStateOf(false) } |
| 99 | + val pressedColor = if (isDarkTheme) Color(0x40888888) else Color(0x40D9D9D9) |
| 100 | + Row( |
| 101 | + modifier = Modifier |
| 102 | + .fillMaxWidth() |
| 103 | + .height(48.dp) |
| 104 | + .pointerInput(Unit) { |
| 105 | + awaitPointerEventScope { |
| 106 | + while (true) { |
| 107 | + val event = awaitPointerEvent() |
| 108 | + val position = event.changes.first().position |
| 109 | + val width = size.width.toFloat() |
| 110 | + val height = size.height.toFloat() |
| 111 | + val isWithinBounds = position.y >= 0 && position.y <= height |
| 112 | + val isLeft = position.x < width / 2 |
| 113 | + event.changes.first().consume() |
| 114 | + when (event.type) { |
| 115 | + PointerEventType.Press -> { |
| 116 | + if (isWithinBounds) { |
| 117 | + leftPressed = isLeft |
| 118 | + rightPressed = !isLeft |
| 119 | + } else { |
| 120 | + leftPressed = false |
| 121 | + rightPressed = false |
| 122 | + } |
| 123 | + } |
| 124 | + PointerEventType.Move -> { |
| 125 | + if (isWithinBounds) { |
| 126 | + leftPressed = isLeft |
| 127 | + rightPressed = !isLeft |
| 128 | + } else { |
| 129 | + leftPressed = false |
| 130 | + rightPressed = false |
| 131 | + } |
| 132 | + } |
| 133 | + PointerEventType.Release -> { |
| 134 | + if (isWithinBounds) { |
| 135 | + if (leftPressed) { |
| 136 | + onDismiss() |
| 137 | + } else if (rightPressed) { |
| 138 | + onConfirm() |
| 139 | + } |
| 140 | + } |
| 141 | + leftPressed = false |
| 142 | + rightPressed = false |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + }, |
| 148 | + horizontalArrangement = Arrangement.Start, |
| 149 | + verticalAlignment = Alignment.CenterVertically |
| 150 | + ) { |
| 151 | + Box( |
| 152 | + modifier = Modifier |
| 153 | + .weight(1f) |
| 154 | + .fillMaxHeight() |
| 155 | + .background(if (leftPressed) pressedColor else Color.Transparent), |
| 156 | + contentAlignment = Alignment.Center |
| 157 | + ) { |
| 158 | + Text(dismissText, color = activeTrackColor) |
| 159 | + } |
| 160 | + Box( |
| 161 | + modifier = Modifier |
| 162 | + .width(1.dp) |
| 163 | + .fillMaxHeight() |
| 164 | + .background(Color(0x40888888)) |
| 165 | + ) |
| 166 | + Box( |
| 167 | + modifier = Modifier |
| 168 | + .weight(1f) |
| 169 | + .fillMaxHeight() |
| 170 | + .background(if (rightPressed) pressedColor else Color.Transparent), |
| 171 | + contentAlignment = Alignment.Center |
| 172 | + ) { |
| 173 | + Text(confirmText, color = activeTrackColor) |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
0 commit comments