> ## Documentation Index
> Fetch the complete documentation index at: https://klyx.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Utilities

> Helper functions and utilities available to extensions

Klyx provides a set of utility functions covering Android context access, formatting, architecture detection, and Compose modifiers.

## Android context

```kotlin theme={null}
// Get the Application instance
val app: Application = applicationContext()

// Execute a block with the Application as receiver
withApplicationContext { app ->
    app.getSystemService(...)
}
```

### Activity detection

```kotlin theme={null}
val activity: Activity? = context.findActivity()
```

## URI utilities

```kotlin theme={null}
// Check if a URI uses the file scheme
val isFile: Boolean = uri.isFileUri

// Convert file:// URI to content:// via FileProvider
val shareable: Uri = uri.shareableUri

// Share a URI via the Android share sheet
uri.share()
```

### Opening URLs and sharing text

```kotlin theme={null}
openUrl("https://example.com")
shareText("Check this out!")
```

### Encoding/decoding

```kotlin theme={null}
val encoded = "hello world".encodeEscaped()  // "hello%20world"
val decoded = encoded.decodeEscaped()        // "hello world"
```

## Date & time formatting

```kotlin theme={null}
import java.time.*

val now = LocalDateTime.now()

now.formatDateTime()         // "July 11th, 2026 at 3:45"
now.toLocalDate().formatDate() // "July 11th, 2026"

dayWithSuffix(1)  // "1st"
dayWithSuffix(2)  // "2nd"
dayWithSuffix(3)  // "3rd"
dayWithSuffix(11) // "11th"

val duration: Duration = ...
val dateTime: LocalDateTime = duration.asLocalDateTime()
```

## Human-readable sizes

```kotlin theme={null}
val bytes: Long = 1_500_000L
bytes.humanBytes()  // "1.5 MB"

val intBytes: Int = 1024
intBytes.humanBytes()  // "1.0 KB"
```

## Error handling

```kotlin theme={null}
// Extract user-friendly error message
val message: String = throwable.extractMessage()

// Try a block, returning null on non-cancellation exceptions
val result: Any? = tryOrNull { riskyOperation() }
```

## Text file detection

```kotlin theme={null}
val isText: Boolean = isTextFile(uri, contentResolver)
```

## Architecture detection

```kotlin theme={null}
val arch: Architecture = currentArchitecture()
arch.toString()  // "aarch64", "arm", "x86", "x86_64"

Architecture.Aarch64
Architecture.Arm
Architecture.X86
Architecture.X86_64
Architecture.Unknown
```

## Compose modifiers

```kotlin theme={null}
import com.klyx.api.util.thenIf
import com.klyx.api.util.conditional
import com.klyx.api.util.applyIfNotNull
import com.klyx.api.util.applyIf

// Apply modifier only if condition is true
Modifier.thenIf(condition) { Modifier.padding(16.dp) }

// Alternative syntax
Modifier.conditional(condition) { padding(16.dp) }

// Apply only if value is non-null
Modifier.applyIfNotNull(value) { padding(it.dp) }

// Ternary-like syntax
Modifier.applyIf(condition, { padding(16.dp) }, { padding(8.dp) })

// Debug border
Modifier.debugBorder(enabled = true)  // Shows red border
```

## StateFlow helpers

```kotlin theme={null}
val stateFlow = someFlow.stateInWhileSubscribed(viewModelScope)
val eagerlyState = someFlow.stateInEagerly(viewModelScope)
val lazilyState = someFlow.stateInLazily(viewModelScope)
```

## Toast

```kotlin theme={null}
// From within a KlyxPlugin
showToast("Operation complete")
showFailureToast("Something went wrong")
showFailureToast(exception)

// From any composable
val toastHostState = LocalToastHostState.current
toastHostState.showToast("Hello")
toastHostState.showFailureToast(exception)
```

### Toast duration

```kotlin theme={null}
ToastDuration.Short         // 3.5 seconds
ToastDuration.Long          // 6.5 seconds
ToastDuration.Custom(5000)  // Custom duration in ms
```
