avoid adding the same contact birthday multiple times

This commit is contained in:
tibbi 2017-10-21 16:06:38 +02:00
parent 0b1b6281d2
commit f2a89e8b77
2 changed files with 16 additions and 5 deletions

View File

@ -307,6 +307,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
eventTypeId = dbHelper.insertEventType(eventType) eventTypeId = dbHelper.insertEventType(eventType)
} }
val birthdayImportIDs = dbHelper.getBirthdays().map { it.importId }
var birthdaysAdded = 0 var birthdaysAdded = 0
val uri = ContactsContract.Data.CONTENT_URI val uri = ContactsContract.Data.CONTENT_URI
val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME, val projection = arrayOf(ContactsContract.Contacts.DISPLAY_NAME,
@ -330,10 +331,11 @@ class MainActivity : SimpleActivity(), NavigationListener {
val event = Event(0, timestamp, timestamp, name, importId = contactId, flags = FLAG_ALL_DAY, repeatInterval = YEAR, val event = Event(0, timestamp, timestamp, name, importId = contactId, flags = FLAG_ALL_DAY, repeatInterval = YEAR,
eventType = eventTypeId, source = SOURCE_CONTACT_BIRTHDAY, lastUpdated = lastUpdated) eventType = eventTypeId, source = SOURCE_CONTACT_BIRTHDAY, lastUpdated = lastUpdated)
if (!birthdayImportIDs.contains(contactId)) {
dbHelper.insert(event, false) { dbHelper.insert(event, false) {
birthdaysAdded++ birthdaysAdded++
} }
}
} while (cursor.moveToNext()) } while (cursor.moveToNext())
} }
} catch (e: Exception) { } catch (e: Exception) {
@ -344,6 +346,7 @@ class MainActivity : SimpleActivity(), NavigationListener {
runOnUiThread { runOnUiThread {
toast(if (birthdaysAdded > 0) R.string.birthdays_added else R.string.no_birthdays) toast(if (birthdaysAdded > 0) R.string.birthdays_added else R.string.no_birthdays)
updateViewPager()
} }
} }

View File

@ -394,6 +394,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return null return null
} }
fun getBirthdays(): List<Event> {
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = ?"
val selectionArgs = arrayOf(SOURCE_CONTACT_BIRTHDAY)
val cursor = getEventsCursor(selection, selectionArgs)
return fillEvents(cursor)
}
fun deleteEvents(ids: Array<String>, deleteFromCalDAV: Boolean) { fun deleteEvents(ids: Array<String>, deleteFromCalDAV: Boolean) {
val args = TextUtils.join(", ", ids) val args = TextUtils.join(", ", ids)
val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)" val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($args)"
@ -547,11 +554,12 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(id.toString())
val cursor = getEventsCursor(selection, selectionArgs) val cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor) val events = fillEvents(cursor)
return if (!events.isEmpty()) return if (events.isNotEmpty()) {
events[0] events[0]
else } else {
null null
} }
}
fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: MutableList<Event>) -> Unit) { fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: MutableList<Event>) -> Unit) {
Thread({ Thread({