Follow Peertube channel

This commit is contained in:
stom79 2018-10-22 17:37:29 +02:00
parent f605fdda95
commit bfe66fd93c
2 changed files with 58 additions and 12 deletions

View File

@ -20,6 +20,7 @@ import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
@ -40,6 +41,7 @@ import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask;
import fr.gouv.etalab.mastodon.client.API;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.client.Entities.Error;
import fr.gouv.etalab.mastodon.helper.CrossActions;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnPostActionInterface;
import fr.gouv.etalab.mastodon.activities.ShowAccountActivity;
@ -74,13 +76,14 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new ViewHolder(layoutInflater.inflate(R.layout.drawer_account, parent, false));
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
final AccountsListAdapter.ViewHolder holder = (AccountsListAdapter.ViewHolder) viewHolder;
final Account account = accounts.get(position);
@ -90,6 +93,9 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA
else if( action == RetrieveAccountsAsyncTask.Type.MUTED)
account.setFollowType(Account.followAction.MUTE);
if( action == RetrieveAccountsAsyncTask.Type.CHANNELS)
account.setFollowType(Account.followAction.NOT_FOLLOW);
if (account.getFollowType() == Account.followAction.NOTHING){
holder.account_follow.hide();
holder.account_follow_request.setVisibility(View.GONE);
@ -173,22 +179,30 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA
holder.account_follow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if( doAction != null) {
account.setMakingAction(true);
new PostActionAsyncTask(context, doAction, account.getId(), AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if( action != RetrieveAccountsAsyncTask.Type.CHANNELS) {
if (doAction != null) {
account.setMakingAction(true);
new PostActionAsyncTask(context, doAction, account.getId(), AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}else {
CrossActions.followPeertubeChannel(context, account, AccountsListAdapter.this);
}
}
});
holder.account_pp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Avoid to reopen details about the current account
if( targetedId == null || !targetedId.equals(account.getId())){
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putString("accountId", account.getId());
intent.putExtras(b);
context.startActivity(intent);
if(action != RetrieveAccountsAsyncTask.Type.CHANNELS) {
//Avoid to reopen details about the current account
if (targetedId == null || !targetedId.equals(account.getId())) {
Intent intent = new Intent(context, ShowAccountActivity.class);
Bundle b = new Bundle();
b.putString("accountId", account.getId());
intent.putExtras(b);
context.startActivity(intent);
}
}else {
CrossActions.doCrossProfile(context, account);
}
}

View File

@ -26,6 +26,7 @@ import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.util.Log;
import android.widget.Toast;
import java.lang.ref.WeakReference;
@ -199,6 +200,37 @@ public class CrossActions {
}
public static void followPeertubeChannel(final Context context, Account remoteAccount, OnPostActionInterface onPostActionInterface){
new AsyncTask<Void, Void, Void>() {
private WeakReference<Context> contextReference = new WeakReference<>(context);
Results response;
@Override
protected void onPreExecute() {
Toast.makeText(contextReference.get(), R.string.retrieve_remote_account, Toast.LENGTH_SHORT).show();
}
@Override
protected Void doInBackground(Void... voids) {
API api = new API(contextReference.get());
String url;
url = "https://" + remoteAccount.getHost() + "/video-channels/" + remoteAccount.getAcct().split("@")[0];
response = api.search(url);
return null;
}
@Override
protected void onPostExecute(Void result) {
if( response == null){
return;
}
List<Account> remoteAccounts = response.getAccounts();
if( remoteAccounts != null && remoteAccounts.size() > 0) {
new PostActionAsyncTask(context, null, remoteAccounts.get(0), API.StatusAction.FOLLOW, onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR );
}
public static void doCrossProfile(final Context context, Account remoteAccount){
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);