From d228d64ded4ddffeaaf35728db69d160ce7ab33e Mon Sep 17 00:00:00 2001
From: Matthias Urhahn <matthias.urhahn@sap.com>
Date: Thu, 8 Apr 2021 14:56:30 +0200
Subject: [PATCH] Align auto-checkout behavior and prevent editing of active
 checkins (DEV) (#2766)

* Process overdue checkouts on data changes.
Handle the case of a 0 length check-in.

* Don't pre-filter based on completeness, checkin-start/end may change and we need to refresh the autocheckout too.

* Set active check-in duration to 0 if negative.

* Test Menu Layout Eye Candy

* Active CheckIns can't be edited. Remove click listener.

Co-authored-by: harambasicluka <64483219+harambasicluka@users.noreply.github.com>
Co-authored-by: Kolya Opahle <k.opahle@sap.com>
---
 .../res/layout/fragment_test_eventregistration.xml |  1 +
 .../checkins/checkout/auto/AutoCheckOut.kt         | 12 +++---------
 .../attendee/checkins/CheckInsViewModel.kt         |  1 -
 .../attendee/checkins/items/ActiveCheckInVH.kt     | 14 +++++++++-----
 4 files changed, 13 insertions(+), 15 deletions(-)

diff --git a/Corona-Warn-App/src/deviceForTesters/res/layout/fragment_test_eventregistration.xml b/Corona-Warn-App/src/deviceForTesters/res/layout/fragment_test_eventregistration.xml
index 1e4441e00..dba2acc1b 100644
--- a/Corona-Warn-App/src/deviceForTesters/res/layout/fragment_test_eventregistration.xml
+++ b/Corona-Warn-App/src/deviceForTesters/res/layout/fragment_test_eventregistration.xml
@@ -16,6 +16,7 @@
             style="@style/Card"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
+            android:layout_marginTop="10dp"
             android:layout_marginHorizontal="@dimen/spacing_tiny"
             android:orientation="vertical">
 
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/presencetracing/checkins/checkout/auto/AutoCheckOut.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/presencetracing/checkins/checkout/auto/AutoCheckOut.kt
index 2a27ff6db..b0b8d2190 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/presencetracing/checkins/checkout/auto/AutoCheckOut.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/presencetracing/checkins/checkout/auto/AutoCheckOut.kt
@@ -10,7 +10,6 @@ import kotlinx.coroutines.CoroutineScope
 import kotlinx.coroutines.flow.distinctUntilChanged
 import kotlinx.coroutines.flow.firstOrNull
 import kotlinx.coroutines.flow.launchIn
-import kotlinx.coroutines.flow.map
 import kotlinx.coroutines.flow.onEach
 import kotlinx.coroutines.flow.onStart
 import kotlinx.coroutines.sync.Mutex
@@ -40,15 +39,10 @@ class AutoCheckOut @Inject constructor(
     fun setupMonitor() {
         repository.allCheckIns
             .onStart { Timber.tag(TAG).v("Monitoring check-ins.") }
-            .map { checkins ->
-                Timber.tag(TAG).v("CheckIns changed")
-                val completed = checkins.filter { it.completed }.map { it.id }
-                val notCompleted = checkins.filter { !it.completed }.map { it.id }
-                completed to notCompleted
-            }
             .distinctUntilChanged()
             .onEach {
-                Timber.tag(TAG).i("Check-in was added or removed, refreshing alarm.")
+                Timber.tag(TAG).i("Check-ins changed, checking for overdue items, refreshing alarm.")
+                processOverDueCheckouts()
                 refreshAlarm()
             }
             .launchIn(appScope)
@@ -93,7 +87,7 @@ class AutoCheckOut @Inject constructor(
             val nowUTC = timeStamper.nowUTC
             val snapshot = repository.allCheckIns.firstOrNull() ?: emptyList()
             snapshot
-                .filter { !it.completed && nowUTC.isAfter(it.checkInEnd) }
+                .filter { !it.completed && (nowUTC.isAfter(it.checkInEnd) || nowUTC.isEqual(it.checkInEnd)) }
                 .sortedBy { it.checkInEnd }
         }.also {
             Timber.tag(TAG).d("${it.size} checkins are overdue for auto checkout: %s", it)
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/CheckInsViewModel.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/CheckInsViewModel.kt
index e78ae163b..3810a8070 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/CheckInsViewModel.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/CheckInsViewModel.kt
@@ -105,7 +105,6 @@ class CheckInsViewModel @AssistedInject constructor(
             when {
                 !checkin.completed -> ActiveCheckInVH.Item(
                     checkin = checkin,
-                    onCardClicked = { events.postValue(CheckInEvent.EditCheckIn(it.id)) },
                     onRemoveItem = { events.postValue(CheckInEvent.ConfirmRemoveItem(it)) },
                     onCheckout = { doCheckOutNow(it) },
                     onSwipeItem = { checkIn, position ->
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/items/ActiveCheckInVH.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/items/ActiveCheckInVH.kt
index 8b2576d1e..ec44fa16e 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/items/ActiveCheckInVH.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkins/items/ActiveCheckInVH.kt
@@ -37,8 +37,15 @@ class ActiveCheckInVH(parent: ViewGroup) :
 
         val checkInStartUserTZ = curItem.checkin.checkInStart.toUserTimeZone()
 
-        val checkinDuration = Duration(checkInStartUserTZ, Instant.now())
-        highlightDuration.text = highlightDurationForamtter.print(checkinDuration.toPeriod())
+        highlightDuration.text = kotlin.run {
+            val currentDuration = Duration(checkInStartUserTZ, Instant.now())
+            val saneDuration = if (currentDuration.isShorterThan(Duration.ZERO)) {
+                Duration.ZERO
+            } else {
+                currentDuration
+            }
+            highlightDurationForamtter.print(saneDuration.toPeriod())
+        }
 
         description.text = curItem.checkin.description
         address.text = curItem.checkin.address
@@ -71,13 +78,10 @@ class ActiveCheckInVH(parent: ViewGroup) :
         }
 
         checkoutAction.setOnClickListener { curItem.onCheckout(curItem.checkin) }
-
-        itemView.setOnClickListener { curItem.onCardClicked(curItem.checkin) }
     }
 
     data class Item(
         val checkin: CheckIn,
-        val onCardClicked: (CheckIn) -> Unit,
         val onRemoveItem: (CheckIn) -> Unit,
         val onCheckout: (CheckIn) -> Unit,
         val onSwipeItem: (CheckIn, Int) -> Unit,
-- 
GitLab