Merge pull request #402 from fraang/master

Improve notification content by displaying date if useful
This commit is contained in:
Tibor Kaputa 2018-03-21 12:57:33 +01:00 committed by GitHub
commit 1f927876d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 3 deletions

View File

@ -37,6 +37,7 @@ import com.simplemobiletools.commons.helpers.isMarshmallowPlus
import com.simplemobiletools.commons.helpers.isOreoPlus
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.LocalDate
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
@ -157,9 +158,19 @@ fun Context.notifyEvent(event: Event) {
val pendingIntent = getPendingIntent(applicationContext, event)
val startTime = Formatter.getTimeFromTS(applicationContext, event.startTS)
val endTime = Formatter.getTimeFromTS(applicationContext, event.endTS)
val startDate = Formatter.getDateFromTS(event.startTS)
val displayedStartDate: String
if (startDate == LocalDate.now()) {
displayedStartDate = ""
} else if (startDate == LocalDate.now().plusDays(1)) {
displayedStartDate = getString(R.string.tomorrow)
} else /* At least 2 days in the future */ {
displayedStartDate = Formatter.getDayAndMonth(startDate)
}
val timeRange = if (event.getIsAllDay()) getString(R.string.all_day) else getFormattedEventTime(startTime, endTime)
val descriptionOrLocation = if (config.replaceDescription) event.location else event.description
val notification = getNotification(applicationContext, pendingIntent, event, "$timeRange $descriptionOrLocation")
val content = arrayOf(displayedStartDate, timeRange, descriptionOrLocation).joinToString(" ")
val notification = getNotification(applicationContext, pendingIntent, event, content)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(event.id, notification)
}
@ -210,7 +221,7 @@ private fun getNotification(context: Context, pendingIntent: PendingIntent, even
return builder.build()
}
private fun getFormattedEventTime(startTime: String, endTime: String) = if (startTime == endTime) startTime else "$startTime - $endTime"
private fun getFormattedEventTime(startTime: String, endTime: String) = if (startTime == endTime) startTime else "$startTime\u2013$endTime"
private fun getPendingIntent(context: Context, event: Event): PendingIntent {
val intent = Intent(context, EventActivity::class.java)

View File

@ -7,7 +7,9 @@ import com.simplemobiletools.calendar.extensions.getNowSeconds
import com.simplemobiletools.calendar.extensions.seconds
import org.joda.time.DateTime
import org.joda.time.DateTimeZone
import org.joda.time.LocalDate
import org.joda.time.format.DateTimeFormat
import java.text.DateFormat
object Formatter {
val DAYCODE_PATTERN = "YYYYMMdd"
@ -76,6 +78,8 @@ object Formatter {
fun getDayCodeFromDateTime(dateTime: DateTime) = dateTime.toString(DAYCODE_PATTERN)
fun getDateFromTS(ts: Int) = LocalDate(ts * 1000L, DateTimeZone.getDefault())
fun getDateTimeFromTS(ts: Int) = DateTime(ts * 1000L, DateTimeZone.getDefault())
fun getUTCDateTimeFromTS(ts: Int) = DateTime(ts * 1000L, DateTimeZone.UTC)
@ -104,4 +108,6 @@ object Formatter {
}
fun getShiftedImportTimestamp(ts: Int) = getUTCDateTimeFromTS(ts).withTime(13, 0, 0, 0).withZoneRetainFields(DateTimeZone.getDefault()).seconds()
fun getDayAndMonth(localDate: LocalDate) = localDate.toString("dd.MM.")
}

View File

@ -89,6 +89,7 @@
<!-- Event Reminders -->
<string name="reminder">Erinnerung</string>
<string name="tomorrow">Morgen</string>
<string name="before">vorher</string>
<string name="add_another_reminder">Füge eine weitere Erinnerung hinzu</string>
<string name="event_reminders">Ereignis Erinnerungen</string>

View File

@ -178,7 +178,7 @@
<string name="sample_description_1">Kojų diena</string>
<string name="sample_title_2">Susitikimas su Jonu</string>
<string name="sample_description_2">Akmenų sode</string>
<string name="sample_title_3">Bibliotekoje/string>
<string name="sample_title_3">Bibliotekoje</string>
<string name="sample_title_4">Pietūs su Marija</string>
<string name="sample_description_4">Dangoraižyje</string>
<string name="sample_title_5">Kavos laikas</string>

View File

@ -89,6 +89,7 @@
<!-- Event Reminders -->
<string name="reminder">Reminder</string>
<string name="tomorrow">Tomorrow</string>
<string name="before">before</string>
<string name="add_another_reminder">Add another reminder</string>
<string name="event_reminders">Event reminders</string>