Skip to content

Commit

Permalink
Solved error unable to create call adapter for GenericOverall
Browse files Browse the repository at this point in the history
Signed-off-by: sowjanyakch <[email protected]>
  • Loading branch information
sowjanyakch committed Aug 23, 2024
1 parent 4ef5e5d commit 51a2218
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 67 deletions.
10 changes: 5 additions & 5 deletions app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,30 @@ interface NcApiCoroutines {
*/
@FormUrlEncoded
@PUT
fun renameRoom(
suspend fun renameRoom(
@Header("Authorization") authorization: String?,
@Url url: String,
@Field("roomName") roomName: String?
): GenericOverall

@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<String, String>?
): 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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 -> {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -139,7 +136,7 @@ class ConversationCreationRepositoryImpl(
)
}

return AllowGuestsResult(result.ocs!!.meta!!.statusCode == STATUS_CODE_OK && allow)
return result
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,8 +26,8 @@ class ConversationCreationViewModel @Inject constructor(
) : ViewModel() {
private val _selectedParticipants = MutableStateFlow<List<AutocompleteUser>>(emptyList())
val selectedParticipants: StateFlow<List<AutocompleteUser>> = _selectedParticipants
private val _roomViewState = MutableStateFlow<RoomUiState>(RoomUiState.None)
val roomViewState: StateFlow<RoomUiState> = _roomViewState
private val _roomViewState = MutableStateFlow<RoomUIState>(RoomUIState.None)
val roomViewState: StateFlow<RoomUIState> = _roomViewState

fun updateSelectedParticipants(participants: List<AutocompleteUser>) {
_selectedParticipants.value = participants
Expand All @@ -44,6 +44,9 @@ class ConversationCreationViewModel @Inject constructor(
private val addParticipantsViewState = MutableStateFlow<AddParticipantsUiState>(AddParticipantsUiState.None)
val addParticipantsUiState: StateFlow<AddParticipantsUiState> = addParticipantsViewState

private val _allowGuestsResult = MutableStateFlow<AllowGuestsUiState>(AllowGuestsUiState.None)
val allowGuestsResult: StateFlow<AllowGuestsUiState> = _allowGuestsResult

fun updateRoomName(roomName: String) {
_roomName.value = roomName
}
Expand All @@ -61,6 +64,7 @@ class ConversationCreationViewModel @Inject constructor(
}
}
}

fun setConversationDescription(roomToken: String) {
viewModelScope.launch {
try {
Expand All @@ -70,18 +74,81 @@ 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<Conversation>? = participantsOverall.ocs?.data
addParticipantsViewState.value = AddParticipantsUiState.Success(participants)
} catch (exception: Exception) {
addParticipantsViewState.value = AddParticipantsUiState.Error(exception.message ?: "")
}
}
}

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<AutocompleteUser>,
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)
}
Expand All @@ -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 ?: "")
}
}
}
}
sealed class AllowGuestsUiState {
data object None : AllowGuestsUiState()
data class Success(val result: Boolean) : AllowGuestsUiState()
data class Error(val message: String) : AllowGuestsUiState()
}

fun allowGuests(token: String, allow: Boolean): ConversationCreationRepositoryImpl.AllowGuestsResult {
return repository.allowGuests(token, allow)
}
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<Conversation>?) : AddParticipantsUiState()
data class Error(val message: String) : AddParticipantsUiState()
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ object Migrations {
)

db.execSQL(
"CREATE INDEX IF NOT EXISTS `index_ChatMessages_internalConversationId` ON `ChatMessages` (`internalConversationId`)"
"CREATE INDEX IF NOT EXISTS `index_ChatMessages_internalConversationId` ON " +
"`ChatMessages` (`internalConversationId`)"
)

db.execSQL(
Expand All @@ -231,7 +232,8 @@ object Migrations {
)

db.execSQL(
"CREATE INDEX IF NOT EXISTS `index_ChatBlocks_internalConversationId` ON `ChatBlocks` (`internalConversationId`)"
"CREATE INDEX IF NOT EXISTS `index_ChatBlocks_internalConversationId` ON " +
"`ChatBlocks` (`internalConversationId`)"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

0 comments on commit 51a2218

Please sign in to comment.