Skip to content

Commit

Permalink
Merge pull request #4461 from nextcloud/coroutine_4
Browse files Browse the repository at this point in the history
Migrating from Rxjava to coroutines #4
  • Loading branch information
mahibi authored Nov 21, 2024
2 parents 6637e8c + 626994e commit 8fa3c7b
Show file tree
Hide file tree
Showing 6 changed files with 322 additions and 325 deletions.
31 changes: 15 additions & 16 deletions app/src/main/java/com/nextcloud/talk/api/NcApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,12 @@ Observable<Unit> registerDeviceForNotificationsWithPushProxy(@Url String url,
Observable<Void> unregisterDeviceForNotificationsWithProxy(@Url String url,
@QueryMap Map<String, String> fields);

@FormUrlEncoded
@PUT
Observable<Response<GenericOverall>> setPassword2(@Header("Authorization") String authorization,
@Url String url,
@Field("password") String password);

@GET
Observable<CapabilitiesOverall> getCapabilities(@Header("Authorization") String authorization, @Url String url);

Expand Down Expand Up @@ -373,16 +379,6 @@ Observable<MentionOverall> getMentionAutocompleteSuggestions(@Header("Authorizat
@Nullable @Query("limit") Integer limit,
@QueryMap Map<String, String> fields);

// Url is: /api/{apiVersion}/room/{token}/pin
@POST
Observable<GenericOverall> addConversationToFavorites(@Header("Authorization") String authorization,
@Url String url);

// Url is: /api/{apiVersion}/room/{token}/favorites
@DELETE
Observable<GenericOverall> removeConversationFromFavorites(@Header("Authorization") String authorization,
@Url String url);

@GET
Observable<NotificationOverall> getNcNotification(@Header("Authorization") String authorization,
@Url String url);
Expand All @@ -393,6 +389,12 @@ Observable<GenericOverall> setNotificationLevel(@Header("Authorization") String
@Url String url,
@Field("level") int level);

@FormUrlEncoded
@PUT
Observable<GenericOverall> setConversationReadOnly(@Header("Authorization") String authorization,
@Url String url,
@Field("state") int state);

@FormUrlEncoded
@POST
Observable<GenericOverall> createRemoteShare(@Nullable @Header("Authorization") String authorization,
Expand Down Expand Up @@ -472,6 +474,9 @@ Observable<GenericOverall> notificationCalls(@Header("Authorization") String aut
@Url String url,
@Field("level") Integer level);

@DELETE
Observable<GenericOverall> clearChatHistory(@Header("Authorization") String authorization, @Url String url);

@GET
Observable<HoverCardOverall> hoverCard(@Header("Authorization") String authorization, @Url String url);

Expand Down Expand Up @@ -573,12 +578,6 @@ Observable<PollOverall> votePoll(@Header("Authorization") String authorization,
Observable<PollOverall> closePoll(@Header("Authorization") String authorization,
@Url String url);

@FormUrlEncoded
@POST
Observable<GenericOverall> setMessageExpiration(@Header("Authorization") String authorization,
@Url String url,
@Field("seconds") Integer seconds);

@GET
Observable<OpenGraphOverall> getOpenGraph(@Header("Authorization") String authorization,
@Url String url,
Expand Down
36 changes: 36 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 @@ -119,6 +119,26 @@ interface NcApiCoroutines {
@DELETE
suspend fun unarchiveConversation(@Header("Authorization") authorization: String, @Url url: String): GenericOverall

@POST
suspend fun addConversationToFavorites(
@Header("Authorization") authorization: String,
@Url url: String
): GenericOverall

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

@FormUrlEncoded
@POST
suspend fun notificationCalls(
@Header("Authorization") authorization: String,
@Url url: String,
@Field("level") level: Int
): GenericOverall

@POST
suspend fun setReadStatusPrivacy(
@Header("Authorization") authorization: String,
Expand All @@ -143,4 +163,20 @@ interface NcApiCoroutines {
@Url url: String,
@Field("state") state: Int
): GenericOverall

@FormUrlEncoded
@POST
suspend fun setNotificationLevel(
@Header("Authorization") authorization: String,
@Url url: String,
@Field("level") level: Int
): GenericOverall

@FormUrlEncoded
@POST
suspend fun setMessageExpiration(
@Header("Authorization") authorization: String,
@Url url: String,
@Field("seconds") seconds: Int
): GenericOverall
}
Original file line number Diff line number Diff line change
Expand Up @@ -835,7 +835,9 @@ class ConversationInfoActivity :
binding.lockConversation.setOnClickListener {
val isLocked = binding.lockConversationSwitch.isChecked
binding.lockConversationSwitch.isChecked = !isLocked
databaseStorageModule!!.saveBoolean("lock_switch", !isLocked)
lifecycleScope.launch {
databaseStorageModule!!.saveBoolean("lock_switch", !isLocked)
}
val state = if (isLocked) 0 else 1
makeConversationReadOnly(conversationToken, state)
}
Expand Down Expand Up @@ -1014,7 +1016,9 @@ class ConversationInfoActivity :
.setSimpleItems(resources.getStringArray(R.array.message_expiring_descriptions))
binding.conversationSettingsDropdown.setOnItemClickListener { _, _, position, _ ->
val v: String = resources.getStringArray(R.array.message_expiring_values)[position]
databaseStorageModule!!.saveString("conversation_settings_dropdown", v)
lifecycleScope.launch {
databaseStorageModule!!.saveString("conversation_settings_dropdown", v)
}
}
binding.messageExpirationSettings.visibility = VISIBLE
} else {
Expand Down Expand Up @@ -1516,18 +1520,24 @@ class ConversationInfoActivity :
binding.notificationSettingsView.notificationSettingsImportantConversation.setOnClickListener {
val isChecked = binding.notificationSettingsView.importantConversationSwitch.isChecked
binding.notificationSettingsView.importantConversationSwitch.isChecked = !isChecked
module.saveBoolean("important_conversation_switch", !isChecked)
lifecycleScope.launch {
module.saveBoolean("important_conversation_switch", !isChecked)
}
}
binding.notificationSettingsView.notificationSettingsCallNotifications.setOnClickListener {
val isChecked = binding.notificationSettingsView.callNotificationsSwitch.isChecked
binding.notificationSettingsView.callNotificationsSwitch.isChecked = !isChecked
module.saveBoolean("call_notifications_switch", !isChecked)
lifecycleScope.launch {
module.saveBoolean("call_notifications_switch", !isChecked)
}
}
binding.notificationSettingsView.conversationInfoMessageNotificationsDropdown
.setOnItemClickListener { _, _, position, _ ->
val value = resources.getStringArray(R.array.message_notification_levels_entry_values)[position]
Log.i(TAG, "saved $value to module from $position")
module.saveString("conversation_info_message_notifications_dropdown", value)
lifecycleScope.launch {
module.saveString("conversation_info_message_notifications_dropdown", value)
}
}

binding.notificationSettingsView.importantConversationSwitch.isChecked = module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
*/
package com.nextcloud.talk.ui.dialog

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.view.ViewGroup
import androidx.lifecycle.lifecycleScope
import androidx.work.Data
import androidx.work.OneTimeWorkRequest
import androidx.work.WorkInfo
Expand All @@ -21,6 +23,7 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
import com.nextcloud.talk.R
import com.nextcloud.talk.activities.MainActivity
import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.api.NcApiCoroutines
import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.conversation.RenameConversationDialogFragment
import com.nextcloud.talk.conversationlist.ConversationsListActivity
Expand All @@ -42,6 +45,9 @@ import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject

@AutoInjector(NextcloudTalkApplication::class)
Expand All @@ -56,6 +62,9 @@ class ConversationsListBottomDialog(
@Inject
lateinit var ncApi: NcApi

@Inject
lateinit var ncApiCoroutines: NcApiCoroutines

@Inject
lateinit var viewThemeUtils: ViewThemeUtils

Expand Down Expand Up @@ -193,84 +202,56 @@ class ConversationsListBottomDialog(
}
}

@SuppressLint("StringFormatInvalid", "TooGenericExceptionCaught")
private fun addConversationToFavorites() {
val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
ncApi.addConversationToFavorites(
credentials,
ApiUtils.getUrlForRoomFavorite(
apiVersion,
currentUser.baseUrl!!,
conversation.token
)
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retry(1)
.subscribe(object : Observer<GenericOverall> {
override fun onSubscribe(d: Disposable) {
// unused atm
val url = ApiUtils.getUrlForRoomFavorite(apiVersion, currentUser.baseUrl!!, conversation.token)
lifecycleScope.launch {
try {
withContext(Dispatchers.IO) {
ncApiCoroutines.addConversationToFavorites(credentials, url)
}

override fun onNext(genericOverall: GenericOverall) {
activity.fetchRooms()
activity.showSnackbar(
String.format(
context.resources.getString(R.string.added_to_favorites),
conversation.displayName
)
activity.fetchRooms()
activity.showSnackbar(
String.format(
context.resources.getString(R.string.added_to_favorites),
conversation.displayName
)
dismiss()
}

override fun onError(e: Throwable) {
)
dismiss()
} catch (e: Exception) {
withContext(Dispatchers.Main) {
activity.showSnackbar(context.resources.getString(R.string.nc_common_error_sorry))
dismiss()
}

override fun onComplete() {
// unused atm
}
})
}
}
}

@SuppressLint("StringFormatInvalid", "TooGenericExceptionCaught")
private fun removeConversationFromFavorites() {
val apiVersion = ApiUtils.getConversationApiVersion(currentUser, intArrayOf(ApiUtils.API_V4, ApiUtils.API_V1))
ncApi.removeConversationFromFavorites(
credentials,
ApiUtils.getUrlForRoomFavorite(
apiVersion,
currentUser.baseUrl!!,
conversation.token
)
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retry(1)
.subscribe(object : Observer<GenericOverall> {
override fun onSubscribe(d: Disposable) {
// unused atm
val url = ApiUtils.getUrlForRoomFavorite(apiVersion, currentUser.baseUrl!!, conversation.token)
lifecycleScope.launch {
try {
withContext(Dispatchers.IO) {
ncApiCoroutines.removeConversationFromFavorites(credentials, url)
}

override fun onNext(genericOverall: GenericOverall) {
activity.fetchRooms()
activity.showSnackbar(
String.format(
context.resources.getString(R.string.removed_from_favorites),
conversation.displayName
)
activity.fetchRooms()
activity.showSnackbar(
String.format(
context.resources.getString(R.string.removed_from_favorites),
conversation.displayName
)
dismiss()
}

override fun onError(e: Throwable) {
)
dismiss()
} catch (e: Exception) {
withContext(Dispatchers.Main) {
activity.showSnackbar(context.resources.getString(R.string.nc_common_error_sorry))
dismiss()
}

override fun onComplete() {
// unused atm
}
})
}
}
}

private fun markConversationAsUnread() {
Expand All @@ -290,6 +271,7 @@ class ConversationsListBottomDialog(
// unused atm
}

@SuppressLint("StringFormatInvalid")
override fun onNext(genericOverall: GenericOverall) {
activity.fetchRooms()
activity.showSnackbar(
Expand Down Expand Up @@ -336,6 +318,7 @@ class ConversationsListBottomDialog(
// unused atm
}

@SuppressLint("StringFormatInvalid")
override fun onNext(genericOverall: GenericOverall) {
activity.fetchRooms()
activity.showSnackbar(
Expand Down Expand Up @@ -372,6 +355,7 @@ class ConversationsListBottomDialog(
}
}

@SuppressLint("StringFormatInvalid")
private fun leaveConversation() {
val dataBuilder = Data.Builder()
dataBuilder.putString(KEY_ROOM_TOKEN, conversation.token)
Expand Down
Loading

0 comments on commit 8fa3c7b

Please sign in to comment.