MainActivity.java 3.8 KB

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