From ab959db5f18dff7031cac9bb08211cddb0ef7b51 Mon Sep 17 00:00:00 2001 From: Marie Date: Sat, 22 Oct 2022 19:06:06 +0200 Subject: [PATCH] Grid vs feed done --- .../pixeldroid/app/profile/ProfileActivity.kt | 69 +++--------------- .../app/profile/ProfileFeedFragment.kt | 71 ++++++++++++++++++- .../main/res/layout/fragment_profile_feed.xml | 48 ------------- 3 files changed, 80 insertions(+), 108 deletions(-) delete mode 100644 app/src/main/res/layout/fragment_profile_feed.xml diff --git a/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt b/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt index 41739a61..bda764fa 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt @@ -32,6 +32,7 @@ import org.pixeldroid.app.R import org.pixeldroid.app.databinding.ActivityProfileBinding import org.pixeldroid.app.databinding.FragmentProfilePostsBinding import org.pixeldroid.app.posts.PostActivity +import org.pixeldroid.app.posts.StatusViewHolder import org.pixeldroid.app.posts.feeds.initAdapter import org.pixeldroid.app.posts.feeds.launch import org.pixeldroid.app.posts.feeds.uncachedFeeds.FeedViewModel @@ -73,16 +74,6 @@ class ProfileActivity : BaseThemedWithBarActivity() { val account = intent.getSerializableExtra(Account.ACCOUNT_TAG) as Account? accountId = account?.id ?: user!!.user_id - // get the view model - @Suppress("UNCHECKED_CAST") - viewModel = ViewModelProvider(this, ProfileViewModelFactory( - ProfileContentRepository( - apiHolder.setToCurrentUser(), - accountId - ) - ) - )[FeedViewModel::class.java] as FeedViewModel - val tabs = createSearchTabs(account) setupTabs(tabs) setContent(account) @@ -93,11 +84,15 @@ class ProfileActivity : BaseThemedWithBarActivity() { val profileGridFragment = ProfileFeedFragment() val profileFeedFragment = ProfileFeedFragment() val profileBookmarksFragment = ProfileFeedFragment() // TODO: bookmark fragment - val arguments = Bundle() - arguments.putSerializable(Account.ACCOUNT_TAG, account) - profileGridFragment.arguments = arguments - profileFeedFragment.arguments = arguments - profileBookmarksFragment.arguments = arguments + val argumentsGrid = Bundle() + val argumentsFeed = Bundle() + argumentsGrid.putSerializable(Account.ACCOUNT_TAG, account) + argumentsFeed.putSerializable(Account.ACCOUNT_TAG, account) + argumentsGrid.putSerializable(ProfileFeedFragment.PROFILE_GRID, true) + argumentsFeed.putSerializable(ProfileFeedFragment.PROFILE_GRID, false) + profileGridFragment.arguments = argumentsGrid + profileFeedFragment.arguments = argumentsFeed + profileBookmarksFragment.arguments = argumentsGrid return arrayOf( profileGridFragment, profileFeedFragment, @@ -363,20 +358,6 @@ class ProfileActivity : BaseThemedWithBarActivity() { } -class ProfileViewModelFactory @ExperimentalPagingApi constructor( - private val searchContentRepository: UncachedContentRepository -) : ViewModelProvider.Factory { - - override fun create(modelClass: Class): T { - if (modelClass.isAssignableFrom(FeedViewModel::class.java)) { - @Suppress("UNCHECKED_CAST") - return FeedViewModel(searchContentRepository) as T - } - throw IllegalArgumentException("Unknown ViewModel class") - } -} - - class ProfilePostsViewHolder(binding: FragmentProfilePostsBinding) : RecyclerView.ViewHolder(binding.root) { private val postPreview: ImageView = binding.postPreview private val albumIcon: ImageView = binding.albumIcon @@ -435,33 +416,3 @@ class ProfilePostsViewHolder(binding: FragmentProfilePostsBinding) : RecyclerVie } } } - - -class ProfilePostsAdapter : PagingDataAdapter( - UIMODEL_COMPARATOR -) { - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { - return ProfilePostsViewHolder.create(parent) - } - - override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { - val post = getItem(position) - - post?.let { - (holder as ProfilePostsViewHolder).bind(it) - } - } - - companion object { - private val UIMODEL_COMPARATOR = object : DiffUtil.ItemCallback() { - override fun areItemsTheSame(oldItem: Status, newItem: Status): Boolean { - return oldItem.id == newItem.id - } - - override fun areContentsTheSame(oldItem: Status, newItem: Status): Boolean = - oldItem.content == newItem.content - } - } - -} diff --git a/app/src/main/java/org/pixeldroid/app/profile/ProfileFeedFragment.kt b/app/src/main/java/org/pixeldroid/app/profile/ProfileFeedFragment.kt index b230caf4..f33acfc7 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/ProfileFeedFragment.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/ProfileFeedFragment.kt @@ -4,26 +4,40 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelProvider +import androidx.lifecycle.lifecycleScope import androidx.paging.ExperimentalPagingApi +import androidx.paging.PagingDataAdapter +import androidx.recyclerview.widget.DiffUtil +import androidx.recyclerview.widget.GridLayoutManager +import androidx.recyclerview.widget.RecyclerView +import org.pixeldroid.app.posts.StatusViewHolder import org.pixeldroid.app.posts.feeds.uncachedFeeds.* import org.pixeldroid.app.posts.feeds.uncachedFeeds.profile.ProfileContentRepository import org.pixeldroid.app.utils.api.objects.Account import org.pixeldroid.app.utils.api.objects.Status import org.pixeldroid.app.utils.db.entities.UserDatabaseEntity +import org.pixeldroid.app.utils.displayDimensionsInPx /** * Fragment to show a list of [Account]s, as a result of a search. */ class ProfileFeedFragment : UncachedFeedFragment() { + companion object { + const val PROFILE_GRID = "ProfileGrid" + } + private lateinit var accountId : String private var user: UserDatabaseEntity? = null + private var grid: Boolean = false override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - adapter = ProfilePostsAdapter() + grid = arguments?.getSerializable(PROFILE_GRID) as Boolean + adapter = ProfilePostsAdapter(grid) //get the currently active user user = db.userDao().getActiveUser() @@ -40,6 +54,10 @@ class ProfileFeedFragment : UncachedFeedFragment() { val view = super.onCreateView(inflater, container, savedInstanceState) + if(grid) { + binding.list.layoutManager = GridLayoutManager(context, 3) + } + // Get the view model @Suppress("UNCHECKED_CAST") viewModel = ViewModelProvider(requireActivity(), ProfileViewModelFactory( @@ -55,4 +73,55 @@ class ProfileFeedFragment : UncachedFeedFragment() { return view } + + private val UIMODEL_COMPARATOR = object : DiffUtil.ItemCallback() { + override fun areItemsTheSame(oldItem: Status, newItem: Status): Boolean { + return oldItem.id == newItem.id + } + + override fun areContentsTheSame(oldItem: Status, newItem: Status): Boolean = + oldItem.content == newItem.content + } + + inner class ProfilePostsAdapter( + private val grid: Boolean + ) : PagingDataAdapter( + UIMODEL_COMPARATOR + ) { + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder { + return if(grid) { + ProfilePostsViewHolder.create(parent) + } else { + StatusViewHolder.create(parent) + } + } + + override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) { + val post = getItem(position) + + post?.let { + if(grid) { + (holder as ProfilePostsViewHolder).bind(it) + } else { + (holder as StatusViewHolder).bind(it, apiHolder, db, + lifecycleScope, requireContext().displayDimensionsInPx()) + } + } + } + } +} + + +class ProfileViewModelFactory @ExperimentalPagingApi constructor( + private val searchContentRepository: UncachedContentRepository +) : ViewModelProvider.Factory { + + override fun create(modelClass: Class): T { + if (modelClass.isAssignableFrom(FeedViewModel::class.java)) { + @Suppress("UNCHECKED_CAST") + return FeedViewModel(searchContentRepository) as T + } + throw IllegalArgumentException("Unknown ViewModel class") + } } \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_profile_feed.xml b/app/src/main/res/layout/fragment_profile_feed.xml deleted file mode 100644 index d9f138a5..00000000 --- a/app/src/main/res/layout/fragment_profile_feed.xml +++ /dev/null @@ -1,48 +0,0 @@ - - - - - - - - - - - - - - - - - - \ No newline at end of file