| 1 |
package io.oxygenhub.android.alerts |
| 2 |
|
| 3 |
import android.Manifest |
| 4 |
import android.content.pm.PackageManager |
| 5 |
import android.os.Build |
| 6 |
import androidx.activity.compose.rememberLauncherForActivityResult |
| 7 |
import androidx.activity.result.contract.ActivityResultContracts |
| 8 |
import androidx.compose.foundation.layout.Arrangement |
| 9 |
import androidx.compose.foundation.layout.Column |
| 10 |
import androidx.compose.foundation.layout.Row |
| 11 |
import androidx.compose.foundation.layout.fillMaxWidth |
| 12 |
import androidx.compose.foundation.layout.padding |
| 13 |
import androidx.compose.material3.ElevatedCard |
| 14 |
import androidx.compose.material3.MaterialTheme |
| 15 |
import androidx.compose.material3.Switch |
| 16 |
import androidx.compose.material3.Text |
| 17 |
import androidx.compose.runtime.Composable |
| 18 |
import androidx.compose.runtime.LaunchedEffect |
| 19 |
import androidx.compose.runtime.getValue |
| 20 |
import androidx.compose.ui.Alignment |
| 21 |
import androidx.compose.ui.Modifier |
| 22 |
import androidx.compose.ui.platform.LocalContext |
| 23 |
import androidx.compose.ui.unit.dp |
| 24 |
import androidx.core.content.ContextCompat |
| 25 |
import androidx.lifecycle.compose.collectAsStateWithLifecycle |
| 26 |
|
| 27 |
/** On the Account screen: notifications when a proposal can be landed (#20), on or off. */ |
| 28 |
@Composable |
| 29 |
fun LandAlertsCard() { |
| 30 |
val context = LocalContext.current |
| 31 |
val on by LandAlerts.on.collectAsStateWithLifecycle() |
| 32 |
LaunchedEffect(Unit) { LandAlerts.refresh(context) } |
| 33 |
// Without the permission there is nothing to show, so it stays off. |
| 34 |
val ask = rememberLauncherForActivityResult(ActivityResultContracts.RequestPermission()) { granted -> |
| 35 |
if (granted) LandAlerts.enable(context) |
| 36 |
} |
| 37 |
val turnOn = { |
| 38 |
val granted = Build.VERSION.SDK_INT < 33 || |
| 39 |
ContextCompat.checkSelfPermission(context, Manifest.permission.POST_NOTIFICATIONS) == PackageManager.PERMISSION_GRANTED |
| 40 |
if (granted) LandAlerts.enable(context) else ask.launch(Manifest.permission.POST_NOTIFICATIONS) |
| 41 |
} |
| 42 |
ElevatedCard(Modifier.fillMaxWidth()) { |
| 43 |
Row(Modifier.padding(16.dp), verticalAlignment = Alignment.CenterVertically, horizontalArrangement = Arrangement.spacedBy(16.dp)) { |
| 44 |
Column(Modifier.weight(1f), verticalArrangement = Arrangement.spacedBy(4.dp)) { |
| 45 |
Text("Ready to land", style = MaterialTheme.typography.titleMedium) |
| 46 |
Text( |
| 47 |
"Tell me when a proposal I can land has its checks and reviews settled. Checked every 15 minutes.", |
| 48 |
style = MaterialTheme.typography.bodyMedium, |
| 49 |
color = MaterialTheme.colorScheme.onSurfaceVariant, |
| 50 |
) |
| 51 |
} |
| 52 |
Switch( |
| 53 |
checked = on == true, |
| 54 |
enabled = on != null, |
| 55 |
onCheckedChange = { if (it) turnOn() else LandAlerts.disable(context) }, |
| 56 |
) |
| 57 |
} |
| 58 |
} |
| 59 |
} |