Advertisement
Guest User

Untitled

a guest
Jun 29th, 2022
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.92 KB | None | 0 0
  1. import androidx.compose.foundation.background
  2. import androidx.compose.foundation.focusable
  3. import androidx.compose.foundation.layout.Box
  4. import androidx.compose.foundation.layout.BoxScope
  5. import androidx.compose.foundation.layout.fillMaxSize
  6. import androidx.compose.foundation.layout.size
  7. import androidx.compose.material3.Text
  8. import androidx.compose.runtime.Composable
  9. import androidx.compose.runtime.DisposableEffect
  10. import androidx.compose.runtime.LaunchedEffect
  11. import androidx.compose.runtime.getValue
  12. import androidx.compose.runtime.mutableStateOf
  13. import androidx.compose.runtime.remember
  14. import androidx.compose.runtime.setValue
  15. import androidx.compose.ui.Alignment
  16. import androidx.compose.ui.Modifier
  17. import androidx.compose.ui.graphics.Color
  18. import androidx.compose.ui.platform.LocalContext
  19. import androidx.compose.ui.semantics.LiveRegionMode
  20. import androidx.compose.ui.semantics.contentDescription
  21. import androidx.compose.ui.semantics.liveRegion
  22. import androidx.compose.ui.semantics.semantics
  23. import androidx.compose.ui.tooling.preview.Preview
  24. import androidx.compose.ui.unit.Dp
  25. import androidx.compose.ui.unit.dp
  26. import kotlinx.coroutines.delay
  27.  
  28. @Composable
  29. fun FullScreenLoading(
  30.     isLoading: Boolean,
  31.     modifier: Modifier = Modifier,
  32.     iconSize: Dp = 32.dp,
  33.     content: @Composable BoxScope.() -> Unit
  34. ) {
  35.     val loadingContentCd = "Loading"
  36.     val finishedLoadingCd = "Finished Loading"
  37.  
  38.     var finishedLoading by remember { mutableStateOf(false) }
  39.  
  40.     // option 1: works for normal usages, but not when compose screen is closed quickly after loading is finished (isLoading=false state is not reached before navigating away)
  41.     // possible fix for that is to delay navigating away shortly after loading is finished  (~350ms delay seems to be enough on my test devices, but not 325ms.. not ideal since it could required different delays for different devices)
  42.     if (finishedLoading) {
  43.         Text(text = " ", modifier = Modifier
  44.             .focusable(false)
  45.             .semantics {
  46.                 liveRegion = LiveRegionMode.Assertive
  47.                 contentDescription = finishedLoadingCd
  48.             }
  49.         )
  50.     }
  51.  
  52.     Box(modifier.fillMaxSize()) {
  53.         content()
  54.         if (isLoading) {
  55.             Box(
  56.                 modifier = Modifier
  57.                     .fillMaxSize()
  58.                     .background(Color.White.copy(alpha = 0.8f))
  59.                     //.simpleClickable { Timber.i("FullScreenLoading clicked") }
  60.                     .semantics {
  61.                         liveRegion = LiveRegionMode.Assertive
  62.                         contentDescription = loadingContentCd
  63.                     }
  64.             ) {
  65.                 // Loading icon goes here
  66.                 Box(
  67.                     modifier = Modifier
  68.                         .size(iconSize)
  69.                         .align(Alignment.Center)
  70.                         .background(Color.Red)
  71.                 )
  72.                 val context = LocalContext.current
  73.                 DisposableEffect(context) {
  74.                     onDispose {
  75.                         // option 1:
  76.                         finishedLoading = true
  77.  
  78.                         // option 2: use announceForAccessibility through host activity
  79.                         // announceForAccessibility is not recommended even for View system anymore and Compose won't have support for it: https://issuetracker.google.com/issues/172590945
  80.                         // context.findActivity().getRootView().announceForAccessibility(finishedLoadingCd)
  81.                     }
  82.                 }
  83.             }
  84.         }
  85.     }
  86. }
  87.  
  88. @Preview
  89. @Composable
  90. private fun FullScreenLoadingPreview() {
  91.     var isLoading by remember { mutableStateOf(true) }
  92.  
  93.     LaunchedEffect(Unit) {
  94.         while (true) {
  95.             delay(3000)
  96.             isLoading = !isLoading
  97.         }
  98.     }
  99.     FullScreenLoading(isLoading = isLoading) {
  100.         Text(text = "Content")
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement