Inbox Read all button function

This commit adds functionality for the read all option in the InboxActivity
This commit is contained in:
Balazs Toldi 2023-08-03 07:51:22 +02:00
parent 0178569f14
commit 465abd4498
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
4 changed files with 55 additions and 29 deletions

View File

@ -47,7 +47,6 @@ import eu.toldi.infinityforlemmy.R;
import eu.toldi.infinityforlemmy.RecyclerViewContentScrollingInterface; import eu.toldi.infinityforlemmy.RecyclerViewContentScrollingInterface;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase; import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
import eu.toldi.infinityforlemmy.RetrofitHolder; import eu.toldi.infinityforlemmy.RetrofitHolder;
import eu.toldi.infinityforlemmy.apis.RedditAPI;
import eu.toldi.infinityforlemmy.asynctasks.SwitchAccount; import eu.toldi.infinityforlemmy.asynctasks.SwitchAccount;
import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper; import eu.toldi.infinityforlemmy.customtheme.CustomThemeWrapper;
import eu.toldi.infinityforlemmy.customviews.slidr.Slidr; 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.fragments.InboxFragment;
import eu.toldi.infinityforlemmy.message.CommentInteraction; import eu.toldi.infinityforlemmy.message.CommentInteraction;
import eu.toldi.infinityforlemmy.message.FetchMessage; 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.SharedPreferencesUtils;
import eu.toldi.infinityforlemmy.utils.Utils; import eu.toldi.infinityforlemmy.utils.Utils;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class InboxActivity extends BaseActivity implements ActivityToolbarInterface, RecyclerViewContentScrollingInterface { public class InboxActivity extends BaseActivity implements ActivityToolbarInterface, RecyclerViewContentScrollingInterface {
@ -305,31 +301,21 @@ public class InboxActivity extends BaseActivity implements ActivityToolbarInterf
return true; return true;
} else if (item.getItemId() == R.id.action_read_all_messages_inbox_activity) { } else if (item.getItemId() == R.id.action_read_all_messages_inbox_activity) {
if (mAccessToken != null) { if (mAccessToken != null) {
Toast.makeText(this, R.string.please_wait, Toast.LENGTH_SHORT).show(); ReadMessage.readAllMessages(mRetrofit.getRetrofit(), mAccessToken, new ReadMessage.ReadMessageListener() {
mRetrofit.getRetrofit().create(RedditAPI.class).readAllMessages(APIUtils.getOAuthHeader(mAccessToken)) @Override
.enqueue(new Callback<>() { public void readSuccess() {
@Override Toast.makeText(InboxActivity.this, R.string.read_all_messages_success, Toast.LENGTH_SHORT).show();
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) { if (sectionsPagerAdapter != null) {
if (response.isSuccessful()) { sectionsPagerAdapter.readAllMessages();
Toast.makeText(InboxActivity.this, R.string.read_all_messages_success, Toast.LENGTH_SHORT).show(); }
if (sectionsPagerAdapter != null) { EventBus.getDefault().post(new ChangeInboxCountEvent(0));
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();
}
}
}
@Override @Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) { public void readFailed() {
Toast.makeText(InboxActivity.this, R.string.read_all_messages_failed, Toast.LENGTH_LONG).show();
} }
}); });
} }
} else if (item.getItemId() == android.R.id.home) { } else if (item.getItemId() == android.R.id.home) {
finish(); finish();

View File

@ -5,6 +5,7 @@ import androidx.annotation.NonNull;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import eu.toldi.infinityforlemmy.dto.AccountLoginDTO; import eu.toldi.infinityforlemmy.dto.AccountLoginDTO;
import eu.toldi.infinityforlemmy.dto.AuthDTO;
import eu.toldi.infinityforlemmy.dto.CommentDTO; import eu.toldi.infinityforlemmy.dto.CommentDTO;
import eu.toldi.infinityforlemmy.dto.CommentVoteDTO; import eu.toldi.infinityforlemmy.dto.CommentVoteDTO;
import eu.toldi.infinityforlemmy.dto.DeleteCommentDTO; import eu.toldi.infinityforlemmy.dto.DeleteCommentDTO;
@ -61,6 +62,10 @@ public interface LemmyAPI {
@POST("api/v3/comment/mark_as_read") @POST("api/v3/comment/mark_as_read")
Call<String> commentMarkAsRead(@Body ReadCommentDTO params); Call<String> commentMarkAsRead(@Body ReadCommentDTO params);
@Headers("Content-Type: application/json")
@POST("api/v3/user/mark_all_as_read")
Call<String> userMarkAllAsRead(@Body AuthDTO params);
@GET("api/v3/community") @GET("api/v3/community")
Call<String> communityInfo(@Query("name") String name, @Query("auth") String access_token); Call<String> communityInfo(@Query("name") String name, @Query("auth") String access_token);

View File

@ -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;
}
}

View File

@ -3,6 +3,7 @@ package eu.toldi.infinityforlemmy.message;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import eu.toldi.infinityforlemmy.apis.LemmyAPI; import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.dto.AuthDTO;
import eu.toldi.infinityforlemmy.dto.ReadCommentDTO; import eu.toldi.infinityforlemmy.dto.ReadCommentDTO;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Callback; 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<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
readMessageListener.readSuccess();
} else {
readMessageListener.readFailed();
}
}
@Override
public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
readMessageListener.readFailed();
}
});
}
public interface ReadMessageListener { public interface ReadMessageListener {
void readSuccess(); void readSuccess();