- SelectFromGallery and SelectFromFiles now accept an enabled param separate from isUploading

- All ViewModels track isUploadingFile alongside isUploadingImage, set via hasNonMedia()
  - All screen call sites pass source-specific flags so only the initiating button spins
  - canPost() methods block posting during file uploads too
  - ChatNewMessageViewModel delegates to ChatFileUploadState (fixes pre-existing dead state)
  - cancel() methods reset both flags in all ViewModels
This commit is contained in:
davotoula
2026-03-14 12:58:18 +01:00
parent 42574f302c
commit c290a1c779
17 changed files with 104 additions and 22 deletions
@@ -372,6 +372,7 @@ private fun BottomRowActions(postViewModel: EditPostViewModel) {
) {
SelectFromGallery(
isUploading = postViewModel.isUploadingImage,
enabled = !postViewModel.isUploadingFile,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -379,7 +380,8 @@ private fun BottomRowActions(postViewModel: EditPostViewModel) {
}
SelectFromFiles(
isUploading = postViewModel.isUploadingImage,
isUploading = postViewModel.isUploadingFile,
enabled = !postViewModel.isUploadingImage,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -80,6 +80,7 @@ open class EditPostViewModel : ViewModel() {
var message by mutableStateOf(TextFieldValue(""))
var urlPreview by mutableStateOf<String?>(null)
var isUploadingImage by mutableStateOf(false)
var isUploadingFile by mutableStateOf(false)
var userSuggestions: UserSuggestionState? = null
var userSuggestionsMainMessage: UserSuggestionAnchor? = null
@@ -180,7 +181,11 @@ open class EditPostViewModel : ViewModel() {
val myAccount = account
val myMultiOrchestrator = multiOrchestrator ?: return@launch
isUploadingImage = true
if (myMultiOrchestrator.hasNonMedia()) {
isUploadingFile = true
} else {
isUploadingImage = true
}
val results =
myMultiOrchestrator.upload(
@@ -244,6 +249,7 @@ open class EditPostViewModel : ViewModel() {
}
isUploadingImage = false
isUploadingFile = false
}
}
@@ -256,6 +262,7 @@ open class EditPostViewModel : ViewModel() {
multiOrchestrator = null
urlPreview = null
isUploadingImage = false
isUploadingFile = false
wantsInvoice = false
@@ -296,7 +303,7 @@ open class EditPostViewModel : ViewModel() {
}
}
fun canPost() = message.text.isNotBlank() && !isUploadingImage && !wantsInvoice && multiOrchestrator == null
fun canPost() = message.text.isNotBlank() && !isUploadingImage && !isUploadingFile && !wantsInvoice && multiOrchestrator == null
fun selectImage(uris: ImmutableList<SelectedMedia>) {
multiOrchestrator = MultiOrchestrator(uris)
@@ -48,6 +48,7 @@ import java.util.concurrent.atomic.AtomicBoolean
@Composable
fun SelectFromFiles(
isUploading: Boolean,
enabled: Boolean = true,
tint: Color,
modifier: Modifier,
onFilesChosen: (ImmutableList<SelectedMedia>) -> Unit,
@@ -64,19 +65,20 @@ fun SelectFromFiles(
)
}
FileSelectButton(isUploading, tint, modifier) { showFileSelect = true }
FileSelectButton(isUploading, enabled, tint, modifier) { showFileSelect = true }
}
@Composable
private fun FileSelectButton(
isUploading: Boolean,
enabled: Boolean,
tint: Color,
modifier: Modifier,
onClick: () -> Unit,
) {
IconButton(
modifier = modifier,
enabled = !isUploading,
enabled = enabled && !isUploading,
onClick = { onClick() },
) {
if (!isUploading) {
@@ -69,6 +69,7 @@ class SelectedMedia(
@Composable
fun SelectFromGallery(
isUploading: Boolean,
enabled: Boolean = true,
tint: Color,
modifier: Modifier,
onImageChosen: (ImmutableList<SelectedMedia>) -> Unit,
@@ -85,12 +86,13 @@ fun SelectFromGallery(
)
}
GallerySelectButton(isUploading, tint, modifier) { showGallerySelect = true }
GallerySelectButton(isUploading, enabled, tint, modifier) { showGallerySelect = true }
}
@Composable
fun SelectSingleFromGallery(
isUploading: Boolean,
enabled: Boolean = true,
tint: Color,
modifier: Modifier,
onImageChosen: (SelectedMedia) -> Unit,
@@ -107,19 +109,20 @@ fun SelectSingleFromGallery(
)
}
GallerySelectButton(isUploading, tint, modifier) { showGallerySelect = true }
GallerySelectButton(isUploading, enabled, tint, modifier) { showGallerySelect = true }
}
@Composable
private fun GallerySelectButton(
isUploading: Boolean,
enabled: Boolean,
tint: Color,
modifier: Modifier,
onClick: () -> Unit,
) {
IconButton(
modifier = modifier,
enabled = !isUploading,
enabled = enabled && !isUploading,
onClick = { onClick() },
) {
if (!isUploading) {
@@ -150,6 +150,7 @@ open class CommentPostViewModel :
val urlPreviews = PreviewState()
var isUploadingImage by mutableStateOf(false)
var isUploadingFile by mutableStateOf(false)
var userSuggestions: UserSuggestionState? = null
var userSuggestionsMainMessage: UserSuggestionAnchor? = null
@@ -487,7 +488,11 @@ open class CommentPostViewModel :
viewModelScope.launch(Dispatchers.IO) {
val myMultiOrchestrator = multiOrchestrator ?: return@launch
isUploadingImage = true
if (myMultiOrchestrator.hasNonMedia()) {
isUploadingFile = true
} else {
isUploadingImage = true
}
val results =
myMultiOrchestrator.upload(
@@ -552,6 +557,7 @@ open class CommentPostViewModel :
}
isUploadingImage = false
isUploadingFile = false
}
}
@@ -565,6 +571,7 @@ open class CommentPostViewModel :
multiOrchestrator = null
isUploadingImage = false
isUploadingFile = false
notifying = null
@@ -677,6 +684,7 @@ open class CommentPostViewModel :
fun canPost(): Boolean =
message.text.isNotBlank() &&
!isUploadingImage &&
!isUploadingFile &&
!wantsInvoice &&
(!wantsZapraiser || zapRaiserAmount.value != null) &&
multiOrchestrator == null
@@ -391,6 +391,7 @@ private fun BottomRowActions(postViewModel: CommentPostViewModel) {
) {
SelectFromGallery(
isUploading = postViewModel.isUploadingImage,
enabled = !postViewModel.isUploadingFile,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -398,7 +399,8 @@ private fun BottomRowActions(postViewModel: CommentPostViewModel) {
}
SelectFromFiles(
isUploading = postViewModel.isUploadingImage,
isUploading = postViewModel.isUploadingFile,
enabled = !postViewModel.isUploadingImage,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -141,7 +141,8 @@ class ChatNewMessageViewModel :
val urlPreviews = PreviewState()
var isUploadingImage by mutableStateOf(false)
val isUploadingImage: Boolean get() = uploadState?.isUploadingImage ?: false
val isUploadingFile: Boolean get() = uploadState?.isUploadingFile ?: false
var userSuggestions: UserSuggestionState? = null
var userSuggestionsMainMessage: UserSuggestionAnchor? = null
@@ -692,6 +693,7 @@ class ChatNewMessageViewModel :
fun canPost(): Boolean =
message.text.isNotBlank() &&
uploadState?.isUploadingImage != true &&
uploadState?.isUploadingFile != true &&
!wantsInvoice &&
(!wantsZapraiser || zapRaiserAmount.value != null) &&
(toUsers.text.isNotBlank()) &&
@@ -386,6 +386,7 @@ private fun BottomRowActions(
if (postViewModel.room != null) {
SelectFromGallery(
isUploading = postViewModel.isUploadingImage,
enabled = !postViewModel.isUploadingFile,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -393,7 +394,8 @@ private fun BottomRowActions(
}
SelectFromFiles(
isUploading = postViewModel.isUploadingImage,
isUploading = postViewModel.isUploadingFile,
enabled = !postViewModel.isUploadingImage,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -43,7 +43,11 @@ class ChatFileUploader(
onceUploaded: suspend (List<SuccessfulUploads>) -> Unit,
) {
val orchestrator = viewState.multiOrchestrator ?: return
viewState.isUploadingImage = true
if (orchestrator.hasNonMedia()) {
viewState.isUploadingFile = true
} else {
viewState.isUploadingImage = true
}
val cipher = AESGCM()
@@ -77,6 +81,7 @@ class ChatFileUploader(
}
viewState.isUploadingImage = false
viewState.isUploadingFile = false
}
// ------
@@ -90,7 +95,11 @@ class ChatFileUploader(
onceUploaded: suspend (List<SuccessfulUploads>) -> Unit,
) {
val orchestrator = viewState.multiOrchestrator ?: return
viewState.isUploadingImage = true
if (orchestrator.hasNonMedia()) {
viewState.isUploadingFile = true
} else {
viewState.isUploadingImage = true
}
val results =
orchestrator.upload(
@@ -121,5 +130,6 @@ class ChatFileUploader(
}
viewState.isUploadingImage = false
viewState.isUploadingFile = false
}
}
@@ -135,6 +135,7 @@ open class ChannelNewMessageViewModel :
var message by mutableStateOf(TextFieldValue(""))
var urlPreview by mutableStateOf<String?>(null)
var isUploadingImage by mutableStateOf(false)
var isUploadingFile by mutableStateOf(false)
var userSuggestions: UserSuggestionState? = null
var userSuggestionsMainMessage: UserSuggestionAnchor? = null
@@ -337,7 +338,11 @@ open class ChannelNewMessageViewModel :
val myMultiOrchestrator = uploadState.multiOrchestrator ?: return@launch
isUploadingImage = true
if (myMultiOrchestrator.hasNonMedia()) {
isUploadingFile = true
} else {
isUploadingImage = true
}
val results =
myMultiOrchestrator.upload(
@@ -379,6 +384,7 @@ open class ChannelNewMessageViewModel :
}
isUploadingImage = false
isUploadingFile = false
}
}
@@ -546,6 +552,9 @@ open class ChannelNewMessageViewModel :
userSuggestions?.reset()
userSuggestionsMainMessage = null
isUploadingImage = false
isUploadingFile = false
iMetaAttachments.reset()
emojiSuggestions?.reset()
@@ -633,6 +642,7 @@ open class ChannelNewMessageViewModel :
fun canPost(): Boolean =
message.text.isNotBlank() &&
uploadState?.isUploadingImage != true &&
uploadState?.isUploadingFile != true &&
!wantsInvoice &&
(!wantsZapraiser || zapRaiserAmount != null) &&
uploadState?.multiOrchestrator == null
@@ -37,6 +37,7 @@ class ChatFileUploadState(
val defaultServer: ServerName,
) {
var isUploadingImage by mutableStateOf(false)
var isUploadingFile by mutableStateOf(false)
var selectedServer by mutableStateOf(defaultServer)
var caption by mutableStateOf("")
@@ -65,6 +66,7 @@ class ChatFileUploadState(
fun reset() {
multiOrchestrator = null
isUploadingImage = false
isUploadingFile = false
caption = ""
selectedServer = defaultServer
}
@@ -359,6 +359,7 @@ private fun BottomRowActions(postViewModel: NewProductViewModel) {
) {
SelectFromGallery(
isUploading = postViewModel.isUploadingImage,
enabled = !postViewModel.isUploadingFile,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -366,7 +367,8 @@ private fun BottomRowActions(postViewModel: NewProductViewModel) {
}
SelectFromFiles(
isUploading = postViewModel.isUploadingImage,
isUploading = postViewModel.isUploadingFile,
enabled = !postViewModel.isUploadingImage,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -134,6 +134,7 @@ open class NewProductViewModel :
val urlPreviews = PreviewState()
var isUploadingImage by mutableStateOf(false)
var isUploadingFile by mutableStateOf(false)
var userSuggestions: UserSuggestionState? = null
var userSuggestionsMainMessage: UserSuggestionAnchor? = null
@@ -399,7 +400,11 @@ open class NewProductViewModel :
val myAccount = account ?: return@launch
val myMultiOrchestrator = multiOrchestrator ?: return@launch
isUploadingImage = true
if (myMultiOrchestrator.hasNonMedia()) {
isUploadingFile = true
} else {
isUploadingImage = true
}
val results =
myMultiOrchestrator.upload(
@@ -445,6 +450,7 @@ open class NewProductViewModel :
}
isUploadingImage = false
isUploadingFile = false
}
}
@@ -455,6 +461,7 @@ open class NewProductViewModel :
multiOrchestrator = null
isUploadingImage = false
isUploadingFile = false
wantsInvoice = false
wantsZapraiser = false
@@ -576,6 +583,7 @@ open class NewProductViewModel :
fun canPost(): Boolean =
message.text.isNotBlank() &&
!isUploadingImage &&
!isUploadingFile &&
!wantsInvoice &&
(!wantsZapraiser || zapRaiserAmount.value != null) &&
title.text.isNotBlank() &&
@@ -494,6 +494,7 @@ private fun BottomRowActions(postViewModel: ShortNotePostViewModel) {
) {
SelectFromGallery(
isUploading = postViewModel.isUploadingImage,
enabled = !postViewModel.isUploadingFile,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -501,7 +502,8 @@ private fun BottomRowActions(postViewModel: ShortNotePostViewModel) {
}
SelectFromFiles(
isUploading = postViewModel.isUploadingImage,
isUploading = postViewModel.isUploadingFile,
enabled = !postViewModel.isUploadingImage,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -179,6 +179,7 @@ open class ShortNotePostViewModel :
val urlPreviews = PreviewState()
var isUploadingImage by mutableStateOf(false)
var isUploadingFile by mutableStateOf(false)
var userSuggestions: UserSuggestionState? = null
var userSuggestionsMainMessage: UserSuggestionAnchor? = null
@@ -829,7 +830,11 @@ open class ShortNotePostViewModel :
viewModelScope.launch(Dispatchers.IO) {
val myMultiOrchestrator = multiOrchestrator ?: return@launch
isUploadingImage = true
if (myMultiOrchestrator.hasNonMedia()) {
isUploadingFile = true
} else {
isUploadingImage = true
}
val results =
myMultiOrchestrator.upload(
@@ -886,6 +891,7 @@ open class ShortNotePostViewModel :
}
isUploadingImage = false
isUploadingFile = false
}
}
@@ -898,6 +904,7 @@ open class ShortNotePostViewModel :
multiOrchestrator = null
isUploadingImage = false
isUploadingFile = false
voiceAnonymization.clear()
deleteVoiceLocalFile()
voiceRecording = null
@@ -1032,12 +1039,13 @@ open class ShortNotePostViewModel :
fun canPost(): Boolean {
// Voice messages can be posted without text (with either uploaded or pending recording)
if (voiceMetadata != null || voiceRecording != null) {
return !isUploadingVoice && !isUploadingImage && processingPreset == null
return !isUploadingVoice && !isUploadingImage && !isUploadingFile && processingPreset == null
}
// Regular text/media posts require text
return message.text.isNotBlank() &&
!isUploadingImage &&
!isUploadingFile &&
!isUploadingVoice &&
!wantsInvoice &&
(!wantsZapRaiser || zapRaiserAmount.value != null) &&
@@ -321,6 +321,7 @@ private fun BottomRowActions(
) {
SelectFromGallery(
isUploading = postViewModel.isUploadingImage,
enabled = !postViewModel.isUploadingFile,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -328,7 +329,8 @@ private fun BottomRowActions(
}
SelectFromFiles(
isUploading = postViewModel.isUploadingImage,
isUploading = postViewModel.isUploadingFile,
enabled = !postViewModel.isUploadingImage,
tint = MaterialTheme.colorScheme.onBackground,
modifier = Modifier,
) {
@@ -150,6 +150,7 @@ class NewPublicMessageViewModel :
val urlPreviews = PreviewState()
var isUploadingImage by mutableStateOf(false)
var isUploadingFile by mutableStateOf(false)
var userSuggestions: UserSuggestionState? = null
var userSuggestionsMainMessage: UserSuggestionAnchor? = null
@@ -439,7 +440,11 @@ class NewPublicMessageViewModel :
viewModelScope.launch(Dispatchers.IO) {
val myMultiOrchestrator = multiOrchestrator ?: return@launch
isUploadingImage = true
if (myMultiOrchestrator.hasNonMedia()) {
isUploadingFile = true
} else {
isUploadingImage = true
}
val results =
myMultiOrchestrator.upload(
@@ -495,6 +500,7 @@ class NewPublicMessageViewModel :
}
isUploadingImage = false
isUploadingFile = false
}
}
@@ -523,6 +529,9 @@ class NewPublicMessageViewModel :
userSuggestions?.reset()
userSuggestionsMainMessage = null
isUploadingImage = false
isUploadingFile = false
iMetaAttachments.reset()
emojiSuggestions?.reset()
@@ -629,6 +638,7 @@ class NewPublicMessageViewModel :
fun canPost(): Boolean =
message.text.isNotBlank() &&
!isUploadingImage &&
!isUploadingFile &&
!wantsInvoice &&
(!wantsZapraiser || zapRaiserAmount.value != null) &&
(toUsers.text.isNotBlank()) &&