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

161 lines
6.1 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2018-09-05 10:19:07 +02:00
/* Copyright 2018 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2018-09-05 10:19:07 +02: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
2018-09-05 10:19:07 +02: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,
2018-09-05 10:19:07 +02:00
* see <http://www.gnu.org/licenses>. */
2019-09-06 17:55:14 +02:00
2018-09-05 10:19:07 +02:00
import android.os.Bundle;
2019-11-15 16:32:25 +01:00
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2019-09-06 17:55:14 +02:00
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
2019-11-13 12:54:01 +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;
2018-09-05 10:19:07 +02:00
2020-07-06 15:04:39 +02: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.RetrieveFeedsAsyncTask;
import app.fedilab.android.asynctasks.UpdateAccountInfoAsyncTask;
2018-09-05 10:19:07 +02:00
/**
* Created by Thomas on 05/09/2018.
* Tablayout selection for toots in a profile
*/
public class TabLayoutTootsFragment extends Fragment {
private String targetedId;
2019-08-15 11:59:21 +02:00
private ViewPager viewPager;
2018-09-05 10:19:07 +02:00
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.tablayout_toots, container, false);
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));
2018-09-05 10:19:07 +02:00
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.toots)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.replies)));
2019-09-06 17:55:14 +02:00
if (MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.GNU && MainActivity.social != UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
2019-02-03 16:06:06 +01:00
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.media)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.pinned_toots)));
}
2018-09-05 10:19:07 +02:00
2019-08-15 11:59:21 +02:00
viewPager = inflatedView.findViewById(R.id.viewpager);
2018-09-05 10:19:07 +02:00
Bundle bundle = this.getArguments();
if (bundle != null) {
2019-01-03 18:59:44 +01:00
this.targetedId = bundle.getString("targetedid", null);
2018-09-05 10:19:07 +02:00
}
viewPager.setAdapter(new PagerAdapter
(getChildFragmentManager(), tabLayout.getTabCount()));
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
2019-09-06 17:55:14 +02:00
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
2018-09-05 10:19:07 +02: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) {
}
});
return inflatedView;
}
2019-11-15 16:32:25 +01:00
public ViewPager getViewPager() {
return viewPager;
}
2018-09-05 10:19:07 +02:00
/**
* Page Adapter for settings
*/
private class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
private PagerAdapter(FragmentManager fm, int NumOfTabs) {
2020-03-08 10:29:06 +01:00
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
2018-09-05 10:19:07 +02:00
this.mNumOfTabs = NumOfTabs;
}
2020-07-06 15:04:39 +02:00
@NotNull
2018-09-05 10:19:07 +02:00
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
DisplayStatusFragment displayStatusFragment = new DisplayStatusFragment();
Bundle bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER);
2019-01-03 18:59:44 +01:00
bundle.putString("targetedid", targetedId);
2019-09-06 17:55:14 +02:00
bundle.putBoolean("showReply", false);
2018-09-05 10:19:07 +02:00
displayStatusFragment.setArguments(bundle);
return displayStatusFragment;
case 1:
displayStatusFragment = new DisplayStatusFragment();
bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER);
2019-01-03 18:59:44 +01:00
bundle.putString("targetedid", targetedId);
2019-09-06 17:55:14 +02:00
bundle.putBoolean("showReply", true);
2018-09-05 10:19:07 +02:00
displayStatusFragment.setArguments(bundle);
return displayStatusFragment;
case 2:
2018-09-05 15:02:48 +02:00
DisplayMediaFragment displayMediaFragment = new DisplayMediaFragment();
2018-09-05 10:19:07 +02:00
bundle = new Bundle();
2019-01-03 18:59:44 +01:00
bundle.putString("targetedid", targetedId);
2018-09-05 15:02:48 +02:00
displayMediaFragment.setArguments(bundle);
return displayMediaFragment;
2018-09-05 10:19:07 +02:00
case 3:
displayStatusFragment = new DisplayStatusFragment();
bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.USER);
2019-01-03 18:59:44 +01:00
bundle.putString("targetedid", targetedId);
2019-09-06 17:55:14 +02:00
bundle.putBoolean("showPinned", true);
2018-09-05 10:19:07 +02:00
displayStatusFragment.setArguments(bundle);
return displayStatusFragment;
default:
displayStatusFragment = new DisplayStatusFragment();
return displayStatusFragment;
}
}
2019-11-27 15:35:14 +01:00
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
}
2018-09-05 10:19:07 +02:00
@Override
public int getCount() {
return mNumOfTabs;
}
}
}