fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/TabLayoutNotificationsFragm...

290 lines
13 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2019-02-03 12:07:43 +01:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2019-02-03 12:07:43 +01:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2019-02-03 12:07:43 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2019-02-03 12:07:43 +01:00
* see <http://www.gnu.org/licenses>. */
2019-02-03 15:00:27 +01:00
2019-02-03 12:07:43 +01:00
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.PorterDuff;
import android.os.Bundle;
2019-11-15 16:32:25 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
2019-09-06 17:55:14 +02:00
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
2019-11-15 16:32:25 +01:00
import androidx.core.content.ContextCompat;
2019-06-11 19:38:26 +02:00
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
2019-09-06 17:55:14 +02:00
2019-11-15 16:32:25 +01:00
import com.google.android.material.tabs.TabLayout;
2019-02-03 12:07:43 +01:00
import org.jetbrains.annotations.NotNull;
2019-11-13 12:54:01 +01:00
import java.util.Objects;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.activities.MainActivity;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.SwipeControledViewPager;
2019-02-03 12:07:43 +01:00
/**
* Created by Thomas on 03/02/2019.
* Tablayout for notifications
*/
public class TabLayoutNotificationsFragment extends Fragment {
private Context context;
private SwipeControledViewPager viewPager;
2019-02-03 12:07:43 +01:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
context = getContext();
2019-02-03 15:00:27 +01:00
View inflatedView = inflater.inflate(R.layout.tablayout_notifications, container, false);
2019-02-03 12:07:43 +01:00
TabLayout tabLayout = inflatedView.findViewById(R.id.tabLayout);
2019-11-13 12:54:01 +01:00
tabLayout.setBackgroundColor(ContextCompat.getColor(Objects.requireNonNull(getContext()), R.color.cyanea_primary));
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA)
2019-02-03 12:07:43 +01:00
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 tabStatus = tabLayout.newTab();
2019-04-19 16:03:54 +02:00
TabLayout.Tab tabPoll = tabLayout.newTab();
2019-02-03 12:07:43 +01:00
TabLayout.Tab tabFollow = tabLayout.newTab();
tabMention.setCustomView(R.layout.tab_badge);
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA)
2019-02-03 15:00:27 +01:00
tabFav.setCustomView(R.layout.tab_badge);
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA)
2019-02-08 15:07:46 +01:00
tabBoost.setCustomView(R.layout.tab_badge);
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON) {
2019-04-19 16:03:54 +02:00
tabPoll.setCustomView(R.layout.tab_badge);
tabStatus.setCustomView(R.layout.tab_badge);
}
2019-02-03 12:07:43 +01:00
tabFollow.setCustomView(R.layout.tab_badge);
2019-02-03 15:00:27 +01:00
2019-02-03 12:07:43 +01:00
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);
2019-09-06 17:55:14 +02:00
ImageView iconFav = null;
if (tabFav.getCustomView() != null) {
2019-02-03 15:00:27 +01:00
iconFav = tabFav.getCustomView().findViewById(R.id.tab_icon);
iconFav.setImageResource(R.drawable.ic_star_notif_tab);
}
2019-02-03 12:07:43 +01:00
2019-09-06 17:55:14 +02:00
ImageView iconBoost = null;
if (tabBoost.getCustomView() != null) {
2019-02-08 15:07:46 +01:00
iconBoost = tabBoost.getCustomView().findViewById(R.id.tab_icon);
iconBoost.setImageResource(R.drawable.ic_repeat_notif_tab);
}
2019-04-19 16:03:54 +02:00
ImageView iconStatus = null;
if (tabStatus.getCustomView() != null) {
iconStatus = tabStatus.getCustomView().findViewById(R.id.tab_icon);
iconStatus.setImageResource(R.drawable.ic_baseline_home_notif);
}
2019-09-06 17:55:14 +02:00
ImageView iconPoll = null;
if (tabPoll.getCustomView() != null) {
2019-04-19 16:03:54 +02:00
iconPoll = tabPoll.getCustomView().findViewById(R.id.tab_icon);
iconPoll.setImageResource(R.drawable.ic_view_list_poll_notif);
}
@SuppressLint("CutPasteId")
2019-02-03 15:00:27 +01:00
ImageView iconFollow = tabFollow.getCustomView().findViewById(R.id.tab_icon);
2019-02-03 12:07:43 +01:00
iconFollow.setImageResource(R.drawable.ic_follow_notif_tab);
2019-02-03 15:00:27 +01:00
tabLayout.addTab(tabMention);
2019-09-06 17:55:14 +02:00
if (tabFav.getCustomView() != null)
2019-02-03 15:00:27 +01:00
tabLayout.addTab(tabFav);
2019-09-06 17:55:14 +02:00
if (tabBoost.getCustomView() != null)
2019-02-08 15:07:46 +01:00
tabLayout.addTab(tabBoost);
2019-09-06 17:55:14 +02:00
if (tabPoll.getCustomView() != null)
2019-04-19 16:03:54 +02:00
tabLayout.addTab(tabPoll);
if (tabStatus.getCustomView() != null)
tabLayout.addTab(tabStatus);
2019-02-03 15:00:27 +01:00
tabLayout.addTab(tabFollow);
2019-05-18 11:10:30 +02:00
if (theme == Helper.THEME_BLACK)
2019-02-03 12:07:43 +01:00
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);
2019-05-18 11:10:30 +02:00
if (theme == Helper.THEME_LIGHT) {
2019-02-03 12:07:43 +01:00
iconMention.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN);
2019-09-06 17:55:14 +02:00
if (iconFav != null)
2019-02-03 15:00:27 +01:00
iconFav.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN);
2019-09-06 17:55:14 +02:00
if (iconBoost != null)
2019-02-08 15:07:46 +01:00
iconBoost.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN);
2019-09-06 17:55:14 +02:00
if (iconPoll != null)
2019-04-19 16:03:54 +02:00
iconPoll.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN);
if (iconStatus != null)
iconStatus.setColorFilter(ContextCompat.getColor(context, R.color.action_light_header), PorterDuff.Mode.SRC_IN);
2019-02-03 12:07:43 +01:00
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);
2019-09-06 17:55:14 +02:00
if (iconFav != null)
2019-02-03 15:00:27 +01:00
iconFav.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN);
2019-09-06 17:55:14 +02:00
if (iconBoost != null)
2019-02-08 15:07:46 +01:00
iconBoost.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN);
2019-09-06 17:55:14 +02:00
if (iconPoll != null)
2019-04-19 16:03:54 +02:00
iconPoll.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN);
if (iconStatus != null)
iconStatus.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN);
2019-02-03 12:07:43 +01:00
iconFollow.setColorFilter(ContextCompat.getColor(context, R.color.dark_text), PorterDuff.Mode.SRC_IN);
}
2019-02-03 15:00:27 +01:00
viewPager = inflatedView.findViewById(R.id.viewpager);
2019-02-03 12:07:43 +01:00
viewPager.setEnableSwipe(false);
viewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), tabLayout.getTabCount()));
2019-02-03 12:07:43 +01:00
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
2019-09-06 17:55:14 +02:00
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
2019-02-03 12:07:43 +01:00
@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) {
2019-09-06 17:55:14 +02:00
if (viewPager.getAdapter() != null) {
Fragment fragment = (Fragment) viewPager.getAdapter().instantiateItem(viewPager, tab.getPosition());
DisplayNotificationsFragment displayNotificationsFragment = ((DisplayNotificationsFragment) fragment);
displayNotificationsFragment.scrollToTop();
}
2019-02-03 12:07:43 +01:00
}
});
return inflatedView;
}
2019-09-06 17:55:14 +02:00
public ViewPager getViewPager() {
return viewPager;
}
2019-02-03 12:07:43 +01:00
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
2019-11-15 16:32:25 +01:00
public void refreshAll() {
if (viewPager == null)
return;
FragmentStatePagerAdapter a = (FragmentStatePagerAdapter) viewPager.getAdapter();
if (a != null) {
DisplayNotificationsFragment notifAll = (DisplayNotificationsFragment) a.instantiateItem(viewPager, 0);
notifAll.refreshAll();
}
}
public void retrieveMissingNotifications(String sinceId) {
FragmentStatePagerAdapter a = (FragmentStatePagerAdapter) viewPager.getAdapter();
if (a != null) {
DisplayNotificationsFragment notifAll = (DisplayNotificationsFragment) a.instantiateItem(viewPager, 0);
notifAll.retrieveMissingNotifications(sinceId);
}
}
2019-02-03 12:07:43 +01:00
/**
* Page Adapter for settings
*/
private static class PagerAdapter extends FragmentStatePagerAdapter {
2019-02-03 12:07:43 +01:00
int mNumOfTabs;
private PagerAdapter(FragmentManager fm, int NumOfTabs) {
2020-03-08 10:29:06 +01:00
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
2019-02-03 12:07:43 +01:00
this.mNumOfTabs = NumOfTabs;
}
@Override
public @NotNull Fragment getItem(int position) {
2019-02-03 12:07:43 +01:00
DisplayNotificationsFragment displayNotificationsFragment = new DisplayNotificationsFragment();
DisplayNotificationsFragment.Type type = null;
2019-02-03 15:00:27 +01:00
String tag = "";
2019-02-03 12:07:43 +01:00
switch (position) {
case 0:
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
2019-02-03 12:07:43 +01:00
type = DisplayNotificationsFragment.Type.ALL;
2019-09-06 17:55:14 +02:00
} else
2019-02-03 12:07:43 +01:00
type = DisplayNotificationsFragment.Type.MENTION;
break;
case 1:
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA)
2019-02-03 12:07:43 +01:00
type = DisplayNotificationsFragment.Type.MENTION;
2019-09-06 17:55:14 +02:00
else if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-03 15:00:27 +01:00
type = DisplayNotificationsFragment.Type.BOOST;
2019-09-06 17:55:14 +02:00
else if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA)
2019-02-08 15:07:46 +01:00
type = DisplayNotificationsFragment.Type.FOLLOW;
2019-02-03 12:07:43 +01:00
break;
case 2:
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU)
2019-02-03 12:07:43 +01:00
type = DisplayNotificationsFragment.Type.FAVORITE;
else
2019-02-03 15:00:27 +01:00
type = DisplayNotificationsFragment.Type.FOLLOW;
2019-02-03 12:07:43 +01:00
break;
case 3:
2019-02-03 15:00:27 +01:00
type = DisplayNotificationsFragment.Type.BOOST;
2019-02-03 12:07:43 +01:00
break;
case 4:
type = DisplayNotificationsFragment.Type.STATUS;
break;
case 5:
2019-04-19 16:03:54 +02:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON)
2019-05-04 10:01:38 +02:00
type = DisplayNotificationsFragment.Type.POLL;
2019-04-19 16:03:54 +02:00
else
type = DisplayNotificationsFragment.Type.FOLLOW;
2019-02-03 12:07:43 +01:00
break;
case 6:
2019-04-19 16:03:54 +02:00
if (MainActivity.social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON)
type = DisplayNotificationsFragment.Type.FOLLOW;
2019-02-03 12:07:43 +01:00
default:
break;
}
Bundle bundle = new Bundle();
bundle.putSerializable("type", type);
displayNotificationsFragment.setArguments(bundle);
return displayNotificationsFragment;
}
2019-02-03 15:00:27 +01:00
2019-02-03 12:07:43 +01:00
@Override
public int getCount() {
return mNumOfTabs;
}
}
}