From 74f2bb47070a1c08950a54a8af642eed63229b16 Mon Sep 17 00:00:00 2001 From: LucasGGamerM Date: Thu, 13 Apr 2023 16:08:26 -0300 Subject: [PATCH] feat: add remote lookups to the PaginatedAccountListFragment --- .../account_list/FollowerListFragment.java | 15 +++ .../account_list/FollowingListFragment.java | 14 +++ .../PaginatedAccountListFragment.java | 97 ++++++++++++++++--- .../StatusFavoritesListFragment.java | 15 +++ .../StatusReblogsListFragment.java | 14 +++ 5 files changed, 143 insertions(+), 12 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowerListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowerListFragment.java index 1a7b3c8e2..f35c8a337 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowerListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowerListFragment.java @@ -6,6 +6,7 @@ import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.accounts.GetAccountFollowers; import org.joinmastodon.android.model.Account; +import org.joinmastodon.android.model.Status; public class FollowerListFragment extends AccountRelatedAccountListFragment{ @@ -15,8 +16,22 @@ public class FollowerListFragment extends AccountRelatedAccountListFragment{ setSubtitle(getResources().getQuantityString(R.plurals.x_followers, (int)(account.followersCount%1000), account.followersCount)); } + @Override + public Account getTargetAccount(){ + return account; + } + @Override + public Status getTargetStatus(){ + return null; + } + @Override public HeaderPaginationRequest onCreateRequest(String maxID, int count){ return new GetAccountFollowers(account.id, maxID, count); } + + @Override + public HeaderPaginationRequest onCreateRemoteRequest(String id, String maxID, int count){ + return new GetAccountFollowers(id, maxID, count); + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowingListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowingListFragment.java index 83351e751..e4339e1f1 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowingListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/FollowingListFragment.java @@ -6,6 +6,7 @@ import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.accounts.GetAccountFollowing; import org.joinmastodon.android.model.Account; +import org.joinmastodon.android.model.Status; public class FollowingListFragment extends AccountRelatedAccountListFragment{ @@ -14,9 +15,22 @@ public class FollowingListFragment extends AccountRelatedAccountListFragment{ super.onCreate(savedInstanceState); setSubtitle(getResources().getQuantityString(R.plurals.x_following, (int)(account.followingCount%1000), account.followingCount)); } + @Override + public Account getTargetAccount(){ + return account; + } + @Override + public Status getTargetStatus(){ + return null; + } @Override public HeaderPaginationRequest onCreateRequest(String maxID, int count){ return new GetAccountFollowing(account.id, maxID, count); } + + @Override + public HeaderPaginationRequest onCreateRemoteRequest(String id, String maxID, int count){ + return new GetAccountFollowing(id, maxID, count); + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/PaginatedAccountListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/PaginatedAccountListFragment.java index 5b34019f1..7195981df 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/PaginatedAccountListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/PaginatedAccountListFragment.java @@ -1,33 +1,106 @@ package org.joinmastodon.android.fragments.account_list; +import android.app.Activity; +import android.widget.Toast; + +import org.joinmastodon.android.GlobalUserPreferences; +import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.HeaderPaginationRequest; +import org.joinmastodon.android.api.requests.search.GetSearchResults; import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.HeaderPaginationList; +import org.joinmastodon.android.model.SearchResults; +import org.joinmastodon.android.model.Status; +import org.joinmastodon.android.ui.utils.UiUtils; +import java.util.Optional; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import java.util.stream.Collectors; +import me.grishka.appkit.api.Callback; +import me.grishka.appkit.api.ErrorResponse; import me.grishka.appkit.api.SimpleCallback; public abstract class PaginatedAccountListFragment extends BaseAccountListFragment{ private String nextMaxID; + public abstract Status getTargetStatus(); + + public abstract Account getTargetAccount(); + + public abstract HeaderPaginationRequest onCreateRemoteRequest(String id, String maxID, int count); + public abstract HeaderPaginationRequest onCreateRequest(String maxID, int count); @Override protected void doLoadData(int offset, int count){ - currentRequest=onCreateRequest(offset==0 ? null : nextMaxID, count) - .setCallback(new SimpleCallback<>(this){ - @Override - public void onSuccess(HeaderPaginationList result){ - if(result.nextPageUri!=null) - nextMaxID=result.nextPageUri.getQueryParameter("max_id"); - else - nextMaxID=null; - if (getActivity() == null) return; - onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), nextMaxID!=null); + if(!GlobalUserPreferences.relocatePublishButton){ + currentRequest=onCreateRequest(offset==0 ? null : nextMaxID, count) + .setCallback(new SimpleCallback<>(this){ + @Override + public void onSuccess(HeaderPaginationList result){ + if(result.nextPageUri!=null) + nextMaxID=result.nextPageUri.getQueryParameter("max_id"); + else + nextMaxID=null; + if (getActivity() == null) return; + onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), nextMaxID!=null); + } + }) + .exec(accountID); + } else { + Status targetStatus = getTargetStatus(); + Account targetAccount = getTargetAccount(); + + if(targetAccount != null){ + UiUtils.lookupRemoteAccount(getContext(), targetAccount, accountID, null, account -> { + Pattern pattern = Pattern.compile("(?<=\\/\\/)([^\\/]+)(?=\\/@)"); + Matcher matcher = pattern.matcher(account.url); + String domain = null; + if(matcher.find()){ + domain = matcher.group(1); } - }) - .exec(accountID); + currentRequest=onCreateRemoteRequest(account.id, offset==0 ? null : nextMaxID, count) + .setCallback(new SimpleCallback<>(this) { + @Override + public void onSuccess(HeaderPaginationList result) { + if(result.nextPageUri!=null) + nextMaxID=result.nextPageUri.getQueryParameter("max_id"); + else + nextMaxID=null; + if (getActivity() == null) return; + onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), nextMaxID!=null); + } + }).execNoAuth(domain); + }); + return; + } + + if(targetStatus != null){ + UiUtils.lookupRemoteStatus(getContext(), targetStatus, accountID, null, status -> { + Pattern pattern = Pattern.compile("(?<=\\/\\/)([^\\/]+)(?=\\/@)"); + Matcher matcher = pattern.matcher(status.url); + String domain = null; + if(matcher.find()){ + domain = matcher.group(1); + } + currentRequest=onCreateRemoteRequest(status.id, offset==0 ? null : nextMaxID, count) + .setCallback(new SimpleCallback<>(this) { + @Override + public void onSuccess(HeaderPaginationList result) { + if(result.nextPageUri!=null) + nextMaxID=result.nextPageUri.getQueryParameter("max_id"); + else + nextMaxID=null; + if (getActivity() == null) return; + onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), nextMaxID!=null); + } + }).execNoAuth(domain); + }); + } + } + } @Override diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusFavoritesListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusFavoritesListFragment.java index f62e40ac5..4c7db70a7 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusFavoritesListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusFavoritesListFragment.java @@ -6,6 +6,7 @@ import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.statuses.GetStatusFavorites; import org.joinmastodon.android.model.Account; +import org.joinmastodon.android.model.Status; public class StatusFavoritesListFragment extends StatusRelatedAccountListFragment{ @Override @@ -14,8 +15,22 @@ public class StatusFavoritesListFragment extends StatusRelatedAccountListFragmen setTitle(getResources().getQuantityString(R.plurals.x_favorites, (int)(status.favouritesCount%1000), status.favouritesCount)); } + @Override + public Account getTargetAccount(){ + return null; + } + @Override + public Status getTargetStatus(){ + return status; + } + @Override public HeaderPaginationRequest onCreateRequest(String maxID, int count){ return new GetStatusFavorites(status.id, maxID, count); } + + @Override + public HeaderPaginationRequest onCreateRemoteRequest(String id, String maxID, int count){ + return new GetStatusFavorites(id, maxID, count); + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusReblogsListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusReblogsListFragment.java index 6d494e198..7a11d8667 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusReblogsListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/StatusReblogsListFragment.java @@ -6,6 +6,7 @@ import org.joinmastodon.android.R; import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.statuses.GetStatusReblogs; import org.joinmastodon.android.model.Account; +import org.joinmastodon.android.model.Status; public class StatusReblogsListFragment extends StatusRelatedAccountListFragment{ @Override @@ -14,8 +15,21 @@ public class StatusReblogsListFragment extends StatusRelatedAccountListFragment{ setTitle(getResources().getQuantityString(R.plurals.x_reblogs, (int)(status.reblogsCount%1000), status.reblogsCount)); } + @Override + public Account getTargetAccount(){ + return null; + } + @Override + public Status getTargetStatus(){ + return status; + } @Override public HeaderPaginationRequest onCreateRequest(String maxID, int count){ return new GetStatusReblogs(status.id, maxID, count); } + + @Override + public HeaderPaginationRequest onCreateRemoteRequest(String id, String maxID, int count){ + return new GetStatusReblogs(id, maxID, count); + } }