From 183036a41b040bdc93035a3a8773f584c392c53e Mon Sep 17 00:00:00 2001 From: Jedi Burrell Date: Fri, 12 May 2017 19:26:13 -0400 Subject: [PATCH] Make Language Settings BottomSheet. Previously a dialog, this commit makes the Language setting into a `BottomSheet`. --- .../settings/LanguageBottomSheetDialog.java | 118 ++++++++++++++++++ .../ui/modules/settings/SettingsActivity.java | 44 +------ .../settings/SettingsBottomSheetDialog.java | 45 ------- .../ui/modules/settings/SettingsFragment.java | 109 ---------------- .../category/SettingsCategoryMvp.java | 5 - app/src/main/res/layout/picker_dialog.xml | 86 +++++++++++++ 6 files changed, 209 insertions(+), 198 deletions(-) create mode 100644 app/src/main/java/com/fastaccess/ui/modules/settings/LanguageBottomSheetDialog.java delete mode 100644 app/src/main/java/com/fastaccess/ui/modules/settings/SettingsBottomSheetDialog.java delete mode 100644 app/src/main/java/com/fastaccess/ui/modules/settings/SettingsFragment.java create mode 100644 app/src/main/res/layout/picker_dialog.xml diff --git a/app/src/main/java/com/fastaccess/ui/modules/settings/LanguageBottomSheetDialog.java b/app/src/main/java/com/fastaccess/ui/modules/settings/LanguageBottomSheetDialog.java new file mode 100644 index 00000000..b103269e --- /dev/null +++ b/app/src/main/java/com/fastaccess/ui/modules/settings/LanguageBottomSheetDialog.java @@ -0,0 +1,118 @@ +package com.fastaccess.ui.modules.settings; + +import android.app.Activity; +import android.content.Context; +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.view.Gravity; +import android.view.View; +import android.widget.RadioButton; +import android.widget.RadioGroup; + +import com.fastaccess.R; +import com.fastaccess.helper.ActivityHelper; +import com.fastaccess.helper.PrefHelper; +import com.fastaccess.ui.base.BaseBottomSheetDialog; +import com.fastaccess.ui.widgets.FontButton; +import com.fastaccess.ui.widgets.FontTextView; + +import java.util.Arrays; + +import butterknife.BindView; +import butterknife.OnClick; + +import static android.app.Activity.RESULT_CANCELED; +import static android.app.Activity.RESULT_OK; + +/** + * Created by JediB on 5/12/2017. + */ + +public class LanguageBottomSheetDialog extends BaseBottomSheetDialog { + public interface LanguageDialogListener { + void onDismissed(); + } + + public static final String TAG = LanguageBottomSheetDialog.class.getSimpleName(); + + private String names[]; + private String values[]; + + @BindView(R.id.title) + FontTextView title; + @BindView(R.id.picker) + RadioGroup radioGroup; + @BindView(R.id.cancel) + FontButton cancel; + @BindView(R.id.ok) FontButton ok; + private SlackBottomSheetDialog.SlackDialogListener listener; + + @Override public void onAttach(Context context) { + super.onAttach(context); + if (context instanceof SlackBottomSheetDialog.SlackDialogListener) { + listener = (SlackBottomSheetDialog.SlackDialogListener) context; + } + + names = context.getResources().getStringArray(R.array.languages_array); + values = context.getResources().getStringArray(R.array.languages_array_values); + } + + @Override public void onDetach() { + listener = null; + super.onDetach(); + } + + @Override protected int layoutRes() { + return R.layout.picker_dialog; + } + + @OnClick({R.id.cancel, R.id.ok}) public void onViewClicked(View view) { + switch (view.getId()) { + case R.id.cancel: + getActivity().setResult(RESULT_CANCELED); + } + if (listener != null) listener.onDismissed(); + dismiss(); + } + + @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + int selected = Arrays.asList(values).indexOf(PrefHelper.getString("app_language")); + String language = PrefHelper.getString("app_language"); + cancel.setText(R.string.no); + ok.setText(R.string.yes); + + for (int i = 0; i < names.length; i++) { + RadioButton radioButtonView = new RadioButton(getContext()); + radioButtonView.setText(names[i]); + radioButtonView.setId(i); + radioButtonView.setGravity(Gravity.CENTER_VERTICAL); + radioButtonView.setPadding((int) getResources().getDimension(R.dimen.spacing_xs_large), (int) getResources().getDimension(R.dimen + .spacing_xs_large), + (int) getResources().getDimension(R.dimen.spacing_xs_large), (int) getResources().getDimension(R.dimen.spacing_xs_large)); + radioGroup.addView(radioButtonView); + if (i == selected) + radioGroup.check(i); + } + + radioGroup.setOnCheckedChangeListener((group, checkedId) -> { + int index = radioGroup.indexOfChild(radioGroup.findViewById(radioGroup.getCheckedRadioButtonId())); + + PrefHelper.set("app_language", values[index]); + if (language != values[index]) + getActivity().setResult(RESULT_OK); + }); + + } + + @Override protected void onHidden() { + if (listener != null) listener.onDismissed(); + super.onHidden(); + } + + @Override protected void onDismissedByScrolling() { + if (listener != null) listener.onDismissed(); + super.onDismissedByScrolling(); + } +} diff --git a/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsActivity.java b/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsActivity.java index 711cd458..fa844538 100644 --- a/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsActivity.java +++ b/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsActivity.java @@ -1,5 +1,6 @@ package com.fastaccess.ui.modules.settings; +import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; @@ -17,6 +18,7 @@ import com.fastaccess.helper.ActivityHelper; import com.fastaccess.helper.PrefHelper; import com.fastaccess.ui.adapter.SettingsAdapter; import com.fastaccess.ui.base.BaseActivity; +import com.fastaccess.ui.modules.changelog.ChangelogBottomSheetDialog; import com.fastaccess.ui.modules.settings.category.SettingsCategoryActivity; import net.grandcentrix.thirtyinch.TiPresenter; @@ -83,45 +85,9 @@ public class SettingsActivity extends BaseActivity { } private void showLanguageList() { - final String language = PrefHelper.getString("app_language"); - - String names[] = getResources().getStringArray(R.array.languages_array); - String values[] = getResources().getStringArray(R.array.languages_array_values); - - int selected = Arrays.asList(values).indexOf(PrefHelper.getString("app_language")); - - AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this); - LayoutInflater inflater = getLayoutInflater(); - View convertView = inflater.inflate(R.layout.dialog_picker, null); - alertDialog.setView(convertView); - alertDialog.setTitle("List"); - RadioGroup radioGroup = (RadioGroup) convertView.findViewById(R.id.picker); - radioGroup.setPadding((int) getResources().getDimension(R.dimen.spacing_xs_large), (int) getResources().getDimension(R.dimen - .spacing_xs_large), - (int) getResources().getDimension(R.dimen.spacing_xs_large), (int) getResources().getDimension(R.dimen.spacing_xs_large)); - for (int i = 0; i < names.length; i++) { - RadioButton radioButtonView = new RadioButton(this); - radioButtonView.setText(names[i]); - radioButtonView.setId(i); - radioButtonView.setGravity(Gravity.CENTER_VERTICAL); - radioButtonView.setPadding((int) getResources().getDimension(R.dimen.spacing_xs_large), (int) getResources().getDimension(R.dimen - .spacing_xs_large), - (int) getResources().getDimension(R.dimen.spacing_xs_large), (int) getResources().getDimension(R.dimen.spacing_xs_large)); - radioGroup.addView(radioButtonView); - if (i == selected) - radioGroup.check(i); - } - - radioGroup.setOnCheckedChangeListener((group, checkedId) -> { - int index = radioGroup.indexOfChild(radioGroup.findViewById(radioGroup.getCheckedRadioButtonId())); - - PrefHelper.set("app_language", values[index]); - if (language != values[index]) - setResult(RESULT_OK); - }); - - alertDialog.setView(convertView); - alertDialog.show(); + LanguageBottomSheetDialog languageBottomSheetDialog = new LanguageBottomSheetDialog(); + languageBottomSheetDialog.onAttach((Context) this); + languageBottomSheetDialog.show(getSupportFragmentManager(), "LanguageBottomSheetDialog"); } @Override diff --git a/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsBottomSheetDialog.java b/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsBottomSheetDialog.java deleted file mode 100644 index 2565d5a9..00000000 --- a/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsBottomSheetDialog.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.fastaccess.ui.modules.settings; - -import android.os.Bundle; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; -import android.support.v4.app.FragmentManager; -import android.support.v7.widget.Toolbar; -import android.view.View; - -import com.fastaccess.R; -import com.fastaccess.ui.base.BaseBottomSheetDialog; - -import butterknife.BindView; - -/** - * Created by Kosh on 02 Mar 2017, 7:51 PM - */ - -public class SettingsBottomSheetDialog extends BaseBottomSheetDialog { - - private static final String TAG = SettingsBottomSheetDialog.class.getSimpleName(); - - @BindView(R.id.toolbar) Toolbar toolbar; - - public static void show(@NonNull FragmentManager fragmentManager) { - new SettingsBottomSheetDialog().show(fragmentManager, SettingsBottomSheetDialog.TAG); - } - - @Override protected int layoutRes() { - return R.layout.settings_layout; - } - - @Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { - super.onViewCreated(view, savedInstanceState); - toolbar.setTitle(R.string.settings); - toolbar.setNavigationIcon(R.drawable.ic_clear); - toolbar.setNavigationOnClickListener(v -> dismiss()); - if (savedInstanceState == null) { - getChildFragmentManager() - .beginTransaction() - .replace(R.id.settingsContainer, new SettingsFragment()) - .commit(); - } - } -} diff --git a/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsFragment.java b/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsFragment.java deleted file mode 100644 index cac34070..00000000 --- a/app/src/main/java/com/fastaccess/ui/modules/settings/SettingsFragment.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.fastaccess.ui.modules.settings; - -import android.app.Activity; -import android.content.ComponentName; -import android.content.Context; -import android.content.pm.PackageManager; -import android.os.Bundle; -import android.support.v7.preference.Preference; -import android.support.v7.preference.PreferenceFragmentCompat; -import android.widget.Toast; - -import com.fastaccess.BuildConfig; -import com.fastaccess.R; -import com.fastaccess.helper.ActivityHelper; -import com.fastaccess.helper.PrefGetter; -import com.fastaccess.helper.PrefHelper; -import com.fastaccess.provider.tasks.notification.NotificationSchedulerJobTask; -import com.fastaccess.ui.base.mvp.BaseMvp; -import com.fastaccess.ui.modules.changelog.ChangelogBottomSheetDialog; -import com.fastaccess.ui.widgets.SpannableBuilder; - -import es.dmoral.toasty.Toasty; - -/** - * Created by Kosh on 02 Mar 2017, 7:51 PM - */ - -public class SettingsFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener { - - private BaseMvp.FAView callback; - private String appTheme; - private String appColor; - private String app_lauguage; - - @Override public void onAttach(Context context) { - super.onAttach(context); - if (context instanceof BaseMvp.FAView) { - callback = (BaseMvp.FAView) context; - } - - appTheme = PrefHelper.getString("appTheme"); - appColor = PrefHelper.getString("appColor"); - app_lauguage = PrefHelper.getString("app_language"); - } - - @Override public void onDetach() { - callback = null; - super.onDetach(); - } - - @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { - addPreferencesFromResource(R.xml.fasthub_settings); - findPreference("notificationTime").setOnPreferenceChangeListener(this); - findPreference("recylerViewAnimation").setOnPreferenceChangeListener(this); - findPreference("rect_avatar").setOnPreferenceChangeListener(this); - findPreference("appTheme").setOnPreferenceChangeListener(this); - findPreference("appColor").setOnPreferenceChangeListener(this); - findPreference("app_language").setOnPreferenceChangeListener(this); - findPreference("showChangelog").setOnPreferenceClickListener(preference -> { - new ChangelogBottomSheetDialog().show(getChildFragmentManager(), "ChangelogBottomSheetDialog"); - return true; - }); - findPreference("joinSlack").setOnPreferenceClickListener(preference -> { - ActivityHelper.startCustomTab(getActivity(), "http://rebrand.ly/fasthub"); - return true; - }); - findPreference("currentVersion").setSummary(SpannableBuilder.builder() - .append(getString(R.string.current_version)) - .append("(") - .bold(BuildConfig.VERSION_NAME) - .append(")")); - if (BuildConfig.FDROID) { - findPreference("enable_ads").setVisible(false); - } - } - - @Override public boolean onPreferenceChange(Preference preference, Object newValue) { - if (preference.getKey().equalsIgnoreCase("notificationTime")) { - NotificationSchedulerJobTask.scheduleJob(getActivity().getApplicationContext(), - PrefGetter.notificationDurationMillis(getActivity().getApplicationContext(), (String) newValue), true); - return true; - } else if (preference.getKey().equalsIgnoreCase("recylerViewAnimation")) { - getActivity().setResult(Activity.RESULT_OK); - return true; - } else if (preference.getKey().equalsIgnoreCase("rect_avatar")) { - getActivity().setResult(Activity.RESULT_OK); - return true; - } else if (preference.getKey().equalsIgnoreCase("appTheme")) { - if(newValue.toString().equalsIgnoreCase(appTheme)) - return true; - Toasty.warning(getContext(), getString(R.string.change_theme_warning), Toast.LENGTH_LONG).show(); - getActivity().setResult(Activity.RESULT_OK); - return true; - } else if (preference.getKey().equalsIgnoreCase("appColor")) { - if(newValue.toString().equalsIgnoreCase(appColor)) - return true; - Toasty.warning(getContext(), getString(R.string.change_theme_warning), Toast.LENGTH_LONG).show(); - getActivity().setResult(Activity.RESULT_OK); - return true; - } else if (preference.getKey().equalsIgnoreCase("app_language")) { - if(newValue.toString().equalsIgnoreCase(app_lauguage)) - return true; - getActivity().setResult(Activity.RESULT_OK); - return true; - } - return false; - } - -} diff --git a/app/src/main/java/com/fastaccess/ui/modules/settings/category/SettingsCategoryMvp.java b/app/src/main/java/com/fastaccess/ui/modules/settings/category/SettingsCategoryMvp.java index 8fe940f8..043fe7e3 100644 --- a/app/src/main/java/com/fastaccess/ui/modules/settings/category/SettingsCategoryMvp.java +++ b/app/src/main/java/com/fastaccess/ui/modules/settings/category/SettingsCategoryMvp.java @@ -7,11 +7,6 @@ import com.fastaccess.ui.base.mvp.BaseMvp; */ public interface SettingsCategoryMvp { - interface View extends BaseMvp.FAView { - @Override - void onThemeChanged(); - } - interface Presenter extends BaseMvp.FAPresenter { } diff --git a/app/src/main/res/layout/picker_dialog.xml b/app/src/main/res/layout/picker_dialog.xml new file mode 100644 index 00000000..2c0500e5 --- /dev/null +++ b/app/src/main/res/layout/picker_dialog.xml @@ -0,0 +1,86 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file