This file contains hidden or 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
| LaunchedEffect(currentMessage.value) { | |
| currentMessage.value?.let { message -> | |
| scope.launch { | |
| val effectiveDuration = message.customDuration | |
| if (effectiveDuration is SnackbarDurationWrapper.Custom && | |
| !effectiveDuration.isIndefinite()) { | |
| // Simpler approach using withTimeoutOrNull | |
| val result = withTimeoutOrNull(effectiveDuration.getMilliseconds()) { |
This file contains hidden or 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
| // Undo action with precise 5-second window | |
| showCustomSnackbar( | |
| message = "Item deleted", | |
| actionLabel = "Undo", | |
| onAction = { restoreItem() }, | |
| durationMillis = 5000 | |
| ) | |
| // Quick success confirmation | |
| showCustomSnackbar( |
This file contains hidden or 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
| LaunchedEffect(currentMessage.value) { | |
| currentMessage.value?.let { message -> | |
| val result: SnackbarResult | |
| if (message.duration is SnackbarDurationWrapper.Custom) { | |
| // Create two competing operations | |
| val snackbarDeferred = async { | |
| snackbarHostState.showSnackbar( | |
| message = message.text, | |
| actionLabel = message.actionLabel, |
This file contains hidden or 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
| sealed class SnackbarDurationWrapper { | |
| data class Standard(val duration: SnackbarDuration) : SnackbarDurationWrapper() | |
| data class Custom(val millis: Long) : SnackbarDurationWrapper() | |
| companion object { | |
| fun fromMillis(milliseconds: Long): SnackbarDurationWrapper { | |
| require(milliseconds > 0) { "Duration must be positive" } | |
| return Custom(milliseconds) | |
| } |
This file contains hidden or 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
| // Before | |
| fun processPayment(payment: Payment) { | |
| val logger = ServiceLocator.get<Logger>() | |
| val paymentGateway = ServiceLocator.get<PaymentGateway>() | |
| // ... | |
| } | |
| // After | |
| context(Logger, PaymentGateway) | |
| fun processPayment(payment: Payment) { |
This file contains hidden or 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
| // Before | |
| class OrderService( | |
| private val database: Database, | |
| private val logger: Logger, | |
| private val emailService: EmailService | |
| ) { | |
| fun processOrder(order: Order) { /* ... */ } | |
| } | |
| // After |
This file contains hidden or 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
| // Good: Focused contexts | |
| context(DatabaseContext, LoggingContext) | |
| fun processOrder(order: Order) { /* ... */ } | |
| // Avoid: Too many contexts | |
| context(DatabaseContext, LoggingContext, EmailContext, PaymentContext, InventoryContext, ShippingContext) | |
| fun processComplexOrder(order: Order) { /* ... */ } |
This file contains hidden or 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
| class FakeTracker : ActivityTracker { | |
| val activities = mutableListOf<String>() | |
| override fun record(activity: String) { activities.add(activity) } | |
| } | |
| @Test | |
| fun verifyUserRegistration() { | |
| val fakeTracker = FakeTracker() | |
| val fakeStorage = InMemoryStorage() | |
This file contains hidden or 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
| interface UserStorage { | |
| suspend fun persist(user: User) | |
| } | |
| interface ActivityTracker { | |
| fun record(activity: String) | |
| } | |
| context(storage: UserStorage, tracker: ActivityTracker) | |
| class AccountManager { |
This file contains hidden or 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
| // Declaring a function with context parameters | |
| context(ExecutionContext, Logger) | |
| suspend fun fetchUserData(userId: String): User { | |
| log("Fetching user data for $userId") | |
| return withContext(Dispatchers.IO) { | |
| // Database operation | |
| userRepository.findById(userId) | |
| } | |
| } |
NewerOlder