less type casting
This commit is contained in:
parent
e52bf51e18
commit
6efa946809
@ -55,7 +55,7 @@ internal fun <T: Any> initAdapter(
|
||||
|
||||
if(!progressBar.isVisible && swipeRefreshLayout.isRefreshing) {
|
||||
// Stop loading spinner when loading is done
|
||||
swipeRefreshLayout.isRefreshing = loadState.refresh is LoadState.Loading
|
||||
swipeRefreshLayout.isRefreshing = loadState.mediator?.refresh is LoadState.Loading
|
||||
} else {
|
||||
// ProgressBar should stop showing as soon as the source stops loading ("source"
|
||||
// meaning the database, so don't wait on the network)
|
||||
@ -89,13 +89,13 @@ internal fun <T: Any> initAdapter(
|
||||
}
|
||||
}
|
||||
|
||||
fun launch(
|
||||
job: Job?, lifecycleScope: LifecycleCoroutineScope, viewModel: FeedViewModel<FeedContent>,
|
||||
pagingDataAdapter: PagingDataAdapter<FeedContent, RecyclerView.ViewHolder>): Job {
|
||||
fun <T: FeedContent> launch(
|
||||
job: Job?, lifecycleScope: LifecycleCoroutineScope, viewModel: FeedViewModel<T>,
|
||||
pagingDataAdapter: PagingDataAdapter<T, RecyclerView.ViewHolder>): Job {
|
||||
// Make sure we cancel the previous job before creating a new one
|
||||
job?.cancel()
|
||||
return lifecycleScope.launch {
|
||||
viewModel.flow().collectLatest {
|
||||
viewModel.flow.collectLatest {
|
||||
pagingDataAdapter.submitData(it)
|
||||
}
|
||||
}
|
||||
|
@ -8,18 +8,16 @@ import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.paging.*
|
||||
import androidx.paging.LoadState.*
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
import kotlinx.coroutines.Job
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
import org.pixeldroid.app.databinding.FragmentFeedBinding
|
||||
import org.pixeldroid.app.posts.feeds.initAdapter
|
||||
import org.pixeldroid.app.utils.BaseFragment
|
||||
import org.pixeldroid.app.utils.api.objects.FeedContentDatabase
|
||||
import org.pixeldroid.app.utils.db.AppDatabase
|
||||
import org.pixeldroid.app.utils.db.dao.feedContent.FeedContentDao
|
||||
import org.pixeldroid.app.utils.BaseFragment
|
||||
import org.pixeldroid.app.posts.feeds.initAdapter
|
||||
import org.pixeldroid.app.utils.api.objects.FeedContentDatabase
|
||||
|
||||
|
||||
/**
|
||||
* A fragment representing a list of [FeedContentDatabase] items that are cached by the database.
|
||||
@ -40,7 +38,7 @@ open class CachedFeedFragment<T: FeedContentDatabase> : BaseFragment() {
|
||||
// Make sure we cancel the previous job before creating a new one
|
||||
job?.cancel()
|
||||
job = lifecycleScope.launchWhenStarted {
|
||||
viewModel.flow().collectLatest {
|
||||
viewModel.flow.collectLatest {
|
||||
adapter.submitData(it)
|
||||
}
|
||||
}
|
||||
@ -48,14 +46,14 @@ open class CachedFeedFragment<T: FeedContentDatabase> : BaseFragment() {
|
||||
|
||||
internal fun initSearch() {
|
||||
// Scroll to top when the list is refreshed from network.
|
||||
lifecycleScope.launch {
|
||||
lifecycleScope.launchWhenCreated {
|
||||
adapter.loadStateFlow
|
||||
// Only emit when REFRESH LoadState for RemoteMediator changes.
|
||||
.distinctUntilChangedBy {
|
||||
it.source.refresh
|
||||
}
|
||||
// Only react to cases where Remote REFRESH completes i.e., NotLoading.
|
||||
.filter { it.refresh is LoadState.NotLoading && it.source.refresh is LoadState.NotLoading}
|
||||
.filter { it.refresh is NotLoading}
|
||||
.collect { binding.list.scrollToPosition(0) }
|
||||
}
|
||||
}
|
||||
@ -72,12 +70,7 @@ open class CachedFeedFragment<T: FeedContentDatabase> : BaseFragment() {
|
||||
initAdapter(binding.progressBar, binding.swipeRefreshLayout,
|
||||
binding.list, binding.motionLayout, binding.errorLayout, adapter)
|
||||
|
||||
//binding.progressBar.visibility = View.GONE
|
||||
binding.swipeRefreshLayout.setOnRefreshListener {
|
||||
//It shouldn't be necessary to also retry() in addition to refresh(),
|
||||
//but if we don't do this, reloads after an error fail immediately...
|
||||
// https://issuetracker.google.com/issues/173438474
|
||||
adapter.retry()
|
||||
adapter.refresh()
|
||||
}
|
||||
|
||||
|
@ -26,19 +26,8 @@ import kotlinx.coroutines.flow.Flow
|
||||
* ViewModel for the cached feeds.
|
||||
* The ViewModel works with the [FeedContentRepository] to get the data.
|
||||
*/
|
||||
class FeedViewModel<T: FeedContentDatabase>(private val repository: FeedContentRepository<T>) : ViewModel() {
|
||||
|
||||
private var currentResult: Flow<PagingData<T>>? = null
|
||||
class FeedViewModel<T: FeedContentDatabase>(repository: FeedContentRepository<T>) : ViewModel() {
|
||||
|
||||
@ExperimentalPagingApi
|
||||
fun flow(): Flow<PagingData<T>> {
|
||||
val lastResult = currentResult
|
||||
if (lastResult != null) {
|
||||
return lastResult
|
||||
}
|
||||
val newResult: Flow<PagingData<T>> = repository.stream()
|
||||
.cachedIn(viewModelScope)
|
||||
currentResult = newResult
|
||||
return newResult
|
||||
}
|
||||
val flow: Flow<PagingData<T>> = repository.stream().cachedIn(viewModelScope)
|
||||
}
|
||||
|
@ -10,20 +10,8 @@ import kotlinx.coroutines.flow.Flow
|
||||
* ViewModel for the uncached feeds.
|
||||
* The ViewModel works with the different [UncachedContentRepository]s to get the data.
|
||||
*/
|
||||
class FeedViewModel<T: FeedContent>(private val repository: UncachedContentRepository<T>) : ViewModel() {
|
||||
|
||||
private var currentResult: Flow<PagingData<T>>? = null
|
||||
|
||||
fun flow(): Flow<PagingData<T>> {
|
||||
val lastResult = currentResult
|
||||
if (lastResult != null) {
|
||||
return lastResult
|
||||
}
|
||||
val newResult: Flow<PagingData<T>> = repository.getStream()
|
||||
.cachedIn(viewModelScope)
|
||||
currentResult = newResult
|
||||
return newResult
|
||||
}
|
||||
class FeedViewModel<T: FeedContent>(repository: UncachedContentRepository<T>) : ViewModel() {
|
||||
val flow: Flow<PagingData<T>> = repository.getStream().cachedIn(viewModelScope)
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -37,10 +37,7 @@ open class UncachedFeedFragment<T: FeedContent> : BaseFragment() {
|
||||
|
||||
|
||||
internal fun launch() {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
job = launch(job, lifecycleScope,
|
||||
viewModel as FeedViewModel<FeedContent>,
|
||||
adapter as PagingDataAdapter<FeedContent, RecyclerView.ViewHolder>)
|
||||
job = launch(job, lifecycleScope, viewModel, adapter)
|
||||
}
|
||||
|
||||
internal fun initSearch() {
|
||||
|
@ -91,9 +91,7 @@ class ProfileActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
setContent(account)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
job = launch(job, lifecycleScope, viewModel as FeedViewModel<FeedContent>,
|
||||
profileAdapter as PagingDataAdapter<FeedContent, RecyclerView.ViewHolder>)
|
||||
job = launch(job, lifecycleScope, viewModel, profileAdapter)
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
x
Reference in New Issue
Block a user