diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/BaseAccountListFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/BaseAccountListFragment.java index bf8dd4c5b..357540ab5 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/BaseAccountListFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/account_list/BaseAccountListFragment.java @@ -74,6 +74,8 @@ public abstract class BaseAccountListFragment extends RecyclerFragment d, boolean more){ + if (getActivity() == null) + return; if(refreshing){ relationships.clear(); } 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 1aacb4bb4..66566fc87 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,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 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 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 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 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 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();