From 1d358a403b72291946df75d44f4a335f149300c5 Mon Sep 17 00:00:00 2001 From: erike Date: Thu, 10 Aug 2023 23:32:47 -0600 Subject: [PATCH] WIP follow profile --- .../followers/domain/FollowingState.kt | 8 +++++++ .../profile/presentation/ProfileViewModel.kt | 13 +++++++++++ .../profile/ui/ProfileMainFragment.kt | 23 +++++++++++++++++++ .../main/res/layout/fragment_discussions.xml | 3 ++- app/src/main/res/values/strings.xml | 2 ++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 app/src/main/java/com/isolaatti/followers/domain/FollowingState.kt diff --git a/app/src/main/java/com/isolaatti/followers/domain/FollowingState.kt b/app/src/main/java/com/isolaatti/followers/domain/FollowingState.kt new file mode 100644 index 0000000..ee3ae03 --- /dev/null +++ b/app/src/main/java/com/isolaatti/followers/domain/FollowingState.kt @@ -0,0 +1,8 @@ +package com.isolaatti.followers.domain + +enum class FollowingState { + FollowingThisUser, + MutuallyFollowing, + ThisUserIsFollowingMe, + NotMutuallyFollowing +} \ No newline at end of file diff --git a/app/src/main/java/com/isolaatti/profile/presentation/ProfileViewModel.kt b/app/src/main/java/com/isolaatti/profile/presentation/ProfileViewModel.kt index 9b732fd..dd433c4 100644 --- a/app/src/main/java/com/isolaatti/profile/presentation/ProfileViewModel.kt +++ b/app/src/main/java/com/isolaatti/profile/presentation/ProfileViewModel.kt @@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope +import com.isolaatti.followers.domain.FollowingState import com.isolaatti.posting.posts.data.remote.FeedDto import com.isolaatti.posting.posts.data.remote.FeedFilterDto import com.isolaatti.posting.posts.presentation.PostListingViewModelBase @@ -32,11 +33,23 @@ class ProfileViewModel @Inject constructor(private val getProfileUseCase: GetPro var profileId: Int = 0 + val followingState: MutableLiveData = MutableLiveData() + fun getProfile() { viewModelScope.launch { getProfileUseCase(profileId).onEach { if(it is Resource.Success) { _profile.postValue(it.data!!) + followingState.postValue( + it.data.let {user-> + when { + user.followingThisUser && user.thisUserIsFollowingMe -> FollowingState.MutuallyFollowing + user.followingThisUser -> FollowingState.FollowingThisUser + user.thisUserIsFollowingMe -> FollowingState.ThisUserIsFollowingMe + else -> FollowingState.NotMutuallyFollowing + } + } + ) } }.flowOn(Dispatchers.IO).launchIn(this) } diff --git a/app/src/main/java/com/isolaatti/profile/ui/ProfileMainFragment.kt b/app/src/main/java/com/isolaatti/profile/ui/ProfileMainFragment.kt index fc4a97e..ed8e1ba 100644 --- a/app/src/main/java/com/isolaatti/profile/ui/ProfileMainFragment.kt +++ b/app/src/main/java/com/isolaatti/profile/ui/ProfileMainFragment.kt @@ -14,6 +14,7 @@ import androidx.recyclerview.widget.LinearLayoutManager import com.isolaatti.BuildConfig import com.isolaatti.R import com.isolaatti.databinding.FragmentDiscussionsBinding +import com.isolaatti.followers.domain.FollowingState import com.isolaatti.home.FeedFragment import com.isolaatti.posting.PostViewerActivity import com.isolaatti.posting.comments.presentation.BottomSheetPostComments @@ -68,6 +69,27 @@ class ProfileMainFragment : Fragment() { } + private val followingStateObserver: Observer = Observer { + when(it) { + FollowingState.FollowingThisUser -> { + viewBinding.textViewFollowingState.setText(R.string.following_user) + viewBinding.followButton.setText(R.string.unfollow) + } + FollowingState.MutuallyFollowing -> { + viewBinding.textViewFollowingState.setText(R.string.mutually_following) + viewBinding.followButton.setText(R.string.unfollow) + } + FollowingState.ThisUserIsFollowingMe -> { + viewBinding.textViewFollowingState.setText(R.string.following_you) + viewBinding.followButton.setText(R.string.follow) + } + FollowingState.NotMutuallyFollowing -> { + viewBinding.textViewFollowingState.text = "" + viewBinding.followButton.setText(R.string.follow) + } + } + } + private lateinit var postListingRecyclerViewAdapterWiring: PostListingRecyclerViewAdapterWiring @@ -117,6 +139,7 @@ class ProfileMainFragment : Fragment() { private fun setObservers() { viewModel.profile.observe(viewLifecycleOwner, profileObserver) viewModel.posts.observe(viewLifecycleOwner, postsObserver) + viewModel.followingState.observe(viewLifecycleOwner, followingStateObserver) viewModel.loadingPosts.observe(viewLifecycleOwner) { viewBinding.swipeToRefresh.isRefreshing = it } diff --git a/app/src/main/res/layout/fragment_discussions.xml b/app/src/main/res/layout/fragment_discussions.xml index 3217bb3..3ca52ba 100644 --- a/app/src/main/res/layout/fragment_discussions.xml +++ b/app/src/main/res/layout/fragment_discussions.xml @@ -70,12 +70,13 @@ tools:text="Following this profile" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 442546b..54a5a9d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -57,4 +57,6 @@ Mutually following Following you User link + Follow + Unfollow \ No newline at end of file