code refactor

This commit is contained in:
h4h13 2018-08-05 22:02:54 +05:30
parent 31467c9b5f
commit b046c85d78

View file

@ -12,139 +12,142 @@ import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar; import android.support.v7.widget.Toolbar;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import android.widget.TextView;
import com.afollestad.materialdialogs.color.ColorChooserDialog;
import butterknife.BindView; import butterknife.BindView;
import butterknife.ButterKnife; import butterknife.ButterKnife;
import code.name.monkey.appthemehelper.ThemeStore; import code.name.monkey.appthemehelper.ThemeStore;
import code.name.monkey.appthemehelper.util.ColorUtil; import code.name.monkey.appthemehelper.util.ColorUtil;
import code.name.monkey.appthemehelper.util.TintHelper;
import code.name.monkey.retromusic.R; import code.name.monkey.retromusic.R;
import code.name.monkey.retromusic.appshortcuts.DynamicShortcutManager; import code.name.monkey.retromusic.appshortcuts.DynamicShortcutManager;
import code.name.monkey.retromusic.ui.activities.base.AbsBaseActivity; import code.name.monkey.retromusic.ui.activities.base.AbsBaseActivity;
import code.name.monkey.retromusic.ui.fragments.settings.MainSettingsFragment; import code.name.monkey.retromusic.ui.fragments.settings.MainSettingsFragment;
import code.name.monkey.retromusic.util.PreferenceUtil; import code.name.monkey.retromusic.util.PreferenceUtil;
import com.afollestad.materialdialogs.color.ColorChooserDialog;
public class SettingsActivity extends AbsBaseActivity implements ColorChooserDialog.ColorCallback { public class SettingsActivity extends AbsBaseActivity implements ColorChooserDialog.ColorCallback {
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.app_bar)
AppBarLayout appBarLayout;
@BindView(R.id.detail_content_frame)
@Nullable
FrameLayout detailsFrame;
private FragmentManager fragmentManager = getSupportFragmentManager(); @BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.app_bar)
AppBarLayout appBarLayout;
@BindView(R.id.detail_content_frame)
@Nullable
FrameLayout detailsFrame;
private FragmentManager fragmentManager = getSupportFragmentManager();
@Override @Override
public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int selectedColor) { public void onColorSelection(@NonNull ColorChooserDialog dialog, @ColorInt int selectedColor) {
switch (dialog.getTitle()) { switch (dialog.getTitle()) {
case R.string.primary_color: case R.string.primary_color:
int theme = ColorUtil.isColorLight(selectedColor) ? int theme = ColorUtil.isColorLight(selectedColor) ?
PreferenceUtil.getThemeResFromPrefValue("light") : PreferenceUtil.getThemeResFromPrefValue("light") :
PreferenceUtil.getThemeResFromPrefValue("dark"); PreferenceUtil.getThemeResFromPrefValue("dark");
ThemeStore.editTheme(this).activityTheme(theme).primaryColor(selectedColor).commit(); ThemeStore.editTheme(this).activityTheme(theme).primaryColor(selectedColor).commit();
break; break;
case R.string.accent_color: case R.string.accent_color:
ThemeStore.editTheme(this).accentColor(selectedColor).commit(); ThemeStore.editTheme(this).accentColor(selectedColor).commit();
break; break;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
new DynamicShortcutManager(this).updateDynamicShortcuts();
}
recreate();
} }
@Override if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N_MR1) {
public void onColorChooserDismissed(@NonNull ColorChooserDialog dialog) { new DynamicShortcutManager(this).updateDynamicShortcuts();
}
recreate();
}
@Override
public void onColorChooserDismissed(@NonNull ColorChooserDialog dialog) {
}
@Override
protected void onCreate(@Nullable Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.activity_settings);
ButterKnife.bind(this);
setStatusbarColorAuto();
setNavigationbarColorAuto();
setTaskDescriptionColorAuto();
setLightNavigationBar(true);
setupToolbar();
if (bundle == null) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new MainSettingsFragment())
.commit();
} else {
restoreFragment();
}
}
private void setupToolbar() {
int primaryColor = ThemeStore.primaryColor(this);
appBarLayout.setBackgroundColor(primaryColor);
toolbar.setBackgroundColor(primaryColor);
toolbar.setNavigationOnClickListener(v -> onBackPressed());
setTitle(R.string.app_name);
setSupportActionBar(toolbar);
}
private void restoreFragment() {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_black_24dp);
if (fragmentManager.getBackStackEntryCount() > 0) {
appBarLayout.setExpanded(false, true);
} else {
appBarLayout.setExpanded(true, true);
}
setupToolbar();
}
public void setupFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.setCustomAnimations(R.animator.slide_up, 0, 0, R.animator.slide_down);
if (detailsFrame == null) {
fragmentTransaction.replace(R.id.content_frame, fragment, fragment.getTag());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
fragmentTransaction.replace(R.id.detail_content_frame, fragment, fragment.getTag());
fragmentTransaction.commit();
} }
@Override fragmentManager.addOnBackStackChangedListener(() -> {
protected void onCreate(@Nullable Bundle bundle) { if (fragmentManager.getBackStackEntryCount() > 0) {
super.onCreate(bundle); appBarLayout.setExpanded(false, true);
setContentView(R.layout.activity_settings); } else {
ButterKnife.bind(this); appBarLayout.setExpanded(true, true);
}
setupToolbar();
});
}
setStatusbarColorAuto();
setNavigationbarColorAuto();
setTaskDescriptionColorAuto();
setLightNavigationBar(true);
setupToolbar(); @Override
public void onBackPressed() {
if (bundle == null) { if (fragmentManager.getBackStackEntryCount() == 0) {
fragmentManager.beginTransaction().replace(R.id.content_frame, new MainSettingsFragment()).commit(); super.onBackPressed();
} else { } else {
restoreFragment(); fragmentManager.popBackStack();
}
} }
}
private void setupToolbar() { @Override
appBarLayout.setBackgroundColor(ThemeStore.primaryColor(this)); public boolean onOptionsItemSelected(MenuItem item) {
toolbar.setBackgroundColor(ThemeStore.primaryColor(this)); if (item.getItemId() == android.R.id.home) {
toolbar.setNavigationOnClickListener(v -> onBackPressed()); onBackPressed();
setTitle(R.string.action_settings); return true;
setSupportActionBar(toolbar);
} }
return super.onOptionsItemSelected(item);
}
private void restoreFragment() { public void addAppbarLayoutElevation(float v) {
toolbar.setNavigationIcon(R.drawable.ic_keyboard_backspace_black_24dp); TransitionManager.beginDelayedTransition(appBarLayout);
if (fragmentManager.getBackStackEntryCount() > 0) { appBarLayout.setElevation(v);
appBarLayout.setExpanded(false, true); }
} else {
appBarLayout.setExpanded(true, true);
}
setupToolbar();
}
public void setupFragment(Fragment fragment) {
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction()
.setCustomAnimations(R.animator.slide_up, 0, 0, R.animator.slide_down);
if (detailsFrame == null) {
fragmentTransaction.replace(R.id.content_frame, fragment, fragment.getTag());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
} else {
fragmentTransaction.replace(R.id.detail_content_frame, fragment, fragment.getTag());
fragmentTransaction.commit();
}
fragmentManager.addOnBackStackChangedListener(() -> {
if (fragmentManager.getBackStackEntryCount() > 0) {
appBarLayout.setExpanded(false, true);
} else {
appBarLayout.setExpanded(true, true);
}
setupToolbar();
});
}
@Override
public void onBackPressed() {
if (fragmentManager.getBackStackEntryCount() == 0) {
super.onBackPressed();
} else {
fragmentManager.popBackStack();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
public void addAppbarLayoutElevation(float v) {
TransitionManager.beginDelayedTransition(appBarLayout);
appBarLayout.setElevation(v);
}
} }