SharedPreference.kt
class SharedPreference(context: Context) {
private val prefsFilename = "Prefs"
private val prefs: SharedPreferences = context.getSharedPreferences(prefsFilename, 0)
fun setNoticePref(item: String, data: Boolean){
prefs.edit().putBoolean(item, data).apply()
}
fun getNoticePref(item: String): Boolean{
return prefs.getBoolean(item, true)
}
}
Setting.kt
@Composable
fun Setting(navController: NavHostController, mainViewModel: MainViewModel) {
val scaffoldState = rememberScaffoldState()
val chatCheckedState = remember {
mutableStateOf(SharedPreference(App.context()).getNoticePref("chat"))
}
val postCheckedState = remember {
mutableStateOf(SharedPreference(App.context()).getNoticePref("post"))
}
CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Ltr) {
Scaffold(
scaffoldState = scaffoldState,
topBar = {
TopAppBar(
title = { Text(text = "설정", textAlign = TextAlign.Center, fontSize = 17.sp) },
navigationIcon = {
IconButton(onClick = { navController.navigateUp() }) {
Icon(
painterResource(id = R.drawable.icon_back), contentDescription = "뒤로가기", modifier = Modifier.size(35.dp), tint = colorResource(
id = R.color.green)
)
}
},
backgroundColor = Color.Transparent,
elevation = 0.dp
)
}
) { it ->
Column(modifier = Modifier.padding(it)) {
Title(title = "알림 설정")
ConstraintLayout(modifier = Modifier
.padding(top = 15.dp, start = 20.dp, end = 20.dp, bottom = 8.dp)
.fillMaxWidth()) {
val (button, switch) = createRefs()
Text(
text = "게시글 알림",
style = TextStyle(fontSize = 16.sp),
modifier = Modifier.constrainAs(button){
start.linkTo(parent.start)
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
}
)
Switch(
checked = postCheckedState.value,
onCheckedChange = {
postCheckedState.value = it
UserSharedPreference(App.context()).setNoticePref("chat", chatCheckedState.value)
},
modifier = Modifier.constrainAs(switch){
end.linkTo(parent.end)
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
}
)
}
ConstraintLayout(modifier = Modifier
.padding(top = 8.dp, start = 20.dp, end = 20.dp, bottom = 15.dp)
.fillMaxWidth()) {
val (button, switch) = createRefs()
Text(
text = "채팅 알림",
style = TextStyle(fontSize = 16.sp),
modifier = Modifier.constrainAs(button){
start.linkTo(parent.start)
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
}
)
Switch(
checked = chatCheckedState.value,
onCheckedChange = {
chatCheckedState.value = it
UserSharedPreference(App.context()).setNoticePref("review", chatCheckedState.value)
},
modifier = Modifier.constrainAs(switch){
end.linkTo(parent.end)
top.linkTo(parent.top)
bottom.linkTo(parent.bottom)
}
)
}