Skip to content

Commit

Permalink
Make notification permission flow less intrusive
Browse files Browse the repository at this point in the history
  • Loading branch information
bubelov committed Nov 5, 2024
1 parent c058c19 commit fb1e4cd
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 40 deletions.
2 changes: 1 addition & 1 deletion app/src/androidMain/kotlin/app/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import element_comment.ElementCommentQueries
import element_comment.ElementCommentRepo

val appModule = module {
single { Database(get<Context>().getDatabasePath("btcmap-2024-10-30.db").absolutePath).conn }
single { Database(get<Context>().getDatabasePath("btcmap-2024-11-05.db").absolutePath).conn }

single { ApiImpl() }.bind(Api::class)

Expand Down
2 changes: 1 addition & 1 deletion app/src/androidMain/kotlin/conf/Conf.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ data class Conf(
val viewportWestLon: Double,
val showAtms: Boolean,
val showSyncSummary: Boolean,
val showAllNewElements: Boolean,
val notifyOfNewElementsNearby: Boolean,
)

fun Conf.mapViewport(): BoundingBox {
Expand Down
4 changes: 2 additions & 2 deletions app/src/androidMain/kotlin/conf/ConfQueries.kt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ConfQueries(private val conn: SQLiteConnection) {
it.bindDouble(5, conf.viewportWestLon)
it.bindLong(6, if (conf.showAtms) 1 else 0)
it.bindLong(7, if (conf.showSyncSummary) 1 else 0)
it.bindLong(8, if (conf.showAllNewElements) 1 else 0)
it.bindLong(8, if (conf.notifyOfNewElementsNearby) 1 else 0)
it.step()
}
}
Expand Down Expand Up @@ -80,7 +80,7 @@ class ConfQueries(private val conn: SQLiteConnection) {
viewportWestLon = it.getDouble(4),
showAtms = it.getBoolean(5),
showSyncSummary = it.getBoolean(6),
showAllNewElements = it.getBoolean(7),
notifyOfNewElementsNearby = it.getBoolean(7),
)
} else {
null
Expand Down
2 changes: 1 addition & 1 deletion app/src/androidMain/kotlin/conf/ConfRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ConfRepo(
viewportWestLon = -68.933333 - 0.04 + 0.03,
showAtms = false,
showSyncSummary = false,
showAllNewElements = false,
notifyOfNewElementsNearby = false,
)
}
}
11 changes: 0 additions & 11 deletions app/src/androidMain/kotlin/map/MapFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.Paint
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.util.TypedValue
import android.view.LayoutInflater
Expand Down Expand Up @@ -102,10 +101,6 @@ class MapFragment : Fragment() {
}
}

private val postNotificationsPermissionRequest = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions(),
) { }

private var emptyClusterBitmap: Bitmap? = null

override fun onCreateView(
Expand Down Expand Up @@ -437,12 +432,6 @@ class MapFragment : Fragment() {
val text = it.toString()
searchModel.setSearchString(text)
}

if (Build.VERSION.SDK_INT >= 33) {
postNotificationsPermissionRequest.launch(
arrayOf(Manifest.permission.POST_NOTIFICATIONS)
)
}
}

private fun BoundingBox.toLatLngBounds(): LatLngBounds {
Expand Down
20 changes: 17 additions & 3 deletions app/src/androidMain/kotlin/settings/SettingsFragment.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package settings

import android.Manifest
import android.os.Build
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
Expand All @@ -25,6 +28,10 @@ class SettingsFragment : Fragment() {
private var _binding: FragmentSettingsBinding? = null
private val binding get() = _binding!!

private val postNotificationsPermissionRequest = registerForActivityResult(
ActivityResultContracts.RequestMultiplePermissions(),
) { }

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -57,9 +64,16 @@ class SettingsFragment : Fragment() {
conf.update { it.copy(showSyncSummary = isChecked) }
}

binding.showAllNewElements.isChecked = conf.conf.value.showAllNewElements
binding.showAllNewElements.setOnCheckedChangeListener { _, isChecked ->
conf.update { it.copy(showAllNewElements = isChecked) }
binding.notifyOfNewElementsNearby.isEnabled = Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU

binding.notifyOfNewElementsNearby.isChecked = conf.conf.value.notifyOfNewElementsNearby
binding.notifyOfNewElementsNearby.setOnCheckedChangeListener { _, isChecked ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
postNotificationsPermissionRequest.launch(
arrayOf(Manifest.permission.POST_NOTIFICATIONS)
)
}
conf.update { it.copy(notifyOfNewElementsNearby = isChecked) }
}

val lastSyncDate = conf.conf.value.lastSyncDate
Expand Down
20 changes: 3 additions & 17 deletions app/src/androidMain/kotlin/sync/SyncNotificationController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SyncNotificationController(
.notify(Random.Default.nextInt(1, Int.MAX_VALUE), builder.build())
}

if (conf.lastSyncDate == null) {
if (conf.lastSyncDate == null || !conf.notifyOfNewElementsNearby) {
return
}

Expand All @@ -85,28 +85,14 @@ class SyncNotificationController(
endLongitude = element.lon,
)

val localRadiusMeters = 100_000f

val distanceThresholdMeters = if (conf.showAllNewElements) {
100_000_000f
} else {
localRadiusMeters
}

if (distanceMeters > distanceThresholdMeters) {
if (distanceMeters > 100_000f) {
return
}

val contentText = if (distanceMeters > localRadiusMeters) {
context.getString(R.string.new_merchant_accepts_bitcoins)
} else {
context.getString(R.string.new_local_merchant_accepts_bitcoins)
}

val builder = NotificationCompat.Builder(context, NEW_MERCHANTS_CHANNEL_ID)
.setSmallIcon(R.drawable.add_location)
.setContentTitle(element.name(context.resources))
.setContentText(contentText)
.setContentText(context.getString(R.string.new_local_merchant_accepts_bitcoins))
.setPriority(NotificationCompat.PRIORITY_DEFAULT)

NotificationManagerCompat.from(context)
Expand Down
8 changes: 4 additions & 4 deletions app/src/androidMain/res/layout/fragment_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@
android:paddingBottom="8dp">

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

</LinearLayout>
Expand All @@ -79,10 +79,10 @@
android:paddingBottom="8dp">

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

</LinearLayout>
Expand Down
1 change: 1 addition & 0 deletions app/src/androidMain/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
<string name="new_merchant_accepts_bitcoins">New merchant accepts bitcoins</string>
<string name="delivery">Delivery</string>
<string name="comments_d" translatable="false">Comments (%1$d)</string>
<string name="new_merchant_notifications" translatable="false">New merchant notifications</string>
<plurals name="d_changes">
<item quantity="zero">%d changes</item>
<item quantity="one">%d change</item>
Expand Down

0 comments on commit fb1e4cd

Please sign in to comment.