-
-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
144 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
286 changes: 131 additions & 155 deletions
286
app/src/main/java/ml/docilealligator/infinityforreddit/activities/SettingsActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,197 +1,173 @@ | ||
package ml.docilealligator.infinityforreddit.activities; | ||
|
||
import android.content.SharedPreferences; | ||
import android.os.Build; | ||
import android.os.Bundle; | ||
import android.view.MenuItem; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.appcompat.widget.Toolbar; | ||
import androidx.core.app.ActivityCompat; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.preference.Preference; | ||
import androidx.preference.PreferenceFragmentCompat; | ||
|
||
import com.google.android.material.appbar.AppBarLayout; | ||
import com.google.android.material.appbar.CollapsingToolbarLayout; | ||
|
||
import org.greenrobot.eventbus.EventBus; | ||
import org.greenrobot.eventbus.Subscribe; | ||
import org.greenrobot.eventbus.ThreadMode; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import butterknife.BindView; | ||
import butterknife.ButterKnife; | ||
import ml.docilealligator.infinityforreddit.Infinity; | ||
import ml.docilealligator.infinityforreddit.R; | ||
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; | ||
import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent; | ||
import ml.docilealligator.infinityforreddit.settings.AboutPreferenceFragment; | ||
import ml.docilealligator.infinityforreddit.settings.AdvancedPreferenceFragment; | ||
import ml.docilealligator.infinityforreddit.settings.CustomizeBottomAppBarFragment; | ||
import ml.docilealligator.infinityforreddit.settings.CustomizeMainPageTabsFragment; | ||
import ml.docilealligator.infinityforreddit.settings.FontPreferenceFragment; | ||
import ml.docilealligator.infinityforreddit.settings.GesturesAndButtonsPreferenceFragment; | ||
import ml.docilealligator.infinityforreddit.settings.InterfacePreferenceFragment; | ||
import ml.docilealligator.infinityforreddit.settings.MainPreferenceFragment; | ||
import ml.docilealligator.infinityforreddit.settings.NsfwAndSpoilerFragment; | ||
import ml.docilealligator.infinityforreddit.settings.PostHistoryFragment; | ||
import ml.docilealligator.infinityforreddit.settings.PostPreferenceFragment; | ||
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils; | ||
|
||
public class SettingsActivity extends BaseActivity implements | ||
PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { | ||
|
||
private static final String TITLE_STATE = "TS"; | ||
|
||
package ml.docilealligator.infinityforreddit.activities | ||
|
||
import ml.docilealligator.infinityforreddit.activities.BaseActivity | ||
import androidx.preference.PreferenceFragmentCompat | ||
import butterknife.BindView | ||
import ml.docilealligator.infinityforreddit.R | ||
import com.google.android.material.appbar.AppBarLayout | ||
import com.google.android.material.appbar.CollapsingToolbarLayout | ||
import javax.inject.Inject | ||
import android.content.SharedPreferences | ||
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper | ||
import android.os.Bundle | ||
import ml.docilealligator.infinityforreddit.Infinity | ||
import butterknife.ButterKnife | ||
import android.os.Build | ||
import android.view.MenuItem | ||
import androidx.appcompat.widget.Toolbar | ||
import ml.docilealligator.infinityforreddit.utils.SharedPreferencesUtils | ||
import ml.docilealligator.infinityforreddit.settings.MainPreferenceFragment | ||
import ml.docilealligator.infinityforreddit.activities.SettingsActivity | ||
import ml.docilealligator.infinityforreddit.settings.AboutPreferenceFragment | ||
import ml.docilealligator.infinityforreddit.settings.InterfacePreferenceFragment | ||
import ml.docilealligator.infinityforreddit.settings.FontPreferenceFragment | ||
import ml.docilealligator.infinityforreddit.settings.GesturesAndButtonsPreferenceFragment | ||
import ml.docilealligator.infinityforreddit.settings.PostPreferenceFragment | ||
import ml.docilealligator.infinityforreddit.settings.AdvancedPreferenceFragment | ||
import ml.docilealligator.infinityforreddit.settings.CustomizeMainPageTabsFragment | ||
import ml.docilealligator.infinityforreddit.settings.NsfwAndSpoilerFragment | ||
import ml.docilealligator.infinityforreddit.settings.CustomizeBottomAppBarFragment | ||
import ml.docilealligator.infinityforreddit.settings.PostHistoryFragment | ||
import org.greenrobot.eventbus.ThreadMode | ||
import ml.docilealligator.infinityforreddit.events.RecreateActivityEvent | ||
import androidx.core.app.ActivityCompat | ||
import androidx.preference.Preference | ||
import org.greenrobot.eventbus.EventBus | ||
import org.greenrobot.eventbus.Subscribe | ||
import javax.inject.Named | ||
|
||
class SettingsActivity : BaseActivity(), PreferenceFragmentCompat.OnPreferenceStartFragmentCallback { | ||
@BindView(R.id.appbar_layout_settings_activity) | ||
AppBarLayout appBarLayout; | ||
var appBarLayout: AppBarLayout? = null | ||
|
||
@BindView(R.id.collapsing_toolbar_layout_settings_activity) | ||
CollapsingToolbarLayout collapsingToolbarLayout; | ||
var collapsingToolbarLayout: CollapsingToolbarLayout? = null | ||
|
||
@BindView(R.id.toolbar_settings_activity) | ||
Toolbar toolbar; | ||
private String mAccountName; | ||
var toolbar: Toolbar? = null | ||
private var mAccountName: String? = null | ||
|
||
@Inject | ||
@Named("default") | ||
SharedPreferences mSharedPreferences; | ||
var mSharedPreferences: SharedPreferences? = null | ||
|
||
@Inject | ||
@Named("current_account") | ||
SharedPreferences mCurrentAccountSharedPreferences; | ||
@Inject | ||
CustomThemeWrapper mCustomThemeWrapper; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
((Infinity) getApplication()).getAppComponent().inject(this); | ||
|
||
setImmersiveModeNotApplicable(); | ||
|
||
super.onCreate(savedInstanceState); | ||
|
||
setContentView(R.layout.activity_settings); | ||
|
||
ButterKnife.bind(this); | ||
|
||
EventBus.getDefault().register(this); | ||
var mCurrentAccountSharedPreferences: SharedPreferences? = null | ||
|
||
applyCustomTheme(); | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor()) { | ||
addOnOffsetChangedListener(appBarLayout); | ||
@Inject | ||
var mCustomThemeWrapper: CustomThemeWrapper? = null | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
(application as Infinity).appComponent.inject(this) | ||
setImmersiveModeNotApplicable() | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_settings) | ||
ButterKnife.bind(this) | ||
EventBus.getDefault().register(this) | ||
applyCustomTheme() | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && isChangeStatusBarIconColor) { | ||
addOnOffsetChangedListener(appBarLayout) | ||
} | ||
|
||
setSupportActionBar(toolbar); | ||
|
||
mAccountName = mCurrentAccountSharedPreferences.getString(SharedPreferencesUtils.ACCOUNT_NAME, null); | ||
|
||
setSupportActionBar(toolbar) | ||
mAccountName = mCurrentAccountSharedPreferences!!.getString(SharedPreferencesUtils.ACCOUNT_NAME, null) | ||
if (savedInstanceState == null) { | ||
getSupportFragmentManager() | ||
supportFragmentManager | ||
.beginTransaction() | ||
.replace(R.id.frame_layout_settings_activity, new MainPreferenceFragment()) | ||
.commit(); | ||
.replace(R.id.frame_layout_settings_activity, MainPreferenceFragment()) | ||
.commit() | ||
} else { | ||
setTitle(savedInstanceState.getCharSequence(TITLE_STATE)); | ||
title = savedInstanceState.getCharSequence(TITLE_STATE) | ||
} | ||
|
||
getSupportFragmentManager().addOnBackStackChangedListener(() -> { | ||
if (getSupportFragmentManager().getBackStackEntryCount() == 0) { | ||
setTitle(R.string.settings_activity_label); | ||
return; | ||
supportFragmentManager.addOnBackStackChangedListener { | ||
if (supportFragmentManager.backStackEntryCount == 0) { | ||
setTitle(R.string.settings_activity_label) | ||
return@addOnBackStackChangedListener | ||
} | ||
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_layout_settings_activity); | ||
if (fragment instanceof AboutPreferenceFragment) { | ||
setTitle(R.string.settings_about_master_title); | ||
} else if (fragment instanceof InterfacePreferenceFragment) { | ||
setTitle(R.string.settings_interface_title); | ||
} else if (fragment instanceof FontPreferenceFragment) { | ||
setTitle(R.string.settings_font_title); | ||
} else if (fragment instanceof GesturesAndButtonsPreferenceFragment) { | ||
setTitle(R.string.settings_gestures_and_buttons_title); | ||
} else if (fragment instanceof PostPreferenceFragment) { | ||
setTitle(R.string.settings_category_post_title); | ||
} else if (fragment instanceof AdvancedPreferenceFragment) { | ||
setTitle(R.string.settings_advanced_master_title); | ||
val fragment = supportFragmentManager.findFragmentById(R.id.frame_layout_settings_activity) | ||
if (fragment is AboutPreferenceFragment) { | ||
setTitle(R.string.settings_about_master_title) | ||
} else if (fragment is InterfacePreferenceFragment) { | ||
setTitle(R.string.settings_interface_title) | ||
} else if (fragment is FontPreferenceFragment) { | ||
setTitle(R.string.settings_font_title) | ||
} else if (fragment is GesturesAndButtonsPreferenceFragment) { | ||
setTitle(R.string.settings_gestures_and_buttons_title) | ||
} else if (fragment is PostPreferenceFragment) { | ||
setTitle(R.string.settings_category_post_title) | ||
} else if (fragment is AdvancedPreferenceFragment) { | ||
setTitle(R.string.settings_advanced_master_title) | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public SharedPreferences getDefaultSharedPreferences() { | ||
return mSharedPreferences; | ||
public override fun getDefaultSharedPreferences(): SharedPreferences { | ||
return mSharedPreferences!! | ||
} | ||
|
||
@Override | ||
protected CustomThemeWrapper getCustomThemeWrapper() { | ||
return mCustomThemeWrapper; | ||
override fun getCustomThemeWrapper(): CustomThemeWrapper { | ||
return mCustomThemeWrapper!! | ||
} | ||
|
||
@Override | ||
protected void applyCustomTheme() { | ||
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, collapsingToolbarLayout, toolbar); | ||
override fun applyCustomTheme() { | ||
applyAppBarLayoutAndCollapsingToolbarLayoutAndToolbarTheme(appBarLayout, collapsingToolbarLayout, toolbar) | ||
} | ||
|
||
@Override | ||
public boolean onOptionsItemSelected(@NonNull MenuItem item) { | ||
if (item.getItemId() == android.R.id.home) { | ||
onBackPressed(); | ||
return true; | ||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
if (item.itemId == android.R.id.home) { | ||
onBackPressed() | ||
return true | ||
} | ||
return false; | ||
return false | ||
} | ||
|
||
@Override | ||
public void onSaveInstanceState(@NonNull Bundle outState) { | ||
super.onSaveInstanceState(outState); | ||
outState.putCharSequence(TITLE_STATE, getTitle()); | ||
public override fun onSaveInstanceState(outState: Bundle) { | ||
super.onSaveInstanceState(outState) | ||
outState.putCharSequence(TITLE_STATE, title) | ||
} | ||
|
||
@Override | ||
public boolean onSupportNavigateUp() { | ||
if (getSupportFragmentManager().popBackStackImmediate()) { | ||
return true; | ||
} | ||
return super.onSupportNavigateUp(); | ||
override fun onSupportNavigateUp(): Boolean { | ||
return if (supportFragmentManager.popBackStackImmediate()) { | ||
true | ||
} else super.onSupportNavigateUp() | ||
} | ||
|
||
@Override | ||
public boolean onPreferenceStartFragment(PreferenceFragmentCompat caller, Preference pref) { | ||
override fun onPreferenceStartFragment(caller: PreferenceFragmentCompat, pref: Preference): Boolean { | ||
// Instantiate the new Fragment | ||
final Bundle args = pref.getExtras(); | ||
final Fragment fragment = getSupportFragmentManager().getFragmentFactory().instantiate( | ||
getClassLoader(), | ||
pref.getFragment()); | ||
if (fragment instanceof CustomizeMainPageTabsFragment) { | ||
args.putString(CustomizeMainPageTabsFragment.EXTRA_ACCOUNT_NAME, mAccountName); | ||
} else if (fragment instanceof NsfwAndSpoilerFragment) { | ||
args.putString(NsfwAndSpoilerFragment.EXTRA_ACCOUNT_NAME, mAccountName); | ||
} else if (fragment instanceof CustomizeBottomAppBarFragment) { | ||
args.putString(CustomizeBottomAppBarFragment.EXTRA_ACCOUNT_NAME, mAccountName); | ||
} else if (fragment instanceof PostHistoryFragment) { | ||
args.putString(PostHistoryFragment.EXTRA_ACCOUNT_NAME, mAccountName); | ||
val args = pref.extras | ||
val fragment = supportFragmentManager.fragmentFactory.instantiate( | ||
classLoader, | ||
pref.fragment!!) | ||
if (fragment is CustomizeMainPageTabsFragment) { | ||
args.putString(CustomizeMainPageTabsFragment.EXTRA_ACCOUNT_NAME, mAccountName) | ||
} else if (fragment is NsfwAndSpoilerFragment) { | ||
args.putString(NsfwAndSpoilerFragment.EXTRA_ACCOUNT_NAME, mAccountName) | ||
} else if (fragment is CustomizeBottomAppBarFragment) { | ||
args.putString(CustomizeBottomAppBarFragment.EXTRA_ACCOUNT_NAME, mAccountName) | ||
} else if (fragment is PostHistoryFragment) { | ||
args.putString(PostHistoryFragment.EXTRA_ACCOUNT_NAME, mAccountName) | ||
} | ||
fragment.setArguments(args); | ||
fragment.setTargetFragment(caller, 0); | ||
|
||
getSupportFragmentManager().beginTransaction() | ||
fragment.arguments = args | ||
fragment.setTargetFragment(caller, 0) | ||
supportFragmentManager.beginTransaction() | ||
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right) | ||
.replace(R.id.frame_layout_settings_activity, fragment) | ||
.addToBackStack(null) | ||
.commit(); | ||
setTitle(pref.getTitle()); | ||
return true; | ||
.commit() | ||
title = pref.title | ||
return true | ||
} | ||
|
||
@Override | ||
protected void onDestroy() { | ||
super.onDestroy(); | ||
EventBus.getDefault().unregister(this); | ||
override fun onDestroy() { | ||
super.onDestroy() | ||
EventBus.getDefault().unregister(this) | ||
} | ||
|
||
@Subscribe(threadMode = ThreadMode.MAIN) | ||
public void onRecreateActivityEvent(RecreateActivityEvent recreateActivityEvent) { | ||
ActivityCompat.recreate(this); | ||
fun onRecreateActivityEvent(recreateActivityEvent: RecreateActivityEvent?) { | ||
ActivityCompat.recreate(this) | ||
} | ||
|
||
companion object { | ||
private const val TITLE_STATE = "TS" | ||
} | ||
} | ||
} |