From 04e515dedd4c0e19742aed3ccbb6bf859cd9c4d9 Mon Sep 17 00:00:00 2001 From: Michael Troger <11340859+michaeltroger@users.noreply.github.com> Date: Mon, 29 Apr 2024 21:07:34 +0200 Subject: [PATCH] Fix crash when adding document to list and reducing delay (#247) --- .../certificates/CertificatesFragment.kt | 7 ++++-- .../certificates/CertificatesViewModel.kt | 22 +++++++++++-------- .../certificates/states/ViewEvent.kt | 4 +--- .../CertificatesListFragment.kt | 14 ++++++------ 4 files changed, 26 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesFragment.kt b/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesFragment.kt index dfae3eb4..3cdbb24c 100644 --- a/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesFragment.kt +++ b/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesFragment.kt @@ -32,6 +32,7 @@ import kotlinx.coroutines.newSingleThreadContext import javax.inject.Inject private const val TOUCH_SLOP_FACTOR = 8 +private const val SCROLL_TO_DELAY_MS = 500L @AndroidEntryPoint class CertificatesFragment : Fragment(R.layout.fragment_certificates) { @@ -219,8 +220,10 @@ class CertificatesFragment : Fragment(R.layout.fragment_certificates) { private fun goToCertificate(event: ViewEvent.GoToCertificate) { lifecycleScope.launch { - delay(event.delayMs) - binding!!.certificates.smoothScrollToPosition(event.position) + if (event.isNewDocument) { + delay(SCROLL_TO_DELAY_MS) + } + binding?.certificates?.smoothScrollToPosition(event.position) } } diff --git a/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesViewModel.kt b/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesViewModel.kt index ef75b09f..a52081e0 100644 --- a/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesViewModel.kt +++ b/app/src/main/java/com/michaeltroger/gruenerpass/certificates/CertificatesViewModel.kt @@ -163,16 +163,20 @@ class CertificatesViewModel @Inject constructor( private suspend fun insertIntoDatabase(certificate: Certificate) { val addDocumentsInFront = addDocumentsInFront.first() insertIntoDatabaseUseCase(certificate, addDocumentsInFront) - if (addDocumentsInFront) { - _viewEvent.emit(ViewEvent.GoToCertificate(position = 0, id = certificate.id)) + val event = if (addDocumentsInFront) { + ViewEvent.GoToCertificate( + position = 0, + id = certificate.id, + isNewDocument = true, + ) } else { - _viewEvent.emit( - ViewEvent.GoToCertificate( - position = getCertificatesFlowUseCase().first().size - 1, - id = certificate.id - ) + ViewEvent.GoToCertificate( + position = getCertificatesFlowUseCase().first().size - 1, + id = certificate.id, + isNewDocument = true, ) } + _viewEvent.emit(event) } fun onDocumentNameChangeConfirmed(filename: String, documentName: String) = viewModelScope.launch { @@ -241,7 +245,7 @@ class CertificatesViewModel @Inject constructor( ViewEvent.GoToCertificate( position = 0, id = docs[0].id, - delayMs = 0, + isNewDocument = false, ) ) } @@ -253,7 +257,7 @@ class CertificatesViewModel @Inject constructor( ViewEvent.GoToCertificate( position = indexLast, docs[indexLast].id, - delayMs = 0, + isNewDocument = false, ) ) } diff --git a/app/src/main/java/com/michaeltroger/gruenerpass/certificates/states/ViewEvent.kt b/app/src/main/java/com/michaeltroger/gruenerpass/certificates/states/ViewEvent.kt index 41ebf522..1dba8068 100644 --- a/app/src/main/java/com/michaeltroger/gruenerpass/certificates/states/ViewEvent.kt +++ b/app/src/main/java/com/michaeltroger/gruenerpass/certificates/states/ViewEvent.kt @@ -2,8 +2,6 @@ package com.michaeltroger.gruenerpass.certificates.states import com.michaeltroger.gruenerpass.db.Certificate -private const val SCROLL_TO_DELAY_MS = 1000L - sealed class ViewEvent { data object AddFile : ViewEvent() data object ShowParsingFileError : ViewEvent() @@ -12,7 +10,7 @@ sealed class ViewEvent { data class GoToCertificate( val position: Int, val id: String, - val delayMs: Long = SCROLL_TO_DELAY_MS, + val isNewDocument: Boolean, ) : ViewEvent() data class Share(val certificate: Certificate) : ViewEvent() data class ShareMultiple(val list: List) : ViewEvent() diff --git a/app/src/main/java/com/michaeltroger/gruenerpass/certificateslist/CertificatesListFragment.kt b/app/src/main/java/com/michaeltroger/gruenerpass/certificateslist/CertificatesListFragment.kt index 8725bd32..15c01cb6 100644 --- a/app/src/main/java/com/michaeltroger/gruenerpass/certificateslist/CertificatesListFragment.kt +++ b/app/src/main/java/com/michaeltroger/gruenerpass/certificateslist/CertificatesListFragment.kt @@ -8,6 +8,7 @@ import androidx.fragment.app.viewModels import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle +import androidx.lifecycle.withStarted import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.DividerItemDecoration import com.google.android.material.snackbar.Snackbar @@ -24,7 +25,6 @@ import com.michaeltroger.gruenerpass.databinding.FragmentCertificatesListBinding import com.michaeltroger.gruenerpass.db.Certificate import com.xwray.groupie.GroupieAdapter import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.delay import javax.inject.Inject import kotlinx.coroutines.launch @@ -209,12 +209,12 @@ class CertificatesListFragment : Fragment(R.layout.fragment_certificates_list) { private fun goToCertificate(event: ViewEvent.GoToCertificate) { lifecycleScope.launch { - delay(event.delayMs) - binding!!.certificates.smoothScrollToPosition(event.position) - delay(event.delayMs) - findNavController().navigate( - CertificatesListFragmentDirections.navigateToCertificateDetails(event.id) - ) + withStarted { + binding?.certificates?.scrollToPosition(event.position) + findNavController().navigate( + CertificatesListFragmentDirections.navigateToCertificateDetails(event.id) + ) + } } }