Skip to content

Stores (Repositories)

Stores follow the repository pattern. Each store handles a single entity's data logic.

Examples: UserStore, OrderStore, DeviceStore.

Defining a Store

@Singleton
class UserStore @Inject constructor() {
    private val userState = MutableStateFlow(User.EMPTY)

    fun setUser(user: User) {
        userState.value = user
    }

    fun getUser(): StateFlow<User> = userState
}

Data Layer Role

Stores are injected into Use Cases, not into ViewModels or Components directly. Besides custom stores, Room Daos and Retrofit/Ktor API interfaces serve the same role in the data layer.

Wrapping Store Access with a Use Case

class ObserveUserFullNameUseCase @Inject constructor(
    private val userStore: UserStore,
) : FlowUseCase<Unit, String>() {
    override fun build(args: Unit): Flow<String> =
        userStore.getUser().map { "${it.firstName} ${it.lastName}" }
}

This pattern is the same for both the Android and KMP paths.