From 31411b115d12643ad3a33d4988758f602501a3a4 Mon Sep 17 00:00:00 2001 From: omer358 Date: Wed, 3 Jul 2024 03:02:08 +0400 Subject: [PATCH] Impl DeletePerson feature --- .../com/example/rememberme/RememberMeApp.kt | 4 +- .../rememberme/data/PeopleRepositoryImpl.kt | 4 +- .../com/example/rememberme/di/AppModule.kt | 4 +- .../domain/repository/PeopleRepository.kt | 2 +- .../usecases/people/DeletePersonById.kt | 12 ++++++ .../domain/usecases/people/PeopleUseCases.kt | 4 +- .../presentation/details/PersonDetails.kt | 23 +++++++++- .../details/PersonDetailsEvent.kt | 3 +- .../details/PersonDetailsViewModel.kt | 43 +++++++++++++++++-- 9 files changed, 87 insertions(+), 12 deletions(-) create mode 100644 app/src/main/java/com/example/rememberme/domain/usecases/people/DeletePersonById.kt diff --git a/app/src/main/java/com/example/rememberme/RememberMeApp.kt b/app/src/main/java/com/example/rememberme/RememberMeApp.kt index 91e4283..6d897ed 100644 --- a/app/src/main/java/com/example/rememberme/RememberMeApp.kt +++ b/app/src/main/java/com/example/rememberme/RememberMeApp.kt @@ -18,7 +18,9 @@ fun RememberMeApp() { Surface( modifier = Modifier.fillMaxSize(), ) { - NavGraph(startDestination = Routes.PeopleNavigation.route) + NavGraph( + startDestination = Routes.PeopleNavigation.route + ) } } } diff --git a/app/src/main/java/com/example/rememberme/data/PeopleRepositoryImpl.kt b/app/src/main/java/com/example/rememberme/data/PeopleRepositoryImpl.kt index 917de4c..bc35004 100644 --- a/app/src/main/java/com/example/rememberme/data/PeopleRepositoryImpl.kt +++ b/app/src/main/java/com/example/rememberme/data/PeopleRepositoryImpl.kt @@ -16,8 +16,8 @@ class PeopleRepositoryImpl( peopleDao.insert(people) } - override suspend fun deletePeople(people: People) { - peopleDao.removePerson(people.id) + override suspend fun deletePersonById(personId: Long) { + peopleDao.removePerson(personId) } override suspend fun updatePeople(people: People) { diff --git a/app/src/main/java/com/example/rememberme/di/AppModule.kt b/app/src/main/java/com/example/rememberme/di/AppModule.kt index 0f555e0..1206e38 100644 --- a/app/src/main/java/com/example/rememberme/di/AppModule.kt +++ b/app/src/main/java/com/example/rememberme/di/AppModule.kt @@ -11,6 +11,7 @@ import com.example.rememberme.domain.usecases.add_person.ValidateGenderSelection import com.example.rememberme.domain.usecases.add_person.ValidatePlaceUseCase import com.example.rememberme.domain.usecases.add_person.ValidateSecondNameUseCase import com.example.rememberme.domain.usecases.add_person.ValidateTimeUseCase +import com.example.rememberme.domain.usecases.people.DeletePersonById import com.example.rememberme.domain.usecases.people.GetAllPeople import com.example.rememberme.domain.usecases.people.GetPersonById import com.example.rememberme.domain.usecases.people.InsertNewPerson @@ -64,7 +65,8 @@ object AppModule { getAllPeople = GetAllPeople(peopleRepository), getPersonById = GetPersonById(peopleRepository), insertPerson = InsertNewPerson(peopleRepository), - updatePerson = UpdatePerson(peopleRepository) + updatePerson = UpdatePerson(peopleRepository), + deletePersonById = DeletePersonById(peopleRepository) ) } diff --git a/app/src/main/java/com/example/rememberme/domain/repository/PeopleRepository.kt b/app/src/main/java/com/example/rememberme/domain/repository/PeopleRepository.kt index 2b964db..7cd7a16 100644 --- a/app/src/main/java/com/example/rememberme/domain/repository/PeopleRepository.kt +++ b/app/src/main/java/com/example/rememberme/domain/repository/PeopleRepository.kt @@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.Flow interface PeopleRepository { fun getAllPeople(): Flow> suspend fun insertNewPerson(people: People) - suspend fun deletePeople(people: People) + suspend fun deletePersonById(personId: Long) suspend fun updatePeople(people: People) suspend fun deleteAllPeople() suspend fun getPeopleById(id: Long): Flow diff --git a/app/src/main/java/com/example/rememberme/domain/usecases/people/DeletePersonById.kt b/app/src/main/java/com/example/rememberme/domain/usecases/people/DeletePersonById.kt new file mode 100644 index 0000000..3e602f5 --- /dev/null +++ b/app/src/main/java/com/example/rememberme/domain/usecases/people/DeletePersonById.kt @@ -0,0 +1,12 @@ +package com.example.rememberme.domain.usecases.people + +import com.example.rememberme.domain.repository.PeopleRepository + +class DeletePersonById( + private val peopleRepository: PeopleRepository +) { + suspend operator fun invoke(personId: Long) { + peopleRepository.deletePersonById(personId) + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/rememberme/domain/usecases/people/PeopleUseCases.kt b/app/src/main/java/com/example/rememberme/domain/usecases/people/PeopleUseCases.kt index a1914a4..b3be0f8 100644 --- a/app/src/main/java/com/example/rememberme/domain/usecases/people/PeopleUseCases.kt +++ b/app/src/main/java/com/example/rememberme/domain/usecases/people/PeopleUseCases.kt @@ -4,5 +4,7 @@ data class PeopleUseCases( val getAllPeople: GetAllPeople, val getPersonById: GetPersonById, val insertPerson: InsertNewPerson, - val updatePerson: UpdatePerson + val updatePerson: UpdatePerson, + val deletePersonById: DeletePersonById + ) \ No newline at end of file diff --git a/app/src/main/java/com/example/rememberme/presentation/details/PersonDetails.kt b/app/src/main/java/com/example/rememberme/presentation/details/PersonDetails.kt index 483ca96..1b97344 100644 --- a/app/src/main/java/com/example/rememberme/presentation/details/PersonDetails.kt +++ b/app/src/main/java/com/example/rememberme/presentation/details/PersonDetails.kt @@ -21,6 +21,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.foundation.verticalScroll import androidx.compose.material.icons.Icons import androidx.compose.material.icons.automirrored.filled.ArrowBack +import androidx.compose.material.icons.filled.Delete import androidx.compose.material.icons.filled.Edit import androidx.compose.material.icons.filled.Warning import androidx.compose.material3.Card @@ -76,7 +77,12 @@ fun PersonDetailsScreen( } uiState.value.person != null -> { Log.d(TAG, "PersonDetailsScreen: ${uiState.value.person}") - PersonDetailsContent(uiState.value.person!!, navigateUp, navigateToEditScreen) + PersonDetailsContent( + uiState.value.person!!, + navigateUp, + navigateToEditScreen, + onDeletePerson = {viewModel.onEvent(PersonDetailsEvent.DeletePerson)} + ) } else -> { Log.e(TAG, "PersonDetailsScreen: Person not found") @@ -120,6 +126,7 @@ fun PersonDetailsContent( person: People, navigateUp: () -> Unit, navigateToEditScreen: (Long?) -> Unit, + onDeletePerson: () -> Unit, modifier: Modifier = Modifier ) { val scrollState = rememberScrollState() @@ -175,6 +182,17 @@ fun PersonDetailsContent( tint = MaterialTheme.colorScheme.onPrimary ) } + IconButton(onClick = { + Log.d(TAG, "PersonDetailsContent: Delete Person Clicked, Deleting person with personId: ${person.id}") + onDeletePerson() + navigateUp() + }) { + Icon( + imageVector = Icons.Filled.Delete, + contentDescription = null, + tint = MaterialTheme.colorScheme.onPrimary + ) + } } } @@ -269,7 +287,8 @@ fun PersonDetailsContentPreview() { navigateUp = { Log.d(TAG, "PersonDetailsContentPreview: Clicked") }, - navigateToEditScreen = {} + navigateToEditScreen = {}, + onDeletePerson = {} ) } } diff --git a/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsEvent.kt b/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsEvent.kt index cdff90e..37d730c 100644 --- a/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsEvent.kt +++ b/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsEvent.kt @@ -2,5 +2,6 @@ package com.example.rememberme.presentation.details sealed class PersonDetailsEvent { data class LoadPerson(val personId: Long) : PersonDetailsEvent() - data object NavigateUp : PersonDetailsEvent() + data object EditPerson: PersonDetailsEvent() + data object DeletePerson: PersonDetailsEvent() } diff --git a/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsViewModel.kt b/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsViewModel.kt index 7d0b8f4..2bcc9f5 100644 --- a/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsViewModel.kt +++ b/app/src/main/java/com/example/rememberme/presentation/details/PersonDetailsViewModel.kt @@ -1,12 +1,16 @@ package com.example.rememberme.presentation.details +import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.example.rememberme.domain.usecases.people.PeopleUseCases import dagger.hilt.android.lifecycle.HiltViewModel +import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import javax.inject.Inject @HiltViewModel @@ -21,14 +25,20 @@ class PersonDetailsViewModel @Inject constructor( is PersonDetailsEvent.LoadPerson -> { loadPerson(event.personId) } - is PersonDetailsEvent.NavigateUp -> { - // Handle navigation + + is PersonDetailsEvent.EditPerson -> { + + } + + is PersonDetailsEvent.DeletePerson -> { + Log.d(TAG, "Delete event received") + deletePerson() } } } private fun loadPerson(personId: Long) { - _uiState.value = _uiState.value.copy(isLoading = true) + _uiState.update { _uiState.value.copy(isLoading = true) } viewModelScope.launch { try { peopleUseCases.getPersonById(personId).collect { person -> @@ -39,4 +49,31 @@ class PersonDetailsViewModel @Inject constructor( } } } + + private fun navigateUp() { + + } + + private fun editPerson() { + + } + + private fun deletePerson() { + viewModelScope.launch { + withContext(Dispatchers.IO) { + try { + Log.d(TAG, "Deleting person with ID: ${_uiState.value.person!!.id}") + peopleUseCases.deletePersonById(_uiState.value.person!!.id) + _uiState.update { PersonDetailsUiState() } // Clear UI state after deletion + } catch (e: Exception) { + Log.e(TAG, "Error deleting person: ${e.message}") + _uiState.update { _uiState.value.copy(error = e.message) } + } + } + } + } + + companion object { + private const val TAG = "PersonDetailsViewModel" + } }