Swipe to change timelines

This commit is contained in:
Thomas 2020-11-27 18:36:31 +01:00
parent 1107391c43
commit e0907f1dc8
3 changed files with 125 additions and 98 deletions

View File

@ -39,6 +39,9 @@ import androidx.appcompat.widget.SearchView;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.kobakei.ratethisapp.RateThisApp;
@ -62,6 +65,7 @@ import app.fedilab.fedilabtube.client.entities.Token;
import app.fedilab.fedilabtube.client.entities.UserMe;
import app.fedilab.fedilabtube.client.entities.UserSettings;
import app.fedilab.fedilabtube.client.entities.WellKnownNodeinfo;
import app.fedilab.fedilabtube.databinding.ActivityMainBinding;
import app.fedilab.fedilabtube.fragment.DisplayOverviewFragment;
import app.fedilab.fedilabtube.fragment.DisplayVideosFragment;
import app.fedilab.fedilabtube.helper.Helper;
@ -87,43 +91,9 @@ public class MainActivity extends AppCompatActivity {
public static UserMe userMe;
public static InstanceData.InstanceConfig instanceConfig;
public static TypeOfConnection typeOfConnection;
final FragmentManager fm = getSupportFragmentManager();
Fragment active;
private DisplayVideosFragment recentFragment, locaFragment, trendingFragment, subscriptionFragment, mostLikedFragment;
private DisplayOverviewFragment overviewFragment;
private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
DisplayVideosFragment displayVideosFragment = null;
int itemId = item.getItemId();
if (itemId == R.id.navigation_subscription) {
displayVideosFragment = subscriptionFragment;
setTitleCustom(R.string.subscriptions);
} else if (itemId == R.id.navigation_trending) {
setTitleCustom(R.string.title_trending);
displayVideosFragment = trendingFragment;
} else if (itemId == R.id.navigation_most_liked) {
setTitleCustom(R.string.title_most_liked);
displayVideosFragment = mostLikedFragment;
} else if (itemId == R.id.navigation_recently_added) {
setTitleCustom(R.string.title_recently_added);
displayVideosFragment = recentFragment;
} else if (itemId == R.id.navigation_local) {
setTitleCustom(R.string.title_local);
displayVideosFragment = locaFragment;
} else if (itemId == R.id.navigation_discover) {
setTitleCustom(R.string.title_discover);
fm.beginTransaction().hide(active).show(overviewFragment).commit();
active = overviewFragment;
return true;
}
if (displayVideosFragment != null) {
fm.beginTransaction().hide(active).show(displayVideosFragment).commit();
active = displayVideosFragment;
return true;
} else {
return false;
}
};
private ActivityMainBinding binding;
@SuppressLint("ApplySharedPref")
public static void showRadioButtonDialogFullInstances(Activity activity, boolean storeInDb) {
@ -192,10 +162,41 @@ public class MainActivity extends AppCompatActivity {
}
}
private final BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= item -> {
int itemId = item.getItemId();
if (itemId == R.id.navigation_discover) {
setTitleCustom(R.string.title_discover);
binding.viewpager.setCurrentItem(0);
} else if (itemId == R.id.navigation_subscription) {
binding.viewpager.setCurrentItem(1);
setTitleCustom(R.string.subscriptions);
} else if (itemId == R.id.navigation_trending) {
setTitleCustom(R.string.title_trending);
if (Helper.isLoggedIn(MainActivity.this)) {
binding.viewpager.setCurrentItem(2);
} else {
binding.viewpager.setCurrentItem(1);
}
} else if (itemId == R.id.navigation_most_liked) {
setTitleCustom(R.string.title_most_liked);
binding.viewpager.setCurrentItem(2);
} else if (itemId == R.id.navigation_recently_added) {
setTitleCustom(R.string.title_recently_added);
binding.viewpager.setCurrentItem(3);
} else if (itemId == R.id.navigation_local) {
setTitleCustom(R.string.title_local);
binding.viewpager.setCurrentItem(4);
}
return true;
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
@ -211,22 +212,6 @@ public class MainActivity extends AppCompatActivity {
}
checkIfConnectedUsers();
Fragment fragment = getSupportFragmentManager().findFragmentByTag("5");
if (fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("4");
if (fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("3");
if (fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("2");
if (fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
fragment = getSupportFragmentManager().findFragmentByTag("1");
if (fragment != null)
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
recentFragment = new DisplayVideosFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.RECENT);
@ -254,23 +239,37 @@ public class MainActivity extends AppCompatActivity {
overviewFragment = new DisplayOverviewFragment();
if (active == null) {
active = overviewFragment;
}
if (!Helper.isLoggedIn(MainActivity.this)) {
fm.beginTransaction().add(R.id.nav_host_fragment, locaFragment, "5").hide(locaFragment).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, recentFragment, "4").hide(recentFragment).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, mostLikedFragment, "3").hide(mostLikedFragment).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, trendingFragment, "2").hide(trendingFragment).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, overviewFragment, "1").commit();
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
binding.viewpager.setAdapter(mPagerAdapter);
}
binding.viewpager.setOffscreenPageLimit(5);
binding.viewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
MenuItem item = binding.navView.getMenu().getItem(position);
binding.navView.setSelectedItemId(item.getItemId());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
toolbar.setOnClickListener(v -> {
if (active instanceof DisplayVideosFragment) {
((DisplayVideosFragment) active).scrollToTop();
} else if (active instanceof DisplayOverviewFragment) {
((DisplayOverviewFragment) active).scrollToTop();
if (binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem()) instanceof DisplayVideosFragment) {
((DisplayVideosFragment) binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem())).scrollToTop();
} else if (binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem()) instanceof DisplayOverviewFragment) {
((DisplayOverviewFragment) binding.viewpager.getAdapter().instantiateItem(binding.viewpager, binding.viewpager.getCurrentItem())).scrollToTop();
}
});
@ -309,11 +308,10 @@ public class MainActivity extends AppCompatActivity {
runOnUiThread(() -> {
//To avoid a token issue with subscriptions, adding fragment is done when the token is refreshed.
new Handler().post(() -> {
fm.beginTransaction().add(R.id.nav_host_fragment, locaFragment, "5").hide(locaFragment).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, recentFragment, "4").hide(recentFragment).commitAllowingStateLoss();
fm.beginTransaction().add(R.id.nav_host_fragment, trendingFragment, "3").hide(trendingFragment).commitAllowingStateLoss();
fm.beginTransaction().add(R.id.nav_host_fragment, subscriptionFragment, "2").hide(subscriptionFragment).commitAllowingStateLoss();
fm.beginTransaction().add(R.id.nav_host_fragment, overviewFragment, "1").commitAllowingStateLoss();
if (Helper.isLoggedIn(MainActivity.this)) {
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
binding.viewpager.setAdapter(mPagerAdapter);
}
});
});
@ -369,6 +367,10 @@ public class MainActivity extends AppCompatActivity {
}
}
public DisplayVideosFragment getSubscriptionFragment() {
return subscriptionFragment;
}
private void startInForeground() {
Intent notificationIntent = new Intent(this, RetrieveInfoService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
@ -605,13 +607,6 @@ public class MainActivity extends AppCompatActivity {
return super.onOptionsItemSelected(item);
}
public void setActive(DisplayVideosFragment displayVideosFragment) {
this.active = displayVideosFragment;
}
public void setSubscriptionFragment(DisplayVideosFragment displayVideosFragment) {
this.subscriptionFragment = displayVideosFragment;
}
@Override
protected void onNewIntent(Intent intent) {
@ -671,6 +666,50 @@ public class MainActivity extends AppCompatActivity {
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
@NotNull
@Override
public Fragment getItem(final int position) {
if (Helper.isLoggedIn(MainActivity.this)) {
switch (position) {
case 0:
return overviewFragment;
case 1:
return subscriptionFragment;
case 2:
return trendingFragment;
case 3:
return recentFragment;
case 4:
return locaFragment;
}
} else {
switch (position) {
case 0:
return overviewFragment;
case 1:
return trendingFragment;
case 2:
return mostLikedFragment;
case 3:
return recentFragment;
case 4:
return locaFragment;
}
}
return null;
}
@Override
public int getCount() {
return 5;
}
}
public enum TypeOfConnection {
UNKNOWN,

View File

@ -29,10 +29,9 @@ import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
@ -461,21 +460,11 @@ public class DisplayVideosFragment extends Fragment implements AccountsHorizonta
public void pullToRefresh(boolean reload) {
if (type == TimelineVM.TimelineType.SUBSCRIBTIONS && reload) {
Fragment fragment = ((AppCompatActivity) context).getSupportFragmentManager().findFragmentByTag("2");
if (fragment != null) {
if (context instanceof MainActivity) {
FragmentManager fm = ((MainActivity) context).getSupportFragmentManager();
fm.beginTransaction().remove(fragment).commit();
DisplayVideosFragment subscriptionFragment = new DisplayVideosFragment();
Bundle bundle = new Bundle();
bundle.putSerializable(Helper.TIMELINE_TYPE, TimelineVM.TimelineType.SUBSCRIBTIONS);
subscriptionFragment.setArguments(bundle);
((MainActivity) context).setActive(subscriptionFragment);
((MainActivity) context).setSubscriptionFragment(subscriptionFragment);
fm.beginTransaction().add(R.id.nav_host_fragment, subscriptionFragment, "2").commit();
DisplayVideosFragment subscriptionFragment = ((MainActivity) context).getSubscriptionFragment();
if (subscriptionFragment != null) {
FragmentTransaction ft = ((MainActivity) context).getSupportFragmentManager().beginTransaction();
ft.detach(subscriptionFragment).attach(subscriptionFragment).commit();
}
}
} else {
int size = peertubes.size();
peertubes.clear();

View File

@ -63,13 +63,12 @@
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
<androidx.viewpager.widget.ViewPager
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:id="@+id/nav_host_fragment"
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize"
/>
android:layout_marginBottom="?attr/actionBarSize" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_gravity="bottom"