-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #229 from myofficework000/feature/Lesson_2_swipe_t…
…o_delete_list Swipe to delete in list implemented.
- Loading branch information
Showing
6 changed files
with
159 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
app/src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_3/Person.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_3 | ||
|
||
data class Person(var id: Int, var name: String) |
5 changes: 5 additions & 0 deletions
5
...src/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_3/PersonRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_3 | ||
|
||
interface PersonRepository { | ||
fun getPersonList(): List<Person> | ||
} |
11 changes: 11 additions & 0 deletions
11
...main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_3/PersonRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_3 | ||
|
||
class PersonRepositoryImpl : PersonRepository { | ||
override fun getPersonList(): List<Person> { | ||
val list = mutableListOf<Person>() | ||
for (i in 0..50) { | ||
list.add(Person(i, "Person $i")) | ||
} | ||
return list | ||
} | ||
} |
112 changes: 111 additions & 1 deletion
112
...rc/main/java/com/example/jetpack_compose_all_in_one/lessons/lesson_3/SwipeToDeleteList.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,118 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_3 | ||
|
||
import android.widget.Toast | ||
import androidx.compose.animation.animateColorAsState | ||
import androidx.compose.animation.core.animateFloatAsState | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.PaddingValues | ||
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.outlined.Delete | ||
import androidx.compose.material3.DismissDirection | ||
import androidx.compose.material3.DismissValue | ||
import androidx.compose.material3.ExperimentalMaterial3Api | ||
import androidx.compose.material3.Icon | ||
import androidx.compose.material3.SwipeToDismiss | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.rememberDismissState | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.getValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.scale | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.platform.LocalContext | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
|
||
@OptIn(ExperimentalMaterial3Api::class) | ||
@Composable | ||
fun SwipeToDeleteScreen() { | ||
fun SwipeToDeleteScreen(viewModel: SwipeToDeleteViewModel = hiltViewModel()) { | ||
val personList = viewModel.personList.collectAsState() | ||
|
||
val contextForToast = LocalContext.current.applicationContext | ||
|
||
LaunchedEffect(key1 = true) { | ||
viewModel.getPersonList() | ||
} | ||
LazyColumn( | ||
modifier = Modifier.fillMaxSize(), | ||
contentPadding = PaddingValues(vertical = 12.dp) | ||
) { | ||
items( | ||
items = personList.value, | ||
key = { person -> | ||
person.id | ||
} | ||
) { person -> | ||
val dismissState = rememberDismissState() | ||
|
||
if (dismissState.isDismissed(direction = DismissDirection.EndToStart)) { | ||
viewModel.deletePerson(person) | ||
Toast.makeText(contextForToast, "Deleted ${person.name}", Toast.LENGTH_SHORT).show() | ||
} | ||
SwipeToDismiss( | ||
state = dismissState, | ||
directions = setOf( | ||
DismissDirection.EndToStart | ||
), | ||
background = { | ||
val backgroundColor by animateColorAsState( | ||
when (dismissState.targetValue) { | ||
DismissValue.DismissedToStart -> Color.Red.copy(alpha = 0.8f) | ||
else -> Color.White | ||
}, label = "Swipe to delete background color" | ||
) | ||
|
||
val iconImageVector = Icons.Outlined.Delete | ||
|
||
val iconAlignment = Alignment.CenterEnd | ||
|
||
val iconScale by animateFloatAsState( | ||
targetValue = if (dismissState.targetValue == DismissValue.Default) 0.5f else 1.3f, | ||
label = "" | ||
) | ||
|
||
Box( | ||
Modifier | ||
.fillMaxSize() | ||
.background(color = backgroundColor) | ||
.padding(start = 16.dp, end = 16.dp), | ||
contentAlignment = iconAlignment | ||
) { | ||
Icon( | ||
modifier = Modifier.scale(iconScale), | ||
imageVector = iconImageVector, | ||
contentDescription = null, | ||
tint = Color.White | ||
) | ||
} | ||
}, | ||
dismissContent = { | ||
Column( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.background(color = Color.White) | ||
) { | ||
Text( | ||
text = person.name, | ||
fontSize = 20.sp, | ||
modifier = Modifier | ||
.fillMaxSize() | ||
.padding(top = 16.dp, bottom = 16.dp, start = 12.dp) | ||
) | ||
} | ||
} | ||
) | ||
} | ||
} | ||
} |
22 changes: 21 additions & 1 deletion
22
...in/java/com/example/jetpack_compose_all_in_one/lessons/lesson_3/SwipeToDeleteViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,27 @@ | ||
package com.example.jetpack_compose_all_in_one.lessons.lesson_3 | ||
|
||
import androidx.lifecycle.ViewModel | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.asStateFlow | ||
import javax.inject.Inject | ||
|
||
class SwipeToDeleteViewModel : ViewModel() { | ||
@HiltViewModel | ||
class SwipeToDeleteViewModel @Inject constructor(private val personRepository: PersonRepository) : | ||
ViewModel() { | ||
|
||
private val _personList = MutableStateFlow<List<Person>>(listOf()) | ||
val personList = _personList.asStateFlow() | ||
|
||
|
||
fun getPersonList() { | ||
_personList.value = personRepository.getPersonList() | ||
} | ||
|
||
fun deletePerson(person: Person) { | ||
val list = _personList.value.toMutableList() | ||
list.remove(person) | ||
_personList.value = list | ||
} | ||
|
||
} |