Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show local time on/Off + Device Time Zone Change #361

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ dependencies {
// Android Utility
compile 'com.deploygate:sdk:3.1.1'
compile 'com.jakewharton.timber:timber:4.5.1'
compile 'com.jakewharton.threetenabp:threetenabp:1.0.5'
debugCompile 'com.facebook.stetho:stetho:1.4.2'
debugCompile 'com.facebook.stetho:stetho-okhttp3:1.4.2'
debugCompile 'com.facebook.stetho:stetho-timber:1.4.2@aar'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.droidkaigi.confsched2017;

import com.deploygate.sdk.DeployGate;
import com.jakewharton.threetenabp.AndroidThreeTen;
import com.squareup.leakcanary.LeakCanary;
import com.tomoima.debot.DebotConfigurator;
import com.tomoima.debot.DebotStrategyBuilder;
Expand Down Expand Up @@ -56,6 +57,7 @@ public AppComponent getComponent() {
@Override
public void onCreate() {
super.onCreate();
AndroidThreeTen.init(this);

appComponent = DaggerAppComponent.builder()
.appModule(new AppModule(this))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

import com.jakewharton.retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

import org.threeten.bp.ZonedDateTime;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
Expand All @@ -19,6 +21,8 @@
import io.github.droidkaigi.confsched2017.api.service.GoogleFormService;
import io.github.droidkaigi.confsched2017.model.OrmaDatabase;
import io.github.droidkaigi.confsched2017.pref.DefaultPrefs;
import io.github.droidkaigi.confsched2017.util.ZonedDateTimeDeserializer;
import io.github.droidkaigi.confsched2017.util.ZonedDateTimeSerializer;
import io.reactivex.disposables.CompositeDisposable;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
Expand Down Expand Up @@ -105,6 +109,10 @@ public GoogleFormService provideGoogleFormService(OkHttpClient client) {
}

private static Gson createGson() {
return new GsonBuilder().setDateFormat("yyyy/MM/dd HH:mm:ss").create();
return new GsonBuilder()
.setDateFormat("yyyy/MM/dd HH:mm:ss")
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeSerializer())
.registerTypeAdapter(ZonedDateTime.class, new ZonedDateTimeDeserializer())
.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import com.github.gfx.android.orma.annotation.PrimaryKey;
import com.github.gfx.android.orma.annotation.Table;

import android.support.annotation.Nullable;
import org.threeten.bp.ZonedDateTime;

import java.util.Date;
import android.support.annotation.Nullable;

@Table
public class Session {
Expand All @@ -34,11 +34,11 @@ public class Session {

@Column
@SerializedName("stime")
public Date stime;
public ZonedDateTime stime;

@Column
@SerializedName("etime")
public Date etime;
public ZonedDateTime etime;

@Column
@SerializedName("duration_min")
Expand Down Expand Up @@ -107,8 +107,8 @@ public boolean isDinner() {
return Type.DINNER.matches(type);
}

public boolean isLiveAt(Date when) {
return stime.before(when) && etime.after(when);
public boolean isLiveAt(ZonedDateTime when) {
return stime.isBefore(when)&& etime.isAfter(when);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class AlarmUtil {
private static final long REMIND_DURATION_MINUTES_FOR_START = TimeUnit.MINUTES.toMillis(10);

public static void registerAlarm(@NonNull Context context, @NonNull Session session) {
long time = session.stime.getTime() - REMIND_DURATION_MINUTES_FOR_START;
long time = session.stime.toInstant().toEpochMilli() - REMIND_DURATION_MINUTES_FOR_START;

DefaultPrefs prefs = DefaultPrefs.get(context);
if (prefs.getNotificationTestFlag()) {
Expand Down Expand Up @@ -51,8 +51,8 @@ private static PendingIntent createAlarmIntent(@NonNull Context context, @NonNul
room = session.room.name;
}
String text = context.getString(R.string.notification_message,
DateUtil.getHourMinute(displaySTime),
DateUtil.getHourMinute(displayETime),
DateUtil.getHourMinute(displaySTime, context),
DateUtil.getHourMinute(displayETime, context),
room);
Intent intent = NotificationReceiver.createIntent(context, session.id, title, text);
return PendingIntent.getBroadcast(context, session.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static String getMonthDate(Date date, Context context) {
}

@NonNull
public static String getHourMinute(Date date) {
public static String getHourMinute(Date date, Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), FORMAT_KKMM);
return new SimpleDateFormat(pattern, Locale.getDefault()).format(date);
Expand All @@ -49,11 +49,10 @@ public static String getHourMinute(Date date) {
}

@NonNull
public static String getLongFormatDate(@Nullable Date date) {
public static String getLongFormatDate(@Nullable Date date, Context context) {
if (date == null) {
return "";
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
String pattern = DateFormat.getBestDateTimePattern(Locale.getDefault(), FORMAT_YYYYMMDDKKMM);
return new SimpleDateFormat(pattern, Locale.getDefault()).format(date);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.github.droidkaigi.confsched2017.util;

import com.github.gfx.android.orma.annotation.StaticTypeAdapter;
import com.github.gfx.android.orma.annotation.StaticTypeAdapters;

import org.threeten.bp.LocalDateTime;
import org.threeten.bp.ZoneId;
import org.threeten.bp.ZonedDateTime;
import org.threeten.bp.format.DateTimeFormatter;

import io.github.droidkaigi.confsched2017.BuildConfig;

@StaticTypeAdapters({
@StaticTypeAdapter(
targetType = ZonedDateTime.class,
serializedType = String.class,
serializer = "serializeZonedDateTime",
deserializer = "deserializeZonedDateTime"
)
})
public class DroidKaigiTypeAdapters {

private static final ZoneId CONFERENCE_TIMEZONE = ZoneId.of(BuildConfig.CONFERENCE_TIMEZONE);
public static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");

public static String serializeZonedDateTime(ZonedDateTime time) {
return time.format(formatter);
}

public static ZonedDateTime deserializeZonedDateTime(String serialized) {
return LocalDateTime.parse(serialized, formatter).atZone(CONFERENCE_TIMEZONE);
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package io.github.droidkaigi.confsched2017.util;

import org.threeten.bp.DateTimeUtils;
import org.threeten.bp.Instant;
import org.threeten.bp.LocalDateTime;
import org.threeten.bp.ZoneId;
import org.threeten.bp.ZoneOffset;
import org.threeten.bp.ZonedDateTime;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.text.TextUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
Expand All @@ -30,6 +34,10 @@ public class LocaleUtil {

private static final TimeZone CONFERENCE_TIMEZONE = TimeZone.getTimeZone(BuildConfig.CONFERENCE_TIMEZONE);

private static final ZoneOffset CONFERENCE_ZONE_OFFSET = LocalDateTime.now()
.atZone(ZoneId.of(BuildConfig.CONFERENCE_TIMEZONE))
.getOffset();

public static void initLocale(Context context) {
setLocale(context, getCurrentLanguageId(context));
}
Expand Down Expand Up @@ -105,22 +113,23 @@ public static int getLanguage(@NonNull String languageId) {
}
}

public static Date getDisplayDate(@NonNull Date date, Context context) {
DateFormat formatTokyo = SimpleDateFormat.getDateTimeInstance();
formatTokyo.setTimeZone(CONFERENCE_TIMEZONE);
DateFormat formatLocal = SimpleDateFormat.getDateTimeInstance();
formatLocal.setTimeZone(getDisplayTimeZone(context));
try {
return formatLocal.parse(formatTokyo.format(date));
} catch (ParseException e) {
Timber.tag(TAG).e(e, "date: %s can not parse.", date.toString());
return date;
public static Date getDisplayDate(@NonNull ZonedDateTime date, Context context) {
Instant instant;
if (DefaultPrefs.get(context).getShowLocalTimeFlag()) {
instant = date.toInstant();
} else {
instant = date.withZoneSameInstant(ZoneId.systemDefault())
.minusSeconds(CONFERENCE_ZONE_OFFSET.compareTo(ZonedDateTime.now().getOffset()))
.toInstant();
}
return DateTimeUtils.toDate(instant);
}

public static TimeZone getDisplayTimeZone(Context context) {
TimeZone defaultTimeZone = TimeZone.getDefault();
boolean shouldShowLocalTime = DefaultPrefs.get(context).getShowLocalTimeFlag();
return (shouldShowLocalTime && defaultTimeZone != null) ? defaultTimeZone : CONFERENCE_TIMEZONE;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.droidkaigi.confsched2017.util;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;

import org.threeten.bp.ZonedDateTime;

import java.lang.reflect.Type;

public class ZonedDateTimeDeserializer implements JsonDeserializer<ZonedDateTime> {
@Override
public ZonedDateTime deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws
JsonParseException {
return DroidKaigiTypeAdapters.deserializeZonedDateTime(jsonElement.getAsString());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.droidkaigi.confsched2017.util;

import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import org.threeten.bp.ZonedDateTime;

import java.lang.reflect.Type;

public class ZonedDateTimeSerializer implements JsonSerializer<ZonedDateTime> {
@Override
public JsonElement serialize(ZonedDateTime zonedDateTime, Type type, JsonSerializationContext jsonSerializationContext) {
return new JsonPrimitive(zonedDateTime.format(DroidKaigiTypeAdapters.formatter));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.lucasr.twowayview.TwoWayLayoutManager;
import org.lucasr.twowayview.widget.DividerItemDecoration;
import org.lucasr.twowayview.widget.SpannableGridLayoutManager;
import org.threeten.bp.ZonedDateTime;

import android.content.Context;
import android.graphics.Point;
Expand All @@ -26,7 +27,6 @@
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.Date;
import java.util.List;
import java.util.Locale;

Expand Down Expand Up @@ -200,7 +200,7 @@ public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
}

private void renderSessions(List<SessionViewModel> adjustedSessionViewModels) {
List<Date> stimes = viewModel.getStimes();
List<ZonedDateTime> stimes = viewModel.getStimes();
List<Room> rooms = viewModel.getRooms();

if (binding.recyclerView.getLayoutManager() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ private String decideSessionTimeRange(Context context, Session session) {
Date displayETime = LocaleUtil.getDisplayDate(session.etime, context);

return context.getString(R.string.session_time_range,
DateUtil.getLongFormatDate(displaySTime),
DateUtil.getHourMinute(displayETime),
DateUtil.getLongFormatDate(displaySTime, context),
DateUtil.getHourMinute(displayETime, context),
DateUtil.getMinutes(displaySTime, displayETime));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ private String decideSessionTimeRange(Context context, Session session) {
Date displayETime = LocaleUtil.getDisplayDate(session.etime, context);

return context.getString(R.string.session_time_range,
DateUtil.getLongFormatDate(displaySTime),
DateUtil.getHourMinute(displayETime),
DateUtil.getMinutes(displaySTime, displayETime));
DateUtil.getLongFormatDate(displaySTime, context),
DateUtil.getHourMinute(displayETime, context),
session.durationMin);
}

public String getSessionTitle() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.github.droidkaigi.confsched2017.viewmodel;

import org.threeten.bp.ZonedDateTime;

import android.content.Context;
import android.databinding.BaseObservable;
import android.databinding.Bindable;
Expand All @@ -17,6 +19,7 @@
import io.github.droidkaigi.confsched2017.util.AlarmUtil;
import io.github.droidkaigi.confsched2017.util.DateUtil;
import io.github.droidkaigi.confsched2017.view.helper.Navigator;
import io.github.droidkaigi.confsched2017.util.LocaleUtil;
import timber.log.Timber;

public class SessionViewModel extends BaseObservable implements ViewModel {
Expand Down Expand Up @@ -69,8 +72,10 @@ public class SessionViewModel extends BaseObservable implements ViewModel {
MySessionsRepository mySessionsRepository) {
this.session = session;
this.navigator = navigator;
this.shortStime = DateUtil.getHourMinute(session.stime);
this.formattedDate = DateUtil.getMonthDate(session.stime, context);

Date displayDate = LocaleUtil.getDisplayDate(session.stime, context);
this.shortStime = DateUtil.getHourMinute(displayDate, context);
this.formattedDate = DateUtil.getMonthDate(displayDate, context);
this.title = session.title;

if (session.speaker != null) {
Expand Down Expand Up @@ -98,7 +103,7 @@ public class SessionViewModel extends BaseObservable implements ViewModel {
this.normalSessionItemVisibility = View.GONE;
} else {
this.isClickable = true;
this.backgroundResId = session.isLiveAt(new Date()) ? R.drawable.clickable_purple : R.drawable.clickable_white;
this.backgroundResId = session.isLiveAt(ZonedDateTime.now()) ? R.drawable.clickable_purple : R.drawable.clickable_white;
this.topicColorResId = TopicColor.from(session.topic).middleColorResId;
this.normalSessionItemVisibility = View.VISIBLE;
}
Expand Down Expand Up @@ -143,7 +148,7 @@ private void decideRowSpan(@NonNull Session session) {
}
}

Date getStime() {
ZonedDateTime getStime() {
return session.stime;
}

Expand Down
Loading