### Start Koin for JVM and iOS entry points Source: https://github.com/yasan-org/toolkit Shows how to start Koin with toolkitModule in JVM desktop and iOS Compose entry points. The JVM example uses a main function with a Compose window; the iOS example configures Koin inside ComposeUIViewController. ```kotlin // JVM desktop: fun main() { startKoin { modules(toolkitModule) } application { Window(onCloseRequest = ::exitApplication, title = "Toolkit") { SampleApp() } } } // iOS: fun MainViewController() = ComposeUIViewController( configure = { startKoin { modules(toolkitModule) } }, ) { SampleApp() } ``` -------------------------------- ### Internal helpers usage example (opt-in required) Source: https://github.com/yasan-org/toolkit Shows how to use the internal helpers with @OptIn(InternalToolkitApi::class). The function returns the raw string if parsing fails, otherwise appends a campaign parameter. ```kotlin @OptIn(InternalToolkitApi::class) fun buildTrackingUrl(raw: String, app: ToolkitApp): String { val url = raw.withUtmSourceParameter(app) // Url? — null if unparseable ?: return raw return url.withParameter("campaign", "summer").toString() } ``` -------------------------------- ### Start Koin with toolkitModule in Android Application Source: https://github.com/yasan-org/toolkit Initializes Koin with the toolkitModule in an Android Application class, supplying the Android context for the UrlLauncher. Required for any app using the toolkit's Koin bindings. ```kotlin import glass.yasan.toolkit.koin.toolkitModule import org.koin.android.ext.koin.androidContext import org.koin.core.context.startKoin class SampleApplication : Application() { override fun onCreate() { super.onCreate() startKoin { androidLogger() androidContext(this@SampleApplication) // supplies Context for the Android UrlLauncher modules(toolkitModule) } } } ``` -------------------------------- ### toolkitModule Source: https://github.com/yasan-org/toolkit Public Koin module that provides dependency injection setup for the toolkit. Users can include this module in their Koin configuration. ```APIDOC ## toolkitModule ### Description Public Koin module that provides dependency injection setup for the toolkit. Users can include this module in their Koin configuration. ### Type `org.koin.core.module.Module` ### Location `ToolkitModule.kt:13` ### Usage Include this module in your Koin setup to use the toolkit's dependencies. ``` -------------------------------- ### Display-only link usage example Source: https://github.com/yasan-org/toolkit Shows how to use the public trackedUrl API to tag a link for display, e.g., QR codes. The comment shows the resulting URL. ```kotlin import glass.yasan.toolkit.core.app.ToolkitApp import glass.yasan.toolkit.core.tracking.trackedUrl val app = ToolkitApp(id = "my-app", utmSource = "my.app.package") val qrScannerValue = app.trackedUrl("https://example.com/landing") // https://example.com/landing?utm_source=my.app.package ``` -------------------------------- ### Inject UrlLauncher and DispatcherProvider with koinInject Source: https://github.com/yasan-org/toolkit Demonstrates injecting UrlLauncher (factory) and DispatcherProvider (singleton) in a composable using koinInject. Note that ViewActionEffect independently injects a DispatcherProvider, so any app using it must have toolkitModule started. ```kotlin import glass.yasan.toolkit.core.coroutines.DispatcherProvider import glass.yasan.toolkit.core.url.UrlLauncher import org.koin.compose.koinInject @Composable fun Screen() { val urlLauncher: UrlLauncher = koinInject() // factory — new per injection point val dispatcher: DispatcherProvider = koinInject() // singleton } ``` -------------------------------- ### SampleViewModel extending ToolkitViewModel with State, Event, Action Source: https://github.com/yasan-org/toolkit Example ViewModel that extends ToolkitViewModel with concrete State, Event, and Action types. It defines a count state, increment/decrement events, and a URL-launch action. Used in the sample app at sample/composeApp/.../SampleViewModel.kt. ```kotlin internal class SampleViewModel : ToolkitViewModel() { data class State(val count: Int = 0) : ViewState sealed interface Event : ViewEvent { data object Increment : Event data object Decrement : Event data object WebsiteClick : Event } sealed interface Action : ViewAction { data class LaunchUrl(val url: String) : Action } } ``` -------------------------------- ### Subclass ToolkitViewModel and wire the loop Source: https://github.com/yasan-org/toolkit Example of a ViewModel subclass that defines state, event, and action types, and overrides defaultViewState and onViewEvent to handle events. ```kotlin class CounterViewModel : ToolkitViewModel() { data class CounterState(val count: Int = 0) : ViewState sealed interface CounterEvent : ViewEvent { data object Increment : CounterEvent } sealed interface CounterAction : ViewAction { data class ShowToast(val message: String) : CounterAction } override fun defaultViewState(): CounterState = CounterState() override suspend fun onViewEvent(event: CounterEvent) { when (event) { CounterEvent.Increment -> updateViewState { copy(count = count + 1) } } } } ``` -------------------------------- ### ViewActionEffect composable helper Source: https://github.com/yasan-org/toolkit Collects view actions from a Flow while the lifecycle is at least STARTED, using flowWithLifecycle and flowOn(dispatcherProvider.main). Requires a DispatcherProvider resolvable from the Koin graph (e.g., toolkitModule) or throws a Koin resolution error at composition time. ```kotlin @Composable public fun ViewActionEffect( viewAction: Flow, onViewAction: (A) -> Unit, ) { val dispatcherProvider: DispatcherProvider = koinInject() val lifecycle = LocalLifecycleOwner.current.lifecycle LaunchedEffect(viewAction, lifecycle) { viewAction .flowWithLifecycle(lifecycle) .flowOn(dispatcherProvider.main) .collect(onViewAction) } } ``` -------------------------------- ### ApplicationScope typealias Source: https://github.com/yasan-org/toolkit Alias for a process-wide CoroutineScope. toolkitModule publishes one as CoroutineScope(SupervisorJob() + get().default), i.e., a scope with a SupervisorJob on the default dispatcher. ```kotlin public typealias ApplicationScope = CoroutineScope ``` -------------------------------- ### Typealias `ApplicationScope` Source: https://github.com/yasan-org/toolkit A named alias for a process-wide CoroutineScope. toolkitModule publishes one as CoroutineScope(SupervisorJob() + get().default). ```APIDOC ## Typealias `ApplicationScope` ### Description A named alias for a process-wide `CoroutineScope`. `toolkitModule` publishes one as `CoroutineScope(SupervisorJob() + get().default)` — i.e. a scope whose job is a `SupervisorJob` on the `default` dispatcher. ### Definition ```kotlin public typealias ApplicationScope = CoroutineScope ``` ``` -------------------------------- ### Usage of collectAsStateWithLifecycleBlocking with StateFlow Source: https://github.com/yasan-org/toolkit Example of collecting a StateFlow (e.g., viewModel.viewState) into a composable state. The blocking first() resolves instantly because StateFlow has an immediate value. ```kotlin import glass.yasan.toolkit.compose.coroutines.collectAsStateWithLifecycleBlocking import glass.yasan.toolkit.compose.viewmodel.ToolkitViewModel @Composable fun Screen(viewModel: MyViewModel) { // StateFlow always has an immediate value, so the blocking first() resolves instantly: val state by viewModel.viewState.collectAsStateWithLifecycleBlocking() Text("Count: ${state.count}") } ``` -------------------------------- ### ViewActionEffect Source: https://github.com/yasan-org/toolkit Composable helper that collects a Flow of ViewActions while the lifecycle is at least STARTED, invoking a handler for each action. It requires a DispatcherProvider from the Koin graph and cancels when the composition leaves or the flow changes. ```APIDOC ## ViewActionEffect ### Description Composable helper that collects a Flow of ViewActions while the lifecycle is at least STARTED, invoking a handler for each action. It requires a DispatcherProvider from the Koin graph and cancels when the composition leaves or the flow changes. ### Signature ```kotlin @Composable public fun ViewActionEffect( viewAction: Flow, onViewAction: (A) -> Unit, ) ``` ### Parameters - `viewAction` (Flow) - Required - Usually `viewModel.viewAction`. - `onViewAction` ((A) -> Unit) - Required - Side-effect handler invoked per action. ### Requirements A `DispatcherProvider` must be resolvable from the Koin graph (`koinInject()`), i.e. `toolkitModule` (or equivalent) must be started — otherwise this throws a Koin resolution error at composition time. ### Behavior Collects actions only while the lifecycle is at least STARTED (`flowWithLifecycle`), collecting on `dispatcherProvider.main`. Cancelled when the composition leaves or the flow changes. ``` -------------------------------- ### Opt-in usage for ExperimentalToolkitApi Source: https://github.com/yasan-org/toolkit Shows how to opt in to `ExperimentalToolkitApi` using `@OptIn` when calling `ResolvableString.resolve()`. Without this annotation, the call would produce a warning. ```kotlin @OptIn(ExperimentalToolkitApi::class) fun render(label: ResolvableString): String = label.resolve() ``` -------------------------------- ### Obtain a provider and build a scope Source: https://github.com/yasan-org/toolkit Creates a default DispatcherProvider and an ApplicationScope using SupervisorJob and the default dispatcher. Then launches I/O work via the provider's io dispatcher. ```kotlin import glass.yasan.toolkit.core.coroutines.ApplicationScope import glass.yasan.toolkit.core.coroutines.createDefaultDispatcherProvider import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.SupervisorJob val dispatchers = createDefaultDispatcherProvider() val appScope: ApplicationScope = CoroutineScope(SupervisorJob() + dispatchers.default) // Launch I/O work via the provider: appScope.launch { withContext(dispatchers.io) { /* disk or network */ } } ``` -------------------------------- ### Manually construct UrlLauncherImpl with InternalToolkitApi Source: https://github.com/yasan-org/toolkit Manual construction requires opting into the internal API with @OptIn(InternalToolkitApi::class). Pass a Context and ToolkitApp to UrlLauncherImpl. ```kotlin @OptIn(InternalToolkitApi::class) fun manualLauncher(context: Context, app: ToolkitApp): UrlLauncher = UrlLauncherImpl(context = context, toolkitApp = app) ``` -------------------------------- ### launch(url: String): UrlLaunchResult Source: https://github.com/yasan-org/toolkit Opens the given URL in the platform browser or app handler and returns a structured result. This method never throws; all exceptions are caught and mapped into a Failure result. ```APIDOC ## launch(url: String): UrlLaunchResult ### Description Opens the given URL in the platform browser or app handler and returns a structured result. This method never throws; all exceptions are caught and mapped into a Failure result. ### Method launch ### Parameters #### Parameters - **url** (String) - Required - The URL to open. No scheme is implied; ktor `parseUrl` is used and non-HTTP(S) schemes are treated as trackable-but-valid on most platforms. ### Response #### Returns - **UrlLaunchResult** - A `Success` or a `Failure` result. ``` -------------------------------- ### Basic usage: construct app identity and display tracked link Source: https://github.com/yasan-org/toolkit Creates a ToolkitApp and uses trackedUrl to tag a link for display (e.g., QR code). The output shows the appended utm_source parameter. ```kotlin import glass.yasan.toolkit.core.app.ToolkitApp val app = ToolkitApp( id = "com.example.myapp", utmSource = "com.example.myapp", ) // A QR-code screen renders the link, so it carries the utm_source: val qrContent = app.trackedUrl("https://yasan.glass") // -> "https://yasan.glass?utm_source=com.example.myapp" ``` -------------------------------- ### Inject UrlLauncher via Koin and launch URL Source: https://github.com/yasan-org/toolkit Recommended usage: inject UrlLauncher with koinInject() and launch a URL in a coroutine scope. Handle UrlLaunchResult.Success and UrlLaunchResult.Failure in a when expression. ```kotlin import glass.yasan.toolkit.core.url.UrlLauncher import glass.yasan.toolkit.core.url.UrlLaunchResult import org.koin.compose.koinInject import kotlinx.coroutines.launch @Composable fun LinkButton() { val urlLauncher: UrlLauncher = koinInject() val scope = rememberCoroutineScope() Button(onClick = { scope.launch { when (val result = urlLauncher.launch("https://yasan.glass")) { is UrlLaunchResult.Success -> { /* opened */ } is UrlLaunchResult.Failure -> { /* handle */ } } } }) { Text("Open website") } } ``` -------------------------------- ### Pattern-match on UrlLaunchResult Source: https://github.com/yasan-org/toolkit Use when handling launch results in consumer code, since `launch` never throws. The `when` expression matches `Success` and the three `Failure` subtypes, invoking `onError` with a message for each failure case. ```kotlin suspend fun open(url: String, onError: (String) -> Unit) { when (val result = urlLauncher.launch(url)) { is UrlLaunchResult.Success -> Unit is UrlLaunchResult.Failure.InvalidUrl -> onError("Malformed URL: $url") is UrlLaunchResult.Failure.Unsupported -> onError("No app/desktop browsing available") is UrlLaunchResult.Failure.Error -> onError("Unexpected: ${result.exception.message}") } } ``` -------------------------------- ### Handle URL launch errors with callback overload Source: https://github.com/yasan-org/toolkit Use the callback overload of launch() to handle failures. Distinguish between InvalidUrl, Unsupported, and Error failure types, showing appropriate messages. ```kotlin suspend fun openWithFeedback(url: String) { urlLauncher.launch(url) { failure -> when (failure) { is UrlLaunchResult.Failure.InvalidUrl -> showError("Malformed URL") is UrlLaunchResult.Failure.Unsupported -> showError("No handler available") is UrlLaunchResult.Failure.Error -> showError(failure.exception.message) } } } ``` -------------------------------- ### launch(url: String, onFailure: (UrlLaunchResult.Failure) -> Unit) Source: https://github.com/yasan-org/toolkit Opens the given URL and invokes the provided callback if the result is a Failure. This is a default interface method that delegates to the single-argument launch method and dispatches the result. ```APIDOC ## launch(url: String, onFailure: (UrlLaunchResult.Failure) -> Unit) ### Description Opens the given URL and invokes the provided callback if the result is a Failure. This is a default interface method that delegates to the single-argument launch method and dispatches the result. ### Method launch ### Parameters #### Parameters - **url** (String) - Required - The URL to open. No scheme is implied; ktor `parseUrl` is used and non-HTTP(S) schemes are treated as trackable-but-valid on most platforms. - **onFailure** ((UrlLaunchResult.Failure) -> Unit) - Required - Callback invoked exactly when the result is a `Failure`; not invoked for `Success`. ### Response #### Returns - **Unit** - This method returns Unit. ``` -------------------------------- ### Flow.collectAsStateWithLifecycleBlocking(): State Source: https://github.com/yasan-org/toolkit A composable extension function that collects a Flow into a Compose State, blocking the calling thread until the first emission to use as the initial value. Subsequent emissions update the state lifecycle-aware (only while at least STARTED). ```APIDOC ## [Composable] Flow.collectAsStateWithLifecycleBlocking(): State ### Description A composable extension function that collects a Flow into a Compose State, blocking the calling thread until the first emission to use as the initial value. Subsequent emissions update the state lifecycle-aware (only while at least STARTED). ### Method Composable extension function ### Signature ```kotlin @Composable public fun Flow.collectAsStateWithLifecycleBlocking(): State ``` ### Parameters None ### Returns - `androidx.compose.runtime.State` — a compose state updated with the flow's emissions, lifecycle-aware ### Behavior Delegates to `androidx.lifecycle.compose.collectAsStateWithLifecycle` with `initialValue = firstBlocking()`. `firstBlocking()` is `runBlocking { first() }`, so at composition time the function blocks the calling thread until the flow emits its first element and uses that element as the state's initial value. Subsequent emissions update the state only while the lifecycle is at least STARTED. ### Availability Non-web targets only (`nonWebMain` source set); the function is absent from JS/WasmJS binaries. ### Usage Example ```kotlin import glass.yasan.toolkit.compose.coroutines.collectAsStateWithLifecycleBlocking import glass.yasan.toolkit.compose.viewmodel.ToolkitViewModel @Composable fun Screen(viewModel: MyViewModel) { // StateFlow always has an immediate value, so the blocking first() resolves instantly: val state by viewModel.viewState.collectAsStateWithLifecycleBlocking() Text("Count: ${state.count}") } ``` ### Integration with ViewModel Helpers Example ```kotlin import glass.yasan.toolkit.compose.viewmodel.ToolkitViewModel class FeedViewModel : ToolkitViewModel() { private val feed: Flow> = repository.observeFeed() // Derive a StateFlow that is already started by the time the UI reads it // (Eagerly = collecting regardless of subscribers): val feedState: StateFlow> = feed.stateInEagerly(initialValue = emptyList()) } @Composable fun Feed(feedState: StateFlow>) { // StateFlow -> first element is its current value; blocking initial read is immediate. val items by feedState.collectAsStateWithLifecycleBlocking() LazyColumn { items(items) { ItemRow(it) } } } ``` ### Caveats - Only use with flows guaranteed to emit promptly (e.g. `StateFlow`, which has an immediate value) — a cold flow that never emits blocks the composition thread forever. - Because it blocks, calling it from a composition during an animation frame risks main-thread stutter for slow first emissions; the interop pattern is designed for `StateFlow`s like `viewModel.viewState`. - No error handling is performed; `firstBlocking()` propagates any exception thrown by the upstream flow untouched. ``` -------------------------------- ### Usage with @PreviewParameter Source: https://github.com/yasan-org/toolkit Shows how to use both providers in @Preview composables. Each @Preview renders one variant per element of values: two for BooleanPreviewParameterProvider, three for NullableBooleanPreviewParameterProvider. ```kotlin import glass.yasan.toolkit.compose.preview.BooleanPreviewParameterProvider import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import glass.yasan.toolkit.compose.animation.VerticalAnimatedVisibility @Preview @Composable private fun CollapsiblePreview( @PreviewParameter(BooleanPreviewParameterProvider::class) visible: Boolean, ) { VerticalAnimatedVisibility(visible = visible) { Text("Preview state: $visible") } } @Preview @Composable private fun NullablePreview( @PreviewParameter(NullableBooleanPreviewParameterProvider::class) value: Boolean?, ) { Text(value?.toString() ?: "null") } ``` -------------------------------- ### BooleanPreviewParameterProvider class Source: https://github.com/yasan-org/toolkit Provides a sequence of Boolean values (true, false) for Compose previews. Parameterless; yields true then false. ```kotlin public class BooleanPreviewParameterProvider : PreviewParameterProvider { override val values: Sequence = sequenceOf(true, false) } ``` -------------------------------- ### NullableBooleanPreviewParameterProvider class Source: https://github.com/yasan-org/toolkit Provides a sequence of Boolean? values (true, false, null) for Compose previews. Parameterless; yields true, false, then null. ```kotlin public class NullableBooleanPreviewParameterProvider : PreviewParameterProvider { override val values: Sequence = sequenceOf(true, false, null) } ``` -------------------------------- ### If you use toolkitModule, the provider comes from Koin Source: https://github.com/yasan-org/toolkit Injects DispatcherProvider and ApplicationScope from Koin, resolved from toolkitModule's single and SupervisorJob + default dispatcher. ```kotlin val provider: DispatcherProvider = koinInject() // resolved from toolkitModule's single val scope: ApplicationScope = koinInject() // SupervisorJob + default dispatcher ``` -------------------------------- ### ExperimentalToolkitApi Source: https://github.com/yasan-org/toolkit Annotation to mark experimental APIs. Requires opt-in with a warning. ```APIDOC ## ExperimentalToolkitApi ### Description Annotation to mark experimental APIs. Requires opt-in with a warning. ### Definition ```kotlin @RequiresOptIn(level = RequiresOptIn.Level.WARNING) annotation class ExperimentalToolkitApi ``` ``` -------------------------------- ### launch(url: String, onFailure: (UrlLaunchResult.Failure) -> Unit) Source: https://github.com/yasan-org/toolkit Launches the given URL and invokes the onFailure callback if the launch fails. This is a suspend function that should be called from a coroutine. ```APIDOC ## [SUSPEND] launch(url: String, onFailure: (UrlLaunchResult.Failure) -> Unit) ### Description Launches the given URL and invokes the onFailure callback if the launch fails. This is a suspend function that should be called from a coroutine. ### Method suspend fun ### Endpoint UrlLauncher.launch(url: String, onFailure: (UrlLaunchResult.Failure) -> Unit) ### Parameters #### Parameters - **url** (String) - Required - The URL to launch. - **onFailure** ((UrlLaunchResult.Failure) -> Unit) - Required - Callback invoked with the failure result if the launch fails. ### Response #### Success Response - **Unit** - The function returns Unit; success is indicated by the absence of the onFailure callback invocation. ### Request Example ```kotlin urlLauncher.launch("https://example.com") { failure -> // handle failure } ``` #### Response Example ```kotlin Unit ``` ``` -------------------------------- ### launch(url: String): UrlLaunchResult Source: https://github.com/yasan-org/toolkit Launches the given URL and returns a UrlLaunchResult indicating success or failure. This is a suspend function that should be called from a coroutine. ```APIDOC ## [SUSPEND] launch(url: String): UrlLaunchResult ### Description Launches the given URL and returns a UrlLaunchResult indicating success or failure. This is a suspend function that should be called from a coroutine. ### Method suspend fun ### Endpoint UrlLauncher.launch(url: String): UrlLaunchResult ### Parameters #### Parameters - **url** (String) - Required - The URL to launch. ### Response #### Success Response - **UrlLaunchResult** - The result of the launch, either Success or a Failure subtype. ### Request Example ```kotlin val result = urlLauncher.launch("https://example.com") ``` #### Response Example ```kotlin UrlLaunchResult.Success ``` ``` -------------------------------- ### UrlLauncher interface with launch methods Source: https://github.com/yasan-org/toolkit Defines the public interface for opening URLs. The two-argument overload is a default method that delegates to the single-argument launch and invokes onFailure only on Failure results. The interface never throws; exceptions are mapped to Failure.Error by implementations. ```kotlin public interface UrlLauncher { public suspend fun launch( url: String, ): UrlLaunchResult public suspend fun launch( url: String, onFailure: (UrlLaunchResult.Failure) -> Unit, ) { when (val result = launch(url)) { is UrlLaunchResult.Success -> Unit is UrlLaunchResult.Failure -> onFailure(result) } } } ``` -------------------------------- ### Add About dependency Source: https://github.com/yasan-org/toolkit/blob/main/README.md Gradle dependency for the About module. Replace with the actual version. ```kotlin implementation("glass.yasan.toolkit:about:") ``` -------------------------------- ### Bridge from internal to public surface with InternalToolkitApi Source: https://github.com/yasan-org/toolkit Shows the pattern used inside the library to bridge from internal to public surface, using `@OptIn(InternalToolkitApi::class)` to call `withUtmSourceParameter`. ```kotlin // Bridge from internal to public surface — the pattern used inside the library: @OptIn(InternalToolkitApi::class) public fun ToolkitApp.trackedUrl(url: String): String = url.withUtmSourceParameter(this)?.toString() ?: url ``` -------------------------------- ### UrlLauncherImpl expect class declaration Source: https://github.com/yasan-org/toolkit Internal expect class for the URL launcher. Annotated with @InternalToolkitApi, constructing it from outside the package is a compile-time error unless opted in. The intended acquisition path is Koin (factory inside platformModule). ```kotlin @InternalToolkitApi public expect class UrlLauncherImpl : UrlLauncher { override suspend fun launch(url: String): UrlLaunchResult } ``` -------------------------------- ### DispatcherProvider (Koin injection) Source: https://github.com/yasan-org/toolkit When using toolkitModule, the DispatcherProvider and ApplicationScope are provided via Koin. They can be injected using koinInject(). ```APIDOC ## [KOIN_INJECTION] DispatcherProvider and ApplicationScope ### Description When using `toolkitModule`, the `DispatcherProvider` and `ApplicationScope` are provided via Koin. They can be injected using `koinInject()`. ### Usage Example ```kotlin val provider: DispatcherProvider = koinInject() // resolved from toolkitModule's single val scope: ApplicationScope = koinInject() // SupervisorJob + default dispatcher ``` ``` -------------------------------- ### Wiring ToolkitApp through Koin Source: https://github.com/yasan-org/toolkit Registers a single ToolkitApp in the Koin graph so UrlLauncher factories can resolve it. Includes toolkitModule and a SampleViewModel. ```kotlin val sampleModule = module { includes(toolkitModule) single { ToolkitApp( id = "toolkit-sample", utmSource = "glass.yasan.toolkit.sample", ) } viewModelOf(::SampleViewModel) } ``` -------------------------------- ### NullableBooleanPreviewParameterProvider Source: https://github.com/yasan-org/toolkit A PreviewParameterProvider that yields Boolean? values true, false, and null for generating Compose preview variants. ```APIDOC ## Class NullableBooleanPreviewParameterProvider ### Description A `PreviewParameterProvider` that yields `true`, `false`, then `null` for generating Compose preview variants. ### Members - `values` (`Sequence`) - Yields `true`, `false`, then `null`. ### Usage Example ```kotlin import glass.yasan.toolkit.compose.preview.NullableBooleanPreviewParameterProvider import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter @Preview @Composable private fun NullablePreview( @PreviewParameter(NullableBooleanPreviewParameterProvider::class) value: Boolean?, ) { Text(value?.toString() ?: "null") } ``` ``` -------------------------------- ### createDefaultDispatcherProvider() Source: https://github.com/yasan-org/toolkit Creates a new DefaultDispatcherProvider instance backed by platform dispatchers. The Koin module toolkitModule uses it to provide the DispatcherProvider singleton. ```APIDOC ## Function `createDefaultDispatcherProvider()` ### Description Creates a new `DefaultDispatcherProvider` instance backed by the platform dispatchers. The Koin module `toolkitModule` uses it to provide the `DispatcherProvider` singleton. ### Signature ```kotlin public expect fun createDefaultDispatcherProvider(): DefaultDispatcherProvider ``` ### Returns A new `DefaultDispatcherProvider` instance backed by the platform dispatchers. ``` -------------------------------- ### Use callback overload with onFailure Source: https://github.com/yasan-org/toolkit Alternative to pattern-matching: the callback overload's `onFailure: (UrlLaunchResult.Failure) -> Unit` receives every failure and is skipped on success. ```kotlin urlLauncher.launch(url) { failure -> log(failure) } ``` -------------------------------- ### Basic usage of ResolvableString states Source: https://github.com/yasan-org/toolkit Shows how to create and use the three ResolvableString variants in a composable. The resolve() method must be called from a composable context. Requires @OptIn(ExperimentalToolkitApi::class). ```kotlin import glass.yasan.toolkit.compose.string.ResolvableString import glass.yasan.toolkit.composeapp.generated.resources.Res import glass.yasan.toolkit.composeapp.generated.resources.greeting import glass.yasan.toolkit.composeapp.generated.resources.hello_name @OptIn(ExperimentalToolkitApi::class) @Composable fun Greeting(title: String) { val label: ResolvableString = when { title.isEmpty() -> ResolvableString.Literal("Generic greeting") else -> ResolvableString.Resource(Res.string.greeting) } val personalized = ResolvableString.ResourceWithArgs( resource = Res.string.hello_name, formatArgs = listOf("Ada"), ) Text(label.resolve()) Text(personalized.resolve()) } ``` -------------------------------- ### UrlLauncher Source: https://github.com/yasan-org/toolkit Interface for launching URLs in the platform's default browser or appropriate handler. ```APIDOC ## UrlLauncher ### Description Interface for launching URLs in the platform's default browser or appropriate handler. ### Definition ```kotlin interface UrlLauncher { suspend fun launch(url: String): UrlLaunchResult } ``` ``` -------------------------------- ### DispatcherProvider Source: https://github.com/yasan-org/toolkit Interface for providing coroutine dispatchers. Default implementations are available for non-web and web targets. ```APIDOC ## DispatcherProvider ### Description Interface for providing coroutine dispatchers. Default implementations are available for non-web and web targets. ### Definition ```kotlin interface DispatcherProvider { val main: CoroutineDispatcher val io: CoroutineDispatcher val default: CoroutineDispatcher val unconfined: CoroutineDispatcher } ``` ### DefaultDispatcherProvider ```kotlin expect class DefaultDispatcherProvider : DispatcherProvider ``` ### createDefaultDispatcherProvider() ```kotlin expect fun createDefaultDispatcherProvider(): DispatcherProvider ``` ``` -------------------------------- ### Add Core dependency Source: https://github.com/yasan-org/toolkit/blob/main/README.md Gradle dependency for the Core module. Replace with the actual version. ```kotlin implementation("glass.yasan.toolkit:core:") ``` -------------------------------- ### BooleanPreviewParameterProvider Source: https://github.com/yasan-org/toolkit A PreviewParameterProvider that yields Boolean values true and false for generating Compose preview variants. ```APIDOC ## Class BooleanPreviewParameterProvider ### Description A `PreviewParameterProvider` that yields `true`, then `false` for generating Compose preview variants. ### Members - `values` (`Sequence`) - Yields `true`, then `false`. ### Usage Example ```kotlin import glass.yasan.toolkit.compose.preview.BooleanPreviewParameterProvider import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.PreviewParameter import glass.yasan.toolkit.compose.animation.VerticalAnimatedVisibility @Preview @Composable private fun CollapsiblePreview( @PreviewParameter(BooleanPreviewParameterProvider::class) visible: Boolean, ) { VerticalAnimatedVisibility(visible = visible) { Text("Preview state: $visible") } } ``` ``` -------------------------------- ### createDefaultDispatcherProvider Source: https://github.com/yasan-org/toolkit A function that creates and returns a new `DefaultDispatcherProvider` instance. ```APIDOC ## createDefaultDispatcherProvider ### Description Creates and returns a new `DefaultDispatcherProvider` instance. ### Returns - `DefaultDispatcherProvider` - A new instance of `DefaultDispatcherProvider`. ``` -------------------------------- ### ToolkitApp.trackedUrl(url: String): String Source: https://github.com/yasan-org/toolkit Extension function that appends a `utm_source` query parameter to a given URL if it is an HTTP or HTTPS URL. Returns the original string unchanged for non-HTTP(S) or malformed inputs. ```APIDOC ## ToolkitApp.trackedUrl(url: String): String ### Description Extension function that appends a `utm_source` query parameter to a given URL if it is an HTTP or HTTPS URL. Returns the original string unchanged for non-HTTP(S) or malformed inputs. ### Parameters - **url** (String) - Required - The raw URL to tag. ### Returns - **String** - The URL with `utm_source=` appended as a query parameter if the input parses as an HTTP or HTTPS URL; the input string unchanged otherwise. ### Behavior Notes - Non-HTTP(S) protocols (e.g. `mailto:`, `tel:`, deep links) pass through untagged. - Unparseable input returns the original string unchanged. - The parameter is set via `URLBuilder` parameter map, so an existing `utm_source` is overwritten. ### Throws None — malformed input is coerced to the original string. ### Example ```kotlin val app = ToolkitApp( id = "com.example.myapp", utmSource = "com.example.myapp", ) val qrContent = app.trackedUrl("https://yasan.glass") // -> "https://yasan.glass?utm_source=com.example.myapp" ``` ``` -------------------------------- ### DefaultDispatcherProvider Source: https://github.com/yasan-org/toolkit An expect/actual class that provides coroutine dispatchers for main, IO, default, and unconfined contexts. Actual bindings differ between web and non-web platforms. ```APIDOC ## DefaultDispatcherProvider ### Description An expect/actual class that provides coroutine dispatchers for main, IO, default, and unconfined contexts. Actual bindings differ between web and non-web platforms. ### Properties - `main` (CoroutineDispatcher) - The main dispatcher. - `io` (CoroutineDispatcher) - The IO dispatcher. - `default` (CoroutineDispatcher) - The default dispatcher. - `unconfined` (CoroutineDispatcher) - The unconfined dispatcher. ### Constructors - `DefaultDispatcherProvider()` - Creates a new instance. Typically obtained via `createDefaultDispatcherProvider()`. ``` -------------------------------- ### ApplicationScope Source: https://github.com/yasan-org/toolkit Typealias for the application-wide coroutine scope. ```APIDOC ## ApplicationScope ### Description Typealias for the application-wide coroutine scope. ### Definition ```kotlin typealias ApplicationScope = CoroutineScope ``` ``` -------------------------------- ### toolkitModule Koin module definition Source: https://github.com/yasan-org/toolkit Defines the public Koin module that includes the platform module and registers DispatcherProvider and ApplicationScope singletons. Requires a ToolkitApp binding to resolve UrlLauncher; otherwise Koin throws a missing-dependency error. ```kotlin @OptIn(InternalToolkitApi::class) public val toolkitModule: Module = module { includes(platformModule) single { createDefaultDispatcherProvider() } single { CoroutineScope(SupervisorJob() + get().default) } } ``` -------------------------------- ### Add Compose dependency Source: https://github.com/yasan-org/toolkit/blob/main/README.md Gradle dependency for the Compose module. Replace with the actual version. ```kotlin implementation("glass.yasan.toolkit:compose:") ``` -------------------------------- ### createDefaultDispatcherProvider() expect declaration Source: https://github.com/yasan-org/toolkit Declares a platform-specific factory for a DefaultDispatcherProvider. Actual implementations are provided for non-web and web targets. The Koin module toolkitModule uses it to provide the DispatcherProvider singleton. ```kotlin public expect fun createDefaultDispatcherProvider(): DefaultDispatcherProvider ``` -------------------------------- ### Using AnimatedVisibilityScope content receiver for nested transitions Source: https://github.com/yasan-org/toolkit Demonstrates nesting another AnimatedVisibility inside the content lambda, which is possible because the receiver is an AnimatedVisibilityScope. The inner transition animates independently of the outer wrapper. ```kotlin VerticalAnimatedVisibility(visible = isVisible) { // Inside AnimatedVisibilityScope — can nest another transition on a child: AnimatedVisibility(visible = innerVisible) { Text("Nested content transition") } } ``` -------------------------------- ### InternalToolkitApi Source: https://github.com/yasan-org/toolkit Annotation to mark internal APIs. Requires opt-in with an error. ```APIDOC ## InternalToolkitApi ### Description Annotation to mark internal APIs. Requires opt-in with an error. ### Definition ```kotlin @RequiresOptIn(level = RequiresOptIn.Level.ERROR) annotation class InternalToolkitApi ``` ``` -------------------------------- ### Url.withUtmSourceParameter - internal helper Source: https://github.com/yasan-org/toolkit Internal function that adds the utm_source parameter to a ktor Url, overwriting any existing value per URLBuilder semantics. ```kotlin @InternalToolkitApi public fun Url.withUtmSourceParameter( toolkitApp: ToolkitApp, ): Url = withParameter( name = "utm_source", value = toolkitApp.utmSource, ) ``` -------------------------------- ### Non-HTTP and malformed inputs pass through Source: https://github.com/yasan-org/toolkit Shows that deep links and malformed URLs are returned unchanged, while HTTP URLs with existing utm_source are overwritten. ```kotlin val app = ToolkitApp(id = "app", utmSource = "app") // Deep link: protocol is not HTTP/HTTPS, unchanged. app.trackedUrl("myapp://screen/42") // -> "myapp://screen/42" // Malformed input: ktor parseUrl returns null, unchanged. app.trackedUrl("not a url") // -> "not a url" // HTTP URL, existing parameter overwritten: app.trackedUrl("https://example.com/path?utm_source=old") // -> "https://example.com/path?utm_source=app" ``` -------------------------------- ### ViewState, ViewEvent, ViewAction marker interfaces Source: https://github.com/yasan-org/toolkit Marker interfaces with no members, used to parametrize ToolkitViewModel. Consuming code subclasses or sealed-interfaces them, as in SampleViewModel. ```kotlin public interface ViewState // ViewState.kt:3 public interface ViewEvent // ViewEvent.kt:8 public interface ViewAction // ViewAction.kt:12 ``` -------------------------------- ### String.withUtmSourceParameter - internal helper Source: https://github.com/yasan-org/toolkit Internal function that parses a string URL and appends utm_source for HTTP/HTTPS protocols. Returns null if parseUrl fails, which triggers Failure.InvalidUrl in launchers. ```kotlin @InternalToolkitApi public fun String.withUtmSourceParameter(toolkitApp: ToolkitApp): Url? = parseUrl(this)?.let { url -> when (url.protocol) { URLProtocol.HTTP, URLProtocol.HTTPS, -> url.withUtmSourceParameter(toolkitApp) else -> url } } ``` -------------------------------- ### ExperimentalToolkitApi annotation declaration Source: https://github.com/yasan-org/toolkit Defines the `ExperimentalToolkitApi` annotation with `@RequiresOptIn` at WARNING level. It marks the `ResolvableString` sealed interface and its `resolve()` method; calling opt-in-only members compiles with a warning. ```kotlin @RequiresOptIn( level = WARNING, message = "This Toolkit API is experimental and is likely to change or to be removed in the future.", ) @Retention(AnnotationRetention.BINARY) @Target( AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, ) public annotation class ExperimentalToolkitApi ``` -------------------------------- ### UrlLauncher interface with launch overloads Source: https://github.com/yasan-org/toolkit Defines the UrlLauncher interface with two suspend launch overloads. The first takes only a URL and returns UrlLaunchResult. The second accepts an onFailure callback of type (UrlLaunchResult.Failure) -> Unit. Implementations include UrlLauncherImpl (expect, @InternalToolkitApi; actuals per target). ```kotlin public interface UrlLauncher { public suspend fun launch( url: String, ): UrlLaunchResult public suspend fun launch( url: String, onFailure: (UrlLaunchResult.Failure) -> Unit, ) { ... } } ``` -------------------------------- ### Add Koin dependency Source: https://github.com/yasan-org/toolkit/blob/main/README.md Gradle dependency for the Koin module. Replace with the actual version. ```kotlin implementation("glass.yasan.toolkit:koin:") ``` -------------------------------- ### DispatcherProvider interface Source: https://github.com/yasan-org/toolkit Defines the four standard kotlinx.coroutines dispatchers (main, io, default, unconfined). Used as a constructor dependency for JVM UrlLauncherImpl and ViewActionEffect, and backs the ApplicationScope. ```kotlin public interface DispatcherProvider { public val main: CoroutineDispatcher public val io: CoroutineDispatcher public val default: CoroutineDispatcher public val unconfined: CoroutineDispatcher } ``` -------------------------------- ### DispatcherProvider Source: https://github.com/yasan-org/toolkit A container for the four standard kotlinx.coroutines dispatchers. It is a constructor dependency of the JVM UrlLauncherImpl and of ViewActionEffect, and it backs the ApplicationScope defined by toolkitModule. ```APIDOC ## Interface DispatcherProvider ### Description A container for the four standard `kotlinx.coroutines` dispatchers. It is a constructor dependency of the JVM `UrlLauncherImpl` and of `ViewActionEffect` (which resolves it via Koin), and it backs the `ApplicationScope` defined by `toolkitModule`. ### Members - **main** (`CoroutineDispatcher`) - The main-thread dispatcher. - **io** (`CoroutineDispatcher`) - I/O dispatcher. - **default** (`CoroutineDispatcher`) - Default CPU-bound dispatcher. - **unconfined** (`CoroutineDispatcher`) - Unconfined dispatcher. ### Source `core/src/commonMain/kotlin/glass/yasan/toolkit/core/coroutines/DispatcherProvider.kt:5` ### Declaration ```kotlin public interface DispatcherProvider { public val main: CoroutineDispatcher public val io: CoroutineDispatcher public val default: CoroutineDispatcher public val unconfined: CoroutineDispatcher } ``` ``` -------------------------------- ### HorizontalSpacer composable Source: https://github.com/yasan-org/toolkit Creates a horizontal spacer with the given width and zero height. The modifier is forwarded to the wrapped Spacer. ```kotlin @Composable public fun HorizontalSpacer( width: Dp, modifier: Modifier = Modifier, ) { Spacer( height = 0.dp, width = width, modifier = modifier, ) } ``` -------------------------------- ### ToolkitApp.trackedUrl(url) Source: https://github.com/yasan-org/toolkit Returns a URL with UTM source parameter added based on the toolkit app configuration. ```APIDOC ## ToolkitApp.trackedUrl(url) ### Description Returns a URL with UTM source parameter added based on the toolkit app configuration. ### Definition ```kotlin fun ToolkitApp.trackedUrl(url: String): String ``` ``` -------------------------------- ### UrlLaunchResult Source: https://github.com/yasan-org/toolkit Sealed interface representing the result of a URL launch attempt. ```APIDOC ## UrlLaunchResult ### Description Sealed interface representing the result of a URL launch attempt. ### Definition ```kotlin sealed interface UrlLaunchResult { object Success : UrlLaunchResult sealed interface Failure : UrlLaunchResult { object InvalidUrl : Failure object Unsupported : Failure data class Error(val throwable: Throwable) : Failure } } ``` ``` -------------------------------- ### CoroutinesExtensions Source: https://github.com/yasan-org/toolkit Extension functions for coroutines and Result handling, including cancellation rethrow helpers. ```APIDOC ## CoroutinesExtensions ### Description Extension functions for coroutines and Result handling, including cancellation rethrow helpers. ### Functions #### `Throwable.isCancellationToRethrow()` ```kotlin suspend fun Throwable.isCancellationToRethrow(): Boolean ``` #### `Throwable.rethrowCancellation()` ```kotlin suspend fun Throwable.rethrowCancellation(): Unit ``` #### `Result.getOrNullOrRethrowCancellation()` ```kotlin suspend fun Result.getOrNullOrRethrowCancellation(): T? ``` #### `Result.getOrElseOrRethrowCancellation()` ```kotlin suspend fun Result.getOrElseOrRethrowCancellation(onFailure: (Throwable) -> T): T ``` ``` -------------------------------- ### createDefaultDispatcherProvider() Source: https://github.com/yasan-org/toolkit Creates a default DispatcherProvider instance. Used to obtain coroutine dispatchers for launching work on different dispatchers. ```APIDOC ## [FUNCTION] createDefaultDispatcherProvider() ### Description Creates a default `DispatcherProvider` instance. This provider supplies coroutine dispatchers (e.g., `default`, `io`) for launching coroutines. ### Method Function ### Signature ```kotlin fun createDefaultDispatcherProvider(): DispatcherProvider ``` ### Returns - **DispatcherProvider** - A provider of coroutine dispatchers. ### Usage Example ```kotlin val dispatchers = createDefaultDispatcherProvider() val appScope: ApplicationScope = CoroutineScope(SupervisorJob() + dispatchers.default) appScope.launch { withContext(dispatchers.io) { /* disk or network */ } } ``` ``` -------------------------------- ### DefaultDispatcherProvider Source: https://github.com/yasan-org/toolkit An expect class that provides the default implementation of DispatcherProvider. It has platform-specific actual implementations for non-web (Android, JVM, iOS) and web (JS, WasmJS) targets. ```APIDOC ## Class DefaultDispatcherProvider ### Description An expect class that provides the default implementation of `DispatcherProvider`. It has platform-specific actual implementations for non-web (Android, JVM, iOS) and web (JS, WasmJS) targets. ### Declaration (expect) `core/src/commonMain/kotlin/glass/yasan/toolkit/core/coroutines/DispatcherProvider.kt:12` ```kotlin public expect class DefaultDispatcherProvider : DispatcherProvider { override val main: CoroutineDispatcher override val io: CoroutineDispatcher override val default: CoroutineDispatcher override val unconfined: CoroutineDispatcher } ``` ### Non-web actual (Android, JVM, iOS) Source: `core/src/nonWebMain/kotlin/glass/yasan/toolkit/core/coroutines/DispatcherProvider.kt:7` | Member | Value | |--------|-------| | `main` | `Dispatchers.Main` | | `io` | `Dispatchers.IO` | | `default` | `Dispatchers.Default` | | `unconfined` | `Dispatchers.Unconfined` | ### Web actual (JS, WasmJS) Source: `core/src/webMain/kotlin/glass/yasan/toolkit/core/coroutines/DispatcherProvider.kt:6` | Member | Value | |--------|-------| | `main` | `Dispatchers.Main` | | `io` | `Dispatchers.Main` (source comment: "Web doesn't have IO") | | `default` | `Dispatchers.Default` | | `unconfined` | `Dispatchers.Unconfined` | ``` -------------------------------- ### VerticalSpacer composable Source: https://github.com/yasan-org/toolkit Creates a vertical spacer with the given height and zero width. The modifier is forwarded to the wrapped Spacer. ```kotlin @Composable public fun VerticalSpacer( height: Dp, modifier: Modifier = Modifier, ) { Spacer( height = height, width = 0.dp, modifier = modifier, ) } ```