Merge pull request #170 from FineFindus/refactor/remote-followers

Refactor remote follower
This commit is contained in:
LucasGGamerM 2023-04-16 17:32:35 -03:00 committed by GitHub
commit ee8ca09e0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 48 deletions

View File

@ -74,6 +74,8 @@ public abstract class BaseAccountListFragment extends RecyclerFragment<BaseAccou
@Override
protected void onDataLoaded(List<AccountItem> d, boolean more){
if (getActivity() == null)
return;
if(refreshing){
relationships.clear();
}

View File

@ -1,17 +1,18 @@
package org.joinmastodon.android.fragments.account_list;
import android.app.VoiceInteractor;
import android.net.Uri;
import org.joinmastodon.android.GlobalUserPreferences;
import org.joinmastodon.android.api.requests.HeaderPaginationRequest;
import org.joinmastodon.android.api.session.AccountSessionManager;
import org.joinmastodon.android.model.Account;
import org.joinmastodon.android.model.HeaderPaginationList;
import org.joinmastodon.android.ui.utils.UiUtils;
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{
@ -25,60 +26,71 @@ public abstract class PaginatedAccountListFragment extends BaseAccountListFragme
@Override
protected void doLoadData(int offset, int count){
if(GlobalUserPreferences.loadRemoteAccountFollowers){
if ((this instanceof FollowingListFragment || this instanceof FollowerListFragment) && targetAccount != null){
if (shouldLoadRemote()) {
UiUtils.lookupRemoteAccount(getContext(), targetAccount, accountID, null, account -> {
if(account != null){
currentRequest=onCreateRemoteRequest(account.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;
result.stream().forEach(account1 -> {
account1.reloadWhenClicked = true;
});
if (getActivity() == null) return;
onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), false);
}
})
.execNoAuth(targetAccount.getDomain());
loadRemoteFollower(offset, count, account);
} else {
currentRequest=onCreateRequest(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);
}
})
.exec(accountID);
loadFollower(offset, count);
}
});
}
} else {
currentRequest=onCreateRequest(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);
}
})
.exec(accountID);
loadFollower(offset, count);
}
}
private boolean shouldLoadRemote() {
if (!GlobalUserPreferences.loadRemoteAccountFollowers && (this instanceof FollowingListFragment || this instanceof FollowerListFragment)) {
return false;
}
return targetAccount != null && targetAccount.getDomain() != null;
}
void loadFollower(int offset, int count) {
currentRequest=onCreateRequest(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;
onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), nextMaxID!=null);
}
})
.exec(accountID);
}
private void loadRemoteFollower(int offset, int count, Account account) {
String ownDomain = AccountSessionManager.getInstance().getLastActiveAccount().domain;
currentRequest=onCreateRemoteRequest(account.id, offset==0 ? null : nextMaxID, count)
.setCallback(new Callback<>(){
@Override
public void onSuccess(HeaderPaginationList<Account> result){
if(result.nextPageUri!=null)
nextMaxID=result.nextPageUri.getQueryParameter("max_id");
else
nextMaxID=null;
result.stream().forEach(remoteAccount -> {
remoteAccount.reloadWhenClicked = true;
if (remoteAccount.getDomain() == null) {
remoteAccount.acct += "@" + Uri.parse(remoteAccount.url).getHost();
} else if (remoteAccount.getDomain().equals(ownDomain)) {
remoteAccount.acct = remoteAccount.username;
}
});
onDataLoaded(result.stream().map(AccountItem::new).collect(Collectors.toList()), false);
}
@Override
public void onError(ErrorResponse error) {
error.showToast(getContext());
loadFollower(offset, count);
}
})
.execNoAuth(targetAccount.getDomain());
}
@Override
public void onResume(){
super.onResume();