Set organization name and organization id in dialog

ManageSpaceActivity: use dialog
This commit is contained in:
BinTianqi
2024-10-26 09:10:42 +08:00
parent 951231abe3
commit 3a19b1acf9
7 changed files with 106 additions and 165 deletions

View File

@@ -1,83 +1,54 @@
package com.bintianqi.owndroid
import android.content.Context
import android.os.Bundle
import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.material3.TextButton
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.lifecycleScope
import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class ManageSpaceActivity: FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE)
val materialYou = sharedPref.getBoolean("material_you", true)
val blackTheme = sharedPref.getBoolean("black_theme", false)
val protected = sharedPref.getBoolean("protect_storage", false)
setContent {
OwnDroidTheme(materialYou, blackTheme) {
ManageSpace()
}
}
}
@Composable
fun ManageSpace() {
Column(
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)
) {
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
val protected = sharedPref.getBoolean("protect_storage", false)
if(protected){
Text(
text = stringResource(R.string.storage_is_protected),
color = MaterialTheme.colorScheme.onBackground,
style = MaterialTheme.typography.titleMedium
)
Text(
text = stringResource(R.string.you_cant_clear_storage),
color = MaterialTheme.colorScheme.onBackground
)
}
Button(
onClick = {
lifecycleScope.launch {
sharedPref.edit().clear().apply()
delay(2000)
finishAndRemoveTask()
AlertDialog(
title = {
Text(stringResource(R.string.clear_storage))
},
text = {
if(protected) Text(stringResource(R.string.storage_is_protected))
},
onDismissRequest = { finish() },
dismissButton = {
if(!protected) TextButton(onClick = { finish() }) {
Text(stringResource(R.string.cancel))
}
},
confirmButton = {
TextButton(
onClick = {
if(!protected) {
applicationContext.filesDir.deleteRecursively()
sharedPref.edit().clear().apply()
}
finish()
}
) {
Text(stringResource(if(protected) R.string.cancel else R.string.confirm))
}
}
Toast.makeText(applicationContext, R.string.clear_storage_success, Toast.LENGTH_SHORT).show()
},
enabled = !protected,
colors = ButtonDefaults.buttonColors(
contentColor = MaterialTheme.colorScheme.onError,
containerColor = MaterialTheme.colorScheme.error
),
modifier = Modifier.padding(top = 10.dp)
) {
Text(text = stringResource(R.string.clear_storage))
)
}
}
}