Add optional commit parameter to database functions.

This commit is contained in:
voussoir 2023-04-04 18:55:46 -07:00
parent 17e85379f8
commit 07bd7bbb0b
4 changed files with 60 additions and 16 deletions

View file

@ -52,23 +52,29 @@ class Database(val trackbook: Trackbook)
this.connection.endTransaction()
}
fun delete_trkpt(device_id: String, time: Long)
fun delete_trkpt(device_id: String, time: Long, commit: Boolean=false)
{
Log.i("VOUSSOIR", "Database.delete_trkpt")
begin_transaction()
connection.delete("trkpt", "device_id = ? AND time = ?", arrayOf(device_id, time.toString()))
commit()
if (commit)
{
this.commit()
}
}
fun delete_trkpt_start_end(device_id: String, start_time: Long, end_time: Long)
fun delete_trkpt_start_end(device_id: String, start_time: Long, end_time: Long, commit: Boolean=false)
{
Log.i("VOUSSOIR", "Track.delete ${device_id} ${start_time} -- ${end_time}.")
this.begin_transaction()
this.connection.delete("trkpt", "device_id = ? AND time >= ? AND time <= ?", arrayOf(device_id, start_time.toString(), end_time.toString()))
this.commit()
if (commit)
{
this.commit()
}
}
fun insert_trkpt(trkpt: Trkpt)
fun insert_trkpt(trkpt: Trkpt, commit: Boolean=false)
{
Log.i("VOUSSOIR", "Database.insert_trkpt")
val values = ContentValues().apply {
@ -83,6 +89,10 @@ class Database(val trackbook: Trackbook)
}
begin_transaction()
connection.insert("trkpt", null, values)
if (commit)
{
this.commit()
}
}
fun select_trkpt_start_end(device_id: String, start_time: Long, end_time: Long, order: String="ASC"): Iterator<Trkpt>
@ -136,15 +146,18 @@ class Database(val trackbook: Trackbook)
}
}
fun delete_homepoint(id: Long)
fun delete_homepoint(id: Long, commit: Boolean=false)
{
Log.i("VOUSSOIR", "Database.delete_homepoint")
begin_transaction()
connection.delete("homepoints", "id = ?", arrayOf(id.toString()))
commit()
if (commit)
{
this.commit()
}
}
fun insert_homepoint(id: Long, name: String, latitude: Double, longitude: Double, radius: Double)
fun insert_homepoint(id: Long, name: String, latitude: Double, longitude: Double, radius: Double, commit: Boolean=false)
{
Log.i("VOUSSOIR", "Database.insert_homepoint")
val values = ContentValues().apply {
@ -156,10 +169,13 @@ class Database(val trackbook: Trackbook)
}
begin_transaction()
connection.insert("homepoints", null, values)
commit()
if (commit)
{
this.commit()
}
}
fun update_homepoint(id: Long, name: String, radius: Double)
fun update_homepoint(id: Long, name: String, radius: Double, commit: Boolean=false)
{
Log.i("VOUSSOIR", "Database.update_homepoint")
val values = ContentValues().apply {
@ -168,7 +184,29 @@ class Database(val trackbook: Trackbook)
}
begin_transaction()
connection.update("homepoints", values, "id = ?", arrayOf(id.toString()))
commit()
if (commit)
{
this.commit()
}
}
fun update_trkpt(trkpt: Trkpt, commit: Boolean=false)
{
Log.i("VOUSSOIR", "Database.update_trkpt")
val values = ContentValues().apply {
put("lat", trkpt.latitude)
put("lon", trkpt.longitude)
put("provider", trkpt.provider)
put("accuracy", trkpt.accuracy)
put("sat", trkpt.numberSatellites)
put("ele", trkpt.altitude)
}
begin_transaction()
connection.update("trkpt", values, "device_id = ? AND time = ?", arrayOf(trkpt.device_id, trkpt.time.toString()))
if (commit)
{
this.commit()
}
}
private fun initialize_tables()

View file

@ -168,6 +168,7 @@ class MapFragment : Fragment()
latitude=point.latitude,
longitude=point.longitude,
radius=radius,
commit=true,
)
trackbook.load_homepoints()
create_homepoint_overlays()
@ -553,14 +554,14 @@ class MapFragment : Fragment()
val save_button: Button = dialog.findViewById(R.id.homepoint_save_button)
delete_button.text = "Delete"
delete_button.setOnClickListener {
trackbook.database.delete_homepoint(homepoint.id)
trackbook.database.delete_homepoint(homepoint.id, commit=true)
trackbook.load_homepoints()
create_homepoint_overlays()
dialog.dismiss()
}
save_button.setOnClickListener {
val radius = radius_input.text.toString().toDoubleOrNull() ?: 25.0
trackbook.database.update_homepoint(homepoint.id, name=name_input.text.toString(), radius=radius)
trackbook.database.update_homepoint(homepoint.id, name=name_input.text.toString(), radius=radius, commit=true)
trackbook.load_homepoints()
create_homepoint_overlays()
dialog.dismiss()

View file

@ -259,7 +259,7 @@ class TrackFragment : Fragment(), MapListener, YesNoDialog.YesNoDialogListener
Log.i("VOUSSOIR", selected.rendered_by_polyline?.actualPoints?.size.toString())
selected.rendered_by_polyline?.setPoints(ArrayList(selected.rendered_by_polyline?.actualPoints))
Log.i("VOUSSOIR", selected.rendered_by_polyline?.actualPoints?.size.toString())
trackbook.database.delete_trkpt(selected.device_id, selected.time)
trackbook.database.delete_trkpt(selected.device_id, selected.time, commit=true)
deselect_trkpt()
mapView.invalidate()
}
@ -674,7 +674,12 @@ class TrackFragment : Fragment(), MapListener, YesNoDialog.YesNoDialogListener
{
if (type == Keys.DIALOG_DELETE_TRACK && dialogResult && track.trkpts.isNotEmpty())
{
trackbook.database.delete_trkpt_start_end(track.device_id, track.trkpts.first().time, track.trkpts.last().time)
trackbook.database.delete_trkpt_start_end(
track.device_id,
track.trkpts.first().time,
track.trkpts.last().time,
commit=true,
)
handler.removeCallbacks(requery_and_render)
handler.postDelayed(requery_and_render, RERENDER_DELAY)
}

View file

@ -339,7 +339,7 @@ class TrackerService: Service()
}
val trkpt = Trkpt(device_id=device_id, location=location)
trackbook.database.insert_trkpt(trkpt)
trackbook.database.insert_trkpt(trkpt, commit=false)
recent_trackpoints_for_mapview.add(trkpt)
while (recent_trackpoints_for_mapview.size > RECENT_TRKPT_COUNT)