Skip to content

Commit

Permalink
feat(MediaDetails): MediaActivity screen
Browse files Browse the repository at this point in the history
  • Loading branch information
axiel7 committed Aug 17, 2024
1 parent 70a2295 commit e58577d
Show file tree
Hide file tree
Showing 10 changed files with 368 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ fragment ListActivityFragment on ListActivity {
likeCount
replyCount
isLocked
type
progress
status
userId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import com.axiel7.anihyou.ui.screens.explore.charts.MediaChartList
import com.axiel7.anihyou.ui.screens.explore.search.Search
import com.axiel7.anihyou.ui.screens.explore.season.SeasonAnime
import com.axiel7.anihyou.ui.screens.mediadetails.MediaDetails
import com.axiel7.anihyou.ui.screens.mediadetails.activity.MediaActivity
import com.axiel7.anihyou.ui.screens.notifications.Notifications
import com.axiel7.anihyou.ui.screens.profile.UserDetails
import com.axiel7.anihyou.ui.screens.reviewdetails.ReviewDetails
Expand All @@ -45,6 +46,10 @@ class NavActionManager(
navController.navigate(MediaDetails(id))
}

fun toMediaActivity(mediaId: Int) {
navController.navigate(MediaActivity(mediaId))
}

fun toCharacterDetails(id: Int) {
navController.navigate(CharacterDetails(id))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import com.axiel7.anihyou.ui.composables.common.FavoriteIconButton
import com.axiel7.anihyou.ui.composables.markdown.DefaultMarkdownText
import com.axiel7.anihyou.ui.composables.media.MediaPoster
import com.axiel7.anihyou.ui.composables.person.PersonItemSmall
import com.axiel7.anihyou.ui.screens.profile.activity.ActivityMenu
import com.axiel7.anihyou.ui.theme.AniHyouTheme
import com.axiel7.anihyou.utils.DateUtils.secondsToLegibleText
import com.axiel7.anihyou.utils.DateUtils.timestampIntervalSinceNow
Expand All @@ -42,10 +43,12 @@ fun ActivityFeedItem(
likeCount: Int,
isLiked: Boolean?,
mediaCoverUrl: String? = null,
showMenu: Boolean = false,
onClick: () -> Unit,
onClickUser: () -> Unit,
onClickLike: () -> Unit,
onClickMedia: () -> Unit = {},
onClickDelete: () -> Unit = {},
navigateToFullscreenImage: (String) -> Unit = {},
) {
Row(
Expand Down Expand Up @@ -107,6 +110,7 @@ fun ActivityFeedItem(
}
Row(
modifier = Modifier.align(Alignment.End),
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.SpaceBetween
) {
CommentIconButton(
Expand All @@ -124,6 +128,11 @@ fun ActivityFeedItem(
fontSize = 14.sp,
iconSize = 20.dp,
)
if (showMenu) {
ActivityMenu(
onClickDelete = onClickDelete
)
}
}
}//:Column
}//Row
Expand All @@ -144,6 +153,7 @@ fun MediaActivityItemPreview() {
likeCount = 999,
isLiked = false,
mediaCoverUrl = "",
showMenu = true,
onClick = {},
onClickUser = {},
onClickLike = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import com.axiel7.anihyou.ui.screens.home.HomeView
import com.axiel7.anihyou.ui.screens.login.LoginView
import com.axiel7.anihyou.ui.screens.mediadetails.MediaDetails
import com.axiel7.anihyou.ui.screens.mediadetails.MediaDetailsView
import com.axiel7.anihyou.ui.screens.mediadetails.activity.MediaActivity
import com.axiel7.anihyou.ui.screens.mediadetails.activity.MediaActivityView
import com.axiel7.anihyou.ui.screens.notifications.Notifications
import com.axiel7.anihyou.ui.screens.notifications.NotificationsView
import com.axiel7.anihyou.ui.screens.profile.Profile
Expand Down Expand Up @@ -395,5 +397,11 @@ fun MainNavigation(
LoginView()
}
}

composable<MediaActivity> {
MediaActivityView(
navActionManager = navActionManager
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.axiel7.anihyou.ui.screens.mediadetails.activity

import com.axiel7.anihyou.ui.common.event.PagedEvent

interface MediaActivityEvent : PagedEvent {
fun setIsMine(value: Boolean)
fun toggleLikeActivity(id: Int)
fun deleteActivity(id: Int)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.axiel7.anihyou.ui.screens.mediadetails.activity

import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.snapshots.SnapshotStateList
import com.axiel7.anihyou.fragment.ListActivityFragment
import com.axiel7.anihyou.ui.common.state.PagedUiState

@Stable
data class MediaActivityUiState(
val activities: SnapshotStateList<ListActivityFragment> = mutableStateListOf(),
val isMine: Boolean = false,
override val page: Int = 1,
override val hasNextPage: Boolean = true,
override val error: String? = null,
override val isLoading: Boolean = true,
): PagedUiState() {
override fun setError(value: String?) = copy(error = value)
override fun setLoading(value: Boolean) = copy(isLoading = value)
override fun setPage(value: Int) = copy(page = value)
override fun setHasNextPage(value: Boolean) = copy(hasNextPage = value)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
package com.axiel7.anihyou.ui.screens.mediadetails.activity

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Surface
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.axiel7.anihyou.R
import com.axiel7.anihyou.data.model.activity.text
import com.axiel7.anihyou.type.ActivityType
import com.axiel7.anihyou.ui.common.navigation.NavActionManager
import com.axiel7.anihyou.ui.composables.DefaultScaffoldWithSmallTopAppBar
import com.axiel7.anihyou.ui.composables.common.BackIconButton
import com.axiel7.anihyou.ui.screens.home.activity.composables.ActivityFeedItem
import com.axiel7.anihyou.ui.screens.profile.activity.ActivityItemPlaceholder
import com.axiel7.anihyou.ui.theme.AniHyouTheme
import kotlinx.serialization.Serializable

@Serializable
@Immutable
data class MediaActivity(
val mediaId: Int
)

@Composable
fun MediaActivityView(
navActionManager: NavActionManager
) {
val viewModel: MediaActivityViewModel = hiltViewModel()
val uiState by viewModel.uiState.collectAsStateWithLifecycle()

MediaActivityContent(
uiState = uiState,
event = viewModel,
navActionManager = navActionManager,
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MediaActivityContent(
uiState: MediaActivityUiState,
event: MediaActivityEvent?,
navActionManager: NavActionManager,
) {
val topAppBarScrollBehavior = TopAppBarDefaults.exitUntilCollapsedScrollBehavior(
rememberTopAppBarState()
)
DefaultScaffoldWithSmallTopAppBar(
title = stringResource(R.string.activity),
navigationIcon = {
BackIconButton(
onClick = navActionManager::goBack
)
},
actions = {
IconButton(onClick = { event?.setIsMine(!uiState.isMine) }) {
Icon(
painter = painterResource(
id = if (uiState.isMine) R.drawable.person_filled_24
else R.drawable.person_24
),
contentDescription = null
)
}
},
scrollBehavior = topAppBarScrollBehavior
) { padding ->
LazyColumn(
modifier = Modifier
.padding(padding)
.nestedScroll(topAppBarScrollBehavior.nestedScrollConnection),
contentPadding = PaddingValues(top = 8.dp)
) {
if (uiState.isLoading) {
items(10) {
ActivityItemPlaceholder(
modifier = Modifier.padding(8.dp)
)
}
}
items(
items = uiState.activities,
contentType = { it }
) { item ->
ActivityFeedItem(
modifier = Modifier.padding(horizontal = 16.dp),
type = ActivityType.MEDIA_LIST,
username = item.user?.name,
avatarUrl = item.user?.avatar?.medium,
createdAt = item.createdAt,
text = item.text(),
replyCount = item.replyCount,
likeCount = item.likeCount,
isLiked = item.isLiked,
mediaCoverUrl = item.media?.coverImage?.medium,
showMenu = uiState.isMine,
onClick = {
navActionManager.toActivityDetails(item.id)
},
onClickUser = {
item.userId?.let(navActionManager::toUserDetails)
},
onClickLike = {
event?.toggleLikeActivity(item.id)
},
onClickDelete = {
event?.deleteActivity(item.id)
}
)
HorizontalDivider(modifier = Modifier.padding(bottom = 16.dp))
}
item(contentType = { 0 }) {
if (uiState.hasNextPage) {
Box(modifier = Modifier.fillMaxWidth()) {
CircularProgressIndicator(
modifier = Modifier.align(Alignment.Center)
)
}
LaunchedEffect(uiState.isLoading) {
if (!uiState.isLoading) event?.onLoadMore()
}
}
}
}
}
}

@Preview
@Composable
private fun MediaActivityViewPreview() {
AniHyouTheme {
Surface {
MediaActivityContent(
uiState = MediaActivityUiState(),
event = null,
navActionManager = NavActionManager.rememberNavActionManager()
)
}
}
}
Loading

0 comments on commit e58577d

Please sign in to comment.