From cb6fdf6fe174f6eb871d7be4d83a0a350cbca75a Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Tue, 27 Aug 2024 21:30:56 +0200 Subject: [PATCH] Refactor createRoom() Signed-off-by: sowjanyakch --- .../com/nextcloud/talk/api/NcApiCoroutines.kt | 8 +++ .../ConversationCreationActivity.kt | 24 ++++---- .../ConversationCreationRepository.kt | 4 +- .../ConversationCreationRepositoryImpl.kt | 60 ++++++++----------- .../ConversationCreationViewModel.kt | 7 +-- 5 files changed, 51 insertions(+), 52 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt index d01734d64..874df93b2 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt +++ b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt @@ -80,4 +80,12 @@ interface NcApiCoroutines { @DELETE suspend fun makeRoomPrivate(@Header("Authorization") authorization: String?, @Url url: String): GenericOverall + + @FormUrlEncoded + @PUT + suspend fun setPassword( + @Header("Authorization") authorization: String?, + @Url url: String?, + @Field("password") password: String? + ): GenericOverall } diff --git a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationActivity.kt b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationActivity.kt index 15607bbb0..4afb058b7 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationActivity.kt @@ -65,7 +65,6 @@ import com.nextcloud.talk.chat.ChatActivity import com.nextcloud.talk.contacts.ContactsActivityCompose import com.nextcloud.talk.contacts.loadImage import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser -import com.nextcloud.talk.models.json.conversations.ConversationEnums import com.nextcloud.talk.utils.bundle.BundleKeys import javax.inject.Inject @@ -407,15 +406,15 @@ fun ConversationOptions(icon: Int? = null, text: Int, switch: @Composable (() -> text = stringResource(id = text), modifier = Modifier.weight(1f) ) - switch?.invoke() + if (switch != null) { + switch() + } } } @Composable fun CreateConversation(conversationCreationViewModel: ConversationCreationViewModel, context: Context) { val selectedParticipants by conversationCreationViewModel.selectedParticipants.collectAsState() - val isGuestsAllowed = conversationCreationViewModel.isGuestsAllowed.value - Box( modifier = Modifier .fillMaxWidth() @@ -424,21 +423,16 @@ fun CreateConversation(conversationCreationViewModel: ConversationCreationViewMo ) { Button( onClick = { - val roomType = if (isGuestsAllowed) { - ConversationEnums.ConversationType.ROOM_PUBLIC_CALL - } else { - ConversationEnums.ConversationType.ROOM_GROUP_CALL - } conversationCreationViewModel.createRoomAndAddParticipants( - roomType = roomType, + roomType = CompanionClass.ROOM_TYPE_GROUP, conversationName = conversationCreationViewModel.roomName.value, participants = selectedParticipants.toSet() - ) { - roomToken -> + ) { roomToken -> val bundle = Bundle() bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken) val chatIntent = Intent(context, ChatActivity::class.java) chatIntent.putExtras(bundle) + chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) context.startActivity(chatIntent) } } @@ -447,3 +441,9 @@ fun CreateConversation(conversationCreationViewModel: ConversationCreationViewMo } } } +class CompanionClass { + companion object { + internal val TAG = ConversationCreationActivity::class.simpleName + internal const val ROOM_TYPE_GROUP = "2" + } +} diff --git a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepository.kt b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepository.kt index 776cabb13..cd3b295c9 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepository.kt @@ -7,7 +7,6 @@ package com.nextcloud.talk.conversationcreation -import com.nextcloud.talk.models.json.conversations.ConversationEnums import com.nextcloud.talk.models.json.conversations.RoomOverall import com.nextcloud.talk.models.json.generic.GenericOverall import com.nextcloud.talk.models.json.participants.AddParticipantOverall @@ -18,6 +17,7 @@ interface ConversationCreationRepository { suspend fun renameConversation(roomToken: String, roomNameNew: String?): GenericOverall suspend fun setConversationDescription(roomToken: String, description: String?): GenericOverall suspend fun addParticipants(conversationToken: String?, userId: String, sourceType: String): AddParticipantOverall - suspend fun createRoom(roomType: ConversationEnums.ConversationType?, conversationName: String?): RoomOverall + suspend fun createRoom(roomType: String, conversationName: String?): RoomOverall fun getImageUri(avatarId: String, requestBigSize: Boolean): String + suspend fun setPassword(roomToken: String, password: String): GenericOverall } diff --git a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepositoryImpl.kt b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepositoryImpl.kt index 4227b0e39..df2fd538c 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepositoryImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepositoryImpl.kt @@ -10,7 +10,6 @@ package com.nextcloud.talk.conversationcreation import com.nextcloud.talk.api.NcApiCoroutines import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.models.RetrofitBucket -import com.nextcloud.talk.models.json.conversations.ConversationEnums import com.nextcloud.talk.models.json.conversations.RoomOverall import com.nextcloud.talk.models.json.generic.GenericOverall import com.nextcloud.talk.models.json.participants.AddParticipantOverall @@ -77,6 +76,23 @@ class ConversationCreationRepositoryImpl( return participants } + override suspend fun createRoom(roomType: String, conversationName: String?): RoomOverall { + val retrofitBucket: RetrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom( + apiVersion, + _currentUser.baseUrl, + roomType, + null, + null, + conversationName + ) + val response = ncApiCoroutines.createRoom( + credentials, + retrofitBucket.url, + retrofitBucket.queryMap + ) + return response + } + override fun getImageUri(avatarId: String, requestBigSize: Boolean): String { return ApiUtils.getUrlForAvatar( _currentUser.baseUrl, @@ -85,36 +101,17 @@ class ConversationCreationRepositoryImpl( ) } - override suspend fun createRoom( - roomType: ConversationEnums.ConversationType?, - conversationName: String? - ): RoomOverall { - val retrofitBucket: RetrofitBucket = - if (roomType == ConversationEnums.ConversationType.ROOM_PUBLIC_CALL) { - ApiUtils.getRetrofitBucketForCreateRoom( - apiVersion, - currentUser.baseUrl!!, - ROOM_TYPE_PUBLIC, - null, - null, - conversationName - ) - } else { - ApiUtils.getRetrofitBucketForCreateRoom( - apiVersion, - currentUser.baseUrl!!, - ROOM_TYPE_GROUP, - null, - null, - conversationName - ) - } - val response = ncApiCoroutines.createRoom( + override suspend fun setPassword(roomToken: String, password: String): GenericOverall { + val result = ncApiCoroutines.setPassword( credentials, - retrofitBucket.url, - retrofitBucket.queryMap + ApiUtils.getUrlForRoomPassword( + apiVersion, + _currentUser.baseUrl!!, + roomToken + ), + password ) - return response + return result } override suspend fun allowGuests(token: String, allow: Boolean): GenericOverall { @@ -138,9 +135,4 @@ class ConversationCreationRepositoryImpl( return result } - - companion object { - private const val ROOM_TYPE_PUBLIC = "3" - private const val ROOM_TYPE_GROUP = "2" - } } diff --git a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationViewModel.kt b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationViewModel.kt index a60005bcb..7e16af01b 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationViewModel.kt @@ -13,7 +13,6 @@ import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser import com.nextcloud.talk.models.json.conversations.Conversation -import com.nextcloud.talk.models.json.conversations.ConversationEnums import com.nextcloud.talk.models.json.generic.GenericMeta import com.nextcloud.talk.repositories.conversations.ConversationsRepositoryImpl.Companion.STATUS_CODE_OK import kotlinx.coroutines.flow.MutableStateFlow @@ -101,7 +100,7 @@ class ConversationCreationViewModel @Inject constructor( } fun createRoomAndAddParticipants( - roomType: ConversationEnums.ConversationType, + roomType: String, conversationName: String, participants: Set, onRoomCreated: (String) -> Unit @@ -131,9 +130,9 @@ class ConversationCreationViewModel @Inject constructor( addParticipantsViewState.value = AddParticipantsUiState.Success(participantOverall) } - onRoomCreated(token) } } + onRoomCreated(token) } catch (exception: Exception) { _allowGuestsResult.value = AllowGuestsUiState.Error(exception.message ?: "") } @@ -153,7 +152,7 @@ class ConversationCreationViewModel @Inject constructor( return repository.getImageUri(avatarId, requestBigSize) } - fun createRoom(roomType: ConversationEnums.ConversationType?, conversationName: String?) { + fun createRoom(roomType: String, conversationName: String?) { viewModelScope.launch { try { val room = repository.createRoom(