Convert Geofence to data class

pull/2266/head
Alex Baker 1 year ago
parent 1a810e0298
commit 2c697fd5b9

@ -18,9 +18,24 @@ import com.todoroo.astrid.service.Upgrader.Companion.getAndroidColor
import org.tasks.LocalBroadcastManager
import org.tasks.R
import org.tasks.caldav.VtodoCache
import org.tasks.data.*
import org.tasks.data.AlarmDao
import org.tasks.data.Attachment
import org.tasks.data.CaldavAccount
import org.tasks.data.CaldavAccount.Companion.TYPE_GOOGLE_TASKS
import org.tasks.data.CaldavCalendar
import org.tasks.data.CaldavDao
import org.tasks.data.CaldavTask
import org.tasks.data.FilterDao
import org.tasks.data.Geofence
import org.tasks.data.LocationDao
import org.tasks.data.Place.Companion.newPlace
import org.tasks.data.Tag
import org.tasks.data.TagDao
import org.tasks.data.TagData
import org.tasks.data.TagDataDao
import org.tasks.data.TaskAttachmentDao
import org.tasks.data.TaskListMetadataDao
import org.tasks.data.UserActivityDao
import org.tasks.db.Migrations.repeatFrom
import org.tasks.db.Migrations.withoutFrom
import org.tasks.preferences.Preferences
@ -207,12 +222,14 @@ class TasksJsonImporter @Inject constructor(
place.url = location.url
place.phone = location.phone
locationDao.insert(place)
val geofence = Geofence()
geofence.task = taskId
geofence.place = place.uid
geofence.isArrival = location.arrival
geofence.isDeparture = location.departure
locationDao.insert(geofence)
locationDao.insert(
Geofence(
task = taskId,
place = place.uid,
isArrival = location.arrival,
isDeparture = location.departure,
)
)
}
for (tag in backup.tags) {
val tagData = findTagData(tag) ?: continue
@ -222,8 +239,9 @@ class TasksJsonImporter @Inject constructor(
tagDao.insert(tag)
}
backup.geofences?.forEach { geofence ->
geofence.task = taskId
locationDao.insert(geofence)
locationDao.insert(
geofence.copy(task = taskId)
)
}
backup.attachments
?.mapNotNull { taskAttachmentDao.getAttachment(it.attachmentUid) }

@ -85,12 +85,14 @@ class iCalendar @Inject constructor(
}
val existing = locationDao.getGeofences(taskId)
if (existing == null) {
val geofence = Geofence(place.uid, preferences)
geofence.task = taskId
geofence.id = locationDao.insert(geofence)
locationDao.insert(
Geofence(
place.uid,
preferences
).copy(task = taskId)
)
} else if (place != existing.place) {
val geofence = existing.geofence
geofence.place = place.uid
val geofence = existing.geofence.copy(place = place.uid)
locationDao.update(geofence)
geofenceApi.update(existing.place)
}

@ -24,64 +24,73 @@ import java.io.Serializable
),
]
)
class Geofence : Serializable, Parcelable {
data class Geofence(
@PrimaryKey(autoGenerate = true)
@ColumnInfo(name = "geofence_id")
@Transient
var id: Long = 0
val id: Long = 0,
@ColumnInfo(name = "task", index = true)
@Transient
var task: Long = 0
val task: Long = 0,
@ColumnInfo(name = "place")
var place: String? = null
val place: String? = null,
@ColumnInfo(name = "arrival")
var isArrival = false
val isArrival: Boolean = false,
@ColumnInfo(name = "departure")
var isDeparture = false
constructor()
var isDeparture: Boolean = false,
) : Serializable, Parcelable {
@Ignore
constructor(task: Long, place: String?, arrival: Boolean, departure: Boolean) : this(place, arrival, departure) {
this.task = task
}
constructor(
task: Long,
place: String?,
arrival: Boolean,
departure: Boolean
): this(
task = task,
place = place,
isArrival = arrival,
isDeparture = departure,
)
@Ignore
constructor(place: String?, preferences: Preferences) {
this.place = place
val defaultReminders = preferences.getIntegerFromString(R.string.p_default_location_reminder_key, 1)
isArrival = defaultReminders == 1 || defaultReminders == 3
isDeparture = defaultReminders == 2 || defaultReminders == 3
}
constructor(
place: String?,
preferences: Preferences,
defaultReminders: Int = preferences.getIntegerFromString(R.string.p_default_location_reminder_key, 1)
): this(
place = place,
isArrival = defaultReminders == 1 || defaultReminders == 3,
isDeparture = defaultReminders == 2 || defaultReminders == 3,
)
@Ignore
constructor(place: String?, arrival: Boolean, departure: Boolean) {
this.place = place
isArrival = arrival
isDeparture = departure
}
constructor(
place: String?,
arrival: Boolean,
departure: Boolean
): this(
place = place,
isArrival = arrival,
isDeparture = departure,
)
@Ignore
constructor(o: Geofence) {
id = o.id
task = o.task
place = o.place
isArrival = o.isArrival
isDeparture = o.isDeparture
}
constructor(o: Geofence): this(
id = o.id,
task = o.task,
place = o.place,
isArrival = o.isArrival,
isDeparture = o.isDeparture,
)
@Ignore
constructor(parcel: Parcel) {
id = parcel.readLong()
task = parcel.readLong()
place = parcel.readString()
isArrival = parcel.readInt() == 1
isDeparture = parcel.readInt() == 1
}
constructor(parcel: Parcel): this(
id = parcel.readLong(),
task = parcel.readLong(),
place = parcel.readString(),
isArrival = parcel.readInt() == 1,
isDeparture = parcel.readInt() == 1,
)
override fun describeContents() = 0
@ -95,31 +104,6 @@ class Geofence : Serializable, Parcelable {
}
}
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Geofence) return false
if (id != other.id) return false
if (task != other.task) return false
if (place != other.place) return false
if (isArrival != other.isArrival) return false
if (isDeparture != other.isDeparture) return false
return true
}
override fun hashCode(): Int {
var result = id.hashCode()
result = 31 * result + task.hashCode()
result = 31 * result + (place?.hashCode() ?: 0)
result = 31 * result + isArrival.hashCode()
result = 31 * result + isDeparture.hashCode()
return result
}
override fun toString(): String =
"Geofence(id=$id, task=$task, place=$place, isArrival=$isArrival, isDeparture=$isDeparture)"
companion object {
const val TABLE_NAME = "geofences"
@JvmField val TABLE = Table(TABLE_NAME)

@ -77,10 +77,13 @@ public class GeofenceDialog extends DialogFragment {
}
private Geofence toGeofence() {
Geofence geofence = new Geofence();
geofence.setArrival(arrivalView.isChecked());
geofence.setDeparture(departureView.isChecked());
return geofence;
return new Geofence(
0L,
0L,
null,
arrivalView.isChecked(),
departureView.isChecked()
);
}
private void sendResult(DialogInterface d, int... i) {

@ -38,9 +38,23 @@ import org.tasks.R
import org.tasks.Strings
import org.tasks.analytics.Firebase
import org.tasks.calendars.CalendarEventProvider
import org.tasks.data.*
import org.tasks.data.Alarm
import org.tasks.data.Alarm.Companion.TYPE_REL_END
import org.tasks.data.Alarm.Companion.TYPE_REL_START
import org.tasks.data.AlarmDao
import org.tasks.data.Attachment
import org.tasks.data.CaldavDao
import org.tasks.data.CaldavTask
import org.tasks.data.GoogleTaskDao
import org.tasks.data.Location
import org.tasks.data.LocationDao
import org.tasks.data.TagDao
import org.tasks.data.TagData
import org.tasks.data.TagDataDao
import org.tasks.data.TaskAttachment
import org.tasks.data.TaskAttachmentDao
import org.tasks.data.UserActivity
import org.tasks.data.UserActivityDao
import org.tasks.date.DateTimeUtils.toDateTime
import org.tasks.files.FileHelper
import org.tasks.location.GeofenceApi
@ -267,10 +281,12 @@ class TaskEditViewModel @Inject constructor(
}
selectedLocation.value?.let { location ->
val place = location.place
val geofence = location.geofence
geofence.task = task.id
geofence.place = place.uid
geofence.id = locationDao.insert(geofence)
locationDao.insert(
location.geofence.copy(
task = task.id,
place = place.uid,
)
)
geofenceApi.update(place)
}
task.putTransitory(SyncFlags.FORCE_CALDAV_SYNC, true)

Loading…
Cancel
Save