Finished refactor
This commit is contained in:
parent
4096af2872
commit
5da2750b1c
@ -6,6 +6,7 @@ import android.widget.ProgressBar
|
|||||||
import androidx.constraintlayout.motion.widget.MotionLayout
|
import androidx.constraintlayout.motion.widget.MotionLayout
|
||||||
import androidx.core.view.isVisible
|
import androidx.core.view.isVisible
|
||||||
import androidx.core.view.size
|
import androidx.core.view.size
|
||||||
|
import androidx.lifecycle.LifecycleCoroutineScope
|
||||||
import androidx.paging.LoadState
|
import androidx.paging.LoadState
|
||||||
import androidx.paging.LoadStateAdapter
|
import androidx.paging.LoadStateAdapter
|
||||||
import androidx.paging.PagingDataAdapter
|
import androidx.paging.PagingDataAdapter
|
||||||
@ -14,6 +15,11 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
|
|||||||
import com.h.pixeldroid.R
|
import com.h.pixeldroid.R
|
||||||
import com.h.pixeldroid.databinding.ErrorLayoutBinding
|
import com.h.pixeldroid.databinding.ErrorLayoutBinding
|
||||||
import com.h.pixeldroid.databinding.LoadStateFooterViewItemBinding
|
import com.h.pixeldroid.databinding.LoadStateFooterViewItemBinding
|
||||||
|
import com.h.pixeldroid.posts.feeds.uncachedFeeds.FeedViewModel
|
||||||
|
import com.h.pixeldroid.utils.api.objects.FeedContent
|
||||||
|
import kotlinx.coroutines.Job
|
||||||
|
import kotlinx.coroutines.flow.collectLatest
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Shows or hides the error in the different FeedFragments
|
* Shows or hides the error in the different FeedFragments
|
||||||
@ -82,6 +88,20 @@ internal fun <T: Any> initAdapter(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun launch(
|
||||||
|
job: Job?, lifecycleScope: LifecycleCoroutineScope, viewModel: FeedViewModel<FeedContent>,
|
||||||
|
pagingDataAdapter: PagingDataAdapter<FeedContent, RecyclerView.ViewHolder>): Job {
|
||||||
|
// Make sure we cancel the previous job before creating a new one
|
||||||
|
job?.cancel()
|
||||||
|
return lifecycleScope.launch {
|
||||||
|
viewModel.flow().collectLatest {
|
||||||
|
pagingDataAdapter.submitData(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Adapter to the show the a [RecyclerView] item for a [LoadState], with a callback to retry if
|
* Adapter to the show the a [RecyclerView] item for a [LoadState], with a callback to retry if
|
||||||
|
@ -9,9 +9,9 @@ import androidx.lifecycle.ViewModelProvider
|
|||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import androidx.paging.*
|
import androidx.paging.*
|
||||||
import androidx.recyclerview.widget.RecyclerView
|
import androidx.recyclerview.widget.RecyclerView
|
||||||
|
import com.h.pixeldroid.posts.feeds.launch
|
||||||
import kotlinx.coroutines.Job
|
import kotlinx.coroutines.Job
|
||||||
import kotlinx.coroutines.flow.collect
|
import kotlinx.coroutines.flow.collect
|
||||||
import kotlinx.coroutines.flow.collectLatest
|
|
||||||
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
import kotlinx.coroutines.flow.distinctUntilChangedBy
|
||||||
import kotlinx.coroutines.flow.filter
|
import kotlinx.coroutines.flow.filter
|
||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
@ -37,13 +37,10 @@ open class UncachedFeedFragment<T: FeedContent> : BaseFragment() {
|
|||||||
|
|
||||||
|
|
||||||
internal fun launch() {
|
internal fun launch() {
|
||||||
// Make sure we cancel the previous job before creating a new one
|
@Suppress("UNCHECKED_CAST")
|
||||||
job?.cancel()
|
job = launch(job, lifecycleScope,
|
||||||
job = lifecycleScope.launch {
|
viewModel as FeedViewModel<FeedContent>,
|
||||||
viewModel.flow().collectLatest {
|
adapter as PagingDataAdapter<FeedContent, RecyclerView.ViewHolder>)
|
||||||
adapter.submitData(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun initSearch() {
|
internal fun initSearch() {
|
||||||
|
@ -22,6 +22,7 @@ import com.h.pixeldroid.databinding.ActivityProfileBinding
|
|||||||
import com.h.pixeldroid.databinding.FragmentProfilePostsBinding
|
import com.h.pixeldroid.databinding.FragmentProfilePostsBinding
|
||||||
import com.h.pixeldroid.posts.PostActivity
|
import com.h.pixeldroid.posts.PostActivity
|
||||||
import com.h.pixeldroid.posts.feeds.initAdapter
|
import com.h.pixeldroid.posts.feeds.initAdapter
|
||||||
|
import com.h.pixeldroid.posts.feeds.launch
|
||||||
import com.h.pixeldroid.posts.feeds.uncachedFeeds.FeedViewModel
|
import com.h.pixeldroid.posts.feeds.uncachedFeeds.FeedViewModel
|
||||||
import com.h.pixeldroid.posts.feeds.uncachedFeeds.UncachedContentRepository
|
import com.h.pixeldroid.posts.feeds.uncachedFeeds.UncachedContentRepository
|
||||||
import com.h.pixeldroid.posts.feeds.uncachedFeeds.profile.ProfileContentRepository
|
import com.h.pixeldroid.posts.feeds.uncachedFeeds.profile.ProfileContentRepository
|
||||||
@ -30,6 +31,7 @@ import com.h.pixeldroid.utils.BaseActivity
|
|||||||
import com.h.pixeldroid.utils.ImageConverter
|
import com.h.pixeldroid.utils.ImageConverter
|
||||||
import com.h.pixeldroid.utils.api.PixelfedAPI
|
import com.h.pixeldroid.utils.api.PixelfedAPI
|
||||||
import com.h.pixeldroid.utils.api.objects.Account
|
import com.h.pixeldroid.utils.api.objects.Account
|
||||||
|
import com.h.pixeldroid.utils.api.objects.FeedContent
|
||||||
import com.h.pixeldroid.utils.api.objects.Status
|
import com.h.pixeldroid.utils.api.objects.Status
|
||||||
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
|
import com.h.pixeldroid.utils.db.entities.UserDatabaseEntity
|
||||||
import com.h.pixeldroid.utils.openUrl
|
import com.h.pixeldroid.utils.openUrl
|
||||||
@ -93,17 +95,9 @@ class ProfileActivity : BaseActivity() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setContent(account)
|
setContent(account)
|
||||||
profileLaunch()
|
@Suppress("UNCHECKED_CAST")
|
||||||
}
|
job = launch(job, lifecycleScope, viewModel as FeedViewModel<FeedContent>,
|
||||||
|
profileAdapter as PagingDataAdapter<FeedContent, RecyclerView.ViewHolder>)
|
||||||
private fun profileLaunch() {
|
|
||||||
// Make sure we cancel the previous job before creating a new one
|
|
||||||
job?.cancel()
|
|
||||||
job = lifecycleScope.launch {
|
|
||||||
viewModel.flow().collectLatest {
|
|
||||||
profileAdapter.submitData(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,7 +7,7 @@ buildscript {
|
|||||||
jcenter()
|
jcenter()
|
||||||
}
|
}
|
||||||
dependencies {
|
dependencies {
|
||||||
classpath 'com.android.tools.build:gradle:4.1.2'
|
classpath 'com.android.tools.build:gradle:4.1.3'
|
||||||
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
||||||
|
|
||||||
// NOTE: Do not place your application dependencies here; they belong
|
// NOTE: Do not place your application dependencies here; they belong
|
||||||
|
Loading…
x
Reference in New Issue
Block a user