Skip to content

Commit

Permalink
[FEAT] Attach DetailedNoticeContent Composable.
Browse files Browse the repository at this point in the history
  - Attach DetailedNoticeContent with AnimatedVisibility.
  - Allow user to fetch full content of selected notice from the list.
  • Loading branch information
doyoonkim3312 committed Oct 6, 2024
1 parent 2b111ee commit 7b662ac
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
)
}

}
}
}
Expand All @@ -85,5 +103,16 @@ fun MoreCategorizedNotification(
state = pullRefreshState
)
}

AnimatedVisibility(
uiState.isDetailedContentVisible
) {
DetailedNoticeContent(
modifier = Modifier.padding(15.dp),
requested = uiState.detailedContentState
) {
viewModel.updatedDetailedContentRequest(false)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -12,14 +13,16 @@ 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
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"

Expand All @@ -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)
Expand Down Expand Up @@ -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<Notice>.addAll(additionalElements: List<Notice>): List<Notice> {
return List<Notice>(this.size + additionalElements.size) {
if (it in indices) this[it]
Expand All @@ -94,5 +139,7 @@ data class MoreNotificationListState(
val notificationCategory: NoticeCategory = NoticeCategory.Unspecified,
val notices: List<Notice> = List<Notice>(20) { Notice() },
val isLoading: Boolean = false,
val isRefreshRequested: Boolean = false
val isRefreshRequested: Boolean = false,
val isDetailedContentVisible: Boolean = false,
val detailedContentState: DetailedContentState = DetailedContentState()
)

0 comments on commit 7b662ac

Please sign in to comment.