diff --git a/app/src/main/java/com/doyoonkim/knutice/presentation/MoreCategorizedNoticiation.kt b/app/src/main/java/com/doyoonkim/knutice/presentation/MoreCategorizedNoticiation.kt index 33cfaa5..1038b2a 100644 --- a/app/src/main/java/com/doyoonkim/knutice/presentation/MoreCategorizedNoticiation.kt +++ b/app/src/main/java/com/doyoonkim/knutice/presentation/MoreCategorizedNoticiation.kt @@ -1,12 +1,17 @@ package com.doyoonkim.knutice.presentation import android.util.Log +import androidx.compose.animation.AnimatedVisibility +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.wrapContentHeight +import androidx.compose.foundation.layout.wrapContentSize import androidx.compose.foundation.lazy.LazyColumn +//noinspection UsingMaterialAndMaterial3Libraries import androidx.compose.material.Divider import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.pullrefresh.PullRefreshIndicator @@ -69,12 +74,25 @@ fun MoreCategorizedNotification( color = MaterialTheme.colorScheme.containerBackground ) } - NotificationPreview( - notificationTitle = notice.title, - notificationInfo = "[${notice.departName}] ${notice.timestamp}", - isImageContained = notice.imageUrl != "Unknown", - imageUrl = notice.imageUrl - ) + Row( + modifier = Modifier.wrapContentSize() + .clickable { + viewModel.updatedDetailedContentRequest( + true, + notice.title, + "[${notice.departName}] ${notice.timestamp}", + notice.url + ) + } + ) { + NotificationPreview( + notificationTitle = notice.title, + notificationInfo = "[${notice.departName}] ${notice.timestamp}", + isImageContained = notice.imageUrl != "Unknown", + imageUrl = notice.imageUrl + ) + } + } } } @@ -85,5 +103,16 @@ fun MoreCategorizedNotification( state = pullRefreshState ) } + + AnimatedVisibility( + uiState.isDetailedContentVisible + ) { + DetailedNoticeContent( + modifier = Modifier.padding(15.dp), + requested = uiState.detailedContentState + ) { + viewModel.updatedDetailedContentRequest(false) + } + } } diff --git a/app/src/main/java/com/doyoonkim/knutice/viewModel/MoreCategorizedNotificationViewModel.kt b/app/src/main/java/com/doyoonkim/knutice/viewModel/MoreCategorizedNotificationViewModel.kt index c7c36f0..1e506b5 100644 --- a/app/src/main/java/com/doyoonkim/knutice/viewModel/MoreCategorizedNotificationViewModel.kt +++ b/app/src/main/java/com/doyoonkim/knutice/viewModel/MoreCategorizedNotificationViewModel.kt @@ -2,6 +2,7 @@ package com.doyoonkim.knutice.viewModel import android.util.Log import androidx.lifecycle.ViewModel +import com.doyoonkim.knutice.domain.CrawlFullContentImpl import com.doyoonkim.knutice.domain.FetchNoticesPerPageInCategory import com.doyoonkim.knutice.model.Notice import com.doyoonkim.knutice.model.NoticeCategory @@ -12,6 +13,7 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.collectLatest +import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.update import kotlinx.coroutines.launch @@ -19,7 +21,8 @@ import javax.inject.Inject @HiltViewModel class MoreCategorizedNotificationViewModel @Inject constructor( - private val fetchListOfNoticesUseCase: FetchNoticesPerPageInCategory + private val fetchListOfNoticesUseCase: FetchNoticesPerPageInCategory, + private val crawlFullContentUseCase: CrawlFullContentImpl ): ViewModel() { private val filename = "MoreCategorizedNotificationViewModel" @@ -43,6 +46,24 @@ class MoreCategorizedNotificationViewModel @Inject constructor( } } + fun updatedDetailedContentRequest( + isDetailedContentVisible: Boolean, + title: String = "", + info: String = "", + url: String = "" + ) { + if (isDetailedContentVisible) { + getFullContent(title, info, url) + } else { + _uiState.update { + it.copy( + isDetailedContentVisible = false, + detailedContentState = DetailedContentState() + ) + } + } + } + fun fetchNotificationPerPage() { _uiState.update { it.copy(isLoading = true) @@ -80,6 +101,30 @@ class MoreCategorizedNotificationViewModel @Inject constructor( } } + private fun getFullContent(title: String, info: String, url: String) { + CoroutineScope(Dispatchers.IO).launch { + crawlFullContentUseCase.getFullContentFromSource(title, info, url) + .map { Result.success(it) } + .catch { emit(Result.failure(it)) } + .flowOn(Dispatchers.IO) + .collectLatest { result -> + result.fold( + onSuccess = { content -> + _uiState.update { + it.copy( + isDetailedContentVisible = true, + detailedContentState = content + ) + } + }, + onFailure = { + Log.d("MoreCategorizedNotificationViewModel", it.stackTraceToString()) + } + ) + } + } + } + private fun List.addAll(additionalElements: List): List { return List(this.size + additionalElements.size) { if (it in indices) this[it] @@ -94,5 +139,7 @@ data class MoreNotificationListState( val notificationCategory: NoticeCategory = NoticeCategory.Unspecified, val notices: List = List(20) { Notice() }, val isLoading: Boolean = false, - val isRefreshRequested: Boolean = false + val isRefreshRequested: Boolean = false, + val isDetailedContentVisible: Boolean = false, + val detailedContentState: DetailedContentState = DetailedContentState() ) \ No newline at end of file