upload conversation avatar

Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
sowjanyakch 2024-09-12 09:21:07 +02:00 committed by backportbot[bot]
parent c7a9b00f43
commit b3bd53b558
3 changed files with 13 additions and 47 deletions

View file

@ -105,7 +105,7 @@ interface NcApiCoroutines {
suspend fun uploadConversationAvatar(
@Header("Authorization") authorization: String,
@Url url: String,
@Part("attachment") attachment: MultipartBody.Part
@Part attachment: MultipartBody.Part
): RoomOverall
@DELETE

View file

@ -212,7 +212,7 @@ fun ConversationCreationScreen(
ConversationNameAndDescription(conversationCreationViewModel)
AddParticipants(launcher, context, conversationCreationViewModel)
RoomCreationOptions(conversationCreationViewModel)
CreateConversation(conversationCreationViewModel, context)
CreateConversation(conversationCreationViewModel, context,selectedImageUri)
}
}
)
@ -275,7 +275,7 @@ fun UploadAvatar(
pickImage.selectLocal(imagePickerLauncher)
}) {
Icon(
painter = painterResource(id = R.drawable.ic_folder_multiple_image),
painter = painterResource(id = R.drawable.upload),
contentDescription = null,
modifier = Modifier.size(24.dp)
)
@ -286,7 +286,7 @@ fun UploadAvatar(
}
) {
Icon(
painter = painterResource(id = R.drawable.baseline_tag_faces_24),
painter = painterResource(id = R.drawable.ic_mimetype_folder),
contentDescription = null,
modifier = Modifier.size(24.dp)
)
@ -571,7 +571,7 @@ fun ShowPasswordDialog(onDismiss: () -> Unit, conversationCreationViewModel: Con
}
@Composable
fun CreateConversation(conversationCreationViewModel: ConversationCreationViewModel, context: Context) {
fun CreateConversation(conversationCreationViewModel: ConversationCreationViewModel, context: Context,selectedImageUri: Uri?) {
val selectedParticipants by conversationCreationViewModel.selectedParticipants.collectAsState()
Box(
modifier = Modifier
@ -584,7 +584,8 @@ fun CreateConversation(conversationCreationViewModel: ConversationCreationViewMo
conversationCreationViewModel.createRoomAndAddParticipants(
roomType = CompanionClass.ROOM_TYPE_GROUP,
conversationName = conversationCreationViewModel.roomName.value,
participants = selectedParticipants.toSet()
participants = selectedParticipants.toSet(),
selectedImageUri = selectedImageUri
) { roomToken ->
val bundle = Bundle()
bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken)

View file

@ -7,12 +7,13 @@
package com.nextcloud.talk.conversationcreation
import android.net.Uri
import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.core.net.toFile
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.domain.ConversationModel
import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser
import com.nextcloud.talk.models.json.conversations.Conversation
import com.nextcloud.talk.models.json.generic.GenericMeta
@ -21,7 +22,6 @@ import com.nextcloud.talk.users.UserManager
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject
class ConversationCreationViewModel @Inject constructor(
@ -32,12 +32,6 @@ class ConversationCreationViewModel @Inject constructor(
val selectedParticipants: StateFlow<List<AutocompleteUser>> = _selectedParticipants
private val roomViewState = MutableStateFlow<RoomUIState>(RoomUIState.None)
private val _uploadState = MutableStateFlow<UploadAvatarState>(UploadAvatarState.Loading)
val uploadState: StateFlow<UploadAvatarState> = _uploadState
private val _deleteState = MutableStateFlow<DeleteAvatarState>(DeleteAvatarState.Loading)
val deleteState: StateFlow<DeleteAvatarState> = _deleteState
private val _currentUser = userManager.currentUser.blockingGet()
val currentUser: User = _currentUser
@ -72,6 +66,7 @@ class ConversationCreationViewModel @Inject constructor(
roomType: String,
conversationName: String,
participants: Set<AutocompleteUser>,
selectedImageUri: Uri?,
onRoomCreated: (String) -> Unit
) {
val scope = when {
@ -114,6 +109,9 @@ class ConversationCreationViewModel @Inject constructor(
repository.setPassword(token, _password.value)
}
repository.openConversation(token, scope)
if(selectedImageUri!= null){
repository.uploadConversationAvatar(selectedImageUri.toFile(), token)
}
onRoomCreated(token)
} catch (exception: Exception) {
allowGuestsResult.value = AllowGuestsUiState.Error(exception.message ?: "")
@ -130,28 +128,6 @@ class ConversationCreationViewModel @Inject constructor(
}
}
fun uploadConversationAvatar(file: File, roomToken: String) {
viewModelScope.launch {
try {
val response = repository.uploadConversationAvatar(file, roomToken)
_uploadState.value = UploadAvatarState.Success(response)
} catch (e: Exception) {
_uploadState.value = UploadAvatarState.Error(e)
}
}
}
fun deleteConversationAvatar(roomToken: String) {
viewModelScope.launch {
try {
val result = repository.deleteConversationAvatar(roomToken)
_deleteState.value = DeleteAvatarState.Success(result)
} catch (e: Exception) {
_deleteState.value = DeleteAvatarState.Error(e)
}
}
}
fun getImageUri(avatarId: String, requestBigSize: Boolean): String {
return repository.getImageUri(avatarId, requestBigSize)
}
@ -191,14 +167,3 @@ sealed class AddParticipantsUiState {
data class Error(val message: String) : AddParticipantsUiState()
}
sealed class UploadAvatarState {
object Loading : UploadAvatarState()
data class Success(val roomOverall: ConversationModel) : UploadAvatarState()
data class Error(val exception: Exception) : UploadAvatarState()
}
sealed class DeleteAvatarState {
object Loading : DeleteAvatarState()
data class Success(val roomOverall: ConversationModel) : DeleteAvatarState()
data class Error(val exception: Exception) : DeleteAvatarState()
}