From c332923aa79a25ebcb560e173c4487e4896425c4 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 17 Dec 2017 11:43:29 +0100 Subject: [PATCH] Update notifications with post calls --- .../asynctasks/PostActionAsyncTask.java | 10 +++++ .../fr/gouv/etalab/mastodon/client/API.java | 22 +++++++++++ .../mastodon/drawers/AccountsListAdapter.java | 19 ++++++++- .../ic_notifications_active.xml | 2 +- .../drawable-anydpi/ic_notifications_off.xml | 2 +- app/src/main/res/layout/drawer_account.xml | 39 +++++++++++++------ 6 files changed, 79 insertions(+), 15 deletions(-) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java index a1b13d786..8873c8fd0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/PostActionAsyncTask.java @@ -43,6 +43,7 @@ public class PostActionAsyncTask extends AsyncTask { private Account account; private fr.gouv.etalab.mastodon.client.Entities.Status remoteStatus; private WeakReference contextReference; + private boolean muteNotifications; public PostActionAsyncTask(Context context, API.StatusAction apiAction, String targetedId, OnPostActionInterface onPostActionInterface){ this.contextReference = new WeakReference<>(context); @@ -75,6 +76,13 @@ public class PostActionAsyncTask extends AsyncTask { this.comment = comment; this.status = status; } + public PostActionAsyncTask(Context context, API.StatusAction apiAction, String targetedId, boolean muteNotifications, OnPostActionInterface onPostActionInterface){ + this.contextReference = new WeakReference<>(context); + this.listener = onPostActionInterface; + this.apiAction = apiAction; + this.targetedId = targetedId; + this.muteNotifications = muteNotifications; + } @Override protected Void doInBackground(Void... params) { @@ -100,6 +108,8 @@ public class PostActionAsyncTask extends AsyncTask { statusCode = api.reportAction(status, comment); else if (apiAction == API.StatusAction.CREATESTATUS) statusCode = api.statusAction(status); + else if( apiAction == API.StatusAction.MUTE_NOTIFICATIONS) + statusCode = api.muteNotifications(targetedId, muteNotifications); else statusCode = api.postAction(apiAction, targetedId); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index dc6b61bfb..4650de0c9 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -62,6 +62,7 @@ public class API { REBLOG, UNREBLOG, MUTE, + MUTE_NOTIFICATIONS, UNMUTE, BLOCK, UNBLOCK, @@ -731,6 +732,27 @@ public class API { return postAction(statusAction, targetedId, null, null); } + /** + * Makes the post action for a status + * @param targetedId String id of the targeted Id *can be this of a status or an account* + * @param muteNotifications - boolean - notifications should be also muted + * @return in status code - Should be equal to 200 when action is done + */ + public int muteNotifications(String targetedId, boolean muteNotifications){ + + HashMap params = new HashMap<>(); + params.put("muting_notifications", Boolean.toString(muteNotifications)); + try { + HttpsConnection httpsConnection = new HttpsConnection(); + httpsConnection.post(getAbsoluteUrl(String.format("/accounts/%s/mute", targetedId)), 60, params, prefKeyOauthTokenT); + actionCode = httpsConnection.getActionCode(); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + }catch (Exception e) { + setDefaultError(); + } + return actionCode; + } /** * Makes the post action diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsListAdapter.java index 4126498a8..7f3dcee2a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsListAdapter.java @@ -80,7 +80,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA } @Override - public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { + public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) { final AccountsListAdapter.ViewHolder holder = (AccountsListAdapter.ViewHolder) viewHolder; final Account account = accounts.get(position); @@ -114,10 +114,24 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA holder.account_follow.setVisibility(View.VISIBLE); holder.account_follow_request.setVisibility(View.GONE); }else if( account.getFollowType() == Account.followAction.MUTE){ + if(account.isMuting_notifications()) + holder.account_mute_notification.setImageResource(R.drawable.ic_notifications_active); + else + holder.account_mute_notification.setImageResource(R.drawable.ic_notifications_off); + holder.account_mute_notification.setVisibility(View.VISIBLE); holder.account_follow.setImageResource(R.drawable.ic_volume_mute); doAction = API.StatusAction.UNMUTE; holder.account_follow.setVisibility(View.VISIBLE); holder.account_follow_request.setVisibility(View.GONE); + + holder.account_mute_notification.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + account.setMuting_notifications(!account.isMuting_notifications()); + new PostActionAsyncTask(context, API.StatusAction.MUTE_NOTIFICATIONS, targetedId, account.isMuting_notifications(), AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + accountsListAdapter.notifyItemChanged(position); + } + }); } @@ -237,7 +251,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA TextView account_sc; TextView account_fgc; TextView account_frc; - FloatingActionButton account_follow; + FloatingActionButton account_follow, account_mute_notification; TextView account_follow_request; LinearLayout account_container; @@ -252,6 +266,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA account_fgc = itemView.findViewById(R.id.account_fgc); account_frc = itemView.findViewById(R.id.account_frc); account_follow = itemView.findViewById(R.id.account_follow); + account_mute_notification = itemView.findViewById(R.id.account_mute_notification); account_follow_request = itemView.findViewById(R.id.account_follow_request); account_container = itemView.findViewById(R.id.account_container); } diff --git a/app/src/main/res/drawable-anydpi/ic_notifications_active.xml b/app/src/main/res/drawable-anydpi/ic_notifications_active.xml index be9f8368d..2b956d16d 100644 --- a/app/src/main/res/drawable-anydpi/ic_notifications_active.xml +++ b/app/src/main/res/drawable-anydpi/ic_notifications_active.xml @@ -4,6 +4,6 @@ android:viewportWidth="24.0" android:viewportHeight="24.0"> diff --git a/app/src/main/res/drawable-anydpi/ic_notifications_off.xml b/app/src/main/res/drawable-anydpi/ic_notifications_off.xml index 415f55811..b40f82d36 100644 --- a/app/src/main/res/drawable-anydpi/ic_notifications_off.xml +++ b/app/src/main/res/drawable-anydpi/ic_notifications_off.xml @@ -4,6 +4,6 @@ android:viewportWidth="24.0" android:viewportHeight="24.0"> diff --git a/app/src/main/res/layout/drawer_account.xml b/app/src/main/res/layout/drawer_account.xml index 634287b31..851fee0ed 100644 --- a/app/src/main/res/layout/drawer_account.xml +++ b/app/src/main/res/layout/drawer_account.xml @@ -134,15 +134,32 @@ android:layout_gravity="center" android:gravity="center" /> - + + + + + \ No newline at end of file