diff --git a/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/AccountAdapter.java b/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/AccountAdapter.java index 27107b8c3..750472feb 100644 --- a/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/AccountAdapter.java +++ b/app/src/main/java/app/fedilab/android/mastodon/ui/drawer/AccountAdapter.java @@ -52,6 +52,7 @@ import app.fedilab.android.mastodon.helper.Helper; import app.fedilab.android.mastodon.helper.MastodonHelper; import app.fedilab.android.mastodon.helper.ThemeHelper; import app.fedilab.android.mastodon.viewmodel.mastodon.AccountsVM; +import app.fedilab.android.mastodon.viewmodel.mastodon.SearchVM; import es.dmoral.toasty.Toasty; @@ -59,19 +60,23 @@ public class AccountAdapter extends RecyclerView.Adapter accountList; private final boolean home_mute; + + private final String remoteInstance; private Context context; - public AccountAdapter(List accountList, boolean home_mute) { + public AccountAdapter(List accountList, boolean home_mute, String remoteInstance) { this.accountList = accountList; this.home_mute = home_mute; + this.remoteInstance = remoteInstance; } public AccountAdapter(List accountList) { this.accountList = accountList; this.home_mute = false; + this.remoteInstance = null; } - public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter adapter, boolean home_mute) { + public static void accountManagement(Context context, AccountViewHolder accountViewHolder, Account account, int position, RecyclerView.Adapter adapter, boolean home_mute, String remoteInstance) { MastodonHelper.loadPPMastodon(accountViewHolder.binding.avatar, account); SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context); @@ -97,16 +102,42 @@ public class AccountAdapter extends RecyclerView.Adapter { - Intent intent = new Intent(context, ProfileActivity.class); - Bundle b = new Bundle(); - b.putSerializable(Helper.ARG_ACCOUNT, account); - 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()); + if (remoteInstance == null) { + Intent intent = new Intent(context, ProfileActivity.class); + Bundle b = new Bundle(); + b.putSerializable(Helper.ARG_ACCOUNT, account); + 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.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. */ +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 android.os.Bundle; @@ -68,6 +70,8 @@ public class FragmentMastodonAccount extends Fragment { private Timeline.TimeLineEnum timelineType; private String order; private Boolean local; + private boolean checkRemotely; + private String instance, token, remoteAccountId; public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -79,7 +83,24 @@ public class FragmentMastodonAccount extends Fragment { timelineType = (Timeline.TimeLineEnum) getArguments().get(Helper.ARG_TIMELINE_TYPE); order = getArguments().getString(Helper.ARG_DIRECTORY_ORDER, "active"); 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; binding = FragmentPaginationBinding.inflate(inflater, container, false); return binding.getRoot(); @@ -104,26 +125,48 @@ public class FragmentMastodonAccount extends Fragment { * Router for timelines */ 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 (firstLoad) { - accountsVM.getAccountFollowers(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null) + accountsVM.getAccountFollowers(instance, token, accountProfileId, null, null) .observe(getViewLifecycleOwner(), this::initializeAccountCommonView); } else { - accountsVM.getAccountFollowers(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, max_id, null) + accountsVM.getAccountFollowers(instance, token, accountProfileId, max_id, null) .observe(getViewLifecycleOwner(), this::dealWithPagination); } } else if (followType == FedilabProfileTLPageAdapter.follow_type.FOLLOWING) { if (firstLoad) { - accountsVM.getAccountFollowing(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, null, null) + accountsVM.getAccountFollowing(instance, token, accountProfileId, null, null) .observe(getViewLifecycleOwner(), this::initializeAccountCommonView); } else { - accountsVM.getAccountFollowing(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, accountTimeline.id, max_id, null) + accountsVM.getAccountFollowing(instance, token, accountProfileId, max_id, null) .observe(getViewLifecycleOwner(), this::dealWithPagination); } } else if (search != null) { SearchVM searchVM = new ViewModelProvider(FragmentMastodonAccount.this).get(viewModelKey, SearchVM.class); 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 -> { if (results != null) { Accounts accounts = new Accounts(); @@ -136,7 +179,7 @@ public class FragmentMastodonAccount extends Fragment { } }); } 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 -> { if (results != null) { Accounts accounts = new Accounts(); @@ -149,10 +192,10 @@ public class FragmentMastodonAccount extends Fragment { } } else if (timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE) { 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); } 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); } } else if (timelineType == Timeline.TimeLineEnum.MUTED_TIMELINE_HOME) { @@ -162,18 +205,18 @@ public class FragmentMastodonAccount extends Fragment { } } else if (timelineType == Timeline.TimeLineEnum.BLOCKED_TIMELINE) { 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); } 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); } } else if (timelineType == Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) { 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); } 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); } } @@ -184,7 +227,7 @@ public class FragmentMastodonAccount extends Fragment { for (Account account : accounts) { ids.add(account.id); } - accountsVM.getRelationships(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, ids) + accountsVM.getRelationships(instance, token, ids) .observe(getViewLifecycleOwner(), relationShips -> { if (relationShips != null) { for (RelationShip relationShip : relationShips) { @@ -252,7 +295,7 @@ public class FragmentMastodonAccount extends Fragment { } 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) { flagLoading = accounts.pagination.max_id == null; } else if (timelineType != Timeline.TimeLineEnum.ACCOUNT_DIRECTORY) { @@ -264,7 +307,9 @@ public class FragmentMastodonAccount extends Fragment { binding.recyclerView.setLayoutManager(mLayoutManager); binding.recyclerView.setAdapter(accountAdapter); //Fetch the relationship - fetchRelationShip(accounts.accounts, 0); + if (!checkRemotely) { + fetchRelationShip(accounts.accounts, 0); + } max_id = accounts.pagination.max_id; binding.recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { @Override @@ -316,7 +361,9 @@ public class FragmentMastodonAccount extends Fragment { int position = accounts.size(); accounts.addAll(fetched_accounts.accounts); //Fetch the relationship - fetchRelationShip(fetched_accounts.accounts, position); + if (!checkRemotely) { + fetchRelationShip(fetched_accounts.accounts, position); + } max_id = fetched_accounts.pagination.max_id; if (search != null) { offset += MastodonHelper.SEARCH_PER_CALL; diff --git a/app/src/main/res/layouts/mastodon/values/strings.xml b/app/src/main/res/layouts/mastodon/values/strings.xml index 78554f9eb..05dc20005 100644 --- a/app/src/main/res/layouts/mastodon/values/strings.xml +++ b/app/src/main/res/layouts/mastodon/values/strings.xml @@ -2,4 +2,5 @@ + Retrieve remote account! \ No newline at end of file