Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ fun CommentsBottomSheet(navigator: DestinationsNavigator) {
is AsyncResource.Content -> {
val comments by state.data.second.comments.collectAsStateWithLifecycle()
val currentUserId = state.data.first.id
val createContentState by viewModel.createContentState.collectAsStateWithLifecycle()

Column {
CommentsBottomSheetContent(
comments = comments,
currentUserId = currentUserId,
createContentState = createContentState,
onEvent = viewModel::onEvent,
)
}
Expand All @@ -115,9 +117,9 @@ fun CommentsBottomSheet(navigator: DestinationsNavigator) {
private fun ColumnScope.CommentsBottomSheetContent(
comments: List<ThreadedCommentData>,
currentUserId: String,
createContentState: CreateContentState,
onEvent: (Event) -> Unit,
) {
var createCommentData: CreateCommentData? by remember { mutableStateOf(null) }
var expandedCommentId: String? by remember { mutableStateOf(null) }

Text(
Expand All @@ -141,9 +143,7 @@ private fun ColumnScope.CommentsBottomSheetContent(
onExpandClick = {
expandedCommentId = comment.id.takeUnless { it == expandedCommentId }
},
onReplyClick = { commentId ->
createCommentData = CreateCommentData(commentId)
},
onReplyClick = { commentId -> onEvent(Event.OnReply(commentId)) },
onEvent = onEvent,
modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp, vertical = 8.dp),
)
Expand All @@ -154,7 +154,7 @@ private fun ColumnScope.CommentsBottomSheetContent(
}

FloatingActionButton(
onClick = { createCommentData = CreateCommentData() },
onClick = { onEvent(Event.OnAddContent) },
modifier = Modifier.align(Alignment.BottomEnd).padding(16.dp),
shape = CircleShape,
) {
Expand All @@ -166,17 +166,13 @@ private fun ColumnScope.CommentsBottomSheetContent(
}
}

if (createCommentData != null) {
CreateContentBottomSheet(
title = "Add comment",
onDismiss = { createCommentData = null },
requireText = true,
onPost = { text, attachments ->
onEvent(Event.OnPost(text, createCommentData?.replyParentId, attachments))
createCommentData = null
},
)
}
CreateContentBottomSheet(
state = createContentState,
title = "Add comment",
onDismiss = { onEvent(Event.OnContentCreateDismiss) },
onPost = { text, attachments -> onEvent(Event.OnPost(text, attachments)) },
requireText = true,
)
}

@Composable
Expand Down Expand Up @@ -311,5 +307,3 @@ private fun Comment(
}
}
}

@JvmInline private value class CreateCommentData(val replyParentId: String? = null)
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ import io.getstream.feeds.android.sample.util.notNull
import io.getstream.feeds.android.sample.util.withFirstContent
import io.getstream.feeds.android.sample.utils.logResult
import javax.inject.Inject
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.stateIn
Expand Down Expand Up @@ -79,6 +82,10 @@ constructor(
.map { loadingState -> loadingState.map { it.first to it.second.state } }
.stateIn(viewModelScope, SharingStarted.Eagerly, AsyncResource.Loading)

private var replyParentId: String? = null
private val _createContentState = MutableStateFlow(CreateContentState.Hidden)
val createContentState: StateFlow<CreateContentState> = _createContentState.asStateFlow()

init {
activity.withFirstContent(viewModelScope) {
get().logResult(TAG, "Loading activity: $activityId")
Expand All @@ -89,9 +96,12 @@ constructor(
when (event) {
Event.OnScrollToBottom -> loadMore()
is Event.OnEdit -> edit(id = event.commentId, text = event.text)
is Event.OnPost -> post(event.text, event.replyParentId, event.attachments)
is Event.OnPost -> post(event.text, replyParentId, event.attachments)
is Event.OnLike -> toggleLike(event.comment)
is Event.OnDelete -> delete(event.commentId)
Event.OnAddContent -> showAddContent(true)
Event.OnContentCreateDismiss -> showAddContent(false)
is Event.OnReply -> showAddContent(true, event.parentId)
}
}

Expand Down Expand Up @@ -130,49 +140,67 @@ constructor(
}
}

private fun showAddContent(show: Boolean, replyParentId: String? = null) {
_createContentState.value =
if (show) CreateContentState.Composing else CreateContentState.Hidden
this.replyParentId = replyParentId
}

private fun post(text: String, replyParentId: String?, attachments: List<Uri>) {
_createContentState.value = CreateContentState.Posting

activity.withFirstContent(viewModelScope) {
val attachmentFiles =
context.copyToCache(attachments).getOrElse { error ->
Log.e(TAG, "Failed to copy attachments", error)
_createContentState.value = CreateContentState.Composing
return@withFirstContent
}

addComment(
ActivityAddCommentRequest(
comment = text,
activityId = activityId,
parentId = replyParentId,
createNotificationActivity = true,
attachmentUploads =
attachmentFiles.map {
FeedUploadPayload(file = it, type = FileType.Image("jpeg"))
},
),
attachmentUploadProgress = { file, progress ->
Log.d(TAG, "Uploading attachment: ${file.type}, progress: $progress")
},
)
.logResult(TAG, "Adding comment to activity: $activityId")
val result =
addComment(
ActivityAddCommentRequest(
comment = text,
activityId = activityId,
parentId = replyParentId,
createNotificationActivity = true,
attachmentUploads =
attachmentFiles.map {
FeedUploadPayload(file = it, type = FileType.Image("jpeg"))
},
),
attachmentUploadProgress = { file, progress ->
Log.d(TAG, "Uploading attachment: ${file.type}, progress: $progress")
},
)
.logResult(TAG, "Adding comment to activity: $activityId")

deleteFiles(attachmentFiles)

_createContentState.value =
result.fold(
onSuccess = { CreateContentState.Hidden },
onFailure = { CreateContentState.Composing },
)
}
}

sealed interface Event {
data object OnScrollToBottom : Event

data object OnAddContent : Event

data class OnLike(val comment: ThreadedCommentData) : Event

data class OnDelete(val commentId: String) : Event

data class OnEdit(val commentId: String, val text: String) : Event

data class OnPost(
val text: String,
val replyParentId: String?,
val attachments: List<Uri>,
) : Event
data class OnPost(val text: String, val attachments: List<Uri>) : Event

data object OnContentCreateDismiss : Event

data class OnReply(val parentId: String) : Event
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,22 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.SheetValue
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -51,22 +54,39 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

enum class CreateContentState {
Hidden,
Composing,
Posting,
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CreateContentBottomSheet(
state: CreateContentState,
title: String,
onDismiss: () -> Unit,
onPost: (text: String, attachments: List<Uri>) -> Unit,
requireText: Boolean,
extraActions: @Composable RowScope.() -> Unit = {},
extraActions: @Composable RowScope.(inputEnabled: Boolean) -> Unit = {},
) {
if (state == CreateContentState.Hidden) return

val state by rememberUpdatedState(state)
val sheetState =
rememberModalBottomSheetState(
skipPartiallyExpanded = true,
// Do not allow hiding while posting
confirmValueChange = { it != SheetValue.Hidden || state != CreateContentState.Posting },
)
val inputEnabled = state == CreateContentState.Composing

var postText by remember { mutableStateOf("") }
var attachments by remember { mutableStateOf<List<Uri>>(emptyList()) }
val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)
var attachments by remember { mutableStateOf(emptyList<Uri>()) }

ModalBottomSheet(
onDismissRequest = onDismiss,
sheetState = bottomSheetState,
sheetState = sheetState,
modifier = Modifier.fillMaxWidth(),
) {
Column(modifier = Modifier.fillMaxWidth().padding(horizontal = 16.dp)) {
Expand All @@ -78,16 +98,21 @@ fun CreateContentBottomSheet(
) {
Text(text = title, fontSize = 18.sp, fontWeight = FontWeight.Companion.Bold)

val canPost = attachments.isNotEmpty() && !requireText || postText.isNotBlank()
TextButton(onClick = { onPost(postText, attachments) }, enabled = canPost) {
Text(text = "Submit", fontWeight = FontWeight.Companion.Medium)
if (state == CreateContentState.Posting) {
CircularProgressIndicator()
} else {
val canPost = attachments.isNotEmpty() && !requireText || postText.isNotBlank()
TextButton(onClick = { onPost(postText, attachments) }, enabled = canPost) {
Text(text = "Submit", fontWeight = FontWeight.Companion.Medium)
}
}
}

// Text Input
OutlinedTextField(
value = postText,
onValueChange = { postText = it },
enabled = inputEnabled,
placeholder = { Text("What's on your mind?") },
modifier = Modifier.fillMaxWidth().padding(vertical = 16.dp),
minLines = 3,
Expand All @@ -106,6 +131,7 @@ fun CreateContentBottomSheet(
AttachmentButton(
hasAttachment = hasAttachments,
onAttachmentsSelected = { uris -> attachments = uris },
enabled = inputEnabled,
)

if (hasAttachments) {
Expand All @@ -117,21 +143,26 @@ fun CreateContentBottomSheet(
}

// Display any extra actions passed to the bottom sheet
extraActions()
extraActions(inputEnabled)
}
}
}
}

@Composable
private fun AttachmentButton(hasAttachment: Boolean, onAttachmentsSelected: (List<Uri>) -> Unit) {
private fun AttachmentButton(
hasAttachment: Boolean,
onAttachmentsSelected: (List<Uri>) -> Unit,
enabled: Boolean,
) {
val activityLauncher =
rememberLauncherForActivityResult(PickMultipleVisualMedia(), onAttachmentsSelected)

IconButton(
onClick = {
activityLauncher.launch(PickVisualMediaRequest(mediaType = PickVisualMedia.ImageOnly))
}
},
enabled = enabled,
) {
Icon(
painter = painterResource(android.R.drawable.ic_menu_gallery),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ import io.getstream.feeds.android.sample.R

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun CreatePollButton(onCreatePoll: (PollFormData) -> Unit) {
fun CreatePollButton(onCreatePoll: (PollFormData) -> Unit, enabled: Boolean) {
var showPollBottomSheet by remember { mutableStateOf(false) }

IconButton(onClick = { showPollBottomSheet = true }) {
IconButton(onClick = { showPollBottomSheet = true }, enabled = enabled) {
Icon(
painter = painterResource(R.drawable.poll),
contentDescription = "Create Poll",
Expand Down
Loading