How to refresh the screen when the data inserted into room db in kotlin?

50 Views Asked by At

Am using paging 3 and i dont know how to do the in screen refresh when the data is inserted in the db.

**`class ContactPagingSource(private val contactDao: ContactDao, private val query: String,
                          private val sort: SortingEnum = SortingEnum.ByName) :
    PagingSource<Int, ContactLocalModel>() {
    override fun getRefreshKey(state: PagingState<Int, ContactLocalModel>): Int? {
        return null
    }
    
    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, ContactLocalModel> {
        val page = params.key ?: 0
        return try {
            Timber.d("load:  + $page")
            Timber.d("onQueryTextChange ContactPagingSource $query")
            val entities = if (query.isNotBlank()) {
                contactDao.getContactPagination(
                    params.loadSize,
                    page * params.loadSize,
                    query = query,
                    isOrderByName = sort.ordinal == SortingEnum.ByName.ordinal
                )
            } else {
                contactDao.getContactPagination(
                    params.loadSize,
                    page * params.loadSize,
                    query = "%",
                    isOrderByName = sort.ordinal == SortingEnum.ByName.ordinal
                )
            }
            if (page != 0) delay(1000)
            LoadResult.Page(
                data = entities,
                prevKey = if (page == 0) null else page - 1,
                nextKey = if (entities.isEmpty()) null else page + 1
            )
        } catch (e: Exception) {
            LoadResult.Error(e)
        }
    }
}`**

Below is my view model

val contacts: LiveData<PagingData<ContactLocalModel>> =
        searchPaginationName.switchMap { query ->
            Pager(PagingConfig(pageSize = 10, enablePlaceholders = false,
                initialLoadSize = 10)) {
                ContactPagingSource(contactDao, query, sort = enumVal)
            }.liveData.cachedIn(viewModelScope)
        }

    val randomContactPostFlow = Pager(
        PagingConfig(
            pageSize = 10,
            enablePlaceholders = false,
            initialLoadSize = 10,
        )
    ) {
        val text = searchPaginationName.value ?: ""
        Timber.d("onQueryTextChange text $text")
        ContactPagingSource(contactDao, query = text, sort = enumVal)
    }
        .flow
        .cachedIn(viewModelScope)

And now in fragment

private fun initRecyclerView(){
        lifecycleScope.launch {
            viewModel.randomContactPostFlow.collectLatest { pagingData ->
                Timber.d("Pagination in contacts screen --> $pagingData")
                contactAdapter.submitData(pagingData)
            }
        }
    }

Below is my room query

@Query("SELECT * FROM ContactLocalModel WHERE name LIKE '%'||:query||'%' " +
        "OR pttNo LIKE :query " +
        "OR furigana LIKE :query ORDER BY " +
        "CASE WHEN :isOrderByName = 1 THEN LOWER(name) END ASC, " +
        "CASE WHEN :isOrderByName = 0 THEN  pttNo END ASC, " +
        "id ASC LIMIT :limit OFFSET :offset "
)
suspend fun getContactPagination(limit: Int, offset: Int, query: String, isOrderByName: Boolean? = true)
: List<ContactLocalModel>

I have opened the contact screen from web am pushing some data and it gets inserted into the room db. When the db is having change the screen need to refresh. How to do that ?

0

There are 0 best solutions below