@file:OptIn(ExperimentalMaterial3Api::class)
package com.example.swipes
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Delete
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.SwipeToDismissBox
import androidx.compose.material3.SwipeToDismissBoxState
import androidx.compose.material3.SwipeToDismissBoxValue
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.rememberSwipeToDismissBoxState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.listSaver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.swipes.ui.theme.SwipesTheme
import kotlinx.coroutines.delay
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
SwipesTheme {
Surface(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colorScheme.background),
color = MaterialTheme.colorScheme.background
) {
val programmingLanguages = rememberSaveable(
saver = ListSaver
) {
mutableStateListOf(
"Kotlin",
"C++",
"C#",
"Java",
"JavaScript",
"Swift",
"Python"
)
}
Scaffold (
topBar = {
TopAppBar(
title = {
Text(text = "Programming Languages")
}
)
}
){padding ->
LazyColumn(
modifier = Modifier
.fillMaxSize()
.padding(padding)
) {
items(items = programmingLanguages, key = { it }) { language ->
SwipeToDeleteContainer(
item = language,
onDelete = { programmingLanguages.remove(language) }) { item ->
Text(
text = item,
modifier = Modifier
.fillMaxWidth()
.background(MaterialTheme.colorScheme.background)
.padding(16.dp),
color = MaterialTheme.colorScheme.primary
)
}
}
}
}
}
}
}
}
}
@Composable
fun <T> SwipeToDeleteContainer(
item: T,
onDelete: (T) -> Unit,
animationDuration: Int = 500,
content: @Composable (T) -> Unit
) {
var isRemoved by remember { mutableStateOf(false) }
val dismissState = rememberSwipeToDismissBoxState(confirmValueChange = { value ->
if (value == SwipeToDismissBoxValue.EndToStart) {
isRemoved = true
true
} else {
false
}
})
LaunchedEffect(key1 = isRemoved) {
if (isRemoved) {
delay(animationDuration.toLong())
onDelete(item)
}
}
AnimatedVisibility(
visible = !isRemoved,
exit = shrinkVertically(
animationSpec = tween(durationMillis = animationDuration),
shrinkTowards = Alignment.Top
) + fadeOut()
) {
SwipeToDismissBox(
state = dismissState,
backgroundContent = {
DeleteBackground(swipeDismissState = dismissState)
},
content = { content(item) },
enableDismissFromStartToEnd = false,
)
}
}
@Composable
fun DeleteBackground(
swipeDismissState: SwipeToDismissBoxState
) {
val color by animateColorAsState(
when (swipeDismissState.targetValue) {
SwipeToDismissBoxValue.Settled -> Color.LightGray
SwipeToDismissBoxValue.StartToEnd -> Color.Green
SwipeToDismissBoxValue.EndToStart -> Color.Red
}, label = ""
)
Box(
modifier = Modifier
.fillMaxSize()
.background(color)
.padding(16.dp),
contentAlignment = Alignment.CenterEnd
) {
Icon(
imageVector = Icons.Default.Delete,
contentDescription = null,
tint = Color.White
)
}
}
val ListSaver: Saver<MutableList<String>, Any> = listSaver(
save = { stateList ->
stateList.toList()
},
restore = { list ->
mutableStateListOf<String>().apply { addAll(list) }
}
)