From f29bd29007fa5d97d6701f41451f498f5bc76498 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 17 Dec 2017 10:33:06 +0100 Subject: [PATCH 1/4] Adds new icons --- .../main/res/drawable-anydpi/ic_notifications_active.xml | 9 +++++++++ .../main/res/drawable-anydpi/ic_notifications_off.xml | 9 +++++++++ 2 files changed, 18 insertions(+) create mode 100644 app/src/main/res/drawable-anydpi/ic_notifications_active.xml create mode 100644 app/src/main/res/drawable-anydpi/ic_notifications_off.xml diff --git a/app/src/main/res/drawable-anydpi/ic_notifications_active.xml b/app/src/main/res/drawable-anydpi/ic_notifications_active.xml new file mode 100644 index 000000000..be9f8368d --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_notifications_active.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-anydpi/ic_notifications_off.xml b/app/src/main/res/drawable-anydpi/ic_notifications_off.xml new file mode 100644 index 000000000..415f55811 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_notifications_off.xml @@ -0,0 +1,9 @@ + + + From ded7cdb51f4f92a83b9d84ef49f1a88b33135c4b Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 17 Dec 2017 11:10:49 +0100 Subject: [PATCH 2/4] Prepares muting notifications parameter --- .../main/java/fr/gouv/etalab/mastodon/client/API.java | 5 +++++ .../gouv/etalab/mastodon/client/Entities/Account.java | 10 +++++++++- .../etalab/mastodon/client/Entities/Relationship.java | 9 +++++++++ .../mastodon/fragments/DisplayAccountsFragment.java | 7 ++++--- 4 files changed, 27 insertions(+), 4 deletions(-) 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 8ba3ecd66..dc6b61bfb 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 @@ -1800,6 +1800,11 @@ public class API { relationship.setFollowed_by(Boolean.valueOf(resobj.get("followed_by").toString())); relationship.setBlocking(Boolean.valueOf(resobj.get("blocking").toString())); relationship.setMuting(Boolean.valueOf(resobj.get("muting").toString())); + try { + relationship.setMuting_notifications(Boolean.valueOf(resobj.get("muting_notifications").toString())); + }catch (Exception ignored){ + relationship.setMuting_notifications(true); + } relationship.setRequested(Boolean.valueOf(resobj.get("requested").toString())); } catch (JSONException e) { setDefaultError(); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Account.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Account.java index e42eeb358..48c6eb1b9 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Account.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Account.java @@ -61,7 +61,7 @@ public class Account implements Parcelable { private followAction followType = followAction.NOTHING; private boolean isMakingAction = false; private Account moved_to_account; - + private boolean muting_notifications; public followAction getFollowType() { return followType; @@ -87,6 +87,14 @@ public class Account implements Parcelable { this.moved_to_account = moved_to_account; } + public boolean isMuting_notifications() { + return muting_notifications; + } + + public void setMuting_notifications(boolean muting_notifications) { + this.muting_notifications = muting_notifications; + } + public enum followAction{ FOLLOW, NOT_FOLLOW, diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Relationship.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Relationship.java index 01c51c74f..cca8878fb 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Relationship.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/Entities/Relationship.java @@ -27,6 +27,7 @@ public class Relationship { private boolean blocking; private boolean muting; private boolean requested; + private boolean muting_notifications; public String getId() { return id; @@ -75,4 +76,12 @@ public class Relationship { public void setRequested(boolean requested) { this.requested = requested; } + + public boolean isMuting_notifications() { + return muting_notifications; + } + + public void setMuting_notifications(boolean muting_notifications) { + this.muting_notifications = muting_notifications; + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java index 98b9ee4fe..a03a912cd 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java @@ -201,7 +201,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou } swipeRefreshLayout.setRefreshing(false); firstLoad = false; - if( type != RetrieveAccountsAsyncTask.Type.BLOCKED && type != RetrieveAccountsAsyncTask.Type.MUTED) + if( type != RetrieveAccountsAsyncTask.Type.BLOCKED ) new RetrieveManyRelationshipsAsyncTask(context, accounts,DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @@ -224,6 +224,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou account.setFollowType(Account.followAction.NOTHING); continue; } + account.setMuting_notifications(relationship.isMuting_notifications()); if( account.getId().equals(relationship.getId())){ if( relationship.isFollowing()) account.setFollowType(Account.followAction.FOLLOW); @@ -231,9 +232,9 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou account.setFollowType(Account.followAction.NOT_FOLLOW); if(relationship.isBlocking()) account.setFollowType(Account.followAction.BLOCK); - else if(relationship.isMuting()) + else if(relationship.isMuting()) { account.setFollowType(Account.followAction.MUTE); - else if(relationship.isRequested()) + }else if(relationship.isRequested()) account.setFollowType(Account.followAction.REQUEST_SENT); break; } From c332923aa79a25ebcb560e173c4487e4896425c4 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 17 Dec 2017 11:43:29 +0100 Subject: [PATCH 3/4] 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 From 4ca6faaa6c83a65b83bd83f5bae354af59081181 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 17 Dec 2017 13:04:17 +0100 Subject: [PATCH 4/4] Post calls to enable/disable notifications for muted --- .../java/fr/gouv/etalab/mastodon/client/API.java | 7 +++++-- .../mastodon/drawers/AccountsListAdapter.java | 13 ++++++++----- .../mastodon/fragments/DisplayAccountsFragment.java | 2 +- 3 files changed, 14 insertions(+), 8 deletions(-) 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 4650de0c9..92511462e 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 @@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.client; import android.content.Context; import android.content.SharedPreferences; + import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -741,15 +742,17 @@ public class API { public int muteNotifications(String targetedId, boolean muteNotifications){ HashMap params = new HashMap<>(); - params.put("muting_notifications", Boolean.toString(muteNotifications)); + params.put("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); + e.printStackTrace(); }catch (Exception e) { setDefaultError(); + e.printStackTrace(); } return actionCode; } @@ -1775,7 +1778,7 @@ public class API { account.setNote(resobj.get("note").toString()); try{ account.setMoved_to_account(parseAccountResponse(context, resobj.getJSONObject("moved"))); - }catch (Exception ignored){ignored.printStackTrace();} + }catch (Exception ignored){account.setMoved_to_account(null);} account.setUrl(resobj.get("url").toString()); account.setAvatar(resobj.get("avatar").toString()); account.setAvatar_static(resobj.get("avatar_static").toString()); 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 7f3dcee2a..47ac6f6d2 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 @@ -24,6 +24,7 @@ import android.support.design.widget.FloatingActionButton; import android.support.v7.widget.RecyclerView; import android.text.Html; import android.text.util.Linkify; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -80,7 +81,7 @@ public class AccountsListAdapter extends RecyclerView.Adapter implements OnPostA } @Override - public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) { + public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) { final AccountsListAdapter.ViewHolder holder = (AccountsListAdapter.ViewHolder) viewHolder; final Account account = accounts.get(position); @@ -114,22 +115,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); + holder.account_follow.setImageResource(R.drawable.ic_volume_up); doAction = API.StatusAction.UNMUTE; holder.account_follow.setVisibility(View.VISIBLE); holder.account_follow_request.setVisibility(View.GONE); - + final int positionFinal = position; 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); + new PostActionAsyncTask(context, API.StatusAction.MUTE_NOTIFICATIONS, account.getId(), account.isMuting_notifications(), AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + accountsListAdapter.notifyItemChanged(positionFinal); } }); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java index a03a912cd..5e67529c8 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java @@ -224,8 +224,8 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou account.setFollowType(Account.followAction.NOTHING); continue; } - account.setMuting_notifications(relationship.isMuting_notifications()); if( account.getId().equals(relationship.getId())){ + account.setMuting_notifications(relationship.isMuting_notifications()); if( relationship.isFollowing()) account.setFollowType(Account.followAction.FOLLOW); else