Skip to content

Commit

Permalink
Merge pull request #29 from omer358/dev
Browse files Browse the repository at this point in the history
Resolves #25
  • Loading branch information
omer358 authored Jul 2, 2024
2 parents 13ebe8b + 31411b1 commit 7232463
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 12 deletions.
4 changes: 3 additions & 1 deletion app/src/main/java/com/example/rememberme/RememberMeApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ fun RememberMeApp() {
Surface(
modifier = Modifier.fillMaxSize(),
) {
NavGraph(startDestination = Routes.PeopleNavigation.route)
NavGraph(
startDestination = Routes.PeopleNavigation.route
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/java/com/example/rememberme/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -64,7 +65,8 @@ object AppModule {
getAllPeople = GetAllPeople(peopleRepository),
getPersonById = GetPersonById(peopleRepository),
insertPerson = InsertNewPerson(peopleRepository),
updatePerson = UpdatePerson(peopleRepository)
updatePerson = UpdatePerson(peopleRepository),
deletePersonById = DeletePersonById(peopleRepository)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.coroutines.flow.Flow
interface PeopleRepository {
fun getAllPeople(): Flow<List<People>>
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<People?>
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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

)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -120,6 +126,7 @@ fun PersonDetailsContent(
person: People,
navigateUp: () -> Unit,
navigateToEditScreen: (Long?) -> Unit,
onDeletePerson: () -> Unit,
modifier: Modifier = Modifier
) {
val scrollState = rememberScrollState()
Expand Down Expand Up @@ -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
)
}
}

}
Expand Down Expand Up @@ -269,7 +287,8 @@ fun PersonDetailsContentPreview() {
navigateUp = {
Log.d(TAG, "PersonDetailsContentPreview: Clicked")
},
navigateToEditScreen = {}
navigateToEditScreen = {},
onDeletePerson = {}
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 ->
Expand All @@ -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"
}
}

0 comments on commit 7232463

Please sign in to comment.