Skip to content

Commit

Permalink
Fix crash when adding document to list and reducing delay (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltroger authored Apr 29, 2024
1 parent 1998054 commit 04e515d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -241,7 +245,7 @@ class CertificatesViewModel @Inject constructor(
ViewEvent.GoToCertificate(
position = 0,
id = docs[0].id,
delayMs = 0,
isNewDocument = false,
)
)
}
Expand All @@ -253,7 +257,7 @@ class CertificatesViewModel @Inject constructor(
ViewEvent.GoToCertificate(
position = indexLast,
docs[indexLast].id,
delayMs = 0,
isNewDocument = false,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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<Certificate>) : ViewEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
)
}
}
}

Expand Down

0 comments on commit 04e515d

Please sign in to comment.