-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
832 additions
and
341 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 12 additions & 8 deletions
20
app/src/main/java/exchange/dydx/carteraExample/MainActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
app/src/main/java/exchange/dydx/carteraExample/ModalTransitionDialog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package exchange.dydx.carteraexample | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.animation.AnimatedVisibilityScope | ||
import androidx.compose.animation.core.tween | ||
import androidx.compose.animation.slideInVertically | ||
import androidx.compose.animation.slideOutVertically | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.compose.runtime.MutableState | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.runtime.rememberCoroutineScope | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.window.Dialog | ||
import androidx.compose.ui.window.DialogProperties | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.flow.MutableSharedFlow | ||
import kotlinx.coroutines.flow.asSharedFlow | ||
import kotlinx.coroutines.flow.collectLatest | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.sync.Mutex | ||
|
||
object ModalTransitionDialog { | ||
@OptIn(ExperimentalComposeUiApi::class) | ||
@Composable | ||
fun ModalTransitionDialog( | ||
onDismissRequest: () -> Unit, | ||
dismissOnBackPress: Boolean = true, | ||
content: @Composable (ModalTransitionDialogHelper) -> Unit | ||
) { | ||
|
||
val onCloseSharedFlow: MutableSharedFlow<Unit> = remember { MutableSharedFlow() } | ||
val coroutineScope: CoroutineScope = rememberCoroutineScope() | ||
val animateContentBackTrigger = remember { mutableStateOf(false) } | ||
val mutex = remember { Mutex(true) } | ||
|
||
LaunchedEffect(key1 = Unit) { | ||
launch { | ||
//delay(DIALOG_BUILD_TIME) | ||
animateContentBackTrigger.value = true | ||
} | ||
launch { | ||
onCloseSharedFlow.asSharedFlow().collectLatest { startDismissWithExitAnimation(animateContentBackTrigger, onDismissRequest) } | ||
} | ||
} | ||
|
||
Dialog( | ||
onDismissRequest = { coroutineScope.launch { startDismissWithExitAnimation(animateContentBackTrigger, onDismissRequest) } }, | ||
properties = DialogProperties(usePlatformDefaultWidth = false, dismissOnBackPress = dismissOnBackPress, dismissOnClickOutside = false) | ||
) { | ||
LaunchedEffect(key1 = Unit) { | ||
if (mutex.isLocked) mutex.unlock() | ||
} | ||
|
||
AnimatedModalBottomSheetTransition(visible = animateContentBackTrigger.value) { | ||
content(ModalTransitionDialogHelper(coroutineScope, onCloseSharedFlow)) | ||
} | ||
} | ||
} | ||
|
||
private suspend fun startDismissWithExitAnimation( | ||
animateContentBackTrigger: MutableState<Boolean>, | ||
onDismissRequest: () -> Unit | ||
) { | ||
animateContentBackTrigger.value = false | ||
delay(ANIMATION_TIME) | ||
onDismissRequest() | ||
} | ||
|
||
/** | ||
* Helper class that can be used inside the content scope from | ||
* composables that implement the [ModalTransitionDialog] to hide | ||
* the [Dialog] with a modal transition animation | ||
*/ | ||
class ModalTransitionDialogHelper( | ||
private val coroutineScope: CoroutineScope, | ||
private val onCloseFlow: MutableSharedFlow<Unit> | ||
) { | ||
fun triggerAnimatedClose() { | ||
coroutineScope.launch { | ||
onCloseFlow.emit(Unit) | ||
} | ||
} | ||
} | ||
|
||
internal const val ANIMATION_TIME = 500L | ||
|
||
@Composable | ||
internal fun AnimatedModalBottomSheetTransition( | ||
visible: Boolean, | ||
content: @Composable AnimatedVisibilityScope.() -> Unit | ||
) { | ||
AnimatedVisibility( | ||
visible = visible, | ||
enter = slideInVertically( | ||
animationSpec = tween(ANIMATION_TIME.toInt()), | ||
initialOffsetY = { fullHeight -> fullHeight } | ||
), | ||
exit = slideOutVertically( | ||
animationSpec = tween(ANIMATION_TIME.toInt()), | ||
targetOffsetY = { fullHeight -> fullHeight } | ||
), | ||
content = content | ||
) | ||
} | ||
} | ||
|
Oops, something went wrong.