Show inbox count on navigation drawer

This commit is contained in:
Balazs Toldi 2023-08-03 06:58:55 +02:00
parent d4b107bc79
commit f7185445e1
No known key found for this signature in database
GPG Key ID: 6C7D440036F99D58
4 changed files with 87 additions and 6 deletions

View File

@ -1076,20 +1076,29 @@ public class MainActivity extends BaseActivity implements SortTypeSelectionCallb
private void loadUserData() {
if (!mFetchUserInfoSuccess) {
FetchUserData.fetchUserData(mRedditDataRoomDatabase, mRetrofit.getRetrofit(), mAccessToken,
mAccountName, new FetchUserData.FetchUserDataListener() {
FetchUserData.fetchUnreadCount(mRetrofit.getRetrofit(), mAccessToken, new FetchUserData.FetchUserUnreadCountListener() {
@Override
public void onFetchUserDataSuccess(UserData userData, int inboxCount) {
public void onFetchUserUnreadCountSuccess(int inboxCount) {
MainActivity.this.inboxCount = inboxCount;
mAccountName = userData.getName();
mFetchUserInfoSuccess = true;
if (adapter != null) {
adapter.setInboxCount(inboxCount);
}
}
@Override
public void onFetchUserDataFailed() {
public void onFetchUserUnreadCountFailed() {
}
});
FetchUserData.fetchUserData(mRedditDataRoomDatabase, mRetrofit.getRetrofit(), mAccessToken,
mAccountName, new FetchUserData.FetchUserDataListener() {
@Override
public void onFetchUserDataSuccess(UserData userData, int inboxCount) {
mAccountName = userData.getName();
mFetchUserInfoSuccess = true;
}
@Override
public void onFetchUserDataFailed() {
mFetchUserInfoSuccess = false;
}
});

View File

@ -1,5 +1,7 @@
package eu.toldi.infinityforlemmy.apis;
import androidx.annotation.NonNull;
import com.google.common.util.concurrent.ListenableFuture;
import eu.toldi.infinityforlemmy.dto.AccountLoginDTO;
@ -15,6 +17,7 @@ import eu.toldi.infinityforlemmy.dto.ReadPostDTO;
import eu.toldi.infinityforlemmy.dto.SaveCommentDTO;
import eu.toldi.infinityforlemmy.dto.SavePostDTO;
import eu.toldi.infinityforlemmy.dto.SubmitPostDTO;
import eu.toldi.infinityforlemmy.message.MessageCount;
import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Response;
@ -45,6 +48,9 @@ public interface LemmyAPI {
@GET("api/v3/private_message/list")
Call<String> privateMessageList(@Query("page") Integer page, @Query("limit") Integer limit, @Query("unread_only") boolean unread_only, @Query("auth") String access_token);
@GET("api/v3/user/unread_count")
Call<MessageCount> userUnreadCount(@NonNull @Query("auth") String access_token);
@GET("api/v3/community")
Call<String> communityInfo(@Query("name") String name, @Query("auth") String access_token);

View File

@ -0,0 +1,38 @@
package eu.toldi.infinityforlemmy.message;
public class MessageCount {
/*
Example:
{
"replies": 1,
"mentions": 0,
"private_messages": 0
}
*/
public int replies;
public int mentions;
public int private_messages;
public MessageCount(int replies, int mentions, int private_messages) {
this.replies = replies;
this.mentions = mentions;
this.private_messages = private_messages;
}
public int getReplies() {
return replies;
}
public int getMentions() {
return mentions;
}
public int getPrivate_messages() {
return private_messages;
}
public int getSum() {
return replies + mentions + private_messages;
}
}

View File

@ -7,6 +7,7 @@ import java.util.ArrayList;
import eu.toldi.infinityforlemmy.RedditDataRoomDatabase;
import eu.toldi.infinityforlemmy.SortType;
import eu.toldi.infinityforlemmy.apis.LemmyAPI;
import eu.toldi.infinityforlemmy.message.MessageCount;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Retrofit;
@ -85,6 +86,33 @@ public class FetchUserData {
});
}
public static void fetchUnreadCount(Retrofit retrofit, String accessToken, FetchUserUnreadCountListener fetchUserUnreadCountListener) {
LemmyAPI api = retrofit.create(LemmyAPI.class);
Call<MessageCount> userUnreadCount = api.userUnreadCount(accessToken);
userUnreadCount.enqueue(new Callback<>() {
@Override
public void onResponse(@NonNull Call<MessageCount> call, @NonNull retrofit2.Response<MessageCount> response) {
if (response.isSuccessful() && response.body() != null) {
fetchUserUnreadCountListener.onFetchUserUnreadCountSuccess(response.body().getSum());
} else {
fetchUserUnreadCountListener.onFetchUserUnreadCountFailed();
}
}
@Override
public void onFailure(@NonNull Call<MessageCount> call, @NonNull Throwable t) {
fetchUserUnreadCountListener.onFetchUserUnreadCountFailed();
}
});
}
public interface FetchUserUnreadCountListener {
void onFetchUserUnreadCountSuccess(int unreadCount);
void onFetchUserUnreadCountFailed();
}
public interface FetchUserDataListener {
void onFetchUserDataSuccess(UserData userData, int inboxCount);