1
0
mirror of https://framagit.org/tom79/fedilab-tube synced 2025-06-05 21:09:11 +02:00

Retrieve subscriber count + change layout for profiles

This commit is contained in:
Thomas
2020-09-03 18:58:36 +02:00
parent f6ca2e8265
commit 19779c2cf6
8 changed files with 199 additions and 209 deletions

View File

@ -27,12 +27,12 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
@ -40,7 +40,6 @@ import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.bumptech.glide.Glide;
import com.google.android.material.appbar.AppBarLayout;
import com.google.android.material.tabs.TabLayout;
@ -50,6 +49,7 @@ import java.util.ArrayList;
import java.util.List;
import app.fedilab.fedilabtube.asynctasks.PostActionAsyncTask;
import app.fedilab.fedilabtube.asynctasks.RetrieveAccountsAsyncTask;
import app.fedilab.fedilabtube.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.fedilabtube.asynctasks.RetrieveRelationshipAsyncTask;
import app.fedilab.fedilabtube.client.APIResponse;
@ -64,6 +64,7 @@ import app.fedilab.fedilabtube.fragment.DisplayAccountsFragment;
import app.fedilab.fedilabtube.fragment.DisplayStatusFragment;
import app.fedilab.fedilabtube.helper.Helper;
import app.fedilab.fedilabtube.interfaces.OnPostActionInterface;
import app.fedilab.fedilabtube.interfaces.OnRetrieveAccountsInterface;
import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsAccountInterface;
import app.fedilab.fedilabtube.interfaces.OnRetrieveFeedsInterface;
import app.fedilab.fedilabtube.interfaces.OnRetrieveRelationshipInterface;
@ -74,7 +75,7 @@ import static app.fedilab.fedilabtube.helper.Helper.getLiveInstance;
import static app.fedilab.fedilabtube.helper.Helper.isLoggedIn;
public class ShowAccountActivity extends AppCompatActivity implements OnPostActionInterface, OnRetrieveFeedsAccountInterface, OnRetrieveRelationshipInterface, OnRetrieveFeedsInterface {
public class ShowAccountActivity extends AppCompatActivity implements OnPostActionInterface, OnRetrieveFeedsAccountInterface, OnRetrieveRelationshipInterface, OnRetrieveFeedsInterface, OnRetrieveAccountsInterface {
private List<Status> statuses;
@ -82,13 +83,10 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
private Button account_follow;
private ViewPager mPager;
private TabLayout tabLayout;
private TextView account_note;
private TextView account_note, subscriber_count;
private Relationship relationship;
private int maxScrollSize;
private boolean avatarShown = true;
private ImageView account_pp;
private TextView account_dn;
private TextView account_un;
private Account account;
private String accountId;
private boolean ischannel;
@ -102,10 +100,10 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
setTitle("");
Bundle b = getIntent().getExtras();
account_follow = findViewById(R.id.account_follow);
subscriber_count = findViewById(R.id.subscriber_count);
account_follow.setEnabled(false);
account_pp = findViewById(R.id.account_pp);
account_dn = findViewById(R.id.account_dn);
account_un = findViewById(R.id.account_un);
account_pp.setBackgroundResource(R.drawable.account_pp_border);
if (b != null) {
account = b.getParcelable("account");
@ -125,23 +123,29 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
statusDrawerParams.setStatuses(statuses);
statusListAdapter = new StatusListAdapter(statusDrawerParams);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_baseline_arrow_white_24);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
tabLayout = findViewById(R.id.account_tabLayout);
account_note = findViewById(R.id.account_note);
/*header_edit_profile.setOnClickListener(v -> {
Intent intent = new Intent(ShowAccountActivity.this, EditProfileActivity.class);
startActivity(intent);
});*/
ImageView action_back = findViewById(R.id.action_back);
action_back.setOnClickListener(v -> finish());
if (account != null) {
ManageAccount();
new RetrieveAccountsAsyncTask(ShowAccountActivity.this, account.getAcct(), RetrieveAccountsAsyncTask.actionType.CHANNEL, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
private void ManageAccount() {
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, MODE_PRIVATE);
@ -150,17 +154,6 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
retrieveRelationship = new RetrieveRelationshipAsyncTask(ShowAccountActivity.this, accountIdRelation, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
String urlHeader = account.getHeader();
if (urlHeader != null && urlHeader.startsWith("/")) {
urlHeader = "https://" + Helper.getLiveInstance(ShowAccountActivity.this) + account.getHeader();
}
if (urlHeader != null && !urlHeader.contains("missing.png")) {
ImageView banner_pp = findViewById(R.id.banner_pp);
Glide.with(banner_pp.getContext())
.load(urlHeader)
.into(banner_pp);
}
TextView actionbar_title = findViewById(R.id.show_account_title);
if (account.getAcct() != null)
actionbar_title.setText(account.getAcct());
@ -170,39 +163,18 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
}
final AppBarLayout appBar = findViewById(R.id.appBar);
maxScrollSize = appBar.getTotalScrollRange();
appBar.addOnOffsetChangedListener((appBarLayout, verticalOffset) -> {
LinearLayout toolbarContent = findViewById(R.id.toolbar_content);
if (toolbarContent != null) {
if (pp_actionBar != null) {
if (Math.abs(verticalOffset) - appBar.getTotalScrollRange() == 0) {
if (toolbarContent.getVisibility() == View.GONE)
toolbarContent.setVisibility(View.VISIBLE);
if (pp_actionBar.getVisibility() == View.GONE)
pp_actionBar.setVisibility(View.VISIBLE);
} else {
if (toolbarContent.getVisibility() == View.VISIBLE)
toolbarContent.setVisibility(View.GONE);
if (pp_actionBar.getVisibility() == View.VISIBLE)
pp_actionBar.setVisibility(View.GONE);
}
}
if (maxScrollSize == 0)
maxScrollSize = appBarLayout.getTotalScrollRange();
int percentage = (Math.abs(verticalOffset)) * 100 / maxScrollSize;
if (percentage >= 40 && avatarShown) {
avatarShown = false;
account_pp.animate()
.scaleY(0).scaleX(0)
.setDuration(400)
.start();
}
if (percentage <= 40 && !avatarShown) {
avatarShown = true;
account_pp.animate()
.scaleY(1).scaleX(1)
.start();
}
});
mPager = findViewById(R.id.account_viewpager);
if (!ischannel) {
@ -275,27 +247,9 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
});
account_dn.setText(account.getDisplay_name());
if (!ischannel || account.getAcct().split("-").length < 4) {
account_un.setText(String.format("@%s", account.getAcct()));
} else {
account_un.setVisibility(View.GONE);
}
SpannableString spannableString;
if (account.getNote() != null && account.getNote().compareTo("null") != 0 && account.getNote().trim().length() > 0) {
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);
}
manageNotes(account);
Helper.loadGiF(ShowAccountActivity.this, account, account_pp);
//Follow button
String target = account.getAcct();
@ -436,6 +390,32 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi
retrieveRelationship = new RetrieveRelationshipAsyncTask(ShowAccountActivity.this, target, ShowAccountActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
@Override
public void onRetrieveAccounts(APIResponse apiResponse) {
if (apiResponse.getAccounts() != null && apiResponse.getAccounts().size() == 1) {
Account account = apiResponse.getAccounts().get(0);
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);
}
}
public enum action {
FOLLOW,

View File

@ -29,18 +29,25 @@ public class RetrieveAccountsAsyncTask extends AsyncTask<Void, Void, APIResponse
private OnRetrieveAccountsInterface listener;
private WeakReference<Context> contextReference;
private String name;
private actionType type;
public RetrieveAccountsAsyncTask(Context context, String name, OnRetrieveAccountsInterface onRetrieveAccountsInterface) {
public RetrieveAccountsAsyncTask(Context context, String name, actionType type, OnRetrieveAccountsInterface onRetrieveAccountsInterface) {
this.contextReference = new WeakReference<>(context);
this.name = name;
this.listener = onRetrieveAccountsInterface;
this.type = type;
}
@Override
protected APIResponse doInBackground(Void... params) {
PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get());
return peertubeAPI.getPeertubeChannel(name);
if (type == actionType.ACCOUNT) {
return peertubeAPI.getPeertubeChannel(name);
} else if (type == actionType.CHANNEL) {
return peertubeAPI.getPeertubeChannelInfo(name);
} else {
return null;
}
}
@Override
@ -48,4 +55,9 @@ public class RetrieveAccountsAsyncTask extends AsyncTask<Void, Void, APIResponse
listener.onRetrieveAccounts(apiResponse);
}
public enum actionType {
ACCOUNT,
CHANNEL
}
}

View File

@ -1400,6 +1400,32 @@ public class PeertubeAPI {
return apiResponse;
}
/**
* Retrieves Peertube channel info from an account *synchronously*
* Peertube channels are dealt like accounts
*
* @return APIResponse
*/
public APIResponse getPeertubeChannelInfo(String name) {
List<Account> accounts = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
String response = httpsConnection.get(getAbsoluteUrl(String.format("/video-channels/%s", name)), 60, null, null);
JSONObject jsonObject = new JSONObject(response);
Account account = parseAccountResponsePeertube(jsonObject);
accounts.add(account);
} catch (HttpsConnection.HttpsConnectionException e) {
e.printStackTrace();
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) {
e.printStackTrace();
}
apiResponse.setAccounts(accounts);
return apiResponse;
}
/**
* Retrieves Peertube videos from an instance *synchronously*
*

View File

@ -76,7 +76,7 @@ public class PeertubeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
boolean ownVideos = peertube.getAccount().getInstance().compareTo(Helper.getLiveInstance(context)) == 0 && userId != null && peertube.getAccount().getId() != null && peertube.getAccount().getId().compareTo(userId) == 0;
if( peertube.getChannel() == null) {
if (peertube.getChannel() == null) {
holder.peertube_account_name.setText(account.getAcct());
if (account.getAvatar() != null && !account.getAvatar().equals("null") && !account.getAvatar().startsWith("http")) {
account.setAvatar("https://" + peertube.getInstance() + account.getAvatar());
@ -105,9 +105,9 @@ public class PeertubeAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
holder.peertube_profile.setOnClickListener(v -> {
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
if( peertube.getChannel() == null) {
if (peertube.getChannel() == null) {
b.putParcelable("account", peertube.getAccount());
}else{
} else {
b.putParcelable("account", peertube.getChannel());
b.putBoolean("ischannel", true);
}

View File

@ -103,7 +103,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
flag_loading = true;
asyncTask = new RetrieveAccountsAsyncTask(context, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
asyncTask = new RetrieveAccountsAsyncTask(context, name, RetrieveAccountsAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
@ -114,7 +114,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
});
swipeRefreshLayout.setOnRefreshListener(this::pullToRefresh);
asyncTask = new RetrieveAccountsAsyncTask(context, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
asyncTask = new RetrieveAccountsAsyncTask(context, name, RetrieveAccountsAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return rootView;
}
@ -189,7 +189,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
flag_loading = true;
swiped = true;
swipeRefreshLayout.setRefreshing(true);
asyncTask = new RetrieveAccountsAsyncTask(context, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
asyncTask = new RetrieveAccountsAsyncTask(context, name, RetrieveAccountsAsyncTask.actionType.ACCOUNT, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}