Skip to content

Commit

Permalink
convert rxjava to coroutines - set conversation read only
Browse files Browse the repository at this point in the history
Signed-off-by: sowjanyakch <[email protected]>
  • Loading branch information
sowjanyakch committed Nov 19, 2024
1 parent ab313ad commit 452a4c7
Show file tree
Hide file tree
Showing 12 changed files with 110 additions and 124 deletions.
9 changes: 7 additions & 2 deletions app/src/main/java/com/nextcloud/talk/api/NcApiCoroutines.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,13 @@ interface NcApiCoroutines {
): GenericOverall

@DELETE
suspend fun clearChatHistory(
suspend fun clearChatHistory(@Header("Authorization") authorization: String, @Url url: String): GenericOverall

@FormUrlEncoded
@PUT
suspend fun setConversationReadOnly(
@Header("Authorization") authorization: String,
@Url url: String
@Url url: String,
@Field("state") state: Int
): GenericOverall
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,12 @@ class RenameConversationDialogFragment : DialogFragment() {
private fun setupStateObserver() {
viewModel.renameRoomUiState.observe(viewLifecycleOwner) { state ->
when (state) {
is ConversationInfoEditViewModel.RenameRoomUiState.None ->{

is ConversationInfoEditViewModel.RenameRoomUiState.None -> {
}
is ConversationInfoEditViewModel.RenameRoomUiState.Success ->{
is ConversationInfoEditViewModel.RenameRoomUiState.Success -> {
handleSuccess()
}
is ConversationInfoEditViewModel.RenameRoomUiState.Error ->{
is ConversationInfoEditViewModel.RenameRoomUiState.Error -> {
showError()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ class ConversationRepositoryImpl(private val ncApi: NcApi, currentUserProvider:
val currentUser: User = currentUserProvider.currentUser.blockingGet()
val credentials: String = ApiUtils.getCredentials(currentUser.username, currentUser.token)!!



override fun createConversation(
roomName: String,
conversationType: ConversationEnums.ConversationType?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,37 +260,33 @@ class ConversationInfoActivity :

viewModel.getConversationReadOnlyState.observe(this) { state ->
when (state) {
is ConversationInfoViewModel.SetConversationReadOnlySuccessState -> {
is ConversationInfoViewModel.SetConversationReadOnlyViewState.Success -> {
}
is ConversationInfoViewModel.SetConversationReadOnlyErrorState -> {
is ConversationInfoViewModel.SetConversationReadOnlyViewState.Error -> {
Snackbar.make(binding.root, R.string.conversation_read_only_failed, Snackbar.LENGTH_LONG).show()
}
else -> {
is ConversationInfoViewModel.SetConversationReadOnlyViewState.None -> {

}
}
}

viewModel.clearChatHistoryViewState.observe(this){uiState ->
when(uiState){
is ConversationInfoViewModel.ClearChatHistoryViewState.None ->{

viewModel.clearChatHistoryViewState.observe(this) { uiState ->
when (uiState) {
is ConversationInfoViewModel.ClearChatHistoryViewState.None -> {
}
is ConversationInfoViewModel.ClearChatHistoryViewState.Success ->{
is ConversationInfoViewModel.ClearChatHistoryViewState.Success -> {
Snackbar.make(
binding.root,
context.getString(R.string.nc_clear_history_success),
Snackbar.LENGTH_LONG
).show()


}
is ConversationInfoViewModel.ClearChatHistoryViewState.Error ->{
is ConversationInfoViewModel.ClearChatHistoryViewState.Error -> {
Snackbar.make(binding.root, R.string.nc_common_error_sorry, Snackbar.LENGTH_LONG).show()
Log.e(TAG, "failed to clear chat history", uiState.exception)

}
}

}
}

Expand Down Expand Up @@ -747,7 +743,7 @@ class ConversationInfoActivity :

private fun clearHistory() {
val apiVersion = ApiUtils.getChatApiVersion(spreedCapabilities, intArrayOf(1))
viewModel.clearChatHistory(apiVersion,conversationToken)
viewModel.clearChatHistory(apiVersion, conversationToken)
}

private fun deleteConversation() {
Expand Down Expand Up @@ -842,7 +838,7 @@ class ConversationInfoActivity :
binding.lockConversationSwitch.isChecked = !isLocked
databaseStorageModule!!.saveBoolean("lock_switch", !isLocked)
val state = if (isLocked) 0 else 1
makeConversationReadOnly(conversationUser, conversationToken, state)
makeConversationReadOnly(conversationToken, state)
}
} else {
binding.lockConversation.visibility = GONE
Expand Down Expand Up @@ -922,8 +918,8 @@ class ConversationInfoActivity :
}
}

private fun makeConversationReadOnly(conversationUser: User, roomToken: String, state: Int) {
viewModel.setConversationReadOnly(conversationUser, roomToken, state)
private fun makeConversationReadOnly(roomToken: String, state: Int) {
viewModel.setConversationReadOnly( roomToken, state)
}

private fun initRecordingConsentOption() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ import com.nextcloud.talk.chat.data.network.ChatNetworkDataSource
import com.nextcloud.talk.data.user.model.User
import com.nextcloud.talk.models.domain.ConversationModel
import com.nextcloud.talk.models.json.capabilities.SpreedCapability
import com.nextcloud.talk.models.json.generic.GenericMeta
import com.nextcloud.talk.models.json.generic.GenericOverall
import com.nextcloud.talk.models.json.participants.TalkBan
import com.nextcloud.talk.repositories.conversations.ConversationsRepository
import com.nextcloud.talk.repositories.conversations.ConversationsRepositoryImpl.Companion.STATUS_CODE_OK
import com.nextcloud.talk.utils.ApiUtils
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
Expand Down Expand Up @@ -81,13 +79,6 @@ class ConversationInfoViewModel @Inject constructor(
val getUnBanActorState: LiveData<ViewState>
get() = _getUnBanActorState

object SetConversationReadOnlySuccessState : ViewState
object SetConversationReadOnlyErrorState : ViewState

private val _getConversationReadOnlyState: MutableLiveData<ViewState> = MutableLiveData()
val getConversationReadOnlyState: LiveData<ViewState>
get() = _getConversationReadOnlyState

object GetRoomStartState : ViewState
object GetRoomErrorState : ViewState
open class GetRoomSuccessState(val conversationModel: ConversationModel) : ViewState
Expand All @@ -112,10 +103,16 @@ class ConversationInfoViewModel @Inject constructor(
val getCapabilitiesViewState: LiveData<ViewState>
get() = _getCapabilitiesViewState

private val _clearChatHistoryViewState:MutableLiveData<ClearChatHistoryViewState> = MutableLiveData(ClearChatHistoryViewState.None)
val clearChatHistoryViewState:LiveData<ClearChatHistoryViewState>
private val _clearChatHistoryViewState: MutableLiveData<ClearChatHistoryViewState> =
MutableLiveData(ClearChatHistoryViewState.None)
val clearChatHistoryViewState: LiveData<ClearChatHistoryViewState>
get() = _clearChatHistoryViewState

private val _getConversationReadOnlyState: MutableLiveData<SetConversationReadOnlyViewState> =
MutableLiveData(SetConversationReadOnlyViewState.None)
val getConversationReadOnlyState: LiveData<SetConversationReadOnlyViewState>
get() = _getConversationReadOnlyState

fun getRoom(user: User, token: String) {
_viewState.value = GetRoomStartState
chatNetworkDataSource.getRoom(user, token)
Expand Down Expand Up @@ -202,28 +199,15 @@ class ConversationInfoViewModel @Inject constructor(
})
}

fun setConversationReadOnly(user: User, token: String, state: Int) {
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
val url = ApiUtils.getUrlForConversationReadOnly(apiVersion, user.baseUrl!!, token)
conversationsRepository.setConversationReadOnly(user.getCredentials(), url, state)
.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread())
?.subscribe(object : Observer<GenericOverall> {
override fun onSubscribe(p0: Disposable) {
}

override fun onError(error: Throwable) {
_getConversationReadOnlyState.value = SetConversationReadOnlyErrorState
}

override fun onComplete() {
// unused atm
}

override fun onNext(p0: GenericOverall) {
_getConversationReadOnlyState.value = SetConversationReadOnlySuccessState
}
})
fun setConversationReadOnly(roomToken: String, state: Int) {
viewModelScope.launch {
try {
conversationsRepository.setConversationReadOnly(roomToken, state)
_getConversationReadOnlyState.value = SetConversationReadOnlyViewState.Success
} catch (exception: Exception) {
_getConversationReadOnlyState.value = SetConversationReadOnlyViewState.Error(exception)
}
}
}

fun unbanActor(user: User, token: String, banId: Int) {
Expand Down Expand Up @@ -285,16 +269,12 @@ class ConversationInfoViewModel @Inject constructor(
conversationsRepository.unarchiveConversation(user.getCredentials(), url)
}

fun clearChatHistory(apiVersion:Int,roomToken:String){
viewModelScope.launch{
try{
val clearChatResult = conversationsRepository.clearChatHistory(apiVersion,roomToken)
val statusCode: GenericMeta? = clearChatResult.ocs?.meta
val result = statusCode?.statusCode == STATUS_CODE_OK
if (result) {
_clearChatHistoryViewState.value = ClearChatHistoryViewState.Success
}
}catch(exception:Exception){
fun clearChatHistory(apiVersion: Int, roomToken: String) {
viewModelScope.launch {
try {
conversationsRepository.clearChatHistory(apiVersion, roomToken)
_clearChatHistoryViewState.value = ClearChatHistoryViewState.Success
} catch (exception: Exception) {
_clearChatHistoryViewState.value = ClearChatHistoryViewState.Error(exception)
}
}
Expand Down Expand Up @@ -323,10 +303,16 @@ class ConversationInfoViewModel @Inject constructor(
private val TAG = ConversationInfoViewModel::class.simpleName
}

sealed class ClearChatHistoryViewState{
data object None: ClearChatHistoryViewState()
data object Success:ClearChatHistoryViewState()
data class Error(val exception:Exception):ClearChatHistoryViewState()
sealed class ClearChatHistoryViewState {
data object None : ClearChatHistoryViewState()
data object Success : ClearChatHistoryViewState()
data class Error(val exception: Exception) : ClearChatHistoryViewState()
}

sealed class SetConversationReadOnlyViewState {
data object None : SetConversationReadOnlyViewState()
data object Success : SetConversationReadOnlyViewState()
data class Error(val exception: Exception) : SetConversationReadOnlyViewState()
}

sealed class AllowGuestsUIState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,18 @@ class ConversationInfoEditActivity : BaseActivity() {
else -> {}
}
}
conversationInfoEditViewModel.renameRoomUiState.observe(this){uiState ->
when(uiState){
is ConversationInfoEditViewModel.RenameRoomUiState.None ->{

conversationInfoEditViewModel.renameRoomUiState.observe(this) { uiState ->
when (uiState) {
is ConversationInfoEditViewModel.RenameRoomUiState.None -> {
}
is ConversationInfoEditViewModel.RenameRoomUiState.Success ->{
is ConversationInfoEditViewModel.RenameRoomUiState.Success -> {
if (CapabilitiesUtil.isConversationDescriptionEndpointAvailable(spreedCapabilities)) {
saveConversationDescription()
} else {
finish()
}
}
is ConversationInfoEditViewModel.RenameRoomUiState.Error ->{
is ConversationInfoEditViewModel.RenameRoomUiState.Error -> {
Snackbar.make(
binding.root,
context.getString(R.string.default_error_msg),
Expand All @@ -190,27 +189,23 @@ class ConversationInfoEditActivity : BaseActivity() {
}
}

conversationInfoEditViewModel.setConversationDescriptionUiState.observe(this){ uiState ->
when(uiState){
is ConversationInfoEditViewModel.SetConversationDescriptionUiState.None ->{

conversationInfoEditViewModel.setConversationDescriptionUiState.observe(this) { uiState ->
when (uiState) {
is ConversationInfoEditViewModel.SetConversationDescriptionUiState.None -> {
}
is ConversationInfoEditViewModel.SetConversationDescriptionUiState.Success ->{
is ConversationInfoEditViewModel.SetConversationDescriptionUiState.Success -> {
finish()
}
is ConversationInfoEditViewModel.SetConversationDescriptionUiState.Error ->{
is ConversationInfoEditViewModel.SetConversationDescriptionUiState.Error -> {
Snackbar.make(
binding.root,
context.getString(R.string.default_error_msg),
Snackbar.LENGTH_LONG
).show()
Log.e(TAG, "Error while saving conversation description", uiState.exception)

}
}

}

}

private fun setupAvatarOptions() {
Expand Down Expand Up @@ -276,14 +271,14 @@ class ConversationInfoEditActivity : BaseActivity() {

private fun saveConversationNameAndDescription() {
val newRoomName = binding.conversationName.text.toString()
conversationInfoEditViewModel.renameRoom(
conversation!!.token,
newRoomName
)
conversationInfoEditViewModel.renameRoom(
conversation!!.token,
newRoomName
)
}

fun saveConversationDescription() {
val conversationDescription = binding.conversationDescription.text.toString()
val conversationDescription = binding.conversationDescription.text.toString()
conversationInfoEditViewModel.setConversationDescription(conversation!!.token, conversationDescription)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ interface ConversationInfoEditRepository {

suspend fun renameConversation(roomToken: String, roomNameNew: String): GenericOverall

suspend fun setConversationDescription(roomToken:String, conversationDescription:String?): GenericOverall
suspend fun setConversationDescription(roomToken: String, conversationDescription: String?): GenericOverall
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ import okhttp3.MultipartBody
import okhttp3.RequestBody.Companion.asRequestBody
import java.io.File

class ConversationInfoEditRepositoryImpl(private val ncApi: NcApi,
class ConversationInfoEditRepositoryImpl(
private val ncApi: NcApi,
private val ncApiCoroutines: NcApiCoroutines,
currentUserProvider: CurrentUserProviderNew) :
currentUserProvider: CurrentUserProviderNew
) :
ConversationInfoEditRepository {

val currentUser: User = currentUserProvider.currentUser.blockingGet()
Expand Down
Loading

0 comments on commit 452a4c7

Please sign in to comment.