From 4eee7f7a70d325a8deead91cf5b5acfc3ea2ffb9 Mon Sep 17 00:00:00 2001 From: gregory Date: Wed, 8 Jan 2025 10:08:00 +0100 Subject: [PATCH] groups are now shown in each group --- .../feature/docs/db/dao/DocumentsDao.kt | 10 +++ .../docs/repoapi/DocumentRepository.kt | 1 + .../docs/repoimpl/DocumentRepositoryImpl.kt | 8 +- feature/group/details/build.gradle.kts | 3 + .../group/details/GroupDetailsScreen.kt | 76 +++++++++++++++++-- .../group/details/GroupDetailsState.kt | 4 +- .../group/details/GroupDetailsViewModel.kt | 14 +++- .../group/navigation/GroupsNavigation.kt | 10 ++- 8 files changed, 111 insertions(+), 15 deletions(-) diff --git a/feature/docs/db/src/main/java/com/grappim/docuvault/feature/docs/db/dao/DocumentsDao.kt b/feature/docs/db/src/main/java/com/grappim/docuvault/feature/docs/db/dao/DocumentsDao.kt index 14718df..62000d2 100644 --- a/feature/docs/db/src/main/java/com/grappim/docuvault/feature/docs/db/dao/DocumentsDao.kt +++ b/feature/docs/db/src/main/java/com/grappim/docuvault/feature/docs/db/dao/DocumentsDao.kt @@ -15,6 +15,16 @@ import kotlinx.coroutines.flow.Flow @Dao interface DocumentsDao { + @Transaction + @Query( + """ + SELECT * FROM document_table +WHERE groupId=:groupId AND isCreated=1 +ORDER BY createdDate + """ + ) + suspend fun getDocumentsByGroupId(groupId: Long): List + @[Transaction Query("SELECT * FROM document_table WHERE isCreated=1 ORDER BY createdDate")] fun getFullDocumentsFlow(): Flow> diff --git a/feature/docs/repo-api/src/main/java/com/grappim/docuvault/feature/docs/repoapi/DocumentRepository.kt b/feature/docs/repo-api/src/main/java/com/grappim/docuvault/feature/docs/repoapi/DocumentRepository.kt index f60dc0c..eacc3ca 100644 --- a/feature/docs/repo-api/src/main/java/com/grappim/docuvault/feature/docs/repoapi/DocumentRepository.kt +++ b/feature/docs/repo-api/src/main/java/com/grappim/docuvault/feature/docs/repoapi/DocumentRepository.kt @@ -21,4 +21,5 @@ interface DocumentRepository { suspend fun updateDocumentWithFiles(document: Document, files: List) suspend fun updateFilesInDocument(documentId: Long, files: List) + suspend fun getDocumentsByGroupId(groupId: Long): List } diff --git a/feature/docs/repo-impl/src/main/java/com/grappim/docuvault/feature/docs/repoimpl/DocumentRepositoryImpl.kt b/feature/docs/repo-impl/src/main/java/com/grappim/docuvault/feature/docs/repoimpl/DocumentRepositoryImpl.kt index 31cf93c..9999abb 100644 --- a/feature/docs/repo-impl/src/main/java/com/grappim/docuvault/feature/docs/repoimpl/DocumentRepositoryImpl.kt +++ b/feature/docs/repo-impl/src/main/java/com/grappim/docuvault/feature/docs/repoimpl/DocumentRepositoryImpl.kt @@ -82,7 +82,7 @@ class DocumentRepositoryImpl @Inject constructor( documentsDao.insert(documentMapper.toDocumentEntity(document)) } - override suspend fun addDocuments(documents: List) = withContext(ioDispatcher) { + override suspend fun addDocuments(documents: List): Unit = withContext(ioDispatcher) { documents.map { document -> async { documentsDao.insertDocumentAndFiles( @@ -94,7 +94,6 @@ class DocumentRepositoryImpl @Inject constructor( ) } }.awaitAll() - Unit } override suspend fun removeDocumentById(id: Long) { @@ -114,4 +113,9 @@ class DocumentRepositoryImpl @Inject constructor( val filesEntities = documentFileMapper.toDocumentFileEntityList(documentId, files) documentsDao.upsertFiles(filesEntities) } + + override suspend fun getDocumentsByGroupId(groupId: Long): List { + val entities = documentsDao.getDocumentsByGroupId(groupId) + return documentMapper.toDocumentList(entities) + } } diff --git a/feature/group/details/build.gradle.kts b/feature/group/details/build.gradle.kts index e4825a5..c657094 100644 --- a/feature/group/details/build.gradle.kts +++ b/feature/group/details/build.gradle.kts @@ -11,8 +11,10 @@ android { dependencies { implementation(project(":uikit")) implementation(project(":core:navigation")) + implementation(project(":utils:files")) implementation(project(":feature:group:domain")) implementation(project(":feature:group:repo-api")) + implementation(project(":feature:docs:repo-api")) implementation(libs.androidx.core.ktx) @@ -28,4 +30,5 @@ dependencies { implementation(libs.androidx.hilt.navigation.compose) implementation(libs.compose.color.picker.android) + implementation(libs.coil) } diff --git a/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsScreen.kt b/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsScreen.kt index 5b7f31c..36ce4c3 100644 --- a/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsScreen.kt +++ b/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsScreen.kt @@ -1,21 +1,85 @@ package com.grappim.docuvault.feature.group.details +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.foundation.lazy.items +import androidx.compose.material.Card import androidx.compose.material.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp import androidx.hilt.navigation.compose.hiltViewModel import androidx.lifecycle.compose.collectAsStateWithLifecycle +import coil.compose.rememberAsyncImagePainter +import com.grappim.docuvault.uikit.theme.DefaultArrangement +import com.grappim.docuvault.uikit.theme.DefaultHorizontalPadding +import com.grappim.docuvault.uikit.widget.PlatoImage @Composable -@Suppress("UnusedParameter") -fun GroupDetailsRoute(viewModel: GroupDetailsViewModel = hiltViewModel(), goBack: () -> Unit) { +fun GroupDetailsRoute( + viewModel: GroupDetailsViewModel = hiltViewModel(), + onDocumentClick: (id: Long) -> Unit +) { val state by viewModel.viewState.collectAsStateWithLifecycle() - GroupDetailsContent(state = state) + if (state.group != null) { + GroupDetailsContent(state = state, onDocumentClick = onDocumentClick) + } } @Composable -private fun GroupDetailsContent(state: GroupDetailsState) { - if (state.group != null) { - Text(text = state.group.name) +private fun GroupDetailsContent(state: GroupDetailsState, onDocumentClick: (id: Long) -> Unit) { + requireNotNull(state.group) + + LazyColumn( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = DefaultHorizontalPadding), + verticalArrangement = Arrangement.spacedBy(DefaultArrangement), + horizontalAlignment = Alignment.CenterHorizontally + ) { + item { + Text(text = state.group.name) + } + items(state.documents) { document -> + Card( + modifier = Modifier + .fillMaxWidth(), + backgroundColor = document.groupColor, + onClick = { + onDocumentClick(document.id.toLong()) + } + ) { + Column( + modifier = Modifier + .padding(horizontal = 8.dp) + ) { + Text( + text = document.name, + modifier = Modifier + .padding( + top = 8.dp + ) + ) + Text(text = document.createdDate) + + Card( + modifier = Modifier + .padding( + top = 8.dp, + bottom = 8.dp + ) + .size(100.dp) + ) { + PlatoImage(painter = rememberAsyncImagePainter(document.preview)) + } + } + } + } } } diff --git a/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsState.kt b/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsState.kt index 48ae809..083df24 100644 --- a/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsState.kt +++ b/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsState.kt @@ -1,7 +1,9 @@ package com.grappim.docuvault.feature.group.details import com.grappim.docuvault.feature.group.domain.Group +import com.grappim.docuvault.utils.files.models.DocumentListUI data class GroupDetailsState( - val group: Group? = null + val group: Group? = null, + val documents: List = emptyList() ) diff --git a/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsViewModel.kt b/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsViewModel.kt index 4376c8e..fa1c51e 100644 --- a/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsViewModel.kt +++ b/feature/group/details/src/main/java/com/grappim/docuvault/feature/group/details/GroupDetailsViewModel.kt @@ -4,7 +4,9 @@ import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.grappim.docuvault.core.navigation.GroupNavDestinations +import com.grappim.docuvault.feature.docs.repoapi.DocumentRepository import com.grappim.docuvault.feature.group.repoapi.GroupRepository +import com.grappim.docuvault.utils.files.mappers.DocsListUIMapper import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow @@ -15,7 +17,9 @@ import javax.inject.Inject @HiltViewModel class GroupDetailsViewModel @Inject constructor( private val savedStateHandle: SavedStateHandle, - private val groupRepository: GroupRepository + private val groupRepository: GroupRepository, + private val docRepository: DocumentRepository, + private val docsListUIMapper: DocsListUIMapper ) : ViewModel() { private val _viewState = MutableStateFlow(GroupDetailsState()) @@ -26,14 +30,16 @@ class GroupDetailsViewModel @Inject constructor( get() = requireNotNull(savedStateHandle[GroupNavDestinations.GroupDetails.KEY_GROUP_ID]) init { - fetchGroup() + getData() } - private fun fetchGroup() { + private fun getData() { viewModelScope.launch { val group = groupRepository.getGroupById(groupId) + val documents = docRepository.getDocumentsByGroupId(groupId.toLong()) + val uiDocuments = docsListUIMapper.toDocumentListUIList(documents) _viewState.update { - it.copy(group = group) + it.copy(group = group, documents = uiDocuments) } } } diff --git a/feature/group/navigation/src/main/java/com/grappim/docuvault/feature/group/navigation/GroupsNavigation.kt b/feature/group/navigation/src/main/java/com/grappim/docuvault/feature/group/navigation/GroupsNavigation.kt index ce793d0..de1646f 100644 --- a/feature/group/navigation/src/main/java/com/grappim/docuvault/feature/group/navigation/GroupsNavigation.kt +++ b/feature/group/navigation/src/main/java/com/grappim/docuvault/feature/group/navigation/GroupsNavigation.kt @@ -5,6 +5,7 @@ import androidx.navigation.NavGraphBuilder import androidx.navigation.NavType import androidx.navigation.compose.composable import androidx.navigation.navArgument +import com.grappim.docuvault.core.navigation.DocumentsNavDestinations import com.grappim.docuvault.core.navigation.GroupNavDestinations import com.grappim.docuvault.core.navigation.MainNavDestinations import com.grappim.docuvault.feature.group.details.GroupDetailsRoute @@ -43,9 +44,14 @@ fun NavGraphBuilder.groupsScreens(navController: NavController) { ) ) { GroupDetailsRoute( - goBack = { - navController.popBackStack() + onDocumentClick = { id -> + navController.navigate( + DocumentsNavDestinations.Details.createRoute(id) + ) } +// goBack = { +// navController.popBackStack() +// } ) } }