From 133d0a4a98c225586a5f8b7cbe42e4b9332b6c79 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 18 Jul 2020 17:04:21 +0200 Subject: [PATCH] fix #264, allow inserting contact events without a year --- app/build.gradle | 2 +- .../pro/activities/EditContactActivity.kt | 16 ++--- .../pro/dialogs/MyDatePickerDialog.kt | 58 +++++++++++++++++++ .../contacts/pro/extensions/String.kt | 2 +- .../main/res/layout/dialog_date_picker.xml | 27 +++++++++ 5 files changed, 91 insertions(+), 14 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/MyDatePickerDialog.kt create mode 100644 app/src/main/res/layout/dialog_date_picker.xml diff --git a/app/build.gradle b/app/build.gradle index 22c2eeb0..ad1d9f26 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -57,7 +57,7 @@ android { } dependencies { - implementation 'com.simplemobiletools:commons:5.29.17' + implementation 'com.simplemobiletools:commons:5.29.18' implementation 'joda-time:joda-time:2.10.1' implementation 'com.googlecode.ez-vcard:ez-vcard:0.10.5' implementation 'com.github.tibbi:IndicatorFastScroll:08f512858a' diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt index 0233321e..2e463177 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/EditContactActivity.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.contacts.pro.activities import android.app.Activity -import android.app.DatePickerDialog import android.content.ClipData import android.content.ContentValues import android.content.Intent @@ -24,6 +23,7 @@ import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.contacts.pro.R 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.extensions.* 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_website.view.* import kotlinx.android.synthetic.main.item_event.view.* -import org.joda.time.DateTime -import org.joda.time.format.DateTimeFormat class EditContactActivity : ContactActivity() { private val INTENT_TAKE_PHOTO = 1 @@ -689,19 +687,13 @@ class EditContactActivity : ContactActivity() { val eventField = eventHolder.contact_event eventField.setOnClickListener { - val setDateListener = DatePickerDialog.OnDateSetListener { view, year, monthOfYear, dayOfMonth -> - eventHolder.contact_event_remove.beVisible() - val date = DateTime().withDate(year, monthOfYear + 1, dayOfMonth).withTimeAtStartOfDay() - val formatted = date.toString(DateTimeFormat.mediumDate()) + MyDatePickerDialog(this, eventField.tag?.toString() ?: "") { dateTag -> eventField.apply { - text = formatted - tag = date.toString("yyyy-MM-dd") + dateTag.getDateTimeFromDateString(this) + tag = dateTag 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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/MyDatePickerDialog.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/MyDatePickerDialog.kt new file mode 100644 index 00000000..4c87f576 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/dialogs/MyDatePickerDialog.kt @@ -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) + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt index 9f775100..33257efb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/String.kt @@ -20,7 +20,7 @@ fun String.getDateTimeFromDateString(viewToUpdate: TextView? = null): DateTime { val hasYear = format.contains("y") if (!hasYear) { - localPattern = localPattern.replace("y", "").trim() + localPattern = localPattern.replace("y", "").replace(",", "").trim() date = date.withYear(DateTime().year) } diff --git a/app/src/main/res/layout/dialog_date_picker.xml b/app/src/main/res/layout/dialog_date_picker.xml new file mode 100644 index 00000000..c8f42195 --- /dev/null +++ b/app/src/main/res/layout/dialog_date_picker.xml @@ -0,0 +1,27 @@ + + + + + + + +