diff --git a/app/build.gradle b/app/build.gradle index 9ef8c38..797c41f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -151,6 +151,9 @@ dependencies { compile 'com.github.hotchemi:permissionsdispatcher:2.0.6' apt 'com.github.hotchemi:permissionsdispatcher-processor:2.0.6' + compile 'com.github.rejasupotaro.kvs-schema:library:2.0.0' + apt 'com.github.rejasupotaro.kvs-schema:compiler:2.0.0' + compile "com.squareup.picasso:picasso:2.5.2" compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4' compile 'com.squareup.retrofit2:adapter-rxjava:2.0.0-beta4' diff --git a/app/src/androidTest/java/io/github/droidkaigi/confsched/util/LocaleUtilsTest.java b/app/src/androidTest/java/io/github/droidkaigi/confsched/util/LocaleUtilsTest.java index 32f6245..5421a74 100644 --- a/app/src/androidTest/java/io/github/droidkaigi/confsched/util/LocaleUtilsTest.java +++ b/app/src/androidTest/java/io/github/droidkaigi/confsched/util/LocaleUtilsTest.java @@ -10,6 +10,8 @@ import java.util.Arrays; import java.util.Locale; +import io.github.droidkaigi.confsched.prefs.DefaultPrefsSchema; + import static org.assertj.core.api.Assertions.assertThat; @RunWith(AndroidJUnit4.class) @@ -21,11 +23,11 @@ public void testCurrentLanguageId() throws Exception { // is not null value. assertThat(LocaleUtil.getCurrentLanguageId(context)).isNotNull(); - PrefUtil.put(context, PrefUtil.KEY_CURRENT_LANGUAGE_ID, "ja"); + DefaultPrefsSchema.get(context).putLanguageId("ja"); // eq to languageID is put in SharedPreferences. assertThat(LocaleUtil.getCurrentLanguageId(context)).isEqualTo("ja"); - PrefUtil.remove(context, PrefUtil.KEY_CURRENT_LANGUAGE_ID); + DefaultPrefsSchema.get(context).removeLanguageId(); String defaultLanguage = Locale.getDefault().getLanguage().toLowerCase(); if (Arrays.asList(LocaleUtil.SUPPORT_LANG).contains(defaultLanguage)) { // eq to Locale.getDefault().getLanguage() when it is supported diff --git a/app/src/main/java/io/github/droidkaigi/confsched/fragment/SettingsFragment.java b/app/src/main/java/io/github/droidkaigi/confsched/fragment/SettingsFragment.java index 4a353fb..54fe1df 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/fragment/SettingsFragment.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/fragment/SettingsFragment.java @@ -11,16 +11,19 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; + +import java.util.Arrays; +import java.util.List; + +import javax.inject.Inject; + import io.github.droidkaigi.confsched.MainApplication; import io.github.droidkaigi.confsched.R; import io.github.droidkaigi.confsched.activity.ActivityNavigator; import io.github.droidkaigi.confsched.dao.SessionDao; import io.github.droidkaigi.confsched.databinding.FragmentSettingsBinding; +import io.github.droidkaigi.confsched.prefs.DefaultPrefsSchema; import io.github.droidkaigi.confsched.util.LocaleUtil; -import io.github.droidkaigi.confsched.util.PrefUtil; -import java.util.Arrays; -import java.util.List; -import javax.inject.Inject; import rx.Observable; public class SettingsFragment extends Fragment { @@ -55,16 +58,26 @@ public void onAttach(Context context) { private void initView() { binding.txtLanguage.setText(LocaleUtil.getCurrentLanguage(getActivity())); binding.languageSettingsContainer.setOnClickListener(v -> showLanguagesDialog()); - binding.notificationSwitchRow.init(PrefUtil.KEY_NOTIFICATION_SETTING, true); - binding.localTimeSwitchRow.init(PrefUtil.KEY_SHOW_LOCAL_TIME, false); + + boolean shouldNotify = DefaultPrefsSchema.get(getContext()).getNotificationFlag(true); + binding.notificationSwitchRow.init(shouldNotify, ((v, isChecked) -> { + DefaultPrefsSchema.get(getContext()).putNotificationFlag(isChecked); + binding.headsUpSwitchRow.setEnabled(isChecked); + })); + binding.headsUpSwitchRow.setEnabled(shouldNotify); + + boolean shouldShowLocalTime = DefaultPrefsSchema.get(getContext()).getShowLocalTimeFlag(false); + binding.localTimeSwitchRow.init(shouldShowLocalTime, ((buttonView, isChecked) -> { + DefaultPrefsSchema.get(getContext()).putShowLocalTimeFlag(isChecked); + })); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - binding.headsUpSwitchRow.init(PrefUtil.KEY_HEADS_UP_SETTING, true); + boolean headsUp = DefaultPrefsSchema.get(getContext()).getHeadsUpFlag(true); + binding.headsUpSwitchRow.init(headsUp, (v, isChecked) -> { + DefaultPrefsSchema.get(getContext()).putHeadsUpFlag(isChecked); + }); binding.headsUpSwitchRow.setVisibility(View.VISIBLE); binding.headsUpBorder.setVisibility(View.VISIBLE); - binding.notificationSwitchRow.setOnCheckedChangeListener((buttonView, isChecked) -> { - binding.headsUpSwitchRow.setEnabled(isChecked); - }); } } diff --git a/app/src/main/java/io/github/droidkaigi/confsched/prefs/DefaultPrefsSchema.java b/app/src/main/java/io/github/droidkaigi/confsched/prefs/DefaultPrefsSchema.java new file mode 100644 index 0000000..6a2a356 --- /dev/null +++ b/app/src/main/java/io/github/droidkaigi/confsched/prefs/DefaultPrefsSchema.java @@ -0,0 +1,27 @@ +package io.github.droidkaigi.confsched.prefs; + +import android.content.Context; + +import com.rejasupotaro.android.kvs.annotations.Key; +import com.rejasupotaro.android.kvs.annotations.Table; + +@Table("io.github.droidkaigi.confsched_preferences") +public abstract class DefaultPrefsSchema { + @Key("current_language_id") + String languageId; + @Key("notification_setting") + boolean notificationFlag; + @Key("heads_up_setting") + boolean headsUpFlag; + @Key("show_local_time") + boolean showLocalTimeFlag; + + private static DefaultPrefs prefs; + + public static synchronized DefaultPrefs get(Context context) { + if (prefs == null) { + prefs = new DefaultPrefs(context); + } + return prefs; + } +} diff --git a/app/src/main/java/io/github/droidkaigi/confsched/receiver/SessionScheduleReceiver.java b/app/src/main/java/io/github/droidkaigi/confsched/receiver/SessionScheduleReceiver.java index 43e8345..674c955 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/receiver/SessionScheduleReceiver.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/receiver/SessionScheduleReceiver.java @@ -17,7 +17,7 @@ import io.github.droidkaigi.confsched.activity.MainActivity; import io.github.droidkaigi.confsched.activity.SessionDetailActivity; import io.github.droidkaigi.confsched.model.Session; -import io.github.droidkaigi.confsched.util.PrefUtil; +import io.github.droidkaigi.confsched.prefs.DefaultPrefsSchema; public class SessionScheduleReceiver extends BroadcastReceiver { @@ -25,7 +25,7 @@ public class SessionScheduleReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { - boolean shouldNotify = PrefUtil.get(context, PrefUtil.KEY_NOTIFICATION_SETTING, true); + boolean shouldNotify = DefaultPrefsSchema.get(context).getNotificationFlag(true); if (!shouldNotify) { return; } @@ -45,7 +45,7 @@ public void onReceive(Context context, Intent intent) { String title = context.getResources().getQuantityString( R.plurals.schedule_notification_title, REMIND_MINUTES, REMIND_MINUTES); - boolean headsUp = PrefUtil.get(context, PrefUtil.KEY_HEADS_UP_SETTING, true); + boolean headsUp = DefaultPrefsSchema.get(context).getHeadsUpFlag(true); Notification notification = new NotificationCompat.Builder(context) .setContentIntent(pendingIntent) diff --git a/app/src/main/java/io/github/droidkaigi/confsched/util/LocaleUtil.java b/app/src/main/java/io/github/droidkaigi/confsched/util/LocaleUtil.java index 7eba372..a5a2125 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/util/LocaleUtil.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/util/LocaleUtil.java @@ -17,6 +17,8 @@ import java.util.TimeZone; import io.github.droidkaigi.confsched.BuildConfig; +import io.github.droidkaigi.confsched.prefs.DefaultPrefs; +import io.github.droidkaigi.confsched.prefs.DefaultPrefsSchema; public class LocaleUtil { @@ -60,7 +62,7 @@ public static void initLocale(Context context) { public static void setLocale(Context context, @NonNull String languageId) { Configuration config = new Configuration(); - PrefUtil.put(context, PrefUtil.KEY_CURRENT_LANGUAGE_ID, languageId); + DefaultPrefsSchema.get(context).putLanguageId(languageId); Locale locale = new Locale(languageId); Locale.setDefault(locale); config.locale = locale; @@ -68,10 +70,9 @@ public static void setLocale(Context context, @NonNull String languageId) { } public static String getCurrentLanguageId(Context context) { - String languageId = null; + String languageId = DefaultPrefsSchema.get(context).getLanguageId(); try { - languageId = PrefUtil.get(context, PrefUtil.KEY_CURRENT_LANGUAGE_ID, null); - if (languageId == null) { + if (TextUtils.isEmpty(languageId)) { languageId = Locale.getDefault().getLanguage().toLowerCase(); } if (!Arrays.asList(SUPPORT_LANG).contains(languageId)) { @@ -124,7 +125,7 @@ public static Date getConfTimezoneCurrentDate() { public static TimeZone getDisplayTimeZone(Context context) { TimeZone defaultTimeZone = TimeZone.getDefault(); - boolean shouldShowLocalTime = PrefUtil.get(context, PrefUtil.KEY_SHOW_LOCAL_TIME, false); + boolean shouldShowLocalTime = DefaultPrefsSchema.get(context).getShowLocalTimeFlag(false); return (shouldShowLocalTime && defaultTimeZone != null) ? defaultTimeZone : CONFERENCE_TIMEZONE; } diff --git a/app/src/main/java/io/github/droidkaigi/confsched/util/PrefUtil.java b/app/src/main/java/io/github/droidkaigi/confsched/util/PrefUtil.java deleted file mode 100644 index 1dc6509..0000000 --- a/app/src/main/java/io/github/droidkaigi/confsched/util/PrefUtil.java +++ /dev/null @@ -1,55 +0,0 @@ -package io.github.droidkaigi.confsched.util; - -import android.content.Context; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.support.annotation.NonNull; -import android.support.annotation.Nullable; - -public class PrefUtil { - - public static final String KEY_CURRENT_LANGUAGE_ID = "current_language_id"; - public static final String KEY_NOTIFICATION_SETTING = "notification_setting"; - public static final String KEY_HEADS_UP_SETTING = "heads_up_setting"; - public static final String KEY_SHOW_LOCAL_TIME = "show_local_time"; - - private static SharedPreferences pref; - - public static SharedPreferences getPref(Context context) { - if (pref == null) { - pref = PreferenceManager.getDefaultSharedPreferences(context); - } - return pref; - } - - public static void put(Context context, @NonNull String name, boolean value) { - SharedPreferences.Editor edit = getPref(context).edit(); - edit.putBoolean(name, value); - edit.apply(); - } - - public static void put(Context context, @NonNull String name, String value) { - SharedPreferences.Editor edit = getPref(context).edit(); - edit.putString(name, value); - edit.apply(); - } - - public static boolean contains(Context context, @NonNull String name) { - return getPref(context).contains(name); - } - - public static void remove(Context context, @NonNull String name) { - SharedPreferences.Editor edit = getPref(context).edit(); - edit.remove(name); - edit.apply(); - } - - public static String get(Context context, @NonNull String name, @Nullable String defaultValue) { - return getPref(context).getString(name, defaultValue); - } - - public static boolean get(Context context, @NonNull String name, boolean defaultValue) { - return getPref(context).getBoolean(name, defaultValue); - } -} - diff --git a/app/src/main/java/io/github/droidkaigi/confsched/widget/SettingSwitchRowView.java b/app/src/main/java/io/github/droidkaigi/confsched/widget/SettingSwitchRowView.java index bdce85d..50f0f8c 100644 --- a/app/src/main/java/io/github/droidkaigi/confsched/widget/SettingSwitchRowView.java +++ b/app/src/main/java/io/github/droidkaigi/confsched/widget/SettingSwitchRowView.java @@ -3,26 +3,20 @@ import android.content.Context; import android.content.res.TypedArray; import android.databinding.DataBindingUtil; -import android.text.TextUtils; import android.util.AttributeSet; -import android.util.Log; import android.view.LayoutInflater; -import android.widget.Checkable; import android.widget.CompoundButton; import android.widget.RelativeLayout; import io.github.droidkaigi.confsched.R; import io.github.droidkaigi.confsched.databinding.ViewSettingSwitchRowBinding; -import io.github.droidkaigi.confsched.util.PrefUtil; -public class SettingSwitchRowView extends RelativeLayout implements Checkable { +public class SettingSwitchRowView extends RelativeLayout { private static final String TAG = SettingSwitchRowView.class.getSimpleName(); private ViewSettingSwitchRowBinding binding; - private String prefKey; - private CompoundButton.OnCheckedChangeListener onCheckedChangeListener; public SettingSwitchRowView(Context context) { @@ -46,9 +40,8 @@ public SettingSwitchRowView(Context context, AttributeSet attrs, int defStyleAtt binding.settingTitle.setText(title); binding.settingDescription.setText(description); - binding.getRoot().setOnClickListener(v -> switchSetting()); + binding.getRoot().setOnClickListener(v -> toggle()); binding.settingSwitch.setOnCheckedChangeListener((buttonView, isChecked) -> { - setSetting(isChecked); if (onCheckedChangeListener != null) { onCheckedChangeListener.onCheckedChanged(buttonView, isChecked); } @@ -58,31 +51,8 @@ public SettingSwitchRowView(Context context, AttributeSet attrs, int defStyleAtt } } - public void init(String prefKey, boolean defaultValue) { - this.prefKey = prefKey; - boolean isChecked = PrefUtil.get(getContext(), prefKey, defaultValue); - binding.settingSwitch.setChecked(isChecked); - } - - private void setSetting(boolean isChecked) { - if (TextUtils.isEmpty(prefKey)) { - Log.d(TAG, "PrefKey must be set. Call init()"); - } else { - PrefUtil.put(getContext(), prefKey, isChecked); - } - } - - private void switchSetting() { - if (TextUtils.isEmpty(prefKey)) { - Log.d(TAG, "PrefKey must be set. Call init()"); - } else { - boolean newValue = !PrefUtil.get(getContext(), prefKey, true); - setSetting(newValue); - binding.settingSwitch.setChecked(newValue); - } - } - - public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener listener) { + public void init(boolean defaultValue, CompoundButton.OnCheckedChangeListener listener) { + binding.settingSwitch.setChecked(defaultValue); onCheckedChangeListener = listener; } @@ -101,19 +71,8 @@ public void setEnabled(boolean enabled) { } } - @Override - public void setChecked(boolean checked) { - setSetting(checked); - binding.settingSwitch.setChecked(checked); - } - - @Override - public boolean isChecked() { - return binding.settingSwitch.isChecked(); - } - - @Override - public void toggle() { - switchSetting(); + private void toggle() { + boolean isChecked = binding.settingSwitch.isChecked(); + binding.settingSwitch.setChecked(!isChecked); } }