SettingsActivity.java 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package eu.kanade.mangafeed.ui.setting;
  2. import android.os.Bundle;
  3. import android.preference.PreferenceFragment;
  4. import android.support.v7.widget.Toolbar;
  5. import javax.inject.Inject;
  6. import butterknife.Bind;
  7. import butterknife.ButterKnife;
  8. import eu.kanade.mangafeed.App;
  9. import eu.kanade.mangafeed.R;
  10. import eu.kanade.mangafeed.data.cache.CacheManager;
  11. import eu.kanade.mangafeed.data.preference.PreferencesHelper;
  12. import eu.kanade.mangafeed.ui.base.activity.BaseActivity;
  13. public class SettingsActivity extends BaseActivity {
  14. @Inject PreferencesHelper preferences;
  15. @Inject CacheManager cacheManager;
  16. @Bind(R.id.toolbar) Toolbar toolbar;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. super.onCreate(savedInstanceState);
  20. App.get(getActivity()).getComponent().inject(this);
  21. setContentView(R.layout.activity_preferences);
  22. ButterKnife.bind(this);
  23. setupToolbar(toolbar);
  24. if (savedInstanceState == null)
  25. getFragmentManager().beginTransaction().replace(R.id.settings_content,
  26. new SettingsMainFragment())
  27. .commit();
  28. }
  29. @Override
  30. public void onBackPressed() {
  31. if( !getFragmentManager().popBackStackImmediate() ) super.onBackPressed();
  32. }
  33. public static class SettingsMainFragment extends PreferenceFragment {
  34. @Override
  35. public void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. addPreferencesFromResource(R.xml.pref_main);
  38. registerSubpreference(R.string.pref_category_reader_key,
  39. SettingsNestedFragment.newInstance(
  40. R.xml.pref_reader, R.string.pref_category_reader));
  41. registerSubpreference(R.string.pref_category_downloads_key,
  42. SettingsDownloadsFragment.newInstance(
  43. R.xml.pref_downloads, R.string.pref_category_downloads));
  44. registerSubpreference(R.string.pref_category_accounts_key,
  45. SettingsAccountsFragment.newInstance(
  46. R.xml.pref_accounts, R.string.pref_category_accounts));
  47. registerSubpreference(R.string.pref_category_cache_key,
  48. SettingsCacheFragment.newInstance(
  49. R.xml.pref_cache, R.string.pref_category_cache));
  50. }
  51. @Override
  52. public void onResume() {
  53. super.onResume();
  54. ((BaseActivity) getActivity()).setToolbarTitle(getString(R.string.label_settings));
  55. }
  56. private void registerSubpreference(int preferenceResource, PreferenceFragment fragment) {
  57. findPreference(getString(preferenceResource))
  58. .setOnPreferenceClickListener(preference -> {
  59. getFragmentManager().beginTransaction()
  60. .replace(R.id.settings_content, fragment)
  61. .addToBackStack(fragment.getClass().getSimpleName()).commit();
  62. return true;
  63. });
  64. }
  65. }
  66. }