TubeLab-App-Android/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java

463 lines
20 KiB
Java
Raw Normal View History

2020-06-27 17:58:13 +02:00
package app.fedilab.fedilabtube;
2020-07-01 16:36:08 +02:00
/* Copyright 2020 Thomas Schneider
*
* This file is a part of TubeLab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* TubeLab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with TubeLab; if not,
* see <http://www.gnu.org/licenses>. */
2020-06-27 17:58:13 +02:00
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
2020-09-03 19:08:53 +02:00
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
2020-06-27 17:58:13 +02:00
import android.view.View;
2020-07-01 17:39:15 +02:00
import android.view.ViewGroup;
import android.widget.Button;
2020-09-03 19:08:53 +02:00
import android.widget.EditText;
2020-06-27 17:58:13 +02:00
import android.widget.ImageView;
2020-09-03 19:08:53 +02:00
import android.widget.LinearLayout;
2020-06-27 17:58:13 +02:00
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
2020-09-08 10:11:11 +02:00
import androidx.lifecycle.ViewModelProvider;
2020-06-27 17:58:13 +02:00
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.tabs.TabLayout;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import app.fedilab.fedilabtube.client.APIResponse;
import app.fedilab.fedilabtube.client.PeertubeAPI;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.client.data.AccountData.Account;
2020-06-27 17:58:13 +02:00
import app.fedilab.fedilabtube.client.entities.Relationship;
import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.fragment.DisplayVideosFragment;
2020-06-27 17:58:13 +02:00
import app.fedilab.fedilabtube.helper.Helper;
2020-09-08 12:08:49 +02:00
import app.fedilab.fedilabtube.viewmodel.AccountsVM;
2020-09-25 18:58:04 +02:00
import app.fedilab.fedilabtube.viewmodel.TimelineVM;
2020-09-08 10:11:11 +02:00
import app.fedilab.fedilabtube.viewmodel.PostActionsVM;
2020-09-08 12:08:49 +02:00
import app.fedilab.fedilabtube.viewmodel.RelationshipVM;
2020-06-27 17:58:13 +02:00
import es.dmoral.toasty.Toasty;
import static androidx.core.text.HtmlCompat.FROM_HTML_MODE_LEGACY;
import static app.fedilab.fedilabtube.helper.Helper.getLiveInstance;
2020-06-27 19:08:52 +02:00
import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn;
2020-06-27 17:58:13 +02:00
2020-09-08 12:08:49 +02:00
public class ShowAccountActivity extends AppCompatActivity {
2020-06-27 17:58:13 +02:00
private Button account_follow;
2020-06-27 17:58:13 +02:00
private ViewPager mPager;
private TabLayout tabLayout;
private TextView account_note, subscriber_count;
2020-06-27 17:58:13 +02:00
private Relationship relationship;
private ImageView account_pp;
private TextView account_dn;
private Account account;
private String accountId;
private boolean ischannel;
private action doAction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_account);
setTitle("");
Bundle b = getIntent().getExtras();
account_follow = findViewById(R.id.account_follow);
subscriber_count = findViewById(R.id.subscriber_count);
2020-06-27 17:58:13 +02:00
account_follow.setEnabled(false);
account_pp = findViewById(R.id.account_pp);
account_dn = findViewById(R.id.account_dn);
account_pp.setBackgroundResource(R.drawable.account_pp_border);
if (b != null) {
account = b.getParcelable("account");
if (account == null) {
accountId = b.getString("accountId");
} else {
accountId = account.getId();
}
ischannel = b.getBoolean("ischannel", false);
} else {
Toasty.error(ShowAccountActivity.this, getString(R.string.toast_error_loading_account), Toast.LENGTH_LONG).show();
}
2020-09-03 19:08:53 +02:00
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
2020-06-27 17:58:13 +02:00
tabLayout = findViewById(R.id.account_tabLayout);
account_note = findViewById(R.id.account_note);
2020-09-08 12:08:49 +02:00
AccountsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(AccountsVM.class);
2020-06-27 17:58:13 +02:00
if (account != null) {
2020-09-03 19:08:53 +02:00
manageAccount();
2020-09-08 15:54:07 +02:00
viewModel.getAccounts(null, account.getAcct(), AccountsVM.accountFetch.SINGLE_CHANNEL).observe(ShowAccountActivity.this, this::manageViewAccounts);
2020-09-03 19:08:53 +02:00
} else {
2020-09-08 12:08:49 +02:00
viewModel.getAccounts(null, accountId, AccountsVM.accountFetch.SINGLE_CHANNEL).observe(ShowAccountActivity.this, this::manageViewAccounts);
2020-06-27 17:58:13 +02:00
}
}
@Override
2020-09-03 19:08:53 +02:00
public boolean onCreateOptionsMenu(@NotNull Menu menu) {
getMenuInflater().inflate(R.menu.main_account, menu);
if (!Helper.isLoggedIn(ShowAccountActivity.this)) {
menu.findItem(R.id.action_mute).setVisible(false);
}
return true;
}
2020-09-03 19:08:53 +02:00
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
} else if (item.getItemId() == R.id.action_mute) {
2020-09-08 10:11:11 +02:00
PostActionsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(PostActionsVM.class);
viewModel.post(PeertubeAPI.StatusAction.MUTE, account.getchannelOwner() != null ? account.getchannelOwner().getAcct() : account.getAcct(), null, null).observe(ShowAccountActivity.this, apiResponse -> manageVIewPostActions(PeertubeAPI.StatusAction.MUTE, apiResponse));
2020-09-03 19:08:53 +02:00
} else if (item.getItemId() == R.id.action_report) {
androidx.appcompat.app.AlertDialog.Builder dialogBuilder = new androidx.appcompat.app.AlertDialog.Builder(ShowAccountActivity.this);
LayoutInflater inflater1 = getLayoutInflater();
View dialogView = inflater1.inflate(R.layout.popup_report, new LinearLayout(ShowAccountActivity.this), false);
dialogBuilder.setView(dialogView);
EditText report_content = dialogView.findViewById(R.id.report_content);
dialogBuilder.setNeutralButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
dialogBuilder.setPositiveButton(R.string.report, (dialog, id) -> {
if (report_content.getText().toString().trim().length() == 0) {
Toasty.info(ShowAccountActivity.this, getString(R.string.report_comment_size), Toasty.LENGTH_LONG).show();
} else {
2020-09-08 10:11:11 +02:00
PostActionsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(PostActionsVM.class);
viewModel.post(PeertubeAPI.StatusAction.REPORT_ACCOUNT, account.getId(), report_content.getText().toString(), null).observe(ShowAccountActivity.this, apiResponse -> manageVIewPostActions(PeertubeAPI.StatusAction.REPORT_ACCOUNT, apiResponse));
2020-09-03 19:08:53 +02:00
dialog.dismiss();
}
});
androidx.appcompat.app.AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();
}
return super.onOptionsItemSelected(item);
}
private void manageAccount() {
2020-06-27 17:58:13 +02:00
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
String accountIdRelation = account.getAcct();
2020-06-27 19:08:52 +02:00
if (isLoggedIn(ShowAccountActivity.this)) {
2020-09-08 12:08:49 +02:00
RelationshipVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(RelationshipVM.class);
viewModel.get(accountIdRelation).observe(ShowAccountActivity.this, this::manageVIewRelationship);
2020-06-27 19:08:52 +02:00
}
2020-06-27 17:58:13 +02:00
2020-09-03 19:08:53 +02:00
setTitle(account.getAcct());
2020-06-27 17:58:13 +02:00
mPager = findViewById(R.id.account_viewpager);
if (!ischannel) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.videos)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.channels)));
mPager.setOffscreenPageLimit(2);
} else {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.videos)));
mPager.setOffscreenPageLimit(1);
}
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
2020-07-03 17:04:04 +02:00
if (ischannel) {
2020-07-01 17:39:15 +02:00
ViewGroup.LayoutParams params = tabLayout.getLayoutParams();
params.height = 0;
tabLayout.setLayoutParams(params);
}
2020-06-27 17:58:13 +02:00
mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
TabLayout.Tab tab = tabLayout.getTabAt(position);
if (tab != null)
tab.select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
mPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
Fragment fragment = null;
if (mPager.getAdapter() != null)
fragment = (Fragment) mPager.getAdapter().instantiateItem(mPager, tab.getPosition());
switch (tab.getPosition()) {
case 0:
if (fragment != null) {
2020-09-25 18:58:04 +02:00
DisplayVideosFragment displayVideosFragment = ((DisplayVideosFragment) fragment);
displayVideosFragment.scrollToTop();
2020-06-27 17:58:13 +02:00
}
break;
case 1:
if (fragment != null) {
DisplayAccountsFragment displayAccountsFragment = ((DisplayAccountsFragment) fragment);
displayAccountsFragment.scrollToTop();
}
break;
}
}
});
account_dn.setText(account.getDisplay_name());
manageNotes(account);
2020-06-27 17:58:13 +02:00
Helper.loadGiF(ShowAccountActivity.this, account, account_pp);
//Follow button
String target = account.getAcct();
2020-07-01 17:39:15 +02:00
2020-06-27 17:58:13 +02:00
account_follow.setOnClickListener(v -> {
if (doAction == action.NOTHING) {
Toasty.info(ShowAccountActivity.this, getString(R.string.nothing_to_do), Toast.LENGTH_LONG).show();
} else if (doAction == action.FOLLOW) {
account_follow.setEnabled(false);
2020-09-08 10:11:11 +02:00
PostActionsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(PostActionsVM.class);
viewModel.post(PeertubeAPI.StatusAction.FOLLOW, target, null, null).observe(ShowAccountActivity.this, apiResponse -> manageVIewPostActions(PeertubeAPI.StatusAction.FOLLOW, apiResponse));
2020-06-27 17:58:13 +02:00
} else if (doAction == action.UNFOLLOW) {
boolean confirm_unfollow = sharedpreferences.getBoolean(Helper.SET_UNFOLLOW_VALIDATION, true);
if (confirm_unfollow) {
AlertDialog.Builder unfollowConfirm = new AlertDialog.Builder(ShowAccountActivity.this);
unfollowConfirm.setTitle(getString(R.string.unfollow_confirm));
unfollowConfirm.setMessage(account.getAcct());
unfollowConfirm.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
unfollowConfirm.setPositiveButton(R.string.yes, (dialog, which) -> {
account_follow.setEnabled(false);
2020-09-08 10:11:11 +02:00
PostActionsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(PostActionsVM.class);
viewModel.post(PeertubeAPI.StatusAction.UNFOLLOW, target, null, null).observe(ShowAccountActivity.this, apiResponse -> manageVIewPostActions(PeertubeAPI.StatusAction.UNFOLLOW, apiResponse));
2020-06-27 17:58:13 +02:00
dialog.dismiss();
});
unfollowConfirm.show();
} else {
account_follow.setEnabled(false);
2020-09-08 10:11:11 +02:00
PostActionsVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(PostActionsVM.class);
viewModel.post(PeertubeAPI.StatusAction.UNFOLLOW, target, null, null).observe(ShowAccountActivity.this, apiResponse -> manageVIewPostActions(PeertubeAPI.StatusAction.UNFOLLOW, apiResponse));
2020-06-27 17:58:13 +02:00
}
}
});
}
2020-09-08 12:08:49 +02:00
public void manageVIewRelationship(APIResponse apiResponse) {
2020-06-27 17:58:13 +02:00
2020-09-08 12:08:49 +02:00
if (apiResponse.getError() != null) {
Toasty.error(ShowAccountActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2020-06-27 17:58:13 +02:00
return;
}
2020-09-08 12:08:49 +02:00
List<Relationship> relationships = apiResponse.getRelationships();
this.relationship = relationships.get(0);
2020-06-27 17:58:13 +02:00
manageButtonVisibility();
invalidateOptionsMenu();
}
//Manages the visibility of the button
private void manageButtonVisibility() {
if (relationship == null)
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int[][] states = new int[][]{
new int[]{android.R.attr.state_enabled}, // enabled
new int[]{-android.R.attr.state_enabled}, // disabled
new int[]{-android.R.attr.state_checked}, // unchecked
new int[]{android.R.attr.state_pressed} // pressed
};
int[] colors = new int[]{
2020-09-14 15:53:31 +02:00
ContextCompat.getColor(ShowAccountActivity.this, Helper.getColorAccent()),
ContextCompat.getColor(ShowAccountActivity.this, Helper.getColorAccent()),
ContextCompat.getColor(ShowAccountActivity.this, Helper.getColorAccent()),
ContextCompat.getColor(ShowAccountActivity.this, Helper.getColorAccent())
2020-06-27 17:58:13 +02:00
};
account_follow.setBackgroundTintList(new ColorStateList(states, colors));
}
account_follow.setEnabled(true);
if (relationship.isFollowing()) {
account_follow.setText(R.string.action_unfollow);
2020-06-27 17:58:13 +02:00
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
account_follow.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(ShowAccountActivity.this, R.color.red_1)));
}
doAction = action.UNFOLLOW;
2020-07-03 17:04:04 +02:00
if (ischannel) {
2020-07-01 17:39:15 +02:00
account_follow.setVisibility(View.VISIBLE);
2020-07-03 17:04:04 +02:00
} else {
2020-07-01 17:39:15 +02:00
account_follow.setVisibility(View.GONE);
}
2020-06-27 17:58:13 +02:00
} else if (!relationship.isFollowing()) {
account_follow.setText(R.string.action_follow);
2020-06-27 17:58:13 +02:00
doAction = action.FOLLOW;
2020-07-03 17:04:04 +02:00
if (ischannel) {
2020-07-01 17:39:15 +02:00
account_follow.setVisibility(View.VISIBLE);
2020-07-03 17:04:04 +02:00
} else {
2020-07-01 17:39:15 +02:00
account_follow.setVisibility(View.GONE);
}
2020-06-27 17:58:13 +02:00
} else {
account_follow.setVisibility(View.GONE);
doAction = action.NOTHING;
}
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onStop() {
super.onStop();
}
2020-09-08 10:11:11 +02:00
public void manageVIewPostActions(PeertubeAPI.StatusAction statusAction, APIResponse apiResponse) {
if (apiResponse.getError() != null) {
Toasty.error(ShowAccountActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2020-06-27 17:58:13 +02:00
return;
}
String target = account.getAcct();
//IF action is unfollow or mute, sends an intent to remove statuses
2020-06-27 19:08:52 +02:00
if (statusAction == PeertubeAPI.StatusAction.UNFOLLOW) {
2020-06-27 17:58:13 +02:00
Bundle b = new Bundle();
2020-09-08 10:11:11 +02:00
b.putString("receive_action", apiResponse.getTargetedId());
2020-06-27 17:58:13 +02:00
Intent intentBC = new Intent(Helper.RECEIVE_ACTION);
intentBC.putExtras(b);
}
2020-09-03 19:08:53 +02:00
if (statusAction == PeertubeAPI.StatusAction.UNFOLLOW || statusAction == PeertubeAPI.StatusAction.FOLLOW) {
2020-09-08 12:08:49 +02:00
RelationshipVM viewModel = new ViewModelProvider(ShowAccountActivity.this).get(RelationshipVM.class);
viewModel.get(target).observe(ShowAccountActivity.this, this::manageVIewRelationship);
2020-09-03 19:08:53 +02:00
} else if (statusAction == PeertubeAPI.StatusAction.MUTE) {
Toasty.info(ShowAccountActivity.this, getString(R.string.muted_done), Toast.LENGTH_LONG).show();
}
2020-06-27 17:58:13 +02:00
}
2020-09-08 12:08:49 +02:00
public void manageViewAccounts(APIResponse apiResponse) {
if (apiResponse.getAccounts() != null && apiResponse.getAccounts().size() == 1) {
Account account = apiResponse.getAccounts().get(0);
2020-09-03 19:08:53 +02:00
if (this.account == null) {
this.account = account;
manageAccount();
}
if (account.getchannelOwner() != null) {
this.account.setchannelOwner(account.getchannelOwner());
}
subscriber_count.setText(getString(R.string.followers_count, Helper.withSuffix(account.getFollowers_count())));
subscriber_count.setVisibility(View.VISIBLE);
manageNotes(account);
}
}
private void manageNotes(Account account) {
if (account.getNote() != null && account.getNote().compareTo("null") != 0 && account.getNote().trim().length() > 0) {
SpannableString spannableString;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
spannableString = new SpannableString(Html.fromHtml(account.getNote(), FROM_HTML_MODE_LEGACY));
else
spannableString = new SpannableString(Html.fromHtml(account.getNote()));
account.setNoteSpan(spannableString);
account_note.setText(account.getNoteSpan(), TextView.BufferType.SPANNABLE);
account_note.setMovementMethod(LinkMovementMethod.getInstance());
account_note.setVisibility(View.VISIBLE);
} else {
account_note.setVisibility(View.GONE);
}
}
2020-06-27 17:58:13 +02:00
public enum action {
FOLLOW,
UNFOLLOW,
NOTHING
}
/**
2020-09-03 19:08:53 +02:00
* Pager adapter for the 2 fragments
2020-06-27 17:58:13 +02:00
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NotNull
@Override
public Fragment getItem(int position) {
Bundle bundle = new Bundle();
if (position == 0) {
2020-09-25 18:58:04 +02:00
DisplayVideosFragment displayVideosFragment = new DisplayVideosFragment();
2020-06-27 17:58:13 +02:00
bundle = new Bundle();
2020-09-25 18:58:04 +02:00
bundle.putSerializable("type", TimelineVM.Type.USER);
2020-06-27 17:58:13 +02:00
bundle.putString("targetedid", account.getAcct());
bundle.putBoolean("ischannel", ischannel);
2020-09-25 18:58:04 +02:00
displayVideosFragment.setArguments(bundle);
return displayVideosFragment;
2020-06-27 17:58:13 +02:00
}
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
bundle.putString("targetedid", account.getId());
bundle.putString("instance", getLiveInstance(ShowAccountActivity.this));
bundle.putString("name", account.getAcct());
displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment;
}
@Override
public int getCount() {
if (ischannel)
return 1;
else
return 2;
}
}
}