From 6439090493190a88aef7038ca9f49732a78de5bd Mon Sep 17 00:00:00 2001 From: stom79 Date: Wed, 25 Oct 2017 07:53:31 +0200 Subject: [PATCH] Manages accounts in context --- .../mastodon/activities/TootActivity.java | 46 ++++++++- .../RetrieveAccountsForReplyAsyncTask.java | 95 +++++++++++++++++++ .../drawers/AccountsSearchAdapter.java | 3 +- .../OnRetrieveAccountsReplyInterface.java | 27 ++++++ 4 files changed, 169 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsForReplyAsyncTask.java create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveAccountsReplyInterface.java diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java index cc5ac4846..acff2f44f 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootActivity.java @@ -101,10 +101,13 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import cz.msebera.android.httpclient.Header; +import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.PostStatusAsyncTask; +import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsForReplyAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAccountsAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveSearchAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.UploadActionAsyncTask; +import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Attachment; @@ -121,6 +124,7 @@ import fr.gouv.etalab.mastodon.drawers.TagsListAdapter; import fr.gouv.etalab.mastodon.drawers.TagsSearchAdapter; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnPostStatusActionInterface; +import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsReplyInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAttachmentInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveSearcAccountshInterface; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveSearchInterface; @@ -139,7 +143,7 @@ import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor; * Toot activity class */ -public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface, OnRetrieveSearchInterface { +public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAccountshInterface, OnRetrieveAttachmentInterface, OnPostStatusActionInterface, OnRetrieveSearchInterface, OnRetrieveAccountsReplyInterface { private String visibility; @@ -766,6 +770,12 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc dialog.dismiss(); } }); + alert.setPositiveButton(R.string.accounts, new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int whichButton) { + new RetrieveAccountsForReplyAsyncTask(getApplicationContext(), tootReply, TootActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + dialog.dismiss(); + } + }); alert.show(); return true; case R.id.action_microphone: @@ -1529,4 +1539,38 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc } + @Override + public void onRetrieveAccountsReply(ArrayList mentions) { + ArrayList accounts = new ArrayList<>(); + for(Mention mention: mentions) { + Account account = new Account(); + account.setAcct(mention.getAcct()); + account.setAvatar(mention.getUrl()); + account.setUsername(mention.getUsername()); + } + AlertDialog.Builder builderSingle = new AlertDialog.Builder(TootActivity.this); + builderSingle.setTitle(getString(R.string.choose_accounts)); + final AccountsSearchAdapter accountsSearchAdapter = new AccountsSearchAdapter(getApplicationContext(), accounts, false); + final Account[] accountArray = new Account[accounts.size()]; + int i = 0; + for(Account account: accounts){ + accountArray[i] = account; + i++; + } + builderSingle.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + builderSingle.setAdapter(accountsSearchAdapter, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Account selectedAccount = accountArray[which]; + + dialog.dismiss(); + } + }); + builderSingle.show(); + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsForReplyAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsForReplyAsyncTask.java new file mode 100644 index 000000000..58d28de25 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsForReplyAsyncTask.java @@ -0,0 +1,95 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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. + * + * Mastalab 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 Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.asynctasks; + +import android.content.Context; +import android.os.AsyncTask; + +import java.util.ArrayList; + +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.Entities.Mention; +import fr.gouv.etalab.mastodon.interfaces.OnRetrieveAccountsReplyInterface; + + +/** + * Created by Thomas on 25/10/2017. + * Retrieves accounts which are involved in a conversation + */ + +public class RetrieveAccountsForReplyAsyncTask extends AsyncTask { + + private Context context; + private fr.gouv.etalab.mastodon.client.Entities.Status status; + private OnRetrieveAccountsReplyInterface listener; + private ArrayList mentions; + private ArrayList addedAccounts; + + public RetrieveAccountsForReplyAsyncTask(Context context, fr.gouv.etalab.mastodon.client.Entities.Status status, OnRetrieveAccountsReplyInterface onRetrieveAccountsReplyInterface){ + this.context = context; + this.status = status; + this.listener = onRetrieveAccountsReplyInterface; + } + + @Override + protected Void doInBackground(Void... params) { + API api = new API(context); + fr.gouv.etalab.mastodon.client.Entities.Context statusContext = api.getStatusContext(status.getId()); + mentions = new ArrayList<>(); + addedAccounts = new ArrayList<>(); + //Retrieves the first toot + if( statusContext.getAncestors().size() > 0 ) { + fr.gouv.etalab.mastodon.client.Entities.Status statusFirst = statusContext.getAncestors().get(0); + statusContext = api.getStatusContext(statusContext.getAncestors().get(0).getId()); + } + if( status != null){ + for(Mention mention : status.getMentions()){ + if( canBeAdded(mention.getAcct())){ + mentions.add(mention); + addedAccounts.add(mention.getAcct()); + } + } + } + if( statusContext != null && statusContext.getDescendants().size() > 0){ + for(fr.gouv.etalab.mastodon.client.Entities.Status status : statusContext.getDescendants()) + for(Mention mention : status.getMentions()){ + if( canBeAdded(mention.getAcct())){ + mentions.add(mention); + addedAccounts.add(mention.getAcct()); + } + } + } + if( statusContext != null && statusContext.getAncestors().size() > 0){ + for(fr.gouv.etalab.mastodon.client.Entities.Status status : statusContext.getAncestors()) + for(Mention mention : status.getMentions()){ + if( canBeAdded(mention.getAcct())){ + mentions.add(mention); + addedAccounts.add(mention.getAcct()); + } + } + } + return null; + } + + private boolean canBeAdded(String acct){ + return acct != null && !acct.equals(status.getAccount().getAcct()) && !addedAccounts.contains(acct); + } + + @Override + protected void onPostExecute(Void result) { + listener.onRetrieveAccountsReply(mentions); + } + +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsSearchAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsSearchAdapter.java index 9b85465b1..9dabde5e0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsSearchAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsSearchAdapter.java @@ -36,6 +36,7 @@ import com.nostra13.universalimageloader.core.display.SimpleBitmapDisplayer; import java.util.ArrayList; import java.util.List; import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.Entities.Mention; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -67,7 +68,6 @@ public class AccountsSearchAdapter extends ArrayAdapter implements Filt .cacheOnDisk(true).resetViewBeforeLoading(true).build(); this.owner = false; } - public AccountsSearchAdapter(Context context, List accounts, boolean owner){ super(context, android.R.layout.simple_list_item_1, accounts); this.accounts = accounts; @@ -81,6 +81,7 @@ public class AccountsSearchAdapter extends ArrayAdapter implements Filt this.owner = owner; } + @Override public int getCount() { return accounts.size(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveAccountsReplyInterface.java b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveAccountsReplyInterface.java new file mode 100644 index 000000000..b20f946c1 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/interfaces/OnRetrieveAccountsReplyInterface.java @@ -0,0 +1,27 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * 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. + * + * Mastalab 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 Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.interfaces; + + +import java.util.ArrayList; +import fr.gouv.etalab.mastodon.client.Entities.Mention; + +/** + * Created by Thomas on 25/10/2017. + * Interface when accounts of a conversation have been retrieved + */ +public interface OnRetrieveAccountsReplyInterface { + void onRetrieveAccountsReply(ArrayList mentions); +}