Replace RxJava3 code with coroutines (#4290)

This pull request removes the remaining RxJava code and replaces it with
coroutine-equivalent implementations.

- Remove all duplicate methods in `MastodonApi`:
- Methods returning a RxJava `Single` have been replaced by suspending
methods returning a `NetworkResult` in order to be consistent with the
new code.
- _sync_/_async_ method variants are replaced with the _async_ version
only (suspending method), and `runBlocking{}` is used to make the async
variant synchronous.
- Create a custom coroutine-based implementation of `Single` for usage
in Java code where launching a coroutine is not possible. This class can
be deleted after remaining Java code has been converted to Kotlin.
- `NotificationsFragment.java` can subscribe to `EventHub` events by
calling the new lifecycle-aware `EventHub.subscribe()` method. This
allows using the `SharedFlow` as single source of truth for all events.
- Rx Autodispose is replaced by `lifecycleScope.launch()` which will
automatically cancel the coroutine when the Fragment view/Activity is
destroyed.
- Background work is launched in the existing injectable
`externalScope`, since using `GlobalScope` is discouraged.
`externalScope` has been changed to be a `@Singleton` and to use the
main dispatcher by default.
- Transform `ShareShortcutHelper` to an injectable utility class so it
can use the application `Context` and `externalScope` as provided
dependencies to launch a background coroutine.
- Implement a custom Glide extension method
`RequestBuilder.submitAsync()` to do the same thing as
`RequestBuilder.submit().get()` in a non-blocking way. This way there is
no need to switch to a background dispatcher and block a background
thread, and cancellation is supported out-of-the-box.
- An utility method `Fragment.updateRelativeTimePeriodically()` has been
added to remove duplicate logic in `TimelineFragment` and
`NotificationsFragment`, and the logic is now implemented using a simple
coroutine instead of `Observable.interval()`. Note that the periodic
update now happens between onStart and onStop instead of between
onResume and onPause, since the Fragment is not interactive but is still
visible in the started state.
- Rewrite `BottomSheetActivityTest` using coroutines tests.
- Remove all RxJava library dependencies.
This commit is contained in:
Christophe Beyls 2024-02-29 15:28:48 +01:00 committed by GitHub
commit 40fde54e0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
32 changed files with 588 additions and 590 deletions

View file

@ -46,7 +46,6 @@ import com.keylesspalace.tusky.entity.StatusEdit
import com.keylesspalace.tusky.entity.StatusSource
import com.keylesspalace.tusky.entity.TimelineAccount
import com.keylesspalace.tusky.entity.TrendingTag
import io.reactivex.rxjava3.core.Single
import okhttp3.MultipartBody
import okhttp3.RequestBody
import okhttp3.ResponseBody
@ -139,22 +138,14 @@ interface MastodonApi {
@GET("api/v1/notifications")
suspend fun notifications(
/** Return results older than this ID */
@Query("max_id") maxId: String? = null,
/** Return results immediately newer than this ID */
@Query("min_id") minId: String? = null,
/** Maximum number of results to return. Defaults to 15, max is 30 */
@Query("limit") limit: Int? = null,
/** Types to excludes from the results */
@Query("exclude_types[]") excludes: Set<Notification.Type>? = null
): Response<List<Notification>>
@GET("api/v1/notifications")
fun notificationsOld(
@Query("max_id") maxId: String?,
/** Return results newer than this ID */
@Query("since_id") sinceId: String?,
/** Maximum number of results to return. Defaults to 15, max is 30 */
@Query("limit") limit: Int?,
/** Types to excludes from the results */
@Query("exclude_types[]") excludes: Set<Notification.Type>?
): Single<Response<List<Notification>>>
): Response<List<Notification>>
/** Fetch a single notification */
@GET("api/v1/notifications/{id}")
@ -185,10 +176,7 @@ interface MastodonApi {
): Response<List<Notification>>
@POST("api/v1/notifications/clear")
suspend fun clearNotifications(): Response<ResponseBody>
@POST("api/v1/notifications/clear")
fun clearNotificationsOld(): Single<ResponseBody>
suspend fun clearNotifications(): NetworkResult<ResponseBody>
@FormUrlEncoded
@PUT("api/v1/media/{mediaId}")
@ -221,9 +209,6 @@ interface MastodonApi {
@Body editedStatus: NewStatus
): NetworkResult<Status>
@GET("api/v1/statuses/{id}")
suspend fun statusAsync(@Path("id") statusId: String): NetworkResult<Status>
@GET("api/v1/statuses/{id}/source")
suspend fun statusSource(@Path("id") statusId: String): NetworkResult<StatusSource>
@ -266,24 +251,6 @@ interface MastodonApi {
@POST("api/v1/statuses/{id}/unbookmark")
suspend fun unbookmarkStatus(@Path("id") statusId: String): NetworkResult<Status>
@POST("api/v1/statuses/{id}/reblog")
fun reblogStatusOld(@Path("id") statusId: String): Single<Status>
@POST("api/v1/statuses/{id}/unreblog")
fun unreblogStatusOld(@Path("id") statusId: String): Single<Status>
@POST("api/v1/statuses/{id}/favourite")
fun favouriteStatusOld(@Path("id") statusId: String): Single<Status>
@POST("api/v1/statuses/{id}/unfavourite")
fun unfavouriteStatusOld(@Path("id") statusId: String): Single<Status>
@POST("api/v1/statuses/{id}/bookmark")
fun bookmarkStatusOld(@Path("id") statusId: String): Single<Status>
@POST("api/v1/statuses/{id}/unbookmark")
fun unbookmarkStatusOld(@Path("id") statusId: String): Single<Status>
@POST("api/v1/statuses/{id}/pin")
suspend fun pinStatus(@Path("id") statusId: String): NetworkResult<Status>
@ -296,17 +263,11 @@ interface MastodonApi {
@POST("api/v1/statuses/{id}/unmute")
suspend fun unmuteConversation(@Path("id") statusId: String): NetworkResult<Status>
@POST("api/v1/statuses/{id}/mute")
fun muteConversationOld(@Path("id") statusId: String): Single<Status>
@POST("api/v1/statuses/{id}/unmute")
fun unmuteConversationOld(@Path("id") statusId: String): Single<Status>
@GET("api/v1/scheduled_statuses")
fun scheduledStatuses(
suspend fun scheduledStatuses(
@Query("limit") limit: Int? = null,
@Query("max_id") maxId: String? = null
): Single<List<ScheduledStatus>>
): NetworkResult<List<ScheduledStatus>>
@DELETE("api/v1/scheduled_statuses/{id}")
suspend fun deleteScheduledStatus(
@ -353,14 +314,6 @@ interface MastodonApi {
@Query("following") following: Boolean? = null
): NetworkResult<List<TimelineAccount>>
@GET("api/v1/accounts/search")
fun searchAccountsSync(
@Query("q") query: String,
@Query("resolve") resolve: Boolean? = null,
@Query("limit") limit: Int? = null,
@Query("following") following: Boolean? = null
): NetworkResult<List<TimelineAccount>>
@GET("api/v1/accounts/{id}")
suspend fun account(@Path("id") accountId: String): NetworkResult<Account>
@ -475,10 +428,10 @@ interface MastodonApi {
suspend fun followRequests(@Query("max_id") maxId: String?): Response<List<TimelineAccount>>
@POST("api/v1/follow_requests/{id}/authorize")
fun authorizeFollowRequest(@Path("id") accountId: String): Single<Relationship>
suspend fun authorizeFollowRequest(@Path("id") accountId: String): NetworkResult<Relationship>
@POST("api/v1/follow_requests/{id}/reject")
fun rejectFollowRequest(@Path("id") accountId: String): Single<Relationship>
suspend fun rejectFollowRequest(@Path("id") accountId: String): NetworkResult<Relationship>
@FormUrlEncoded
@POST("api/v1/apps")
@ -641,10 +594,6 @@ interface MastodonApi {
@Field("choices[]") choices: List<Int>
): NetworkResult<Poll>
@FormUrlEncoded
@POST("api/v1/polls/{id}/votes")
fun voteInPollOld(@Path("id") id: String, @Field("choices[]") choices: List<Int>): Single<Poll>
@GET("api/v1/announcements")
suspend fun listAnnouncements(
@Query("with_dismissed") withDismissed: Boolean = true
@ -675,30 +624,17 @@ interface MastodonApi {
): NetworkResult<Unit>
@GET("api/v1/accounts/{id}/statuses")
fun accountStatusesObservable(
suspend fun accountStatuses(
@Path("id") accountId: String,
@Query("max_id") maxId: String?,
@Query("since_id") sinceId: String?,
@Query("min_id") minId: String?,
@Query("limit") limit: Int?,
@Query("exclude_reblogs") excludeReblogs: Boolean?
): Single<List<Status>>
@GET("api/v1/statuses/{id}")
fun statusObservable(@Path("id") statusId: String): Single<Status>
): NetworkResult<List<Status>>
@GET("api/v2/search")
fun searchObservable(
@Query("q") query: String?,
@Query("type") type: String? = null,
@Query("resolve") resolve: Boolean? = null,
@Query("limit") limit: Int? = null,
@Query("offset") offset: Int? = null,
@Query("following") following: Boolean? = null
): Single<SearchResult>
@GET("api/v2/search")
fun searchSync(
suspend fun search(
@Query("q") query: String?,
@Query("type") type: String? = null,
@Query("resolve") resolve: Boolean? = null,