Cross actions

This commit is contained in:
stom79 2018-08-21 16:20:20 +02:00
parent 071136a7aa
commit 8224a825e6
6 changed files with 67 additions and 52 deletions

View File

@ -774,7 +774,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
account_follow.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
CrossActions.doCrossAction(ShowAccountActivity.this, null, account, API.StatusAction.FOLLOW , null, ShowAccountActivity.this, false);
CrossActions.doCrossAction(ShowAccountActivity.this, null, null, account, API.StatusAction.FOLLOW , null, ShowAccountActivity.this, false);
return false;
}
});

View File

@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.asynctasks;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import java.lang.ref.WeakReference;
import java.util.List;
@ -98,11 +99,13 @@ public class PostActionAsyncTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
Log.v(Helper.TAG,"account: " + account);
//Remote action
if (account != null)
api = new API(contextReference.get(), account.getInstance(), account.getToken());
else
api = new API(contextReference.get());
Log.v(Helper.TAG,"remoteStatus: " + remoteStatus);
if (remoteStatus != null) {
String uri;
if (remoteStatus.getReblog() != null) {
@ -116,7 +119,9 @@ public class PostActionAsyncTask extends AsyncTask<Void, Void, Void> {
else
uri = remoteStatus.getUrl();
}
Log.v(Helper.TAG,"uri: " + uri);
Results search = api.search(uri);
Log.v(Helper.TAG,"search: " + search);
if (search != null) {
List<fr.gouv.etalab.mastodon.client.Entities.Status> remoteStatuses = search.getStatuses();
if (remoteStatuses != null && remoteStatuses.size() > 0) {

View File

@ -526,14 +526,14 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
holder.status_favorite_count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CrossActions.doCrossAction(context, status, null, status.isFavourited()? API.StatusAction.UNFAVOURITE:API.StatusAction.FAVOURITE, notificationsListAdapter, NotificationsListAdapter.this, true);
CrossActions.doCrossAction(context, null, status, null, status.isFavourited()? API.StatusAction.UNFAVOURITE:API.StatusAction.FAVOURITE, notificationsListAdapter, NotificationsListAdapter.this, true);
}
});
holder.status_reblog_count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CrossActions.doCrossAction(context, status, null, status.isReblogged()? API.StatusAction.UNREBLOG:API.StatusAction.REBLOG, notificationsListAdapter, NotificationsListAdapter.this, true);
CrossActions.doCrossAction(context, null, status, null, status.isReblogged()? API.StatusAction.UNREBLOG:API.StatusAction.REBLOG, notificationsListAdapter, NotificationsListAdapter.this, true);
}
});
@ -549,7 +549,7 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
holder.status_favorite_count.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
CrossActions.doCrossAction(context, status, null, status.isFavourited()? API.StatusAction.UNFAVOURITE:API.StatusAction.FAVOURITE, notificationsListAdapter, NotificationsListAdapter.this, false);
CrossActions.doCrossAction(context, null, status, null, status.isFavourited()? API.StatusAction.UNFAVOURITE:API.StatusAction.FAVOURITE, notificationsListAdapter, NotificationsListAdapter.this, false);
return true;
}
});
@ -565,7 +565,7 @@ public class NotificationsListAdapter extends RecyclerView.Adapter implements On
holder.status_reblog_count.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
CrossActions.doCrossAction(context, status, null, status.isReblogged()? API.StatusAction.UNREBLOG:API.StatusAction.REBLOG, notificationsListAdapter, NotificationsListAdapter.this, false);
CrossActions.doCrossAction(context, null, status, null, status.isReblogged()? API.StatusAction.UNREBLOG:API.StatusAction.REBLOG, notificationsListAdapter, NotificationsListAdapter.this, false);
return true;
}
});

View File

@ -1142,20 +1142,20 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
holder.status_favorite_count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CrossActions.doCrossAction(context, status, null, (status.isFavourited()|| (status.getReblog() != null && status.getReblog().isFavourited()))? API.StatusAction.UNFAVOURITE:API.StatusAction.FAVOURITE, statusListAdapter, StatusListAdapter.this, true);
CrossActions.doCrossAction(context, type, status, null, (status.isFavourited()|| (status.getReblog() != null && status.getReblog().isFavourited()))? API.StatusAction.UNFAVOURITE:API.StatusAction.FAVOURITE, statusListAdapter, StatusListAdapter.this, true);
}
});
holder.status_reblog_count.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CrossActions.doCrossAction(context, status, null, (status.isReblogged()|| (status.getReblog() != null && status.getReblog().isReblogged()))? API.StatusAction.UNREBLOG:API.StatusAction.REBLOG, statusListAdapter, StatusListAdapter.this, true);
CrossActions.doCrossAction(context, type, status, null, (status.isReblogged()|| (status.getReblog() != null && status.getReblog().isReblogged()))? API.StatusAction.UNREBLOG:API.StatusAction.REBLOG, statusListAdapter, StatusListAdapter.this, true);
}
});
holder.status_pin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CrossActions.doCrossAction(context, status, null, (status.isPinned()|| (status.getReblog() != null && status.getReblog().isPinned()))? API.StatusAction.UNPIN:API.StatusAction.PIN, statusListAdapter, StatusListAdapter.this, true);
CrossActions.doCrossAction(context, type, status, null, (status.isPinned()|| (status.getReblog() != null && status.getReblog().isPinned()))? API.StatusAction.UNPIN:API.StatusAction.PIN, statusListAdapter, StatusListAdapter.this, true);
}
});
@ -1163,7 +1163,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
holder.status_favorite_count.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
CrossActions.doCrossAction(context, status, null, API.StatusAction.FAVOURITE, statusListAdapter, StatusListAdapter.this, false);
CrossActions.doCrossAction(context, type, status, null, API.StatusAction.FAVOURITE, statusListAdapter, StatusListAdapter.this, false);
return true;
}
});
@ -1171,7 +1171,7 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct
holder.status_reblog_count.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
CrossActions.doCrossAction(context, status, null, API.StatusAction.REBLOG, statusListAdapter, StatusListAdapter.this, false);
CrossActions.doCrossAction(context, type, status, null, API.StatusAction.REBLOG, statusListAdapter, StatusListAdapter.this, false);
return true;
}
});

View File

@ -55,7 +55,6 @@ public class CrossActions {
/**
* Returns the list of connected accounts when cross actions are allowed otherwise, returns the current account
* @param context Context
@ -110,32 +109,37 @@ public class CrossActions {
}
public static void doCrossAction(final Context context, final Status status, final Account targetedAccount, final API.StatusAction doAction, final RecyclerView.Adapter baseAdapter, final OnPostActionInterface onPostActionInterface, boolean limitedToOwner){
public static void doCrossAction(final Context context, RetrieveFeedsAsyncTask.Type type, final Status status, final Account targetedAccount, final API.StatusAction doAction, final RecyclerView.Adapter baseAdapter, final OnPostActionInterface onPostActionInterface, boolean limitedToOwner) {
List<Account> accounts = connectedAccounts(context, status, limitedToOwner);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
boolean undoAction = (doAction == API.StatusAction.UNPIN || doAction == API.StatusAction.UNREBLOG || doAction == API.StatusAction.UNFAVOURITE );
boolean undoAction = (doAction == API.StatusAction.UNPIN || doAction == API.StatusAction.UNREBLOG || doAction == API.StatusAction.UNFAVOURITE);
//Undo actions won't ask for choosing a user
if( accounts.size() == 1 || undoAction ) {
if(type != null && type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE && limitedToOwner){
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account currentAccount = new AccountDAO(context, db).getAccountByID(userId);
finalCrossAction(context, currentAccount, targetedAccount, doAction, status, baseAdapter, onPostActionInterface);
} else if (accounts.size() == 1 || undoAction) {
boolean confirmation = false;
if( doAction == API.StatusAction.UNFAVOURITE || doAction == API.StatusAction.FAVOURITE)
if (doAction == API.StatusAction.UNFAVOURITE || doAction == API.StatusAction.FAVOURITE)
confirmation = sharedpreferences.getBoolean(Helper.SET_NOTIF_VALIDATION_FAV, false);
else if( doAction == API.StatusAction.UNREBLOG || doAction == API.StatusAction.REBLOG )
else if (doAction == API.StatusAction.UNREBLOG || doAction == API.StatusAction.REBLOG)
confirmation = sharedpreferences.getBoolean(Helper.SET_NOTIF_VALIDATION, true);
else if( doAction == API.StatusAction.FOLLOW || doAction == API.StatusAction.UNFOLLOW)
else if (doAction == API.StatusAction.FOLLOW || doAction == API.StatusAction.UNFOLLOW)
confirmation = false;
if (confirmation)
displayConfirmationDialog(context, doAction, status, baseAdapter, onPostActionInterface);
else {
if( doAction == API.StatusAction.REBLOG || doAction == API.StatusAction.UNREBLOG)
if (doAction == API.StatusAction.REBLOG || doAction == API.StatusAction.UNREBLOG)
reblogAction(context, status, baseAdapter, onPostActionInterface);
else if( doAction == API.StatusAction.FAVOURITE || doAction == API.StatusAction.UNFAVOURITE)
else if (doAction == API.StatusAction.FAVOURITE || doAction == API.StatusAction.UNFAVOURITE)
favouriteAction(context, status, baseAdapter, onPostActionInterface);
else if ( doAction == API.StatusAction.PIN || doAction == API.StatusAction.UNPIN)
else if (doAction == API.StatusAction.PIN || doAction == API.StatusAction.UNPIN)
pinAction(context, status, baseAdapter, onPostActionInterface);
}
}else {
} else {
AlertDialog.Builder builderSingle = new AlertDialog.Builder(context);
builderSingle.setTitle(context.getString(R.string.choose_accounts));
final AccountsSearchAdapter accountsSearchAdapter = new AccountsSearchAdapter(context, accounts, true);
@ -155,35 +159,7 @@ public class CrossActions {
@Override
public void onClick(DialogInterface dialog, int which) {
Account selectedAccount = accountArray[which];
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account loggedAccount = new AccountDAO(context, db).getAccountByID(userId);
if( targetedAccount == null){
if(loggedAccount.getInstance().equals(selectedAccount.getInstance())){
new PostActionAsyncTask(context, selectedAccount, doAction, status.getId(), onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else{ //Account is from another instance
new PostActionAsyncTask(context, selectedAccount, status, doAction, onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
if( selectedAccount.getInstance().equals(loggedAccount.getInstance()) && selectedAccount.getId().equals(loggedAccount.getId())) {
if (doAction == API.StatusAction.REBLOG) {
status.setReblogged(true);
} else if (doAction == API.StatusAction.FAVOURITE) {
status.setFavourited(true);
} else if (doAction == API.StatusAction.PIN) {
status.setPinned(true);
}
baseAdapter.notifyDataSetChanged();
}
}else{
new PostActionAsyncTask(context, selectedAccount, targetedAccount, doAction, onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if( selectedAccount.getInstance().equals(loggedAccount.getInstance()) && selectedAccount.getId().equals(loggedAccount.getId())) {
if (doAction == API.StatusAction.FOLLOW) {
targetedAccount.setFollowing(true);
}
baseAdapter.notifyDataSetChanged();
}
}
finalCrossAction(context, selectedAccount, targetedAccount, doAction, status, baseAdapter, onPostActionInterface);
dialog.dismiss();
}
});
@ -191,6 +167,40 @@ public class CrossActions {
}
}
private static void finalCrossAction(Context context, Account selectedAccount, Account targetedAccount, API.StatusAction doAction, Status status, RecyclerView.Adapter baseAdapter, final OnPostActionInterface onPostActionInterface ){
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account loggedAccount = new AccountDAO(context, db).getAccountByID(userId);
new PostActionAsyncTask(context, selectedAccount, status, doAction, onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if( targetedAccount == null){
if(loggedAccount.getInstance().equals(selectedAccount.getInstance())){
new PostActionAsyncTask(context, selectedAccount, doAction, status.getId(), onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}else{ //Account is from another instance
new PostActionAsyncTask(context, selectedAccount, status, doAction, onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
if( selectedAccount.getInstance().equals(loggedAccount.getInstance()) && selectedAccount.getId().equals(loggedAccount.getId())) {
if (doAction == API.StatusAction.REBLOG) {
status.setReblogged(true);
} else if (doAction == API.StatusAction.FAVOURITE) {
status.setFavourited(true);
} else if (doAction == API.StatusAction.PIN) {
status.setPinned(true);
}
baseAdapter.notifyDataSetChanged();
}
}else{
new PostActionAsyncTask(context, selectedAccount, targetedAccount, doAction, onPostActionInterface).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if( selectedAccount.getInstance().equals(loggedAccount.getInstance()) && selectedAccount.getId().equals(loggedAccount.getId())) {
if (doAction == API.StatusAction.FOLLOW) {
targetedAccount.setFollowing(true);
}
baseAdapter.notifyDataSetChanged();
}
}
}
public static void doCrossReply(final Context context, final Status status, final RetrieveFeedsAsyncTask.Type type, boolean limitedToOwner){
List<Account> accounts = connectedAccounts(context, status, limitedToOwner);

View File

@ -4,7 +4,7 @@
android:layout_height="wrap_content">
<AutoCompleteTextView
android:id="@+id/search_instance"
android:inputType="text"
android:inputType="textWebEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/instance"