Check remotely following/followers from profiles

This commit is contained in:
Thomas 2023-03-14 11:24:20 +01:00
parent ba1aa01d46
commit 50f5f15c6f
3 changed files with 106 additions and 27 deletions

View File

@ -52,6 +52,7 @@ import app.fedilab.android.mastodon.helper.Helper;
import app.fedilab.android.mastodon.helper.MastodonHelper; import app.fedilab.android.mastodon.helper.MastodonHelper;
import app.fedilab.android.mastodon.helper.ThemeHelper; import app.fedilab.android.mastodon.helper.ThemeHelper;
import app.fedilab.android.mastodon.viewmodel.mastodon.AccountsVM; import app.fedilab.android.mastodon.viewmodel.mastodon.AccountsVM;
import app.fedilab.android.mastodon.viewmodel.mastodon.SearchVM;
import es.dmoral.toasty.Toasty; import es.dmoral.toasty.Toasty;
@ -59,19 +60,23 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
private final List<Account> accountList; private final List<Account> accountList;
private final boolean home_mute; private final boolean home_mute;
private final String remoteInstance;
private Context context; private Context context;
public AccountAdapter(List<Account> accountList, boolean home_mute) { public AccountAdapter(List<Account> accountList, boolean home_mute, String remoteInstance) {
this.accountList = accountList; this.accountList = accountList;
this.home_mute = home_mute; this.home_mute = home_mute;
this.remoteInstance = remoteInstance;
} }
public AccountAdapter(List<Account> accountList) { public AccountAdapter(List<Account> accountList) {
this.accountList = accountList; this.accountList = accountList;
this.home_mute = false; this.home_mute = false;
this.remoteInstance = null;
} }
public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter<RecyclerView.ViewHolder> adapter, boolean home_mute) { public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter<RecyclerView.ViewHolder> adapter, boolean home_mute, String remoteInstance) {
MastodonHelper.loadPPMastodon(accountViewHolder.binding.avatar, account); MastodonHelper.loadPPMastodon(accountViewHolder.binding.avatar, account);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
@ -97,16 +102,42 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
} else { } else {
accountViewHolder.binding.muteHome.setVisibility(View.GONE); accountViewHolder.binding.muteHome.setVisibility(View.GONE);
} }
if (remoteInstance != null) {
accountViewHolder.binding.muteGroup.setVisibility(View.GONE);
accountViewHolder.binding.followAction.setVisibility(View.GONE);
accountViewHolder.binding.block.setVisibility(View.GONE);
}
accountViewHolder.binding.avatar.setOnClickListener(v -> { accountViewHolder.binding.avatar.setOnClickListener(v -> {
Intent intent = new Intent(context, ProfileActivity.class); if (remoteInstance == null) {
Bundle b = new Bundle(); Intent intent = new Intent(context, ProfileActivity.class);
b.putSerializable(Helper.ARG_ACCOUNT, account); Bundle b = new Bundle();
intent.putExtras(b); b.putSerializable(Helper.ARG_ACCOUNT, account);
ActivityOptionsCompat options = ActivityOptionsCompat intent.putExtras(b);
.makeSceneTransitionAnimation((Activity) context, accountViewHolder.binding.avatar, context.getString(R.string.activity_porfile_pp)); ActivityOptionsCompat options = ActivityOptionsCompat
// start the new activity .makeSceneTransitionAnimation((Activity) context, accountViewHolder.binding.avatar, context.getString(R.string.activity_porfile_pp));
context.startActivity(intent, options.toBundle()); // start the new activity
context.startActivity(intent, options.toBundle());
} else {
Toasty.info(context, context.getString(R.string.retrieve_remote_account), Toasty.LENGTH_SHORT).show();
SearchVM searchVM = new ViewModelProvider((ViewModelStoreOwner) context).get(SearchVM.class);
searchVM.search(remoteInstance, null, account.acct, null, "accounts", null, null, null, null, null, null, null)
.observe((LifecycleOwner) context, results -> {
if (results != null && results.accounts != null && results.accounts.size() > 0) {
Account accountSearch = results.accounts.get(0);
Intent intent = new Intent(context, ProfileActivity.class);
Bundle b = new Bundle();
b.putSerializable(Helper.ARG_ACCOUNT, accountSearch);
intent.putExtras(b);
ActivityOptionsCompat options = ActivityOptionsCompat
.makeSceneTransitionAnimation((Activity) context, accountViewHolder.binding.avatar, context.getString(R.string.activity_porfile_pp));
// start the new activity
context.startActivity(intent, options.toBundle());
} else {
Toasty.info(context, context.getString(R.string.toast_error_search), Toasty.LENGTH_SHORT).show();
}
});
}
}); });
@ -302,7 +333,7 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) { public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
Account account = accountList.get(position); Account account = accountList.get(position);
AccountViewHolder holder = (AccountViewHolder) viewHolder; AccountViewHolder holder = (AccountViewHolder) viewHolder;
accountManagement(context, holder, account, position, this, home_mute); accountManagement(context, holder, account, position, this, home_mute, remoteInstance);
} }

View File

@ -15,6 +15,8 @@ package app.fedilab.android.mastodon.ui.fragment.timeline;
* see <http://www.gnu.org/licenses>. */ * see <http://www.gnu.org/licenses>. */
import static app.fedilab.android.BaseMainActivity.currentInstance;
import static app.fedilab.android.BaseMainActivity.currentToken;
import static app.fedilab.android.mastodon.helper.MastodonHelper.ACCOUNTS_PER_CALL; import static app.fedilab.android.mastodon.helper.MastodonHelper.ACCOUNTS_PER_CALL;
import android.os.Bundle; import android.os.Bundle;
@ -68,6 +70,8 @@ public class FragmentMastodonAccount extends Fragment {
private Timeline.TimeLineEnum timelineType; private Timeline.TimeLineEnum timelineType;
private String order; private String order;
private Boolean local; private Boolean local;
private boolean checkRemotely;
private String instance, token, remoteAccountId;
public View onCreateView(@NonNull LayoutInflater inflater, public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) { ViewGroup container, Bundle savedInstanceState) {
@ -79,7 +83,24 @@ public class FragmentMastodonAccount extends Fragment {
timelineType = (Timeline.TimeLineEnum) getArguments().get(Helper.ARG_TIMELINE_TYPE); timelineType = (Timeline.TimeLineEnum) getArguments().get(Helper.ARG_TIMELINE_TYPE);
order = getArguments().getString(Helper.ARG_DIRECTORY_ORDER, "active"); order = getArguments().getString(Helper.ARG_DIRECTORY_ORDER, "active");
local = getArguments().getBoolean(Helper.ARG_DIRECTORY_LOCAL, false); local = getArguments().getBoolean(Helper.ARG_DIRECTORY_LOCAL, false);
checkRemotely = getArguments().getBoolean(Helper.ARG_CHECK_REMOTELY, false);
} }
instance = currentInstance;
token = currentToken;
if (checkRemotely) {
String[] acctArray = accountTimeline.acct.split("@");
if (acctArray.length > 1) {
instance = acctArray[1];
token = null;
}
if (instance != null && instance.equalsIgnoreCase(currentInstance)) {
checkRemotely = false;
instance = currentInstance;
token = currentToken;
}
}
flagLoading = false; flagLoading = false;
binding = FragmentPaginationBinding.inflate(inflater, container, false); binding = FragmentPaginationBinding.inflate(inflater, container, false);
return binding.getRoot(); return binding.getRoot();
@ -104,26 +125,48 @@ public class FragmentMastodonAccount extends Fragment {
* Router for timelines * Router for timelines
*/ */
private void router(boolean firstLoad) { private void router(boolean firstLoad) {
if (checkRemotely) {
if (remoteAccountId == null) {
SearchVM searchVM = new ViewModelProvider(FragmentMastodonAccount.this).get(viewModelKey, SearchVM.class);
searchVM.search(instance, token, accountTimeline.acct, null, "accounts", null, null, null, null, null, null, null)
.observe(getViewLifecycleOwner(), results -> {
if (results != null && results.accounts.size() > 0) {
remoteAccountId = results.accounts.get(0).id;
fetchAccount(firstLoad, remoteAccountId);
} else {
Toasty.error(requireActivity(), getString(R.string.toast_error), Toasty.LENGTH_SHORT).show();
}
});
} else {
fetchAccount(firstLoad, remoteAccountId);
}
} else {
fetchAccount(firstLoad, accountTimeline.id);
}
}
private void fetchAccount(boolean firstLoad, String accountProfileId) {
if (followType == FedilabProfileTLPageAdapter.follow_type.FOLLOWERS) { if (followType == FedilabProfileTLPageAdapter.follow_type.FOLLOWERS) {
if (firstLoad) { if (firstLoad) {
accountsVM.getAccountFollowers(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null) accountsVM.getAccountFollowers(instance, token, accountProfileId, null, null)
.observe(getViewLifecycleOwner(), this::initializeAccountCommonView); .observe(getViewLifecycleOwner(), this::initializeAccountCommonView);
} else { } else {
accountsVM.getAccountFollowers(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, max_id, null) accountsVM.getAccountFollowers(instance, token, accountProfileId, max_id, null)
.observe(getViewLifecycleOwner(), this::dealWithPagination); .observe(getViewLifecycleOwner(), this::dealWithPagination);
} }
} else if (followType == FedilabProfileTLPageAdapter.follow_type.FOLLOWING) { } else if (followType == FedilabProfileTLPageAdapter.follow_type.FOLLOWING) {
if (firstLoad) { if (firstLoad) {
accountsVM.getAccountFollowing(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null) accountsVM.getAccountFollowing(instance, token, accountProfileId, null, null)
.observe(getViewLifecycleOwner(), this::initializeAccountCommonView); .observe(getViewLifecycleOwner(), this::initializeAccountCommonView);
} else { } else {
accountsVM.getAccountFollowing(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, max_id, null) accountsVM.getAccountFollowing(instance, token, accountProfileId, max_id, null)
.observe(getViewLifecycleOwner(), this::dealWithPagination); .observe(getViewLifecycleOwner(), this::dealWithPagination);
} }
} else if (search != null) { } else if (search != null) {
SearchVM searchVM = new ViewModelProvider(FragmentMastodonAccount.this).get(viewModelKey, SearchVM.class); SearchVM searchVM = new ViewModelProvider(FragmentMastodonAccount.this).get(viewModelKey, SearchVM.class);
if (firstLoad) { if (firstLoad) {
searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, search.trim(), null, "accounts", false, true, false, 0, null, null, MastodonHelper.SEARCH_PER_CALL) searchVM.search(instance, token, search.trim(), null, "accounts", false, true, false, 0, null, null, MastodonHelper.SEARCH_PER_CALL)
.observe(getViewLifecycleOwner(), results -> { .observe(getViewLifecycleOwner(), results -> {
if (results != null) { if (results != null) {
Accounts accounts = new Accounts(); Accounts accounts = new Accounts();
@ -136,7 +179,7 @@ public class FragmentMastodonAccount extends Fragment {
} }
}); });
} else { } else {
searchVM.search(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, search.trim(), null, "accounts", false, true, false, offset, null, null, MastodonHelper.SEARCH_PER_CALL) searchVM.search(instance, token, search.trim(), null, "accounts", false, true, false, offset, null, null, MastodonHelper.SEARCH_PER_CALL)
.observe(getViewLifecycleOwner(), results -> { .observe(getViewLifecycleOwner(), results -> {
if (results != null) { if (results != null) {
Accounts accounts = new Accounts(); Accounts accounts = new Accounts();
@ -149,10 +192,10 @@ public class FragmentMastodonAccount extends Fragment {
} }
} else if (timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE) { } else if (timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE) {
if (firstLoad) { if (firstLoad) {
accountsVM.getMutes(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), null, null) accountsVM.getMutes(instance, token, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), null, null)
.observe(getViewLifecycleOwner(), this::initializeAccountCommonView); .observe(getViewLifecycleOwner(), this::initializeAccountCommonView);
} else { } else {
accountsVM.getMutes(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), max_id, null) accountsVM.getMutes(instance, token, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), max_id, null)
.observe(getViewLifecycleOwner(), this::dealWithPagination); .observe(getViewLifecycleOwner(), this::dealWithPagination);
} }
} else if (timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE_HOME) { } else if (timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE_HOME) {
@ -162,18 +205,18 @@ public class FragmentMastodonAccount extends Fragment {
} }
} else if (timelineType == Timeline.TimeLineEnum.BLOCKED_TIMELINE) { } else if (timelineType == Timeline.TimeLineEnum.BLOCKED_TIMELINE) {
if (firstLoad) { if (firstLoad) {
accountsVM.getBlocks(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), null, null) accountsVM.getBlocks(instance, token, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), null, null)
.observe(getViewLifecycleOwner(), this::initializeAccountCommonView); .observe(getViewLifecycleOwner(), this::initializeAccountCommonView);
} else { } else {
accountsVM.getBlocks(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), max_id, null) accountsVM.getBlocks(instance, token, String.valueOf(MastodonHelper.accountsPerCall(requireActivity())), max_id, null)
.observe(getViewLifecycleOwner(), this::dealWithPagination); .observe(getViewLifecycleOwner(), this::dealWithPagination);
} }
} else if (timelineType == Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) { } else if (timelineType == Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) {
if (firstLoad) { if (firstLoad) {
accountsVM.getDirectory(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, 0, ACCOUNTS_PER_CALL, order, local) accountsVM.getDirectory(instance, token, 0, ACCOUNTS_PER_CALL, order, local)
.observe(getViewLifecycleOwner(), this::initializeAccountCommonView); .observe(getViewLifecycleOwner(), this::initializeAccountCommonView);
} else { } else {
accountsVM.getDirectory(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, offset, ACCOUNTS_PER_CALL, order, local) accountsVM.getDirectory(instance, token, offset, ACCOUNTS_PER_CALL, order, local)
.observe(getViewLifecycleOwner(), this::dealWithPagination); .observe(getViewLifecycleOwner(), this::dealWithPagination);
} }
} }
@ -184,7 +227,7 @@ public class FragmentMastodonAccount extends Fragment {
for (Account account : accounts) { for (Account account : accounts) {
ids.add(account.id); ids.add(account.id);
} }
accountsVM.getRelationships(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, ids) accountsVM.getRelationships(instance, token, ids)
.observe(getViewLifecycleOwner(), relationShips -> { .observe(getViewLifecycleOwner(), relationShips -> {
if (relationShips != null) { if (relationShips != null) {
for (RelationShip relationShip : relationShips) { for (RelationShip relationShip : relationShips) {
@ -252,7 +295,7 @@ public class FragmentMastodonAccount extends Fragment {
} }
this.accounts = accounts.accounts; this.accounts = accounts.accounts;
accountAdapter = new AccountAdapter(this.accounts, timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE_HOME); accountAdapter = new AccountAdapter(this.accounts, timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE_HOME, checkRemotely ? instance : null);
if (search == null && timelineType != Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) { if (search == null && timelineType != Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) {
flagLoading = accounts.pagination.max_id == null; flagLoading = accounts.pagination.max_id == null;
} else if (timelineType != Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) { } else if (timelineType != Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) {
@ -264,7 +307,9 @@ public class FragmentMastodonAccount extends Fragment {
binding.recyclerView.setLayoutManager(mLayoutManager); binding.recyclerView.setLayoutManager(mLayoutManager);
binding.recyclerView.setAdapter(accountAdapter); binding.recyclerView.setAdapter(accountAdapter);
//Fetch the relationship //Fetch the relationship
fetchRelationShip(accounts.accounts, 0); if (!checkRemotely) {
fetchRelationShip(accounts.accounts, 0);
}
max_id = accounts.pagination.max_id; max_id = accounts.pagination.max_id;
binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override @Override
@ -316,7 +361,9 @@ public class FragmentMastodonAccount extends Fragment {
int position = accounts.size(); int position = accounts.size();
accounts.addAll(fetched_accounts.accounts); accounts.addAll(fetched_accounts.accounts);
//Fetch the relationship //Fetch the relationship
fetchRelationShip(fetched_accounts.accounts, position); if (!checkRemotely) {
fetchRelationShip(fetched_accounts.accounts, position);
}
max_id = fetched_accounts.pagination.max_id; max_id = fetched_accounts.pagination.max_id;
if (search != null) { if (search != null) {
offset += MastodonHelper.SEARCH_PER_CALL; offset += MastodonHelper.SEARCH_PER_CALL;

View File

@ -2,4 +2,5 @@
<resources> <resources>
<string name="retrieve_remote_account">Retrieve remote account!</string>
</resources> </resources>