Young Devs Bin
๐Ÿ“ Today I Learned

API Key Validation UX in Android Settings

ยท1 min readยท#today-i-learned#android#kotlin#hilt#testing#compose
  • A KeyValidationState enum (Idle / Checking / Valid / Invalid) is cleaner than booleans โ€” the Compose when becomes exhaustive, and there's no ambiguous isLoading + isError combination state
  • Hilt auto-provides concrete @Singleton classes but not interfaces โ€” you need @Provides fun bindFoo(impl: FooImpl): FooInterface = impl in a module to inject by interface
  • Always save the key to storage even if validation fails โ€” the user might have a quota limit or a timeout, not a wrong key
  • ?.trim()?.takeIf { it.isNotBlank() } ?: return is a clean one-liner to guard against empty/blank input before triggering async work
  • For testing: extract any network dependency into an interface, implement a FakeFoo with a pre-configured result, and the ViewModel tests run with no network and no Android context
  • AnchoredDraggableState.requireOffset() crashes with IllegalStateException if called during composition โ€” anchors are set in SideEffect (after composition), so the offset isn't ready yet; fix: use state.offset.takeIf { !it.isNaN() } ?: 0f for composition-phase reads, and call requireOffset() only inside Modifier.offset { } which runs during layout