From dba56ddac00e73bd9dc6efbcda0fa44f536868d6 Mon Sep 17 00:00:00 2001 From: sowjanyakch Date: Fri, 23 Aug 2024 23:19:50 +0200 Subject: [PATCH] Solved error unable to create call adapter for GenericOverall Signed-off-by: sowjanyakch --- .../com/nextcloud/talk/api/NcApiCoroutines.kt | 10 +- .../ConversationCreationActivity.kt | 54 +++------- .../ConversationCreationRepository.kt | 2 +- .../ConversationCreationRepositoryImpl.kt | 13 +-- .../ConversationCreationViewModel.kt | 102 ++++++++++++++++-- .../repository/FakeRepositorySuccess.kt | 4 - 6 files changed, 119 insertions(+), 66 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 b05714c0a..d01734d64 100644 --- a/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt +++ b/app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt @@ -54,7 +54,7 @@ interface NcApiCoroutines { */ @FormUrlEncoded @PUT - fun renameRoom( + suspend fun renameRoom( @Header("Authorization") authorization: String?, @Url url: String, @Field("roomName") roomName: String? @@ -62,22 +62,22 @@ interface NcApiCoroutines { @FormUrlEncoded @PUT - fun setConversationDescription( + suspend fun setConversationDescription( @Header("Authorization") authorization: String?, @Url url: String, @Field("description") description: String? ): GenericOverall @POST - fun addParticipant( + suspend fun addParticipant( @Header("Authorization") authorization: String?, @Url url: String?, @QueryMap options: Map? ): AddParticipantOverall @POST - fun makeRoomPublic(@Header("Authorization") authorization: String?, @Url url: String?): GenericOverall + suspend fun makeRoomPublic(@Header("Authorization") authorization: String?, @Url url: String): GenericOverall @DELETE - fun makeRoomPrivate(@Header("Authorization") authorization: String?, @Url url: String?): GenericOverall + suspend fun makeRoomPrivate(@Header("Authorization") authorization: String?, @Url url: 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 490fb850c..15607bbb0 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationActivity.kt @@ -24,7 +24,6 @@ import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxHeight -import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.size @@ -64,7 +63,6 @@ import com.nextcloud.talk.activities.BaseActivity import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.chat.ChatActivity import com.nextcloud.talk.contacts.ContactsActivityCompose -import com.nextcloud.talk.contacts.RoomUiState import com.nextcloud.talk.contacts.loadImage import com.nextcloud.talk.models.json.autocomplete.AutocompleteUser import com.nextcloud.talk.models.json.conversations.ConversationEnums @@ -415,55 +413,37 @@ fun ConversationOptions(icon: Int? = null, text: Int, switch: @Composable (() -> @Composable fun CreateConversation(conversationCreationViewModel: ConversationCreationViewModel, context: Context) { - val roomUiState by conversationCreationViewModel.roomViewState.collectAsState() - val participants = conversationCreationViewModel.selectedParticipants.collectAsState().value.toSet() + val selectedParticipants by conversationCreationViewModel.selectedParticipants.collectAsState() + val isGuestsAllowed = conversationCreationViewModel.isGuestsAllowed.value + Box( modifier = Modifier .fillMaxWidth() - .padding(all = 16.dp) - .clickable { - }, + .padding(all = 16.dp), contentAlignment = Alignment.Center ) { Button( onClick = { - val roomType = if (conversationCreationViewModel.isGuestsAllowed.value) { + val roomType = if (isGuestsAllowed) { ConversationEnums.ConversationType.ROOM_PUBLIC_CALL } else { ConversationEnums.ConversationType.ROOM_GROUP_CALL } - conversationCreationViewModel.createRoom( - roomType, - conversationCreationViewModel.roomName.value - ) + conversationCreationViewModel.createRoomAndAddParticipants( + roomType = roomType, + conversationName = conversationCreationViewModel.roomName.value, + participants = selectedParticipants.toSet() + ) { + roomToken -> + val bundle = Bundle() + bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken) + val chatIntent = Intent(context, ChatActivity::class.java) + chatIntent.putExtras(bundle) + context.startActivity(chatIntent) + } } ) { Text(text = stringResource(id = R.string.create_conversation)) } } - when (roomUiState) { - is RoomUiState.Success -> { - val conversation = (roomUiState as RoomUiState.Success).conversation - val token = conversation?.token - if (token != null) { - conversationCreationViewModel.allowGuests(token, conversationCreationViewModel.isGuestsAllowed.value) - } - for (participant in participants) { - participant.id?.let { conversationCreationViewModel.addParticipants(token, it, participant.source!!) } - } - val bundle = Bundle() - bundle.putString(BundleKeys.KEY_ROOM_TOKEN, token) - val chatIntent = Intent(context, ChatActivity::class.java) - chatIntent.putExtras(bundle) - chatIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) - context.startActivity(chatIntent) - } - is RoomUiState.Error -> { - val errorMessage = (roomUiState as RoomUiState.Error).message - Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { - Text(text = "Error: $errorMessage", color = Color.Red) - } - } - is RoomUiState.None -> {} - } } 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 842f98821..776cabb13 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepository.kt @@ -14,7 +14,7 @@ import com.nextcloud.talk.models.json.participants.AddParticipantOverall interface ConversationCreationRepository { - fun allowGuests(token: String, allow: Boolean): ConversationCreationRepositoryImpl.AllowGuestsResult + suspend fun allowGuests(token: String, allow: Boolean): GenericOverall 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 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 54d9ce09d..4227b0e39 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepositoryImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationRepositoryImpl.kt @@ -14,7 +14,6 @@ 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 -import com.nextcloud.talk.repositories.conversations.ConversationsRepositoryImpl.Companion.STATUS_CODE_OK import com.nextcloud.talk.users.UserManager import com.nextcloud.talk.utils.ApiUtils import com.nextcloud.talk.utils.ApiUtils.getRetrofitBucketForAddParticipant @@ -28,9 +27,6 @@ class ConversationCreationRepositoryImpl( val currentUser: User = _currentUser val credentials = ApiUtils.getCredentials(_currentUser.username, _currentUser.token) val apiVersion = ApiUtils.getConversationApiVersion(_currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1)) - data class AllowGuestsResult( - val allow: Boolean - ) override suspend fun renameConversation(roomToken: String, roomNameNew: String?): GenericOverall { return ncApiCoroutines.renameRoom( @@ -77,7 +73,8 @@ class ConversationCreationRepositoryImpl( userId ) } - return ncApiCoroutines.addParticipant(credentials, retrofitBucket.url, retrofitBucket.queryMap) + val participants = ncApiCoroutines.addParticipant(credentials, retrofitBucket.url, retrofitBucket.queryMap) + return participants } override fun getImageUri(avatarId: String, requestBigSize: Boolean): String { @@ -120,14 +117,14 @@ class ConversationCreationRepositoryImpl( return response } - override fun allowGuests(token: String, allow: Boolean): AllowGuestsResult { + override suspend fun allowGuests(token: String, allow: Boolean): GenericOverall { val url = ApiUtils.getUrlForRoomPublic( apiVersion, _currentUser.baseUrl!!, token ) - val result = if (allow) { + val result: GenericOverall = if (allow) { ncApiCoroutines.makeRoomPublic( credentials, url @@ -139,7 +136,7 @@ class ConversationCreationRepositoryImpl( ) } - return AllowGuestsResult(result.ocs!!.meta!!.statusCode == STATUS_CODE_OK && allow) + return result } companion object { 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 08f754adc..a60005bcb 100644 --- a/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/conversationcreation/ConversationCreationViewModel.kt @@ -11,11 +11,11 @@ import android.util.Log import androidx.compose.runtime.mutableStateOf import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.nextcloud.talk.contacts.AddParticipantsUiState -import com.nextcloud.talk.contacts.RoomUiState 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 import kotlinx.coroutines.flow.StateFlow import kotlinx.coroutines.launch @@ -26,8 +26,8 @@ class ConversationCreationViewModel @Inject constructor( ) : ViewModel() { private val _selectedParticipants = MutableStateFlow>(emptyList()) val selectedParticipants: StateFlow> = _selectedParticipants - private val _roomViewState = MutableStateFlow(RoomUiState.None) - val roomViewState: StateFlow = _roomViewState + private val _roomViewState = MutableStateFlow(RoomUIState.None) + val roomViewState: StateFlow = _roomViewState fun updateSelectedParticipants(participants: List) { _selectedParticipants.value = participants @@ -44,6 +44,9 @@ class ConversationCreationViewModel @Inject constructor( private val addParticipantsViewState = MutableStateFlow(AddParticipantsUiState.None) val addParticipantsUiState: StateFlow = addParticipantsViewState + private val _allowGuestsResult = MutableStateFlow(AllowGuestsUiState.None) + val allowGuestsResult: StateFlow = _allowGuestsResult + fun updateRoomName(roomName: String) { _roomName.value = roomName } @@ -61,6 +64,7 @@ class ConversationCreationViewModel @Inject constructor( } } } + fun setConversationDescription(roomToken: String) { viewModelScope.launch { try { @@ -70,11 +74,12 @@ class ConversationCreationViewModel @Inject constructor( } } } + fun addParticipants(conversationToken: String?, userId: String, sourceType: String) { viewModelScope.launch { try { val participantsOverall = repository.addParticipants(conversationToken, userId, sourceType) - val participants = participantsOverall.ocs?.data + val participants: List? = participantsOverall.ocs?.data addParticipantsViewState.value = AddParticipantsUiState.Success(participants) } catch (exception: Exception) { addParticipantsViewState.value = AddParticipantsUiState.Error(exception.message ?: "") @@ -82,6 +87,68 @@ class ConversationCreationViewModel @Inject constructor( } } + fun allowGuests(token: String, allow: Boolean) { + viewModelScope.launch { + try { + val response = repository.allowGuests(token, allow) + val statusCode: Int = response.ocs?.meta?.statusCode!! + val result = (statusCode == STATUS_CODE_OK) + _allowGuestsResult.value = AllowGuestsUiState.Success(result) + } catch (exception: Exception) { + _allowGuestsResult.value = AllowGuestsUiState.Error(exception.message ?: "") + } + } + } + + fun createRoomAndAddParticipants( + roomType: ConversationEnums.ConversationType, + conversationName: String, + participants: Set, + onRoomCreated: (String) -> Unit + ) { + viewModelScope.launch { + _roomViewState.value = RoomUIState.None + try { + val roomResult = repository.createRoom(roomType, conversationName) + val conversation = roomResult.ocs?.data + + if (conversation != null) { + val token = conversation.token + if (token != null) { + try { + val allowGuestsResult = repository.allowGuests(token, isGuestsAllowed.value) + val statusCode: GenericMeta? = allowGuestsResult.ocs?.meta + val result = (statusCode?.statusCode == STATUS_CODE_OK) + if (result) { + _allowGuestsResult.value = AllowGuestsUiState.Success(result) + for (participant in participants) { + if (participant.id != null) { + val participantOverall = repository.addParticipants( + token, + participant.id!!, + participant.source!! + ).ocs?.data + addParticipantsViewState.value = + AddParticipantsUiState.Success(participantOverall) + } + onRoomCreated(token) + } + } + } catch (exception: Exception) { + _allowGuestsResult.value = AllowGuestsUiState.Error(exception.message ?: "") + } + } + _roomViewState.value = RoomUIState.Success(conversation) + } else { + _roomViewState.value = RoomUIState.Error("Conversation is null") + } + } catch (e: Exception) { + _roomViewState.value = RoomUIState.Error(e.message ?: "Unknown error") + Log.e("ConversationCreationViewModel", "Error - ${e.message}") + } + } + } + fun getImageUri(avatarId: String, requestBigSize: Boolean): String { return repository.getImageUri(avatarId, requestBigSize) } @@ -95,14 +162,27 @@ class ConversationCreationViewModel @Inject constructor( ) val conversation: Conversation? = room.ocs?.data - _roomViewState.value = RoomUiState.Success(conversation) + _roomViewState.value = RoomUIState.Success(conversation) } catch (exception: Exception) { - _roomViewState.value = RoomUiState.Error(exception.message ?: "") + _roomViewState.value = RoomUIState.Error(exception.message ?: "") } } } - - fun allowGuests(token: String, allow: Boolean): ConversationCreationRepositoryImpl.AllowGuestsResult { - return repository.allowGuests(token, allow) - } +} +sealed class AllowGuestsUiState { + data object None : AllowGuestsUiState() + data class Success(val result: Boolean) : AllowGuestsUiState() + data class Error(val message: String) : AllowGuestsUiState() +} + +sealed class RoomUIState { + data object None : RoomUIState() + data class Success(val conversation: Conversation?) : RoomUIState() + data class Error(val message: String) : RoomUIState() +} + +sealed class AddParticipantsUiState() { + data object None : AddParticipantsUiState() + data class Success(val participants: List?) : AddParticipantsUiState() + data class Error(val message: String) : AddParticipantsUiState() } diff --git a/app/src/test/java/com/nextcloud/talk/contacts/repository/FakeRepositorySuccess.kt b/app/src/test/java/com/nextcloud/talk/contacts/repository/FakeRepositorySuccess.kt index 9022ba059..f5d87d919 100644 --- a/app/src/test/java/com/nextcloud/talk/contacts/repository/FakeRepositorySuccess.kt +++ b/app/src/test/java/com/nextcloud/talk/contacts/repository/FakeRepositorySuccess.kt @@ -29,8 +29,4 @@ class FakeRepositorySuccess : ContactsRepository { override fun getImageUri(avatarId: String, requestBigSize: Boolean): String { return "https://mydomain.com/index.php/avatar/$avatarId/512" } - - override fun getImageUri(avatarId: String, requestBigSize: Boolean): String { - TODO("Not yet implemented") - } }