|
| 1 | +package com.androidvip.sysctlgui.ui.start |
| 2 | + |
| 3 | +import android.content.Intent |
| 4 | +import android.os.Bundle |
| 5 | +import androidx.activity.ComponentActivity |
| 6 | +import androidx.activity.compose.setContent |
| 7 | +import androidx.activity.enableEdgeToEdge |
| 8 | +import androidx.compose.foundation.background |
| 9 | +import androidx.compose.foundation.clickable |
| 10 | +import androidx.compose.foundation.layout.Arrangement |
| 11 | +import androidx.compose.foundation.layout.Box |
| 12 | +import androidx.compose.foundation.layout.Column |
| 13 | +import androidx.compose.foundation.layout.Row |
| 14 | +import androidx.compose.foundation.layout.Spacer |
| 15 | +import androidx.compose.foundation.layout.displayCutoutPadding |
| 16 | +import androidx.compose.foundation.layout.fillMaxHeight |
| 17 | +import androidx.compose.foundation.layout.fillMaxSize |
| 18 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 19 | +import androidx.compose.foundation.layout.padding |
| 20 | +import androidx.compose.foundation.layout.size |
| 21 | +import androidx.compose.foundation.layout.systemBarsPadding |
| 22 | +import androidx.compose.foundation.rememberScrollState |
| 23 | +import androidx.compose.foundation.verticalScroll |
| 24 | +import androidx.compose.material3.Button |
| 25 | +import androidx.compose.material3.Card |
| 26 | +import androidx.compose.material3.Checkbox |
| 27 | +import androidx.compose.material3.FilledTonalButton |
| 28 | +import androidx.compose.material3.Icon |
| 29 | +import androidx.compose.material3.MaterialTheme |
| 30 | +import androidx.compose.material3.Text |
| 31 | +import androidx.compose.runtime.Composable |
| 32 | +import androidx.compose.runtime.getValue |
| 33 | +import androidx.compose.runtime.mutableStateOf |
| 34 | +import androidx.compose.runtime.remember |
| 35 | +import androidx.compose.runtime.setValue |
| 36 | +import androidx.compose.ui.Alignment |
| 37 | +import androidx.compose.ui.Modifier |
| 38 | +import androidx.compose.ui.graphics.vector.ImageVector |
| 39 | +import androidx.compose.ui.res.painterResource |
| 40 | +import androidx.compose.ui.res.stringResource |
| 41 | +import androidx.compose.ui.res.vectorResource |
| 42 | +import androidx.compose.ui.text.style.TextAlign |
| 43 | +import androidx.compose.ui.tooling.preview.Preview |
| 44 | +import androidx.compose.ui.tooling.preview.PreviewLightDark |
| 45 | +import androidx.compose.ui.unit.dp |
| 46 | +import com.androidvip.sysctlgui.R |
| 47 | +import com.androidvip.sysctlgui.design.theme.SysctlGuiTheme |
| 48 | +import com.androidvip.sysctlgui.design.utils.AnimatedStrikeThroughIcon |
| 49 | +import com.androidvip.sysctlgui.design.utils.StatusBarProtection |
| 50 | +import com.androidvip.sysctlgui.design.utils.isLandscape |
| 51 | +import com.androidvip.sysctlgui.domain.repository.AppPrefs |
| 52 | +import org.koin.android.ext.android.inject |
| 53 | + |
| 54 | +class ConsentActivity : ComponentActivity() { |
| 55 | + private val prefs by inject<AppPrefs>() |
| 56 | + |
| 57 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 58 | + enableEdgeToEdge() |
| 59 | + super.onCreate(savedInstanceState) |
| 60 | + |
| 61 | + setContent { |
| 62 | + SysctlGuiTheme { |
| 63 | + ScreenContent( |
| 64 | + onClosePressed = { finish() }, |
| 65 | + onContinuePressed = { granted -> |
| 66 | + if (granted) startMainActivity() else finish() |
| 67 | + } |
| 68 | + ) |
| 69 | + StatusBarProtection() |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private fun startMainActivity() { |
| 75 | + prefs.consentGranted = true |
| 76 | + val mainIntent = Intent(this, StartActivity::class.java).apply { |
| 77 | + addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK) |
| 78 | + putExtras(intent.extras ?: Bundle()) |
| 79 | + } |
| 80 | + startActivity(mainIntent) |
| 81 | + finish() |
| 82 | + } |
| 83 | + |
| 84 | + @Composable |
| 85 | + private fun ScreenContent( |
| 86 | + onContinuePressed: (granted: Boolean) -> Unit = {}, |
| 87 | + onClosePressed: () -> Unit = {} |
| 88 | + ) { |
| 89 | + var consentGranted by remember { mutableStateOf(false) } |
| 90 | + |
| 91 | + Box( |
| 92 | + modifier = Modifier |
| 93 | + .fillMaxSize() |
| 94 | + .background(MaterialTheme.colorScheme.background) |
| 95 | + .verticalScroll(rememberScrollState()), |
| 96 | + ) { |
| 97 | + if (isLandscape()) { |
| 98 | + Row( |
| 99 | + modifier = Modifier |
| 100 | + .fillMaxWidth() |
| 101 | + .systemBarsPadding() |
| 102 | + .displayCutoutPadding() |
| 103 | + .padding(24.dp), |
| 104 | + horizontalArrangement = Arrangement.spacedBy(16.dp) |
| 105 | + ) { |
| 106 | + Column( |
| 107 | + modifier = Modifier.weight(1f), |
| 108 | + horizontalAlignment = Alignment.CenterHorizontally |
| 109 | + ) { |
| 110 | + ConsentInfo(consentGranted) |
| 111 | + } |
| 112 | + |
| 113 | + Column( |
| 114 | + modifier = Modifier.weight(1f), |
| 115 | + horizontalAlignment = Alignment.CenterHorizontally |
| 116 | + ) { |
| 117 | + RevertInfoCard() |
| 118 | + ConsentActions( |
| 119 | + onContinuePressed = { onContinuePressed(consentGranted) }, |
| 120 | + onConsentChanged = { consentGranted = it }, |
| 121 | + onClosePressed = onClosePressed |
| 122 | + ) |
| 123 | + } |
| 124 | + } |
| 125 | + } else { |
| 126 | + Column( |
| 127 | + modifier = Modifier |
| 128 | + .fillMaxHeight() |
| 129 | + .systemBarsPadding() |
| 130 | + .padding(24.dp), |
| 131 | + horizontalAlignment = Alignment.CenterHorizontally |
| 132 | + ) { |
| 133 | + Spacer(modifier = Modifier.size(24.dp)) |
| 134 | + ConsentInfo(consentGranted) |
| 135 | + Spacer(modifier = Modifier.size(24.dp)) |
| 136 | + RevertInfoCard() |
| 137 | + ConsentActions( |
| 138 | + onContinuePressed = { onContinuePressed(consentGranted) }, |
| 139 | + onConsentChanged = { consentGranted = it }, |
| 140 | + onClosePressed = onClosePressed |
| 141 | + ) |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + @Composable |
| 148 | + private fun RevertInfoCard() { |
| 149 | + Card( |
| 150 | + modifier = Modifier.fillMaxWidth() |
| 151 | + ) { |
| 152 | + Row( |
| 153 | + verticalAlignment = Alignment.CenterVertically, |
| 154 | + horizontalArrangement = Arrangement.spacedBy(16.dp), |
| 155 | + modifier = Modifier.padding(16.dp) |
| 156 | + ) { |
| 157 | + Icon( |
| 158 | + painter = painterResource(R.drawable.ic_restore_settings), |
| 159 | + contentDescription = null, |
| 160 | + tint = MaterialTheme.colorScheme.onSurfaceVariant, |
| 161 | + ) |
| 162 | + |
| 163 | + Text( |
| 164 | + text = stringResource(R.string.consent_revert_info), |
| 165 | + style = MaterialTheme.typography.bodyMedium, |
| 166 | + color = MaterialTheme.colorScheme.onSurface, |
| 167 | + modifier = Modifier.weight(1f) |
| 168 | + ) |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + @Composable |
| 174 | + private fun ConsentInfo(consentGranted: Boolean) { |
| 175 | + AnimatedStrikeThroughIcon( |
| 176 | + icon = ImageVector.vectorResource(R.drawable.ic_edit), |
| 177 | + modifier = Modifier.size(92.dp), |
| 178 | + isOff = !consentGranted, |
| 179 | + tint = MaterialTheme.colorScheme.primary |
| 180 | + ) |
| 181 | + |
| 182 | + Text( |
| 183 | + text = stringResource(R.string.consent_title), |
| 184 | + style = MaterialTheme.typography.titleLarge, |
| 185 | + color = MaterialTheme.colorScheme.onBackground, |
| 186 | + textAlign = TextAlign.Center, |
| 187 | + modifier = Modifier.padding(top = 24.dp) |
| 188 | + ) |
| 189 | + |
| 190 | + Text( |
| 191 | + text = stringResource(R.string.consent_message), |
| 192 | + style = MaterialTheme.typography.bodyMedium, |
| 193 | + color = MaterialTheme.colorScheme.onBackground, |
| 194 | + modifier = Modifier.padding(top = 16.dp) |
| 195 | + ) |
| 196 | + } |
| 197 | + |
| 198 | + @Composable |
| 199 | + private fun ConsentActions( |
| 200 | + modifier: Modifier = Modifier, |
| 201 | + onContinuePressed: () -> Unit, |
| 202 | + onClosePressed: () -> Unit, |
| 203 | + onConsentChanged: (granted: Boolean) -> Unit |
| 204 | + ) { |
| 205 | + var checked by remember { mutableStateOf(false) } |
| 206 | + |
| 207 | + Row( |
| 208 | + modifier = modifier |
| 209 | + .fillMaxWidth() |
| 210 | + .clickable { checked = !checked; onConsentChanged(checked) }, |
| 211 | + verticalAlignment = Alignment.CenterVertically, |
| 212 | + ) { |
| 213 | + Checkbox( |
| 214 | + checked = checked, |
| 215 | + onCheckedChange = { checked = it; onConsentChanged(it) } |
| 216 | + ) |
| 217 | + Text( |
| 218 | + text = stringResource(R.string.consent_checkbox), |
| 219 | + style = MaterialTheme.typography.bodyMedium, |
| 220 | + color = MaterialTheme.colorScheme.onBackground, |
| 221 | + ) |
| 222 | + } |
| 223 | + |
| 224 | + Button( |
| 225 | + onClick = onContinuePressed, |
| 226 | + enabled = checked, |
| 227 | + modifier = Modifier.padding(top = 24.dp) |
| 228 | + ) { |
| 229 | + Text( |
| 230 | + text = stringResource(R.string.continue_), |
| 231 | + style = MaterialTheme.typography.bodyMedium, |
| 232 | + ) |
| 233 | + } |
| 234 | + |
| 235 | + FilledTonalButton(onClick = onClosePressed) { |
| 236 | + Text( |
| 237 | + text = stringResource(R.string.exit), |
| 238 | + style = MaterialTheme.typography.bodyMedium, |
| 239 | + ) |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + @Composable |
| 244 | + @PreviewLightDark |
| 245 | + @Preview(device = "spec:parent=pixel_5,orientation=landscape", showSystemUi = true) |
| 246 | + private fun Preview() { |
| 247 | + SysctlGuiTheme { |
| 248 | + Box(modifier = Modifier.background(MaterialTheme.colorScheme.background)) { |
| 249 | + ScreenContent() |
| 250 | + } |
| 251 | + StatusBarProtection() |
| 252 | + } |
| 253 | + } |
| 254 | +} |
0 commit comments