leonard-palm / compose-state-events

A new way to implement One-Time-UI-Events (former SingleLiveEvent) in a Compose world.
Apache License 2.0
174 stars 12 forks source link

How is the library used together with paging3? #5

Closed mainrs closed 9 months ago

mainrs commented 1 year ago

I have a Room DAO that has the following query. My ViewModel then defines the Pager instance I use inside my ViewModel as shown below:

/// DAO
@Query("SELECT * FROM <table> ORDER BY createdAt DESC")
fun getAllPaged(): PagingSource<Int, Model>

/// ViewModel
class MyViewModel: ViewModel() {
    val allItems = Pager(PagingConfig(pageSize = 10)) {
        myDao.getAllPaged()
    }.flow.cachedIn(viewModelScope)
}

/// UI
@Composable
fun MyContent(viewModel: MyViewModel = hiltViewModel()) {
    val items = viewModel.allItems.collectAsLazyPagingItems()
}

How do I keep the Pager lazy when adding it to the ViewState data class? I don't know if this is possible and by checking online I don't think it is.

leonard-palm commented 9 months ago

@mainrs I really dont see how your question/issue applies to any implementation of the compose-state-events library. The library doesnt dictate how to structure your ViewState object or your outgoing flows in your ViewModel class.