From c25e5c4bf32e10cb337e068e95a4448700c39a86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20W=C3=BCrl?= Date: Thu, 11 Jul 2024 17:24:53 +0200 Subject: [PATCH] fix pre API 24 behaviour --- .../java/org/blitzortung/android/app/Main.kt | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/org/blitzortung/android/app/Main.kt b/app/src/main/java/org/blitzortung/android/app/Main.kt index 1b4b363d..1b63b71a 100644 --- a/app/src/main/java/org/blitzortung/android/app/Main.kt +++ b/app/src/main/java/org/blitzortung/android/app/Main.kt @@ -140,13 +140,7 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener { statusComponent.indicateError(event.failed) if (!event.failed && event.sequenceNumber != null) { - val updatedSequenceNumber = - if (isAtLeast(24)) { - currentSequenceNumber.updateAndGet { previousSequenceNumber -> - if (previousSequenceNumber < event.sequenceNumber) event.sequenceNumber else previousSequenceNumber - }} else { - if (currentSequenceNumber.get() < event.sequenceNumber) event.sequenceNumber else currentSequenceNumber.get() - } + val updatedSequenceNumber = determineUpdatedSequenceNumber(event.sequenceNumber) if (updatedSequenceNumber == event.sequenceNumber) { currentResult = event @@ -207,6 +201,19 @@ class Main : FragmentActivity(), OnSharedPreferenceChangeListener { } } + private fun determineUpdatedSequenceNumber(sequenceNumber: Long) = if (isAtLeast(24)) { + currentSequenceNumber.updateAndGet { previousSequenceNumber -> + if (previousSequenceNumber < sequenceNumber) event.sequenceNumber else previousSequenceNumber + } + } else { + synchronized(currentSequenceNumber) { + val previousSequenceNumber = currentSequenceNumber.get() + val updated = if (previousSequenceNumber < sequenceNumber) event.sequenceNumber else previousSequenceNumber + currentSequenceNumber.set(updated) + updated + } + } + private lateinit var mapFragment: MapFragment private lateinit var binding: MainBinding