MainActivity.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package eu.kanade.mangafeed.ui.activity;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.support.v4.app.FragmentTransaction;
  5. import android.support.v7.widget.Toolbar;
  6. import android.widget.FrameLayout;
  7. import com.mikepenz.materialdrawer.Drawer;
  8. import com.mikepenz.materialdrawer.DrawerBuilder;
  9. import com.mikepenz.materialdrawer.model.PrimaryDrawerItem;
  10. import butterknife.Bind;
  11. import butterknife.ButterKnife;
  12. import eu.kanade.mangafeed.R;
  13. import eu.kanade.mangafeed.ui.fragment.LibraryFragment;
  14. import rx.subscriptions.CompositeSubscription;
  15. public class MainActivity extends BaseActivity {
  16. @Bind(R.id.toolbar)
  17. Toolbar toolbar;
  18. @Bind(R.id.drawer_container)
  19. FrameLayout container;
  20. private Drawer drawer;
  21. private CompositeSubscription mSubscriptions;
  22. @Override
  23. protected void onCreate(Bundle savedInstanceState) {
  24. super.onCreate(savedInstanceState);
  25. setContentView(R.layout.activity_main);
  26. ButterKnife.bind(this);
  27. mSubscriptions = new CompositeSubscription();
  28. setupToolbar(toolbar);
  29. drawer = new DrawerBuilder()
  30. .withActivity(this)
  31. .withRootView(container)
  32. .withToolbar(toolbar)
  33. .withActionBarDrawerToggleAnimated(true)
  34. .addDrawerItems(
  35. new PrimaryDrawerItem()
  36. .withName(R.string.library_title)
  37. .withIdentifier(R.id.nav_drawer_library),
  38. new PrimaryDrawerItem()
  39. .withName(R.string.recent_updates_title)
  40. .withIdentifier(R.id.nav_drawer_recent_updates),
  41. new PrimaryDrawerItem()
  42. .withName(R.string.catalogues_title)
  43. .withIdentifier(R.id.nav_drawer_catalogues),
  44. new PrimaryDrawerItem()
  45. .withName(R.string.settings_title)
  46. .withIdentifier(R.id.nav_drawer_settings)
  47. )
  48. .withSavedInstance(savedInstanceState)
  49. .withOnDrawerItemClickListener(
  50. (view, position, drawerItem) -> {
  51. if (drawerItem != null) {
  52. int identifier = drawerItem.getIdentifier();
  53. switch (identifier) {
  54. case R.id.nav_drawer_library:
  55. setFragment(LibraryFragment.newInstance());
  56. break;
  57. case R.id.nav_drawer_catalogues:
  58. case R.id.nav_drawer_recent_updates:
  59. case R.id.nav_drawer_settings:
  60. break;
  61. }
  62. }
  63. return false;
  64. }
  65. )
  66. .build();
  67. drawer.setSelection(R.id.nav_drawer_library);
  68. }
  69. @Override
  70. protected void onDestroy() {
  71. super.onDestroy();
  72. mSubscriptions.unsubscribe();
  73. }
  74. private void setFragment(Fragment fragment) {
  75. try {
  76. if (fragment != null && getSupportFragmentManager() != null) {
  77. FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
  78. if (ft != null) {
  79. ft.replace(R.id.content_layout, fragment);
  80. ft.commit();
  81. }
  82. }
  83. } catch (Exception e) {
  84. }
  85. }
  86. public void setToolbarTitle(int titleResource) {
  87. getSupportActionBar().setTitle(getString(titleResource));
  88. }
  89. }