mirror of
https://github.com/nextcloud/talk-android.git
synced 2024-11-21 12:35:30 +03:00
convert rxjava to coroutines
Signed-off-by: sowjanyakch <sowjanya.kch@gmail.com>
This commit is contained in:
parent
2d5facc905
commit
af9b2b29c7
7 changed files with 81 additions and 55 deletions
|
@ -89,10 +89,10 @@ interface NcApiCoroutines {
|
|||
): AddParticipantOverall
|
||||
|
||||
@POST
|
||||
suspend fun makeRoomPublic(@Header("Authorization") authorization: String?, @Url url: String): GenericOverall
|
||||
suspend fun makeRoomPublic(@Header("Authorization") authorization: String, @Url url: String): GenericOverall
|
||||
|
||||
@DELETE
|
||||
suspend fun makeRoomPrivate(@Header("Authorization") authorization: String?, @Url url: String): GenericOverall
|
||||
suspend fun makeRoomPrivate(@Header("Authorization") authorization: String, @Url url: String): GenericOverall
|
||||
|
||||
@FormUrlEncoded
|
||||
@PUT
|
||||
|
|
|
@ -168,16 +168,15 @@ class ConversationCreationRepositoryImpl(
|
|||
|
||||
val result: GenericOverall = if (allow) {
|
||||
ncApiCoroutines.makeRoomPublic(
|
||||
credentials,
|
||||
credentials!!,
|
||||
url
|
||||
)
|
||||
} else {
|
||||
ncApiCoroutines.makeRoomPrivate(
|
||||
credentials,
|
||||
credentials!!,
|
||||
url
|
||||
)
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
|
|
@ -916,7 +916,9 @@ class ConversationInfoActivity :
|
|||
it,
|
||||
conversation!!,
|
||||
spreedCapabilities,
|
||||
conversationUser
|
||||
conversationUser,
|
||||
viewModel,
|
||||
this
|
||||
).setupGuestAccess()
|
||||
}
|
||||
if (ConversationUtils.isNoteToSelfConversation(conversation!!)) {
|
||||
|
|
|
@ -12,9 +12,11 @@ import android.util.Log
|
|||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.lifecycle.LifecycleOwner
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
||||
import com.google.android.material.snackbar.Snackbar
|
||||
import com.nextcloud.talk.R
|
||||
import com.nextcloud.talk.conversationinfo.viewmodel.ConversationInfoViewModel
|
||||
import com.nextcloud.talk.data.user.model.User
|
||||
import com.nextcloud.talk.databinding.ActivityConversationInfoBinding
|
||||
import com.nextcloud.talk.databinding.DialogPasswordBinding
|
||||
|
@ -33,9 +35,10 @@ class GuestAccessHelper(
|
|||
private val binding: ActivityConversationInfoBinding,
|
||||
private val conversation: ConversationModel,
|
||||
private val spreedCapabilities: SpreedCapability,
|
||||
private val conversationUser: User
|
||||
private val conversationUser: User,
|
||||
private val viewModel: ConversationInfoViewModel,
|
||||
private val lifecycleOwner: LifecycleOwner
|
||||
) {
|
||||
|
||||
private val conversationsRepository = activity.conversationsRepository
|
||||
private val viewThemeUtils = activity.viewThemeUtils
|
||||
private val context = activity.context
|
||||
|
@ -61,11 +64,27 @@ class GuestAccessHelper(
|
|||
binding.guestAccessView.guestAccessSettingsAllowGuest.setOnClickListener {
|
||||
val isChecked = binding.guestAccessView.allowGuestsSwitch.isChecked
|
||||
binding.guestAccessView.allowGuestsSwitch.isChecked = !isChecked
|
||||
conversationsRepository.allowGuests(
|
||||
conversation.token!!,
|
||||
!isChecked
|
||||
).subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread()).subscribe(AllowGuestsResultObserver())
|
||||
viewModel.allowGuests(conversation.token, !isChecked)
|
||||
viewModel.allowGuestsViewState.observe(lifecycleOwner){uiState ->
|
||||
when(uiState){
|
||||
is ConversationInfoViewModel.AllowGuestsUIState.Success ->{
|
||||
if(uiState.result){
|
||||
showAllOptions()
|
||||
}else{
|
||||
hideAllOptions()
|
||||
}
|
||||
}
|
||||
is ConversationInfoViewModel.AllowGuestsUIState.Error ->{
|
||||
val exception = uiState.message
|
||||
val message = context.getString(R.string.nc_guest_access_allow_failed)
|
||||
Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show()
|
||||
Log.e(TAG, exception)
|
||||
}
|
||||
ConversationInfoViewModel.AllowGuestsUIState.None ->{
|
||||
//unused atm
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
binding.guestAccessView.guestAccessSettingsPasswordProtection.setOnClickListener {
|
||||
|
@ -143,32 +162,6 @@ class GuestAccessHelper(
|
|||
}
|
||||
}
|
||||
|
||||
inner class AllowGuestsResultObserver : Observer<ConversationsRepository.AllowGuestsResult> {
|
||||
|
||||
private lateinit var allowGuestsResult: ConversationsRepository.AllowGuestsResult
|
||||
|
||||
override fun onNext(t: ConversationsRepository.AllowGuestsResult) {
|
||||
allowGuestsResult = t
|
||||
}
|
||||
|
||||
override fun onError(e: Throwable) {
|
||||
val message = context.getString(R.string.nc_guest_access_allow_failed)
|
||||
Snackbar.make(binding.root, message, Snackbar.LENGTH_LONG).show()
|
||||
Log.e(TAG, message, e)
|
||||
}
|
||||
|
||||
override fun onComplete() {
|
||||
binding.guestAccessView.allowGuestsSwitch.isChecked = allowGuestsResult.allow
|
||||
if (allowGuestsResult.allow) {
|
||||
showAllOptions()
|
||||
} else {
|
||||
hideAllOptions()
|
||||
}
|
||||
}
|
||||
|
||||
override fun onSubscribe(d: Disposable) = Unit
|
||||
}
|
||||
|
||||
private fun showAllOptions() {
|
||||
binding.guestAccessView.guestAccessSettingsPasswordProtection.visibility = View.VISIBLE
|
||||
if (conversationUser.capabilities?.spreedCapability?.features?.contains("sip-support") == true) {
|
||||
|
|
|
@ -12,18 +12,27 @@ import androidx.lifecycle.LifecycleOwner
|
|||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.MutableLiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.nextcloud.talk.chat.data.network.ChatNetworkDataSource
|
||||
import com.nextcloud.talk.conversationcreation.AllowGuestsUiState
|
||||
import com.nextcloud.talk.conversationcreation.RoomUIState
|
||||
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.conversations.Conversation
|
||||
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
|
||||
import io.reactivex.disposables.Disposable
|
||||
import io.reactivex.schedulers.Schedulers
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.launch
|
||||
import javax.inject.Inject
|
||||
|
||||
class ConversationInfoViewModel @Inject constructor(
|
||||
|
@ -95,6 +104,10 @@ class ConversationInfoViewModel @Inject constructor(
|
|||
object GetCapabilitiesErrorState : ViewState
|
||||
open class GetCapabilitiesSuccessState(val spreedCapabilities: SpreedCapability) : ViewState
|
||||
|
||||
private val _allowGuestsViewState = MutableLiveData<AllowGuestsUIState>(AllowGuestsUIState.None)
|
||||
val allowGuestsViewState: LiveData<AllowGuestsUIState>
|
||||
get() = _allowGuestsViewState
|
||||
|
||||
private val _getCapabilitiesViewState: MutableLiveData<ViewState> = MutableLiveData(GetCapabilitiesStartState)
|
||||
val getCapabilitiesViewState: LiveData<ViewState>
|
||||
get() = _getCapabilitiesViewState
|
||||
|
@ -233,6 +246,23 @@ class ConversationInfoViewModel @Inject constructor(
|
|||
})
|
||||
}
|
||||
|
||||
|
||||
fun allowGuests(token:String,allow:Boolean){
|
||||
viewModelScope.launch{
|
||||
try{
|
||||
val allowGuestsResult = conversationsRepository.allowGuests(token,allow)
|
||||
val statusCode: GenericMeta? = allowGuestsResult.ocs?.meta
|
||||
val result = (statusCode?.statusCode == STATUS_CODE_OK)
|
||||
if (result) {
|
||||
_allowGuestsViewState.value = AllowGuestsUIState.Success(result)
|
||||
}
|
||||
}catch(exception:Exception){
|
||||
_allowGuestsViewState.value = AllowGuestsUIState.Error(exception.message?: "")
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun archiveConversation(user: User, token: String) {
|
||||
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
|
||||
val url = ApiUtils.getUrlForArchive(apiVersion, user.baseUrl, token)
|
||||
|
@ -267,4 +297,10 @@ class ConversationInfoViewModel @Inject constructor(
|
|||
companion object {
|
||||
private val TAG = ConversationInfoViewModel::class.simpleName
|
||||
}
|
||||
|
||||
sealed class AllowGuestsUIState {
|
||||
data object None : AllowGuestsUIState()
|
||||
data class Success(val result: Boolean) : AllowGuestsUIState()
|
||||
data class Error(val message: String) : AllowGuestsUIState()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,11 +12,7 @@ import io.reactivex.Observable
|
|||
|
||||
interface ConversationsRepository {
|
||||
|
||||
data class AllowGuestsResult(
|
||||
val allow: Boolean
|
||||
)
|
||||
|
||||
fun allowGuests(token: String, allow: Boolean): Observable<AllowGuestsResult>
|
||||
suspend fun allowGuests(token: String, allow: Boolean): GenericOverall
|
||||
|
||||
data class PasswordResult(
|
||||
val passwordSet: Boolean,
|
||||
|
|
|
@ -13,7 +13,6 @@ import com.nextcloud.talk.api.NcApiCoroutines
|
|||
import com.nextcloud.talk.data.user.model.User
|
||||
import com.nextcloud.talk.models.json.conversations.password.PasswordOverall
|
||||
import com.nextcloud.talk.models.json.generic.GenericOverall
|
||||
import com.nextcloud.talk.repositories.conversations.ConversationsRepository.AllowGuestsResult
|
||||
import com.nextcloud.talk.repositories.conversations.ConversationsRepository.PasswordResult
|
||||
import com.nextcloud.talk.repositories.conversations.ConversationsRepository.ResendInvitationsResult
|
||||
import com.nextcloud.talk.utils.ApiUtils
|
||||
|
@ -24,8 +23,7 @@ class ConversationsRepositoryImpl(
|
|||
private val api: NcApi,
|
||||
private val coroutineApi: NcApiCoroutines,
|
||||
private val userProvider: CurrentUserProviderNew
|
||||
) :
|
||||
ConversationsRepository {
|
||||
) : ConversationsRepository {
|
||||
|
||||
private val user: User
|
||||
get() = userProvider.currentUser.blockingGet()
|
||||
|
@ -33,29 +31,31 @@ class ConversationsRepositoryImpl(
|
|||
private val credentials: String
|
||||
get() = ApiUtils.getCredentials(user.username, user.token)!!
|
||||
|
||||
override fun allowGuests(token: String, allow: Boolean): Observable<AllowGuestsResult> {
|
||||
val apiVersion = ApiUtils.getConversationApiVersion(user, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
|
||||
|
||||
override suspend fun allowGuests(token: String, allow: Boolean): GenericOverall {
|
||||
val url = ApiUtils.getUrlForRoomPublic(
|
||||
apiVersion(),
|
||||
apiVersion,
|
||||
user.baseUrl!!,
|
||||
token
|
||||
)
|
||||
|
||||
val apiObservable = if (allow) {
|
||||
api.makeRoomPublic(
|
||||
val result: GenericOverall = if (allow) {
|
||||
coroutineApi.makeRoomPublic(
|
||||
credentials,
|
||||
url
|
||||
)
|
||||
} else {
|
||||
api.makeRoomPrivate(
|
||||
coroutineApi.makeRoomPrivate(
|
||||
credentials,
|
||||
url
|
||||
)
|
||||
}
|
||||
|
||||
return apiObservable.map { AllowGuestsResult(it.ocs!!.meta!!.statusCode == STATUS_CODE_OK && allow) }
|
||||
return result
|
||||
}
|
||||
|
||||
override fun password(password: String, token: String): Observable<PasswordResult> {
|
||||
|
||||
override fun password(password: String, token: String): Observable<PasswordResult> {
|
||||
val apiObservable = api.setPassword2(
|
||||
credentials,
|
||||
ApiUtils.getUrlForRoomPassword(
|
||||
|
|
Loading…
Reference in a new issue