From 7e01c841e1d28df6ae0671ac9c33c067b257d2d6 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sun, 3 Feb 2019 12:07:43 +0100 Subject: [PATCH] Start splitting notification Fragment --- .../mastodon/activities/BaseMainActivity.java | 12 +- ...RetrieveMissingNotificationsAsyncTask.java | 7 +- .../RetrieveNotificationsAsyncTask.java | 13 +- .../fr/gouv/etalab/mastodon/client/API.java | 79 ++++--- .../gouv/etalab/mastodon/client/GNUAPI.java | 15 +- .../DisplayNotificationsFragment.java | 27 ++- .../TabLayoutNotificationsFragment.java | 200 ++++++++++++++++++ .../mastodon/jobs/NotificationsSyncJob.java | 3 +- .../drawable-anydpi/ic_follow_notif_tab.xml | 9 + .../drawable-anydpi/ic_mention_notif_tab.xml | 9 + .../drawable-anydpi/ic_repeat_notif_tab.xml | 9 + .../res/drawable-anydpi/ic_star_notif_tab.xml | 9 + app/src/main/res/values/strings.xml | 1 + 13 files changed, 337 insertions(+), 56 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/fragments/TabLayoutNotificationsFragment.java create mode 100644 app/src/main/res/drawable-anydpi/ic_follow_notif_tab.xml create mode 100644 app/src/main/res/drawable-anydpi/ic_mention_notif_tab.xml create mode 100644 app/src/main/res/drawable-anydpi/ic_repeat_notif_tab.xml create mode 100644 app/src/main/res/drawable-anydpi/ic_star_notif_tab.xml diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 617944b1a..5fe224f26 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -134,6 +134,7 @@ import fr.gouv.etalab.mastodon.fragments.DisplayPeertubeNotificationsFragment; import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment; import fr.gouv.etalab.mastodon.fragments.SettingsFragment; import fr.gouv.etalab.mastodon.fragments.SettingsPeertubeFragment; +import fr.gouv.etalab.mastodon.fragments.TabLayoutNotificationsFragment; import fr.gouv.etalab.mastodon.fragments.TabLayoutScheduleFragment; import fr.gouv.etalab.mastodon.fragments.TabLayoutSettingsFragment; import fr.gouv.etalab.mastodon.fragments.WhoToFollowFragment; @@ -703,11 +704,8 @@ public abstract class BaseMainActivity extends BaseActivity displayStatusFragment.scrollToTop(); displayStatusFragment.updateLastReadToot(); } else if( tab.getPosition() == 1) { - DisplayNotificationsFragment notificationsFragment = ((DisplayNotificationsFragment) fragment); countNewNotifications = 0; updateNotifCounter(); - notificationsFragment.scrollToTop(); - }else if (tab.getPosition() > 1) { if (typePosition.containsKey(tab.getPosition())) updateTimeLine(typePosition.get(tab.getPosition()), 0); @@ -761,8 +759,6 @@ public abstract class BaseMainActivity extends BaseActivity displayStatusFragment.scrollToTop(); break; case 1: - DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment); - displayNotificationsFragment.scrollToTop(); break; } } @@ -2486,8 +2482,8 @@ public abstract class BaseMainActivity extends BaseActivity homeFragment.setArguments(bundle); return homeFragment; } else if (position == 1) { - notificationsFragment = new DisplayNotificationsFragment(); - return notificationsFragment; + TabLayoutNotificationsFragment tabLayoutNotificationsFragment = new TabLayoutNotificationsFragment(); + return tabLayoutNotificationsFragment; } else { statusFragment = new DisplayStatusFragment(); bundle.putSerializable("type", typePosition.get(position)); @@ -2589,7 +2585,7 @@ public abstract class BaseMainActivity extends BaseActivity if (position == 0) { homeFragment = (DisplayStatusFragment) createdFragment; } else if (position == 1) { - notificationsFragment = (DisplayNotificationsFragment) createdFragment; + tabLayoutNotificationsFragment = (TabLayoutNotificationsFragment) createdFragment; } else { if (display_local && position == tabPosition.get("local")) localFragment = (DisplayStatusFragment) createdFragment; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java index 05d830fad..8c30ad5cd 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveMissingNotificationsAsyncTask.java @@ -24,6 +24,7 @@ import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.client.API; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Notification; +import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveMissingNotificationsInterface; @@ -39,18 +40,20 @@ public class RetrieveMissingNotificationsAsyncTask extends AsyncTask contextReference; private List notifications; + private DisplayNotificationsFragment.Type type; - public RetrieveMissingNotificationsAsyncTask(Context context, String since_id, OnRetrieveMissingNotificationsInterface onRetrieveMissingNotifications){ + public RetrieveMissingNotificationsAsyncTask(Context context, DisplayNotificationsFragment.Type type, String since_id, OnRetrieveMissingNotificationsInterface onRetrieveMissingNotifications){ this.contextReference = new WeakReference<>(context); this.since_id = since_id; this.listener = onRetrieveMissingNotifications; + this.type = type; } @Override protected Void doInBackground(Void... params) { API api = new API(this.contextReference.get()); - APIResponse apiResponse = api.getNotificationsSince(since_id, 40, false); + APIResponse apiResponse = api.getNotificationsSince(type, since_id, 40, false); since_id = apiResponse.getSince_id(); notifications = apiResponse.getNotifications(); if( notifications != null && notifications.size() > 0) { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveNotificationsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveNotificationsAsyncTask.java index eb7ce9866..6fe398ab0 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveNotificationsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveNotificationsAsyncTask.java @@ -26,6 +26,7 @@ import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Error; import fr.gouv.etalab.mastodon.client.GNUAPI; +import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.interfaces.OnRetrieveNotificationsInterface; @@ -46,14 +47,16 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask private boolean refreshData; private WeakReference contextReference; private boolean display; + private DisplayNotificationsFragment.Type type; - public RetrieveNotificationsAsyncTask(Context context, boolean display, Account account, String max_id, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){ + public RetrieveNotificationsAsyncTask(Context context, DisplayNotificationsFragment.Type type, boolean display, Account account, String max_id, OnRetrieveNotificationsInterface onRetrieveNotificationsInterface){ this.contextReference = new WeakReference<>(context); this.max_id = max_id; this.listener = onRetrieveNotificationsInterface; this.account = account; this.refreshData = true; this.display = display; + this.type = type; } @@ -64,27 +67,27 @@ public class RetrieveNotificationsAsyncTask extends AsyncTask API api; if (account == null) { api = new API(this.contextReference.get()); - apiResponse = api.getNotifications(max_id, display); + apiResponse = api.getNotifications(type, max_id, display); } else { if (this.contextReference.get() == null) { apiResponse.setError(new Error()); return null; } api = new API(this.contextReference.get(), account.getInstance(), account.getToken()); - apiResponse = api.getNotificationsSince(max_id, display); + apiResponse = api.getNotificationsSince(type, max_id, display); } }else{ GNUAPI gnuapi; if (account == null) { gnuapi = new GNUAPI(this.contextReference.get()); - apiResponse = gnuapi.getNotifications(max_id, display); + apiResponse = gnuapi.getNotifications(type, max_id, display); } else { if (this.contextReference.get() == null) { apiResponse.setError(new Error()); return null; } gnuapi = new GNUAPI(this.contextReference.get(), account.getInstance(), account.getToken()); - apiResponse = gnuapi.getNotificationsSince(max_id, display); + apiResponse = gnuapi.getNotificationsSince(type, max_id, display); } } return null; 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 cf92d85c2..e9a1d6017 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 @@ -63,6 +63,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Schedule; import fr.gouv.etalab.mastodon.client.Entities.Status; import fr.gouv.etalab.mastodon.client.Entities.StoredStatus; import fr.gouv.etalab.mastodon.client.Entities.Tag; +import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -2043,8 +2044,8 @@ public class API { * @param since_id String since max * @return APIResponse */ - public APIResponse getNotificationsSince(String since_id, boolean display){ - return getNotifications(null, since_id, notificationPerPage, display); + public APIResponse getNotificationsSince(DisplayNotificationsFragment.Type type, String since_id, boolean display){ + return getNotifications(type, null, since_id, notificationPerPage, display); } /** @@ -2053,8 +2054,8 @@ public class API { * @return APIResponse */ @SuppressWarnings("SameParameterValue") - public APIResponse getNotificationsSince(String since_id, int notificationPerPage, boolean display){ - return getNotifications(null, since_id, notificationPerPage, display); + public APIResponse getNotificationsSince(DisplayNotificationsFragment.Type type, String since_id, int notificationPerPage, boolean display){ + return getNotifications(type, null, since_id, notificationPerPage, display); } /** @@ -2062,8 +2063,8 @@ public class API { * @param max_id String id max * @return APIResponse */ - public APIResponse getNotifications(String max_id, boolean display){ - return getNotifications(max_id, null, notificationPerPage, display); + public APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, boolean display){ + return getNotifications(type, max_id, null, notificationPerPage, display); } @@ -2074,7 +2075,7 @@ public class API { * @param limit int limit - max value 40 * @return APIResponse */ - private APIResponse getNotifications(String max_id, String since_id, int limit, boolean display){ + private APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, String since_id, int limit, boolean display){ HashMap params = new HashMap<>(); if( max_id != null ) @@ -2087,33 +2088,59 @@ public class API { final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); boolean notif_follow, notif_add, notif_mention, notif_share; - if( display) { - notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW_FILTER, true); - notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD_FILTER, true); - notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION_FILTER, true); - notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE_FILTER, true); - }else{ - notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true); - notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true); - notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true); - notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true); - } StringBuilder parameters = new StringBuilder(); + if( type == DisplayNotificationsFragment.Type.ALL){ + if( display) { + notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW_FILTER, true); + notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD_FILTER, true); + notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION_FILTER, true); + notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE_FILTER, true); + }else{ + notif_follow = sharedpreferences.getBoolean(Helper.SET_NOTIF_FOLLOW, true); + notif_add = sharedpreferences.getBoolean(Helper.SET_NOTIF_ADD, true); + notif_mention = sharedpreferences.getBoolean(Helper.SET_NOTIF_MENTION, true); + notif_share = sharedpreferences.getBoolean(Helper.SET_NOTIF_SHARE, true); + } - if( !notif_follow ) + + if( !notif_follow ) + parameters.append("exclude_types[]=").append("follow").append("&"); + if( !notif_add ) + parameters.append("exclude_types[]=").append("favourite").append("&"); + if( !notif_share ) + parameters.append("exclude_types[]=").append("reblog").append("&"); + if( !notif_mention ) + parameters.append("exclude_types[]=").append("mention").append("&"); + if( parameters.length() > 0) { + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16)); + params.put("exclude_types[]", parameters.toString()); + } + }else if(type == DisplayNotificationsFragment.Type.MENTION){ + parameters.append("exclude_types[]=").append("follow").append("&"); + parameters.append("exclude_types[]=").append("favourite").append("&"); + parameters.append("exclude_types[]=").append("reblog").append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16)); + params.put("exclude_types[]", parameters.toString()); + }else if(type == DisplayNotificationsFragment.Type.FAVORITE){ parameters.append("exclude_types[]=").append("follow").append("&"); - if( !notif_add ) - parameters.append("exclude_types[]=").append("favourite").append("&"); - if( !notif_share ) - parameters.append("exclude_types[]=").append("reblog").append("&"); - if( !notif_mention ) parameters.append("exclude_types[]=").append("mention").append("&"); - if( parameters.length() > 0) { + parameters.append("exclude_types[]=").append("reblog").append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16)); + params.put("exclude_types[]", parameters.toString()); + }else if(type == DisplayNotificationsFragment.Type.BOOST){ + parameters.append("exclude_types[]=").append("follow").append("&"); + parameters.append("exclude_types[]=").append("mention").append("&"); + parameters.append("exclude_types[]=").append("favourite").append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16)); + params.put("exclude_types[]", parameters.toString()); + }else if(type == DisplayNotificationsFragment.Type.FOLLOW){ + parameters.append("exclude_types[]=").append("reblog").append("&"); + parameters.append("exclude_types[]=").append("mention").append("&"); + parameters.append("exclude_types[]=").append("favourite").append("&"); parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(16)); params.put("exclude_types[]", parameters.toString()); } - List notifications = new ArrayList<>(); try { diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java index 5c866b913..bffb79525 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/GNUAPI.java @@ -50,6 +50,7 @@ import fr.gouv.etalab.mastodon.client.Entities.Notification; import fr.gouv.etalab.mastodon.client.Entities.Relationship; import fr.gouv.etalab.mastodon.client.Entities.Results; import fr.gouv.etalab.mastodon.client.Entities.Status; +import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -1460,8 +1461,8 @@ public class GNUAPI { * @param since_id String since max * @return APIResponse */ - public APIResponse getNotificationsSince(String since_id, boolean display){ - return getNotifications(null, since_id, notificationPerPage, display); + public APIResponse getNotificationsSince(DisplayNotificationsFragment.Type type, String since_id, boolean display){ + return getNotifications(type, null, since_id, notificationPerPage, display); } /** @@ -1470,8 +1471,8 @@ public class GNUAPI { * @return APIResponse */ @SuppressWarnings("SameParameterValue") - public APIResponse getNotificationsSince(String since_id, int notificationPerPage, boolean display){ - return getNotifications(null, since_id, notificationPerPage, display); + public APIResponse getNotificationsSince(DisplayNotificationsFragment.Type type, String since_id, int notificationPerPage, boolean display){ + return getNotifications(type, null, since_id, notificationPerPage, display); } /** @@ -1479,8 +1480,8 @@ public class GNUAPI { * @param max_id String id max * @return APIResponse */ - public APIResponse getNotifications(String max_id, boolean display){ - return getNotifications(max_id, null, notificationPerPage, display); + public APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, boolean display){ + return getNotifications(type, max_id, null, notificationPerPage, display); } @@ -1491,7 +1492,7 @@ public class GNUAPI { * @param limit int limit - max value 40 * @return APIResponse */ - private APIResponse getNotifications(String max_id, String since_id, int limit, boolean display){ + private APIResponse getNotifications(DisplayNotificationsFragment.Type type, String max_id, String since_id, int limit, boolean display){ HashMap params = new HashMap<>(); if( max_id != null ) diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java index b9a91d47a..0739556a2 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayNotificationsFragment.java @@ -84,6 +84,16 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve public DisplayNotificationsFragment(){ } + public enum Type{ + ALL, + MENTION, + FAVORITE, + BOOST, + FOLLOW + } + + Type type; + @Override public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -96,7 +106,10 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve swiped = false; swipeRefreshLayout = rootView.findViewById(R.id.swipeContainer); sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); - + Bundle bundle = this.getArguments(); + if (bundle != null) { + type = (Type) bundle.get("type"); + } lv_notifications = rootView.findViewById(R.id.lv_notifications); mainLoader = rootView.findViewById(R.id.loader); @@ -122,7 +135,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve if (firstVisibleItem + visibleItemCount == totalItemCount && context != null) { if (!flag_loading) { flag_loading = true; - asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveNotificationsAsyncTask(context, type,true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); nextElementLoader.setVisibility(View.VISIBLE); } } else { @@ -184,7 +197,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve if( notifications != null && notifications.size() > 0 ) sinceId = notifications.get(0).getId(); if( context != null) - asyncTask = new RetrieveMissingNotificationsAsyncTask(context, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveMissingNotificationsAsyncTask(context, type, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }); SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); @@ -210,13 +223,13 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve break; } if( context != null) - asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveNotificationsAsyncTask(context, type,true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); else new Handler(Looper.getMainLooper()).postDelayed(new Runnable() { @Override public void run() { if( context != null) - asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveNotificationsAsyncTask(context, type,true, null, max_id, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } }, 500); return rootView; @@ -316,7 +329,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve * @param sinceId String */ public void retrieveMissingNotifications(String sinceId){ - asyncTask = new RetrieveMissingNotificationsAsyncTask(context, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveMissingNotificationsAsyncTask(context, type, sinceId, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } @Override @@ -348,7 +361,7 @@ public class DisplayNotificationsFragment extends Fragment implements OnRetrieve flag_loading = true; swiped = true; MainActivity.countNewNotifications = 0; - asyncTask = new RetrieveNotificationsAsyncTask(context, true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + asyncTask = new RetrieveNotificationsAsyncTask(context, type,true, null, null, DisplayNotificationsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/TabLayoutNotificationsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/TabLayoutNotificationsFragment.java new file mode 100644 index 000000000..cde227b46 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/TabLayoutNotificationsFragment.java @@ -0,0 +1,200 @@ +package fr.gouv.etalab.mastodon.fragments; +/* 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 . */ +import android.annotation.SuppressLint; +import android.content.Context; +import android.content.SharedPreferences; +import android.graphics.PorterDuff; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.design.widget.TabLayout; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.content.ContextCompat; +import android.support.v4.view.ViewPager; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; + +import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.activities.MainActivity; +import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask; +import fr.gouv.etalab.mastodon.helper.Helper; + +import static fr.gouv.etalab.mastodon.helper.Helper.THEME_BLACK; +import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT; + + +/** + * Created by Thomas on 03/02/2019. + * Tablayout for notifications + */ + +public class TabLayoutNotificationsFragment extends Fragment { + + private Context context; + + @Override + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + + context = getContext(); + View inflatedView = inflater.inflate(R.layout.tablayout_settings, container, false); + + TabLayout tabLayout = inflatedView.findViewById(R.id.tabLayout); + + if(MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU) + tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.all))); + + TabLayout.Tab tabMention = tabLayout.newTab(); + TabLayout.Tab tabFav = tabLayout.newTab(); + TabLayout.Tab tabBoost = tabLayout.newTab(); + TabLayout.Tab tabFollow = tabLayout.newTab(); + + tabMention.setCustomView(R.layout.tab_badge); + tabFav.setCustomView(R.layout.tab_badge); + tabBoost.setCustomView(R.layout.tab_badge); + tabFollow.setCustomView(R.layout.tab_badge); + + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); + + @SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId") + ImageView iconMention = tabMention.getCustomView().findViewById(R.id.tab_icon); + iconMention.setImageResource(R.drawable.ic_mention_notif_tab); + + + @SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId") + ImageView iconFav = tabMention.getCustomView().findViewById(R.id.tab_icon); + iconFav.setImageResource(R.drawable.ic_star_notif_tab); + + @SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId") + ImageView iconBoost = tabMention.getCustomView().findViewById(R.id.tab_icon); + iconBoost.setImageResource(R.drawable.ic_repeat_notif_tab); + + @SuppressWarnings("ConstantConditions") @SuppressLint("CutPasteId") + ImageView iconFollow = tabMention.getCustomView().findViewById(R.id.tab_icon); + iconFollow.setImageResource(R.drawable.ic_follow_notif_tab); + + if (theme == THEME_BLACK) + iconMention.setColorFilter(ContextCompat.getColor(context, R.color.dark_icon), PorterDuff.Mode.SRC_IN); + else + iconMention.setColorFilter(ContextCompat.getColor(context, R.color.mastodonC4), PorterDuff.Mode.SRC_IN); + + if (theme == THEME_LIGHT) { + iconMention.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN); + iconFav.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN); + iconBoost.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN); + iconFollow.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN); + } else { + iconMention.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN); + iconFav.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN); + iconBoost.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN); + iconFollow.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN); + } + + + final ViewPager viewPager = inflatedView.findViewById(R.id.viewpager); + + viewPager.setAdapter(new PagerAdapter + (getChildFragmentManager(), tabLayout.getTabCount())); + viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout)); + tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { + @Override + public void onTabSelected(TabLayout.Tab tab) { + viewPager.setCurrentItem(tab.getPosition()); + } + + @Override + public void onTabUnselected(TabLayout.Tab tab) { + + } + + @Override + public void onTabReselected(TabLayout.Tab tab) { + + } + }); + + return inflatedView; + } + + @Override + public void onAttach(Context context) { + super.onAttach(context); + this.context = context; + } + + /** + * Page Adapter for settings + */ + private class PagerAdapter extends FragmentStatePagerAdapter { + int mNumOfTabs; + + private PagerAdapter(FragmentManager fm, int NumOfTabs) { + super(fm); + this.mNumOfTabs = NumOfTabs; + } + + @Override + public Fragment getItem(int position) { + DisplayNotificationsFragment displayNotificationsFragment = new DisplayNotificationsFragment(); + DisplayNotificationsFragment.Type type = null; + switch (position) { + case 0: + if( MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU ) + type = DisplayNotificationsFragment.Type.ALL; + else + type = DisplayNotificationsFragment.Type.MENTION; + break; + case 1: + if( MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU ) + type = DisplayNotificationsFragment.Type.MENTION; + else + type = DisplayNotificationsFragment.Type.FAVORITE; + break; + case 2: + if( MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU ) + type = DisplayNotificationsFragment.Type.FAVORITE; + else + type = DisplayNotificationsFragment.Type.BOOST; + break; + case 3: + if( MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU ) + type = DisplayNotificationsFragment.Type.BOOST; + else + type = DisplayNotificationsFragment.Type.FOLLOW; + break; + case 4: + type = DisplayNotificationsFragment.Type.FOLLOW; + break; + default: + + break; + } + Bundle bundle = new Bundle(); + bundle.putSerializable("type", type); + displayNotificationsFragment.setArguments(bundle); + return displayNotificationsFragment; + } + + @Override + public int getCount() { + return mNumOfTabs; + } + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java index c59e8053b..b4c1e091a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/jobs/NotificationsSyncJob.java @@ -47,6 +47,7 @@ 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.Notification; +import fr.gouv.etalab.mastodon.fragments.DisplayNotificationsFragment; import fr.gouv.etalab.mastodon.helper.Helper; import fr.gouv.etalab.mastodon.sqlite.AccountDAO; import fr.gouv.etalab.mastodon.sqlite.Sqlite; @@ -133,7 +134,7 @@ public class NotificationsSyncJob extends Job { for (Account account: accounts) { if( account.getSocial() == null || account.getSocial().equals("MASTODON")|| account.getSocial().equals("PLEROMA")) { API api = new API(getContext(), account.getInstance(), account.getToken()); - APIResponse apiResponse = api.getNotificationsSince(null, false); + APIResponse apiResponse = api.getNotificationsSince(DisplayNotificationsFragment.Type.ALL, null, false); onRetrieveNotifications(apiResponse, account); } } diff --git a/app/src/main/res/drawable-anydpi/ic_follow_notif_tab.xml b/app/src/main/res/drawable-anydpi/ic_follow_notif_tab.xml new file mode 100644 index 000000000..225ae0a86 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_follow_notif_tab.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-anydpi/ic_mention_notif_tab.xml b/app/src/main/res/drawable-anydpi/ic_mention_notif_tab.xml new file mode 100644 index 000000000..3eeab8286 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_mention_notif_tab.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-anydpi/ic_repeat_notif_tab.xml b/app/src/main/res/drawable-anydpi/ic_repeat_notif_tab.xml new file mode 100644 index 000000000..e7c67d710 --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_repeat_notif_tab.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable-anydpi/ic_star_notif_tab.xml b/app/src/main/res/drawable-anydpi/ic_star_notif_tab.xml new file mode 100644 index 000000000..a87ca098d --- /dev/null +++ b/app/src/main/res/drawable-anydpi/ic_star_notif_tab.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f2c09951c..86ab8108b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -868,6 +868,7 @@ Markdown Logout account Optimize loading time + All