feat: add remote lookups to the PaginatedAccountListFragment

This commit is contained in:
LucasGGamerM 2023-04-13 16:08:26 -03:00
parent 76a85af0d7
commit 74f2bb4707
5 changed files with 143 additions and 12 deletions

View File

@ -6,6 +6,7 @@ import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.HeaderPaginationRequest;
import org.joinmastodon.android.api.requests.accounts.GetAccountFollowers; import org.joinmastodon.android.api.requests.accounts.GetAccountFollowers;
import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Status;
public class FollowerListFragment extends AccountRelatedAccountListFragment{ 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)); 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 @Override
public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){ public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){
return new GetAccountFollowers(account.id, maxID, count); return new GetAccountFollowers(account.id, maxID, count);
} }
@Override
public HeaderPaginationRequest<Account> onCreateRemoteRequest(String id, String maxID, int count){
return new GetAccountFollowers(id, maxID, count);
}
} }

View File

@ -6,6 +6,7 @@ import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.HeaderPaginationRequest;
import org.joinmastodon.android.api.requests.accounts.GetAccountFollowing; import org.joinmastodon.android.api.requests.accounts.GetAccountFollowing;
import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Status;
public class FollowingListFragment extends AccountRelatedAccountListFragment{ public class FollowingListFragment extends AccountRelatedAccountListFragment{
@ -14,9 +15,22 @@ public class FollowingListFragment extends AccountRelatedAccountListFragment{
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setSubtitle(getResources().getQuantityString(R.plurals.x_following, (int)(account.followingCount%1000), account.followingCount)); 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 @Override
public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){ public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){
return new GetAccountFollowing(account.id, maxID, count); return new GetAccountFollowing(account.id, maxID, count);
} }
@Override
public HeaderPaginationRequest<Account> onCreateRemoteRequest(String id, String maxID, int count){
return new GetAccountFollowing(id, maxID, count);
}
} }

View File

@ -1,33 +1,106 @@
package org.joinmastodon.android.fragments.account_list; 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.HeaderPaginationRequest;
import org.joinmastodon.android.api.requests.search.GetSearchResults;
import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.HeaderPaginationList; 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 java.util.stream.Collectors;
import me.grishka.appkit.api.Callback;
import me.grishka.appkit.api.ErrorResponse;
import me.grishka.appkit.api.SimpleCallback; import me.grishka.appkit.api.SimpleCallback;
public abstract class PaginatedAccountListFragment extends BaseAccountListFragment{ public abstract class PaginatedAccountListFragment extends BaseAccountListFragment{
private String nextMaxID; private String nextMaxID;
public abstract Status getTargetStatus();
public abstract Account getTargetAccount();
public abstract HeaderPaginationRequest<Account> onCreateRemoteRequest(String id, String maxID, int count);
public abstract HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count); public abstract HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count);
@Override @Override
protected void doLoadData(int offset, int count){ protected void doLoadData(int offset, int count){
currentRequest=onCreateRequest(offset==0 ? null : nextMaxID, count) if(!GlobalUserPreferences.relocatePublishButton){
.setCallback(new SimpleCallback<>(this){ currentRequest=onCreateRequest(offset==0 ? null : nextMaxID, count)
@Override .setCallback(new SimpleCallback<>(this){
public void onSuccess(HeaderPaginationList<Account> result){ @Override
if(result.nextPageUri!=null) public void onSuccess(HeaderPaginationList<Account> result){
nextMaxID=result.nextPageUri.getQueryParameter("max_id"); if(result.nextPageUri!=null)
else nextMaxID=result.nextPageUri.getQueryParameter("max_id");
nextMaxID=null; else
if (getActivity() == null) return; nextMaxID=null;
onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), 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);
} }
}) currentRequest=onCreateRemoteRequest(account.id, offset==0 ? null : nextMaxID, count)
.exec(accountID); .setCallback(new SimpleCallback<>(this) {
@Override
public void onSuccess(HeaderPaginationList<Account> 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<Account> 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 @Override

View File

@ -6,6 +6,7 @@ import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.HeaderPaginationRequest;
import org.joinmastodon.android.api.requests.statuses.GetStatusFavorites; import org.joinmastodon.android.api.requests.statuses.GetStatusFavorites;
import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Status;
public class StatusFavoritesListFragment extends StatusRelatedAccountListFragment{ public class StatusFavoritesListFragment extends StatusRelatedAccountListFragment{
@Override @Override
@ -14,8 +15,22 @@ public class StatusFavoritesListFragment extends StatusRelatedAccountListFragmen
setTitle(getResources().getQuantityString(R.plurals.x_favorites, (int)(status.favouritesCount%1000), status.favouritesCount)); 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 @Override
public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){ public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){
return new GetStatusFavorites(status.id, maxID, count); return new GetStatusFavorites(status.id, maxID, count);
} }
@Override
public HeaderPaginationRequest<Account> onCreateRemoteRequest(String id, String maxID, int count){
return new GetStatusFavorites(id, maxID, count);
}
} }

View File

@ -6,6 +6,7 @@ import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.HeaderPaginationRequest; import org.joinmastodon.android.api.requests.HeaderPaginationRequest;
import org.joinmastodon.android.api.requests.statuses.GetStatusReblogs; import org.joinmastodon.android.api.requests.statuses.GetStatusReblogs;
import org.joinmastodon.android.model.Account; import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.Status;
public class StatusReblogsListFragment extends StatusRelatedAccountListFragment{ public class StatusReblogsListFragment extends StatusRelatedAccountListFragment{
@Override @Override
@ -14,8 +15,21 @@ public class StatusReblogsListFragment extends StatusRelatedAccountListFragment{
setTitle(getResources().getQuantityString(R.plurals.x_reblogs, (int)(status.reblogsCount%1000), status.reblogsCount)); 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 @Override
public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){ public HeaderPaginationRequest<Account> onCreateRequest(String maxID, int count){
return new GetStatusReblogs(status.id, maxID, count); return new GetStatusReblogs(status.id, maxID, count);
} }
@Override
public HeaderPaginationRequest<Account> onCreateRemoteRequest(String id, String maxID, int count){
return new GetStatusReblogs(id, maxID, count);
}
} }