Make Language Settings BottomSheet.

Previously a dialog, this commit makes the Language setting into a
`BottomSheet`.
This commit is contained in:
Jedi Burrell 2017-05-12 19:26:13 -04:00
parent a443656029
commit 183036a41b
6 changed files with 209 additions and 198 deletions

View File

@ -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();
}
}

View File

@ -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

View File

@ -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();
}
}
}

View File

@ -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;
}
}

View File

@ -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 {
}

View File

@ -0,0 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/messageLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?card_background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.fastaccess.ui.widgets.FontTextView
android:id="@+id/title"
style="@style/TextAppearance.AppCompat.Title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="@dimen/spacing_xs_large"
android:textColor="?android:textColorPrimary"
android:textStyle="bold"
tools:text="@string/language"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingEnd="@dimen/spacing_xs_large"
android:paddingStart="@dimen/spacing_xs_large">
<com.prettifier.pretty.PrettifyWebView
android:id="@+id/prettifyWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="200dp"
android:visibility="gone"
app:webview_background="?card_background"/>
<RadioGroup
android:id="@+id/picker"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="end"
android:paddingBottom="@dimen/spacing_normal"
android:paddingTop="@dimen/spacing_normal">
<com.fastaccess.ui.widgets.FontButton
android:id="@+id/cancel"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/cancel"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="@color/material_pink_700"/>
<com.fastaccess.ui.widgets.FontButton
android:id="@+id/ok"
style="@style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="@string/ok"
android:textAppearance="@style/TextAppearance.AppCompat.Title"
android:textColor="?colorAccent"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>