improve event date formatting, handle events without year

This commit is contained in:
tibbi
2017-12-28 00:03:42 +01:00
parent 86be50d6da
commit 946b90cdda

View File

@ -28,6 +28,7 @@ import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS
import com.simplemobiletools.commons.helpers.getDateFormats
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.BuildConfig import com.simplemobiletools.contacts.BuildConfig
import com.simplemobiletools.contacts.R import com.simplemobiletools.contacts.R
@ -47,7 +48,8 @@ import kotlinx.android.synthetic.main.item_phone_number.view.*
import org.joda.time.DateTime import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat import org.joda.time.format.DateTimeFormat
import java.io.File import java.io.File
import java.lang.IllegalArgumentException import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.* import java.util.*
class ContactActivity : SimpleActivity() { class ContactActivity : SimpleActivity() {
@ -237,9 +239,8 @@ class ContactActivity : SimpleActivity() {
} }
(eventHolder as? ViewGroup)?.apply { (eventHolder as? ViewGroup)?.apply {
val date = getDateTime(event.value) getDateTime(event.value, contact_event)
contact_event.text = date.toString(DateTimeFormat.mediumDate()) contact_event.tag = event.value
contact_event.tag = date.toString()
contact_event.alpha = 1f contact_event.alpha = 1f
setupEventTypePicker(contact_event_type, contact_event, event.type) setupEventTypePicker(contact_event_type, contact_event, event.type)
} }
@ -339,7 +340,7 @@ class ContactActivity : SimpleActivity() {
val date = DateTime().withDate(year, monthOfYear + 1, dayOfMonth).withTimeAtStartOfDay() val date = DateTime().withDate(year, monthOfYear + 1, dayOfMonth).withTimeAtStartOfDay()
val formatted = date.toString(DateTimeFormat.mediumDate()) val formatted = date.toString(DateTimeFormat.mediumDate())
eventField.text = formatted eventField.text = formatted
eventField.tag = date.toString() eventField.tag = date.toString("yyyy-MM-dd")
eventField.alpha = 1f eventField.alpha = 1f
} }
@ -348,11 +349,27 @@ class ContactActivity : SimpleActivity() {
} }
} }
private fun getDateTime(dateString: String): DateTime { private fun getDateTime(dateString: String, viewToUpdate: TextView? = null): DateTime {
val dateFormats = getDateFormats()
var date = DateTime() var date = DateTime()
try { for (format in dateFormats) {
date = DateTime.parse(dateString) try {
} catch (ignored: IllegalArgumentException) { date = DateTime.parse(dateString, DateTimeFormat.forPattern(format))
val formatter = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.getDefault())
var localPattern = (formatter as SimpleDateFormat).toLocalizedPattern()
val hasYear = format.contains("y")
if (!hasYear) {
localPattern = localPattern.replace("y", "").trim()
date = date.withYear(DateTime().year)
}
val formattedString = date.toString(localPattern)
viewToUpdate?.text = formattedString
break
} catch (ignored: Exception) {
}
} }
return date return date
} }
@ -471,9 +488,7 @@ class ContactActivity : SimpleActivity() {
val eventType = getEventTypeId(eventHolder.contact_event_type.value) val eventType = getEventTypeId(eventHolder.contact_event_type.value)
if (event.isNotEmpty() && event != unknown) { if (event.isNotEmpty() && event != unknown) {
val date = getDateTime(eventHolder.contact_event.tag.toString()) events.add(Event(eventHolder.contact_event.tag.toString(), eventType))
val formattedEventDate = date.toString(DateTimeFormat.shortDate())
events.add(Event(formattedEventDate, eventType))
} }
} }
return events return events