From 07bd7bbb0b8b0762cdacf70dce933d79bc557fa7 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 4 Apr 2023 18:55:46 -0700 Subject: [PATCH] Add optional commit parameter to database functions. --- .../main/java/net/voussoir/trkpt/Database.kt | 60 +++++++++++++++---- .../java/net/voussoir/trkpt/MapFragment.kt | 5 +- .../java/net/voussoir/trkpt/TrackFragment.kt | 9 ++- .../java/net/voussoir/trkpt/TrackerService.kt | 2 +- 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/net/voussoir/trkpt/Database.kt b/app/src/main/java/net/voussoir/trkpt/Database.kt index 4ecefdf..44a2b43 100644 --- a/app/src/main/java/net/voussoir/trkpt/Database.kt +++ b/app/src/main/java/net/voussoir/trkpt/Database.kt @@ -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 @@ -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() diff --git a/app/src/main/java/net/voussoir/trkpt/MapFragment.kt b/app/src/main/java/net/voussoir/trkpt/MapFragment.kt index 3ec2c4c..343188d 100644 --- a/app/src/main/java/net/voussoir/trkpt/MapFragment.kt +++ b/app/src/main/java/net/voussoir/trkpt/MapFragment.kt @@ -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() diff --git a/app/src/main/java/net/voussoir/trkpt/TrackFragment.kt b/app/src/main/java/net/voussoir/trkpt/TrackFragment.kt index 63e6eb0..428d643 100644 --- a/app/src/main/java/net/voussoir/trkpt/TrackFragment.kt +++ b/app/src/main/java/net/voussoir/trkpt/TrackFragment.kt @@ -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) } diff --git a/app/src/main/java/net/voussoir/trkpt/TrackerService.kt b/app/src/main/java/net/voussoir/trkpt/TrackerService.kt index 52179c5..1347898 100644 --- a/app/src/main/java/net/voussoir/trkpt/TrackerService.kt +++ b/app/src/main/java/net/voussoir/trkpt/TrackerService.kt @@ -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)