diff --git a/app/src/main/java/com/h/pixeldroid/posts/feeds/CommonFeedFragmentUtils.kt b/app/src/main/java/com/h/pixeldroid/posts/feeds/CommonFeedFragmentUtils.kt index 53f8e62e..cb59653f 100644 --- a/app/src/main/java/com/h/pixeldroid/posts/feeds/CommonFeedFragmentUtils.kt +++ b/app/src/main/java/com/h/pixeldroid/posts/feeds/CommonFeedFragmentUtils.kt @@ -6,6 +6,7 @@ import android.widget.ProgressBar import androidx.constraintlayout.motion.widget.MotionLayout import androidx.core.view.isVisible import androidx.core.view.size +import androidx.lifecycle.LifecycleCoroutineScope import androidx.paging.LoadState import androidx.paging.LoadStateAdapter import androidx.paging.PagingDataAdapter @@ -14,6 +15,11 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout import com.h.pixeldroid.R import com.h.pixeldroid.databinding.ErrorLayoutBinding 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 @@ -82,6 +88,20 @@ internal fun initAdapter( } } +fun launch( + job: Job?, lifecycleScope: LifecycleCoroutineScope, viewModel: FeedViewModel, + pagingDataAdapter: PagingDataAdapter): 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 diff --git a/app/src/main/java/com/h/pixeldroid/posts/feeds/uncachedFeeds/UncachedFeedFragment.kt b/app/src/main/java/com/h/pixeldroid/posts/feeds/uncachedFeeds/UncachedFeedFragment.kt index 6ad46d16..1e48bf16 100644 --- a/app/src/main/java/com/h/pixeldroid/posts/feeds/uncachedFeeds/UncachedFeedFragment.kt +++ b/app/src/main/java/com/h/pixeldroid/posts/feeds/uncachedFeeds/UncachedFeedFragment.kt @@ -9,9 +9,9 @@ import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.lifecycleScope import androidx.paging.* import androidx.recyclerview.widget.RecyclerView +import com.h.pixeldroid.posts.feeds.launch import kotlinx.coroutines.Job import kotlinx.coroutines.flow.collect -import kotlinx.coroutines.flow.collectLatest import kotlinx.coroutines.flow.distinctUntilChangedBy import kotlinx.coroutines.flow.filter import kotlinx.coroutines.launch @@ -37,13 +37,10 @@ open class UncachedFeedFragment : BaseFragment() { internal fun launch() { - // Make sure we cancel the previous job before creating a new one - job?.cancel() - job = lifecycleScope.launch { - viewModel.flow().collectLatest { - adapter.submitData(it) - } - } + @Suppress("UNCHECKED_CAST") + job = launch(job, lifecycleScope, + viewModel as FeedViewModel, + adapter as PagingDataAdapter) } internal fun initSearch() { diff --git a/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt b/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt index 8e2d2ac8..2d924dfc 100644 --- a/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt +++ b/app/src/main/java/com/h/pixeldroid/profile/ProfileActivity.kt @@ -22,6 +22,7 @@ import com.h.pixeldroid.databinding.ActivityProfileBinding import com.h.pixeldroid.databinding.FragmentProfilePostsBinding import com.h.pixeldroid.posts.PostActivity 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.UncachedContentRepository 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.api.PixelfedAPI 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.db.entities.UserDatabaseEntity import com.h.pixeldroid.utils.openUrl @@ -93,17 +95,9 @@ class ProfileActivity : BaseActivity() { } setContent(account) - profileLaunch() - } - - 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) - } - } + @Suppress("UNCHECKED_CAST") + job = launch(job, lifecycleScope, viewModel as FeedViewModel, + profileAdapter as PagingDataAdapter) } /** diff --git a/build.gradle b/build.gradle index efd6d745..859d677a 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ buildscript { jcenter() } 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" // NOTE: Do not place your application dependencies here; they belong