From 4f865ec95fab3b78d66875018de62e39f7c8320e Mon Sep 17 00:00:00 2001 From: Lakoja Date: Mon, 11 Sep 2023 22:19:34 +0200 Subject: [PATCH] Add a bunch of "old" methods to be able to still use java code --- .../keylesspalace/tusky/appstore/EventsHub.kt | 4 + .../tusky/fragment/NotificationsFragment.java | 86 +++++++++---------- .../tusky/network/MastodonApi.kt | 47 ++++++++++ .../tusky/usecase/TimelineCases.kt | 54 ++++++++++++ 4 files changed, 148 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/com/keylesspalace/tusky/appstore/EventsHub.kt b/app/src/main/java/com/keylesspalace/tusky/appstore/EventsHub.kt index 231af7bf..9fb4a3c8 100644 --- a/app/src/main/java/com/keylesspalace/tusky/appstore/EventsHub.kt +++ b/app/src/main/java/com/keylesspalace/tusky/appstore/EventsHub.kt @@ -23,4 +23,8 @@ class EventHub @Inject constructor() { sharedEventFlow.emit(event) eventsSubject.onNext(event) } + + fun dispatchOld(event: Event) { + eventsSubject.onNext(event) + } } diff --git a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java index 1af1ed43..da9d8902 100644 --- a/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java +++ b/app/src/main/java/com/keylesspalace/tusky/fragment/NotificationsFragment.java @@ -417,17 +417,17 @@ public class NotificationsFragment extends SFragment implements @Override public void onReblog(final boolean reblog, final int position) { -// final Notification notification = notifications.get(position).asRight(); -// final Status status = notification.getStatus(); -// Objects.requireNonNull(status, "Reblog on notification without status"); -// timelineCases.reblog(status.getId(), reblog) -// .observeOn(AndroidSchedulers.mainThread()) -// .to(autoDisposable(from(this))) -// .subscribe( -// (newStatus) -> setReblogForStatus(status.getId(), reblog), -// (t) -> Log.d(getClass().getSimpleName(), -// "Failed to reblog status: " + status.getId(), t) -// ); + final Notification notification = notifications.get(position).asRight(); + final Status status = notification.getStatus(); + Objects.requireNonNull(status, "Reblog on notification without status"); + timelineCases.reblogOld(status.getId(), reblog) + .observeOn(AndroidSchedulers.mainThread()) + .to(autoDisposable(from(this))) + .subscribe( + (newStatus) -> setReblogForStatus(status.getId(), reblog), + (t) -> Log.d(getClass().getSimpleName(), + "Failed to reblog status: " + status.getId(), t) + ); } private void setReblogForStatus(String statusId, boolean reblog) { @@ -436,17 +436,17 @@ public class NotificationsFragment extends SFragment implements @Override public void onFavourite(final boolean favourite, final int position) { -// final Notification notification = notifications.get(position).asRight(); -// final Status status = notification.getStatus(); -// -// timelineCases.favourite(status.getId(), favourite) -// .observeOn(AndroidSchedulers.mainThread()) -// .to(autoDisposable(from(this))) -// .subscribe( -// (newStatus) -> setFavouriteForStatus(status.getId(), favourite), -// (t) -> Log.d(getClass().getSimpleName(), -// "Failed to favourite status: " + status.getId(), t) -// ); + final Notification notification = notifications.get(position).asRight(); + final Status status = notification.getStatus(); + + timelineCases.favouriteOld(status.getId(), favourite) + .observeOn(AndroidSchedulers.mainThread()) + .to(autoDisposable(from(this))) + .subscribe( + (newStatus) -> setFavouriteForStatus(status.getId(), favourite), + (t) -> Log.d(getClass().getSimpleName(), + "Failed to favourite status: " + status.getId(), t) + ); } private void setFavouriteForStatus(String statusId, boolean favourite) { @@ -455,17 +455,17 @@ public class NotificationsFragment extends SFragment implements @Override public void onBookmark(final boolean bookmark, final int position) { -// final Notification notification = notifications.get(position).asRight(); -// final Status status = notification.getStatus(); -// -// timelineCases.bookmark(status.getActionableId(), bookmark) -// .observeOn(AndroidSchedulers.mainThread()) -// .to(autoDisposable(from(this))) -// .subscribe( -// (newStatus) -> setBookmarkForStatus(status.getId(), bookmark), -// (t) -> Log.d(getClass().getSimpleName(), -// "Failed to bookmark status: " + status.getId(), t) -// ); + final Notification notification = notifications.get(position).asRight(); + final Status status = notification.getStatus(); + + timelineCases.bookmarkOld(status.getActionableId(), bookmark) + .observeOn(AndroidSchedulers.mainThread()) + .to(autoDisposable(from(this))) + .subscribe( + (newStatus) -> setBookmarkForStatus(status.getId(), bookmark), + (t) -> Log.d(getClass().getSimpleName(), + "Failed to bookmark status: " + status.getId(), t) + ); } private void setBookmarkForStatus(String statusId, boolean bookmark) { @@ -473,16 +473,16 @@ public class NotificationsFragment extends SFragment implements } public void onVoteInPoll(int position, @NonNull List choices) { -// final Notification notification = notifications.get(position).asRight(); -// final Status status = notification.getStatus().getActionableStatus(); -// timelineCases.voteInPoll(status.getId(), status.getPoll().getId(), choices) -// .observeOn(AndroidSchedulers.mainThread()) -// .to(autoDisposable(from(this))) -// .subscribe( -// (newPoll) -> setVoteForPoll(status, newPoll), -// (t) -> Log.d(TAG, -// "Failed to vote in poll: " + status.getId(), t) -// ); + final Notification notification = notifications.get(position).asRight(); + final Status status = notification.getStatus().getActionableStatus(); + timelineCases.voteInPollOld(status.getId(), status.getPoll().getId(), choices) + .observeOn(AndroidSchedulers.mainThread()) + .to(autoDisposable(from(this))) + .subscribe( + (newPoll) -> setVoteForPoll(status, newPoll), + (t) -> Log.d(TAG, + "Failed to vote in poll: " + status.getId(), t) + ); } @Override diff --git a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt index 2dbaf059..2a8d0fd7 100644 --- a/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt +++ b/app/src/main/java/com/keylesspalace/tusky/network/MastodonApi.kt @@ -290,6 +290,36 @@ interface MastodonApi { @Path("id") statusId: String ): NetworkResult + @POST("api/v1/statuses/{id}/reblog") + fun reblogStatusOld( + @Path("id") statusId: String + ): Single + + @POST("api/v1/statuses/{id}/unreblog") + fun unreblogStatusOld( + @Path("id") statusId: String + ): Single + + @POST("api/v1/statuses/{id}/favourite") + fun favouriteStatusOld( + @Path("id") statusId: String + ): Single + + @POST("api/v1/statuses/{id}/unfavourite") + fun unfavouriteStatusOld( + @Path("id") statusId: String + ): Single + + @POST("api/v1/statuses/{id}/bookmark") + fun bookmarkStatusOld( + @Path("id") statusId: String + ): Single + + @POST("api/v1/statuses/{id}/unbookmark") + fun unbookmarkStatusOld( + @Path("id") statusId: String + ): Single + @POST("api/v1/statuses/{id}/pin") suspend fun pinStatus( @Path("id") statusId: String @@ -310,6 +340,16 @@ interface MastodonApi { @Path("id") statusId: String ): NetworkResult + @POST("api/v1/statuses/{id}/mute") + fun muteConversationOld( + @Path("id") statusId: String + ): Single + + @POST("api/v1/statuses/{id}/unmute") + fun unmuteConversationOld( + @Path("id") statusId: String + ): Single + @GET("api/v1/scheduled_statuses") fun scheduledStatuses( @Query("limit") limit: Int? = null, @@ -681,6 +721,13 @@ interface MastodonApi { @Field("choices[]") choices: List ): NetworkResult + @FormUrlEncoded + @POST("api/v1/polls/{id}/votes") + fun voteInPollOld( + @Path("id") id: String, + @Field("choices[]") choices: List + ): Single + @GET("api/v1/announcements") suspend fun listAnnouncements( @Query("with_dismissed") withDismissed: Boolean = true diff --git a/app/src/main/java/com/keylesspalace/tusky/usecase/TimelineCases.kt b/app/src/main/java/com/keylesspalace/tusky/usecase/TimelineCases.kt index fc6ccbf3..093df262 100644 --- a/app/src/main/java/com/keylesspalace/tusky/usecase/TimelineCases.kt +++ b/app/src/main/java/com/keylesspalace/tusky/usecase/TimelineCases.kt @@ -88,6 +88,50 @@ class TimelineCases @Inject constructor( } } + fun reblogOld(statusId: String, reblog: Boolean): Single { + val call = if (reblog) { + mastodonApi.reblogStatusOld(statusId) + } else { + mastodonApi.unreblogStatusOld(statusId) + } + return call.doAfterSuccess { + eventHub.dispatchOld(ReblogEvent(statusId, reblog)) + } + } + + fun favouriteOld(statusId: String, favourite: Boolean): Single { + val call = if (favourite) { + mastodonApi.favouriteStatusOld(statusId) + } else { + mastodonApi.unfavouriteStatusOld(statusId) + } + return call.doAfterSuccess { + eventHub.dispatchOld(FavoriteEvent(statusId, favourite)) + } + } + + fun bookmarkOld(statusId: String, bookmark: Boolean): Single { + val call = if (bookmark) { + mastodonApi.bookmarkStatusOld(statusId) + } else { + mastodonApi.unbookmarkStatusOld(statusId) + } + return call.doAfterSuccess { + eventHub.dispatchOld(BookmarkEvent(statusId, bookmark)) + } + } + + fun muteConversationOld(statusId: String, mute: Boolean): Single { + val call = if (mute) { + mastodonApi.muteConversationOld(statusId) + } else { + mastodonApi.unmuteConversationOld(statusId) + } + return call.doAfterSuccess { + eventHub.dispatchOld(MuteConversationEvent(statusId, mute)) + } + } + suspend fun mute(statusId: String, notifications: Boolean, duration: Int?) { try { mastodonApi.muteAccount(statusId, notifications, duration) @@ -136,6 +180,16 @@ class TimelineCases @Inject constructor( } } + fun voteInPollOld(statusId: String, pollId: String, choices: List): Single { + if (choices.isEmpty()) { + return Single.error(IllegalStateException()) + } + + return mastodonApi.voteInPollOld(pollId, choices).doAfterSuccess { + eventHub.dispatchOld(PollVoteEvent(statusId, it)) + } + } + fun acceptFollowRequest(accountId: String): Single { return mastodonApi.authorizeFollowRequest(accountId) }