fix #264, allow inserting contact events without a year

This commit is contained in:
tibbi 2020-07-18 17:04:21 +02:00
parent 484d9c5abd
commit 133d0a4a98
5 changed files with 91 additions and 14 deletions

View File

@ -57,7 +57,7 @@ android {
} }
dependencies { dependencies {
implementation 'com.simplemobiletools:commons:5.29.17' implementation 'com.simplemobiletools:commons:5.29.18'
implementation 'joda-time:joda-time:2.10.1' implementation 'joda-time:joda-time:2.10.1'
implementation 'com.googlecode.ez-vcard:ez-vcard:0.10.5' implementation 'com.googlecode.ez-vcard:ez-vcard:0.10.5'
implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a' implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a'

View File

@ -1,7 +1,6 @@
package com.simplemobiletools.contacts.pro.activities package com.simplemobiletools.contacts.pro.activities
import android.app.Activity import android.app.Activity
import android.app.DatePickerDialog
import android.content.ClipData import android.content.ClipData
import android.content.ContentValues import android.content.ContentValues
import android.content.Intent import android.content.Intent
@ -24,6 +23,7 @@ import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog import com.simplemobiletools.contacts.pro.dialogs.CustomLabelDialog
import com.simplemobiletools.contacts.pro.dialogs.MyDatePickerDialog
import com.simplemobiletools.contacts.pro.dialogs.SelectGroupsDialog import com.simplemobiletools.contacts.pro.dialogs.SelectGroupsDialog
import com.simplemobiletools.contacts.pro.extensions.* import com.simplemobiletools.contacts.pro.extensions.*
import com.simplemobiletools.contacts.pro.helpers.* import com.simplemobiletools.contacts.pro.helpers.*
@ -39,8 +39,6 @@ import kotlinx.android.synthetic.main.item_edit_im.view.*
import kotlinx.android.synthetic.main.item_edit_phone_number.view.* import kotlinx.android.synthetic.main.item_edit_phone_number.view.*
import kotlinx.android.synthetic.main.item_edit_website.view.* import kotlinx.android.synthetic.main.item_edit_website.view.*
import kotlinx.android.synthetic.main.item_event.view.* import kotlinx.android.synthetic.main.item_event.view.*
import org.joda.time.DateTime
import org.joda.time.format.DateTimeFormat
class EditContactActivity : ContactActivity() { class EditContactActivity : ContactActivity() {
private val INTENT_TAKE_PHOTO = 1 private val INTENT_TAKE_PHOTO = 1
@ -689,19 +687,13 @@ class EditContactActivity : ContactActivity() {
val eventField = eventHolder.contact_event val eventField = eventHolder.contact_event
eventField.setOnClickListener { eventField.setOnClickListener {
val setDateListener = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> MyDatePickerDialog(this, eventField.tag?.toString() ?: "") { dateTag ->
eventHolder.contact_event_remove.beVisible()
val date = DateTime().withDate(year, monthOfYear + 1, dayOfMonth).withTimeAtStartOfDay()
val formatted = date.toString(DateTimeFormat.mediumDate())
eventField.apply { eventField.apply {
text = formatted dateTag.getDateTimeFromDateString(this)
tag = date.toString("yyyy-MM-dd") tag = dateTag
alpha = 1f alpha = 1f
} }
} }
val date = (eventField.tag?.toString() ?: "").getDateTimeFromDateString()
DatePickerDialog(this, getDialogTheme(), setDateListener, date.year, date.monthOfYear - 1, date.dayOfMonth).show()
} }
eventHolder.contact_event_remove.apply { eventHolder.contact_event_remove.apply {

View File

@ -0,0 +1,58 @@
package com.simplemobiletools.contacts.pro.dialogs
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.contacts.pro.R
import kotlinx.android.synthetic.main.dialog_date_picker.view.*
import org.joda.time.DateTime
import java.util.*
class MyDatePickerDialog(val activity: BaseSimpleActivity, val defaultDate: String, val callback: (dateTag: String) -> Unit) {
private var view = activity.layoutInflater.inflate(R.layout.dialog_date_picker, null)
init {
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok) { dialog, which -> dialogConfirmed() }
.setNegativeButton(R.string.cancel, null)
.create().apply {
activity.setupDialogStuff(view, this) {
val today = Calendar.getInstance()
var year = today.get(Calendar.YEAR)
var month = today.get(Calendar.MONTH)
var day = today.get(Calendar.DAY_OF_MONTH)
if (defaultDate.isNotEmpty()) {
val ignoreYear = defaultDate.startsWith("-")
view.hide_year.isChecked = ignoreYear
if (ignoreYear) {
month = defaultDate.substring(2, 4).toInt() - 1
day = defaultDate.substring(5, 7).toInt()
} else {
year = defaultDate.substring(0, 4).toInt()
month = defaultDate.substring(5, 7).toInt() - 1
day = defaultDate.substring(8, 10).toInt()
}
}
view.date_picker.updateDate(year, month, day)
}
}
}
private fun dialogConfirmed() {
val year = view.date_picker.year
val month = view.date_picker.month + 1
val day = view.date_picker.dayOfMonth
val date = DateTime().withDate(year, month, day).withTimeAtStartOfDay()
val tag = if (view.hide_year.isChecked) {
date.toString("--MM-dd")
} else {
date.toString("yyyy-MM-dd")
}
callback(tag)
}
}

View File

@ -20,7 +20,7 @@ fun String.getDateTimeFromDateString(viewToUpdate: TextView? = null): DateTime {
val hasYear = format.contains("y") val hasYear = format.contains("y")
if (!hasYear) { if (!hasYear) {
localPattern = localPattern.replace("y", "").trim() localPattern = localPattern.replace("y", "").replace(",", "").trim()
date = date.withYear(DateTime().year) date = date.withYear(DateTime().year)
} }

View File

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dialog_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<DatePicker
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" />
<com.simplemobiletools.commons.views.MyAppCompatCheckbox
android:id="@+id/hide_year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/date_picker"
android:layout_alignStart="@+id/date_picker"
android:layout_marginStart="@dimen/activity_margin"
android:paddingStart="@dimen/medium_margin"
android:paddingTop="@dimen/medium_margin"
android:paddingEnd="@dimen/activity_margin"
android:paddingBottom="@dimen/medium_margin"
android:text="@string/hide_year" />
</RelativeLayout>