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 8ba3ecd66..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; @@ -62,6 +63,7 @@ public class API { REBLOG, UNREBLOG, MUTE, + MUTE_NOTIFICATIONS, UNMUTE, BLOCK, UNBLOCK, @@ -731,6 +733,29 @@ 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("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; + } /** * Makes the post action @@ -1753,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()); @@ -1800,6 +1825,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/drawers/AccountsListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/AccountsListAdapter.java index 4126498a8..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; @@ -114,10 +115,26 @@ 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){ - holder.account_follow.setImageResource(R.drawable.ic_volume_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_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, account.getId(), account.isMuting_notifications(), AccountsListAdapter.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + accountsListAdapter.notifyItemChanged(positionFinal); + } + }); } @@ -237,7 +254,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 +269,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/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java index 98b9ee4fe..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 @@ -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); } @@ -225,15 +225,16 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou continue; } if( account.getId().equals(relationship.getId())){ + account.setMuting_notifications(relationship.isMuting_notifications()); if( relationship.isFollowing()) account.setFollowType(Account.followAction.FOLLOW); else 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; } 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..2b956d16d --- /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..b40f82d36 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_notifications_off.xml @@ -0,0 +1,9 @@ + + + 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