use 12 or 24 hours format as appropriate

This commit is contained in:
tibbi 2016-12-18 17:13:30 +01:00
parent ecd384f1af
commit 32dc35b6c7
7 changed files with 27 additions and 23 deletions

View File

@ -290,19 +290,19 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
}
private fun updateStartDate() {
event_start_date.text = Formatter.getEventDate(applicationContext, mEventStartDateTime)
event_start_date.text = Formatter.getDate(applicationContext, mEventStartDateTime)
}
private fun updateStartTime() {
event_start_time.text = Formatter.getEventTime(mEventStartDateTime)
event_start_time.text = Formatter.getTime(this, mEventStartDateTime)
}
private fun updateEndDate() {
event_end_date.text = Formatter.getEventDate(applicationContext, mEventEndDateTime)
event_end_date.text = Formatter.getDate(applicationContext, mEventEndDateTime)
}
private fun updateEndTime() {
event_end_time.text = Formatter.getEventTime(mEventEndDateTime)
event_end_time.text = Formatter.getTime(this, mEventEndDateTime)
}
fun setupStartDate() {

View File

@ -51,7 +51,7 @@ class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>,
mTopDivider = activity.resources.getDrawable(R.drawable.divider)
mOrangeColor = activity.resources.getColor(R.color.colorPrimary)
val mTodayCode = Formatter.getDayCodeFromTS(mNow)
mTodayDate = Formatter.getEventDate(activity, mTodayCode)
mTodayDate = Formatter.getDate(activity, mTodayCode)
}
val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) {
@ -121,19 +121,19 @@ class EventListAdapter(val activity: SimpleActivity, val mItems: List<ListItem>,
itemView.apply {
event_item_title.text = item.title
event_item_description.text = item.description
event_item_start.text = Formatter.getTime(item.startTS)
event_item_start.text = Formatter.getTimeFromTS(context, item.startTS)
toggleItemSelection(this, markedItems.contains(pos), pos)
if (item.startTS == item.endTS) {
event_item_end.visibility = View.INVISIBLE
} else {
event_item_end.text = Formatter.getTime(item.endTS)
event_item_end.text = Formatter.getTimeFromTS(context, item.endTS)
event_item_end.visibility = View.VISIBLE
val startCode = Formatter.getDayCodeFromTS(item.startTS)
val endCode = Formatter.getDayCodeFromTS(item.endTS)
if (startCode != endCode) {
event_item_end.append(" (${Formatter.getEventDate(context, endCode)})")
event_item_end.append(" (${Formatter.getDate(context, endCode)})")
}
}

View File

@ -46,8 +46,8 @@ class EventListWidgetAdapter(val context: Context, val intent: Intent) : RemoteV
remoteView.apply {
setTextViewText(R.id.event_item_title, item.title)
setTextViewText(R.id.event_item_description, item.description)
setTextViewText(R.id.event_item_start, Formatter.getTime(item.startTS))
setTextViewText(R.id.event_item_end, Formatter.getTime(item.endTS))
setTextViewText(R.id.event_item_start, Formatter.getTimeFromTS(context, item.startTS))
setTextViewText(R.id.event_item_end, Formatter.getTimeFromTS(context, item.endTS))
setInt(R.id.event_item_title, "setTextColor", textColor)
setInt(R.id.event_item_description, "setTextColor", textColor)

View File

@ -50,18 +50,18 @@ class EventListWidgetAdapterOld(val context: Context, val mEvents: List<ListItem
viewHolder.apply {
title.text = item.title
description?.text = item.description
start?.text = Formatter.getTime(item.startTS)
start?.text = Formatter.getTimeFromTS(context, item.startTS)
if (item.startTS == item.endTS) {
end?.visibility = View.INVISIBLE
} else {
end?.text = Formatter.getTime(item.endTS)
end?.text = Formatter.getTimeFromTS(context, item.endTS)
end?.visibility = View.VISIBLE
val startCode = Formatter.getDayCodeFromTS(item.startTS)
val endCode = Formatter.getDayCodeFromTS(item.endTS)
if (startCode != endCode) {
end?.append(" (${Formatter.getEventDate(context, endCode)})")
end?.append(" (${Formatter.getDate(context, endCode)})")
}
}

View File

@ -93,13 +93,13 @@ class EventsAdapter(val activity: SimpleActivity, val mItems: List<Event>, val l
itemView.apply {
event_item_title.text = event.title
event_item_description.text = event.description
event_item_start.text = Formatter.getTime(event.startTS)
event_item_start.text = Formatter.getTimeFromTS(context, event.startTS)
toggleItemSelection(this, markedItems.contains(pos), pos)
if (event.startTS == event.endTS) {
event_item_end.visibility = View.INVISIBLE
} else {
event_item_end.text = Formatter.getTime(event.endTS)
event_item_end.text = Formatter.getTimeFromTS(context, event.endTS)
event_item_end.visibility = View.VISIBLE
}

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.calendar.helpers
import android.content.Context
import android.text.format.DateFormat
import com.simplemobiletools.calendar.R
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
@ -11,9 +12,10 @@ object Formatter {
val YEAR_PATTERN = "YYYY"
private val DAY_PATTERN = "d"
private val DAY_OF_WEEK_PATTERN = "EEE"
private val EVENT_TIME_PATTERN = "HH:mm"
private val PATTERN_TIME_12 = "h:mm a"
private val PATTERN_TIME_24 = "k:mm"
fun getEventDate(context: Context, dayCode: String): String {
fun getDate(context: Context, dayCode: String): String {
val dateTime = getDateTimeFromCode(dayCode)
val day = dateTime.toString(DAY_PATTERN)
val year = dateTime.toString(YEAR_PATTERN)
@ -26,21 +28,21 @@ object Formatter {
}
fun getDayTitle(context: Context, dayCode: String): String {
val date = getEventDate(context, dayCode)
val date = getDate(context, dayCode)
val dateTime = getDateTimeFromCode(dayCode)
val day = dateTime.toString(DAY_OF_WEEK_PATTERN)
return "$date ($day)"
}
fun getEventDate(context: Context, dateTime: DateTime) = getDayTitle(context, getDayCodeFromDateTime(dateTime))
fun getDate(context: Context, dateTime: DateTime) = getDayTitle(context, getDayCodeFromDateTime(dateTime))
fun getEventTime(dateTime: DateTime) = dateTime.toString(EVENT_TIME_PATTERN)
fun getTime(context: Context, dateTime: DateTime) = dateTime.toString(getTimePattern(context))
fun getDateTimeFromCode(dayCode: String) = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.UTC).parseDateTime(dayCode)
fun getLocalDateTimeFromCode(dayCode: String) = DateTimeFormat.forPattern(DAYCODE_PATTERN).withZone(DateTimeZone.getDefault()).parseDateTime(dayCode)
fun getTime(ts: Int) = getEventTime(getDateTimeFromTS(ts))
fun getTimeFromTS(context: Context, ts: Int) = getTime(context, getDateTimeFromTS(ts))
fun getDayStartTS(dayCode: String) = (getLocalDateTimeFromCode(dayCode).millis / 1000).toInt()
@ -54,4 +56,6 @@ object Formatter {
// use manually translated month names, as DateFormat and Joda have issues with a lot of languages
fun getMonthName(context: Context, id: Int) = context.resources.getStringArray(R.array.months)[id]
fun getTimePattern(context: Context) = if (DateFormat.is24HourFormat(context)) PATTERN_TIME_24 else PATTERN_TIME_12
}

View File

@ -30,8 +30,8 @@ class NotificationReceiver : BroadcastReceiver() {
return
val pendingIntent = getPendingIntent(context, event)
val startTime = Formatter.getTime(event.startTS)
val endTime = Formatter.getTime(event.endTS)
val startTime = Formatter.getTimeFromTS(context, event.startTS)
val endTime = Formatter.getTimeFromTS(context, event.endTS)
val title = event.title
val notification = getNotification(context, pendingIntent, "${getEventTime(startTime, endTime)} $title")
notificationManager.notify(id, notification)