From 465abd4498939b355a53cf38ae9b63dfec4496d5 Mon Sep 17 00:00:00 2001 From: Balazs Toldi Date: Thu, 3 Aug 2023 07:51:22 +0200 Subject: [PATCH] Inbox Read all button function This commit adds functionality for the read all option in the InboxActivity --- .../activities/InboxActivity.java | 44 +++++++------------ .../toldi/infinityforlemmy/apis/LemmyAPI.java | 5 +++ .../toldi/infinityforlemmy/dto/AuthDTO.java | 14 ++++++ .../infinityforlemmy/message/ReadMessage.java | 21 +++++++++ 4 files changed, 55 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/eu/toldi/infinityforlemmy/dto/AuthDTO.java diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java b/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java index 6cf4c290..5d63f290 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/activities/InboxActivity.java @@ -47,7 +47,6 @@ import eu.toldi.infinityforlemmy.R; import eu.toldi.infinityforlemmy.RecyclerViewContentScrollingInterface; import eu.toldi.infinityforlemmy.RedditDataRoomDatabase; import eu.toldi.infinityforlemmy.RetrofitHolder; -import eu.toldi.infinityforlemmy.apis.RedditAPI; import eu.toldi.infinityforlemmy.asynctasks.SwitchAccount; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; import eu.toldi.infinityforlemmy.customviews.slidr.Slidr; @@ -58,12 +57,9 @@ import eu.toldi.infinityforlemmy.events.SwitchAccountEvent; import eu.toldi.infinityforlemmy.fragments.InboxFragment; import eu.toldi.infinityforlemmy.message.CommentInteraction; import eu.toldi.infinityforlemmy.message.FetchMessage; -import eu.toldi.infinityforlemmy.utils.APIUtils; +import eu.toldi.infinityforlemmy.message.ReadMessage; import eu.toldi.infinityforlemmy.utils.SharedPreferencesUtils; import eu.toldi.infinityforlemmy.utils.Utils; -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; public class InboxActivity extends BaseActivity implements ActivityToolbarInterface, RecyclerViewContentScrollingInterface { @@ -305,31 +301,21 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf return true; } else if (item.getItemId() == R.id.action_read_all_messages_inbox_activity) { if (mAccessToken != null) { - Toast.makeText(this, R.string.please_wait, Toast.LENGTH_SHORT).show(); - mRetrofit.getRetrofit().create(RedditAPI.class).readAllMessages(APIUtils.getOAuthHeader(mAccessToken)) - .enqueue(new Callback<>() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - Toast.makeText(InboxActivity.this, R.string.read_all_messages_success, Toast.LENGTH_SHORT).show(); - if (sectionsPagerAdapter != null) { - sectionsPagerAdapter.readAllMessages(); - } - EventBus.getDefault().post(new ChangeInboxCountEvent(0)); - } else { - if (response.code() == 429) { - Toast.makeText(InboxActivity.this, R.string.read_all_messages_time_limit, Toast.LENGTH_LONG).show(); - } else { - Toast.makeText(InboxActivity.this, R.string.read_all_messages_failed, Toast.LENGTH_LONG).show(); - } - } - } + ReadMessage.readAllMessages(mRetrofit.getRetrofit(), mAccessToken, new ReadMessage.ReadMessageListener() { + @Override + public void readSuccess() { + Toast.makeText(InboxActivity.this, R.string.read_all_messages_success, Toast.LENGTH_SHORT).show(); + if (sectionsPagerAdapter != null) { + sectionsPagerAdapter.readAllMessages(); + } + EventBus.getDefault().post(new ChangeInboxCountEvent(0)); + } - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - Toast.makeText(InboxActivity.this, R.string.read_all_messages_failed, Toast.LENGTH_LONG).show(); - } - }); + @Override + public void readFailed() { + + } + }); } } else if (item.getItemId() == android.R.id.home) { finish(); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java index 9d79edb0..e82ac3e3 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/apis/LemmyAPI.java @@ -5,6 +5,7 @@ import androidx.annotation.NonNull; import com.google.common.util.concurrent.ListenableFuture; import eu.toldi.infinityforlemmy.dto.AccountLoginDTO; +import eu.toldi.infinityforlemmy.dto.AuthDTO; import eu.toldi.infinityforlemmy.dto.CommentDTO; import eu.toldi.infinityforlemmy.dto.CommentVoteDTO; import eu.toldi.infinityforlemmy.dto.DeleteCommentDTO; @@ -61,6 +62,10 @@ public interface LemmyAPI { @POST("api/v3/comment/mark_as_read") Call commentMarkAsRead(@Body ReadCommentDTO params); + @Headers("Content-Type: application/json") + @POST("api/v3/user/mark_all_as_read") + Call userMarkAllAsRead(@Body AuthDTO params); + @GET("api/v3/community") Call communityInfo(@Query("name") String name, @Query("auth") String access_token); diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/dto/AuthDTO.java b/app/src/main/java/eu/toldi/infinityforlemmy/dto/AuthDTO.java new file mode 100644 index 00000000..bafa61c0 --- /dev/null +++ b/app/src/main/java/eu/toldi/infinityforlemmy/dto/AuthDTO.java @@ -0,0 +1,14 @@ +package eu.toldi.infinityforlemmy.dto; + +public class AuthDTO { + + private String auth; + + public AuthDTO(String auth) { + this.auth = auth; + } + + public String getAuth() { + return auth; + } +} diff --git a/app/src/main/java/eu/toldi/infinityforlemmy/message/ReadMessage.java b/app/src/main/java/eu/toldi/infinityforlemmy/message/ReadMessage.java index 9cb60734..b0516eee 100644 --- a/app/src/main/java/eu/toldi/infinityforlemmy/message/ReadMessage.java +++ b/app/src/main/java/eu/toldi/infinityforlemmy/message/ReadMessage.java @@ -3,6 +3,7 @@ package eu.toldi.infinityforlemmy.message; import androidx.annotation.NonNull; import eu.toldi.infinityforlemmy.apis.LemmyAPI; +import eu.toldi.infinityforlemmy.dto.AuthDTO; import eu.toldi.infinityforlemmy.dto.ReadCommentDTO; import retrofit2.Call; import retrofit2.Callback; @@ -30,6 +31,26 @@ public class ReadMessage { }); } + public static void readAllMessages(Retrofit retrofit, String accessToken, + ReadMessageListener readMessageListener) { + retrofit.create(LemmyAPI.class).userMarkAllAsRead(new AuthDTO(accessToken)) + .enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + readMessageListener.readSuccess(); + } else { + readMessageListener.readFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + readMessageListener.readFailed(); + } + }); + } + public interface ReadMessageListener { void readSuccess();