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.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<RecyclerView.ViewHolder
private final List<Account> accountList;
private final boolean home_mute;
private final String remoteInstance;
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.home_mute = home_mute;
this.remoteInstance = remoteInstance;
}
public AccountAdapter(List<Account> 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<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);
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(context);
@ -97,8 +102,14 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
} else {
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 -> {
if (remoteInstance == null) {
Intent intent = new Intent(context, ProfileActivity.class);
Bundle b = new Bundle();
b.putSerializable(Helper.ARG_ACCOUNT, account);
@ -107,6 +118,26 @@ public class AccountAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
.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<RecyclerView.ViewHolder
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
Account account = accountList.get(position);
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>. */
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
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
if (!checkRemotely) {
fetchRelationShip(fetched_accounts.accounts, position);
}
max_id = fetched_accounts.pagination.max_id;
if (search != null) {
offset += MastodonHelper.SEARCH_PER_CALL;

View File

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