Skip to content

Commit

Permalink
Merge pull request #311 from rejasupotaro/introduce-kvs-schema
Browse files Browse the repository at this point in the history
[Proposal] Introduce kvs-schema
  • Loading branch information
konifar committed Feb 22, 2016
2 parents 264dbd5 + 655310d commit 90bbf0e
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 123 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
});
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
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 {

private static final int REMIND_MINUTES = 5;

@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;
}
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -60,18 +62,17 @@ 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;
context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
}

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)) {
Expand Down Expand Up @@ -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;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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);
}
Expand All @@ -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;
}

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

0 comments on commit 90bbf0e

Please sign in to comment.