Skip to content

Commit

Permalink
Refactor createRoom()
Browse files Browse the repository at this point in the history
Signed-off-by: sowjanyakch <[email protected]>
  • Loading branch information
sowjanyakch committed Aug 27, 2024
1 parent 75412ec commit 529a850
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 52 deletions.
8 changes: 8 additions & 0 deletions app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}
Expand All @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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 {
Expand All @@ -138,9 +135,4 @@ class ConversationCreationRepositoryImpl(

return result
}

companion object {
private const val ROOM_TYPE_PUBLIC = "3"
private const val ROOM_TYPE_GROUP = "2"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -101,7 +100,7 @@ class ConversationCreationViewModel @Inject constructor(
}

fun createRoomAndAddParticipants(
roomType: ConversationEnums.ConversationType,
roomType: String,
conversationName: String,
participants: Set<AutocompleteUser>,
onRoomCreated: (String) -> Unit
Expand Down Expand Up @@ -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 ?: "")
}
Expand All @@ -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(
Expand Down

0 comments on commit 529a850

Please sign in to comment.