Skip to content

Commit

Permalink
Implement basic notification management
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Apr 25, 2024
1 parent 220817b commit ddf3270
Show file tree
Hide file tree
Showing 42 changed files with 158 additions and 21 deletions.
2 changes: 2 additions & 0 deletions app/src/main/kotlin/conf/Conf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ data class Conf(
val viewportWestLon: Double,
val showAtms: Boolean,
val showOsmAttribution: Boolean,
val showSyncSummary: Boolean,
val showAllNewElements: Boolean,
)

fun Conf.mapViewport(): BoundingBox {
Expand Down
14 changes: 11 additions & 3 deletions app/src/main/kotlin/conf/ConfQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ class ConfQueries(private val db: SQLiteOpenHelper) {
viewport_south_lat,
viewport_west_lon,
show_atms,
show_osm_attribution
show_osm_attribution,
show_sync_summary,
show_all_new_elements
)
VALUES (?, ?, ?, ?, ?, ?, ?);
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);
""",
arrayOf(
conf.lastSyncDate ?: "",
Expand All @@ -36,6 +38,8 @@ class ConfQueries(private val db: SQLiteOpenHelper) {
conf.viewportWestLon,
if (conf.showAtms) 1 else 0,
if (conf.showOsmAttribution) 1 else 0,
if (conf.showSyncSummary) 1 else 0,
if (conf.showAllNewElements) 1 else 0,
),
)
}
Expand All @@ -55,7 +59,9 @@ class ConfQueries(private val db: SQLiteOpenHelper) {
viewport_south_lat,
viewport_west_lon,
show_atms,
show_osm_attribution
show_osm_attribution,
show_sync_summary,
show_all_new_elements
FROM conf;
""",
)
Expand All @@ -72,6 +78,8 @@ class ConfQueries(private val db: SQLiteOpenHelper) {
viewportWestLon = cursor.getDouble(4),
showAtms = cursor.getBoolean(5),
showOsmAttribution = cursor.getBoolean(6),
showSyncSummary = cursor.getBoolean(7),
showAllNewElements = cursor.getBoolean(8),
)
}
}
Expand Down
11 changes: 8 additions & 3 deletions app/src/main/kotlin/conf/ConfRepo.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package conf

import android.content.Context
import app.isDebuggable
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -11,10 +13,11 @@ import kotlinx.coroutines.runBlocking

class ConfRepo(
private val queries: ConfQueries,
private val context: Context,
) {

private val _conf: MutableStateFlow<Conf> = MutableStateFlow(
runBlocking { queries.select() ?: DEFAULT_CONF }
runBlocking { queries.select() ?: default() }
)

val conf: StateFlow<Conf> = _conf.asStateFlow()
Expand All @@ -29,15 +32,17 @@ class ConfRepo(
_conf.update { newConf(conf.value) }
}

companion object {
val DEFAULT_CONF = Conf(
fun default(): Conf {
return Conf(
lastSyncDate = null,
viewportNorthLat = 12.116667 + 0.04,
viewportEastLon = -68.933333 + 0.04 + 0.03,
viewportSouthLat = 12.116667 - 0.04,
viewportWestLon = -68.933333 - 0.04 + 0.03,
showAtms = true,
showOsmAttribution = true,
showSyncSummary = context.isDebuggable(),
showAllNewElements = context.isDebuggable(),
)
}
}
6 changes: 4 additions & 2 deletions app/src/main/kotlin/db/Database.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.time.LocalDateTime
val elementsUpdatedAt = MutableStateFlow(LocalDateTime.now())

fun persistentDatabase(context: Context): SQLiteOpenHelper {
return Database(context, "btcmap-2024-03-17.db")
return Database(context, "btcmap-2024-04-25.db")
}

fun inMemoryDatabase(): SQLiteOpenHelper {
Expand Down Expand Up @@ -45,7 +45,9 @@ private class Database(context: Context?, name: String?) : SQLiteOpenHelper(
viewport_south_lat REAL NOT NULL,
viewport_west_lon REAL NOT NULL,
show_atms INTEGER NOT NULL,
show_osm_attribution INTEGER NOT NULL
show_osm_attribution INTEGER NOT NULL,
show_sync_summary INTEGER NOT NULL,
show_all_new_elements INTEGER NOT NULL
);
"""
)
Expand Down
10 changes: 10 additions & 0 deletions app/src/main/kotlin/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ class SettingsFragment : Fragment() {
conf.update { it.copy(showOsmAttribution = isChecked) }
}

binding.showSyncSummary.isChecked = conf.conf.value.showSyncSummary
binding.showSyncSummary.setOnCheckedChangeListener { _, isChecked ->
conf.update { it.copy(showSyncSummary = isChecked) }
}

binding.showAllNewElements.isChecked = conf.conf.value.showAllNewElements
binding.showAllNewElements.setOnCheckedChangeListener { _, isChecked ->
conf.update { it.copy(showAllNewElements = isChecked) }
}

val lastSyncDate = conf.conf.value.lastSyncDate

if (lastSyncDate != null) {
Expand Down
16 changes: 7 additions & 9 deletions app/src/main/kotlin/sync/Sync.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class Sync(
Log.d(TAG, "Sync was requested")

val lastSyncDateTime = confRepo.conf.value.lastSyncDate
val initialSyncComplete = lastSyncDateTime != null
val minSyncIntervalExpiryDate = ZonedDateTime.now(ZoneOffset.UTC).minusMinutes(10)
val minSyncIntervalExpiryDate = ZonedDateTime.now(ZoneOffset.UTC).minusSeconds(1)
Log.d(TAG, "Last sync date: $lastSyncDateTime")
Log.d(TAG, "Min sync interval expiry date: $minSyncIntervalExpiryDate")

Expand Down Expand Up @@ -74,14 +73,13 @@ class Sync(
}.onSuccess {
val syncTimeMs = System.currentTimeMillis() - startTime
Log.d(TAG, "Finished sync in $syncTimeMs ms")
syncNotificationController.showPostSyncNotifications(
syncTimeMs = syncTimeMs,
newEvents = eventsSyncReport?.newEvents ?: emptyList(),
mapViewport = confRepo.conf.value.mapViewport(),
conf = confRepo.conf.value,
)
confRepo.update { it.copy(lastSyncDate = ZonedDateTime.now(ZoneOffset.UTC)) }
if (initialSyncComplete) {
syncNotificationController.showPostSyncNotifications(
syncTimeMs = syncTimeMs,
newEvents = eventsSyncReport?.newEvents ?: emptyList(),
mapViewport = confRepo.conf.value.mapViewport(),
)
}
_active.update { false }
}.onFailure {
Log.e(TAG, "Sync failed", it)
Expand Down
12 changes: 8 additions & 4 deletions app/src/main/kotlin/sync/SyncNotificationController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import app.isDebuggable
import conf.Conf
import element.ElementsRepo
import element.name
import event.Event
Expand All @@ -31,6 +32,7 @@ class SyncNotificationController(
syncTimeMs: Long,
newEvents: List<Event>,
mapViewport: BoundingBox,
conf: Conf,
) {
if (ActivityCompat.checkSelfPermission(
context,
Expand All @@ -40,9 +42,7 @@ class SyncNotificationController(
return
}

val showSyncSummary = context.isDebuggable()

if (showSyncSummary) {
if (conf.showSyncSummary) {
createSyncSummaryNotificationChannel(context)

val builder = NotificationCompat.Builder(context, SYNC_SUMMARY_CHANNEL_ID)
Expand All @@ -65,6 +65,10 @@ class SyncNotificationController(
.notify(Random.Default.nextInt(1, Int.MAX_VALUE), builder.build())
}

if (conf.lastSyncDate == null) {
return
}

createNewMerchantsNotificationChannel(context)

newEvents.forEach { newEvent ->
Expand All @@ -79,7 +83,7 @@ class SyncNotificationController(
endLongitude = element.lon,
)

val distanceThresholdMeters = if (context.isDebuggable()) {
val distanceThresholdMeters = if (conf.showAllNewElements) {
100_000_000f
} else {
100_000f
Expand Down
40 changes: 40 additions & 0 deletions app/src/main/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,46 @@

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp">

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/showSyncSummary"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/show_sync_summary"
android:textSize="16sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:orientation="vertical"
android:paddingStart="16dp"
android:paddingTop="8dp"
android:paddingEnd="16dp"
android:paddingBottom="8dp">

<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/showAllNewElements"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/show_all_new_elements"
android:textSize="16sp" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-af/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="other">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="zero">%d changes</item>
<item quantity="one">%d change</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-bg/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d промяна</item>
<item quantity="other">%d промени</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-bn/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="other">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-ca/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="many">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-cs/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d změn</item>
<item quantity="few">%d změn</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-da/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="other">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d Änderung</item>
<item quantity="other">%d Änderungen</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-el/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d αλλαγή</item>
<item quantity="other">%d αλλαγές</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d cambio</item>
<item quantity="many">%d cambios</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fa/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="other">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="other">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d modification</item>
<item quantity="many">%d modifications</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-hi/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="other">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-hu/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="other">%d changes</item>
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
<string name="issues">Issues</string>
<string name="sync_failed_s">Sync failed: %1$s</string>
<string name="new_local_merchant_accepts_bitcoins">New local merchant accepts bitcoins</string>
<string name="show_sync_summary">Show sync summary</string>
<string name="show_all_new_elements">Show all new elements</string>
<plurals name="d_changes">
<item quantity="one">%d change</item>
<item quantity="many">%d changes</item>
Expand Down
Loading

0 comments on commit ddf3270

Please sign in to comment.