Skip to content

Commit

Permalink
33-other-people-profile (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zenin0 authored May 24, 2024
2 parents 82a5b59 + b3d8a4f commit 9bb02fa
Show file tree
Hide file tree
Showing 20 changed files with 214 additions and 75 deletions.
26 changes: 24 additions & 2 deletions app/src/main/java/com/isanz/inmomarket/ui/chat/ChatFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.isanz.inmomarket.InmoMarket
import com.isanz.inmomarket.R
import com.isanz.inmomarket.databinding.FragmentChatBinding
import com.isanz.inmomarket.rv.chatItem.ChatListAdapter
import com.isanz.inmomarket.utils.entities.User
import kotlinx.coroutines.launch

class ChatFragment : Fragment() {
Expand Down Expand Up @@ -49,8 +51,7 @@ class ChatFragment : Fragment() {
}

private fun setUpOtherUser() = lifecycleScope.launch {
val users = chatViewModel.getUsersInConversation(idChat).await()
val otherUser = users.find { it.uid != InmoMarket.getAuth().currentUser!!.uid }
val otherUser = retrieveOtherUser()
mBinding.tvNameChat.text = otherUser?.displayName
otherUser?.photoUrl?.let { loadImage(it) }
}
Expand Down Expand Up @@ -89,6 +90,27 @@ class ChatFragment : Fragment() {
mBinding.ibBack.setOnClickListener {
navigateBack()
}
mBinding.llProfileChat.setOnClickListener {
navigateToProfile()
}
}


private suspend fun retrieveOtherUser(): User? {
val users = chatViewModel.getUsersInConversation(idChat).await()
return users.find { it.uid != InmoMarket.getAuth().currentUser!!.uid }
}

private fun navigateToProfile() {
val bundle = Bundle()
lifecycleScope.launch {
val otherUser = retrieveOtherUser()
bundle.putString("profileId", otherUser?.uid)
this@ChatFragment.findNavController().navigate(
R.id.action_navigation_chat_to_navigation_profile, bundle
)

}
}

private fun sendMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class ProfileFragment : Fragment() {
private lateinit var mBinding: FragmentProfileBinding
private lateinit var navView: NavigationView
private lateinit var drawerLayout: DrawerLayout
private lateinit var profileUserId: String

private val profileViewModel: ProfileViewModel by lazy {
ViewModelProvider(this)[ProfileViewModel::class.java]
}
Expand All @@ -38,6 +40,9 @@ class ProfileFragment : Fragment() {
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
mBinding = FragmentProfileBinding.inflate(inflater, container, false)
arguments?.let {
profileUserId = it.getString("profileId") ?: ""
}
setUp()
return mBinding.root
}
Expand All @@ -46,11 +51,21 @@ class ProfileFragment : Fragment() {
lifecycleScope.launch {
setUpView()
}
setUpButtons()
setUpDrawer()
setUpTabs()
setUpProfileImagePicker()
}

private fun setUpButtons() {
mBinding.profileLayout.ibDrawer.setOnClickListener {
mBinding.drawerLayout.openDrawer(GravityCompat.START)
}
mBinding.profileLayout.ibBack.setOnClickListener {
findNavController().popBackStack()
}
}

private fun setUpProfileImagePicker() {
mBinding.profileLayout.ivProfile.setOnClickListener {
launchImagePicker()
Expand All @@ -63,12 +78,13 @@ class ProfileFragment : Fragment() {
imagePickerResultLauncher.launch(intent)
}

private val imagePickerResultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val imageUri = result.data?.data
handleImagePicked(imageUri)
private val imagePickerResultLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val imageUri = result.data?.data
handleImagePicked(imageUri)
}
}
}

private fun handleImagePicked(imageUri: Uri?) {
if (imageUri != null) {
Expand All @@ -82,24 +98,24 @@ class ProfileFragment : Fragment() {
val viewPager = mBinding.profileLayout.viewPager


val adapter = ViewPagerAdapter(requireActivity())
viewPager.adapter = adapter

if (profileUserId.isEmpty()) {
viewPager.adapter = ViewPagerAdapter(requireActivity())
} else {
viewPager.adapter = ViewPagerAdapter(requireActivity(), profileUserId)
}


TabLayoutMediator(tabLayout, viewPager) { tab, position ->
when (position) {
0 -> tab.text = getString(R.string.tab_favorites)
1 -> tab.text = getString(R.string.tab_your_uploads)
1 -> tab.text = getString(R.string.tab_uploads)
else -> error("Invalid position")
}
}.attach()
}

private fun setUpDrawer() {
mBinding.profileLayout.ibDrawer.setOnClickListener {
mBinding.drawerLayout.openDrawer(GravityCompat.START)
}

navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.nav_logout -> {
Expand Down Expand Up @@ -142,6 +158,16 @@ class ProfileFragment : Fragment() {
private fun setUpView() {
navView = mBinding.navView
drawerLayout = mBinding.drawerLayout
if (profileUserId.isEmpty()) {
viewProfileForOther()
} else {
viewProfileForSelf()
}
}

private fun viewProfileForOther() {
mBinding.profileLayout.ibDrawer.visibility = View.VISIBLE
mBinding.profileLayout.ibBack.visibility = View.GONE
profileViewModel.retrieveProfile { user ->
if (user != null) {
loadImage(mBinding.profileLayout.ivProfile, user.photoUrl!!)
Expand All @@ -150,6 +176,17 @@ class ProfileFragment : Fragment() {
}
}

private fun viewProfileForSelf() {
mBinding.profileLayout.ibDrawer.visibility = View.GONE
mBinding.profileLayout.ibBack.visibility = View.VISIBLE
profileViewModel.retrieveProfile(profileUserId) { user ->
if (user != null) {
loadImage(mBinding.profileLayout.ivProfile, user.photoUrl!!)
mBinding.profileLayout.tvName.text = user.displayName
}
}
}

private fun loadImage(view: ImageView, url: String) {
try {
Glide.with(view.context).load(url).circleCrop().into(view)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,10 @@ class ProfileViewModel : ViewModel() {

private val db = InmoMarket.getDb()

fun retrieveProfile(callback: (User?) -> Unit) {
fun retrieveProfile(profileUserId: String = FirebaseAuth.getInstance().currentUser!!.uid, callback: (User?) -> Unit) {
viewModelScope.launch {
try {
val userId = InmoMarket.getAuth().currentUser!!.uid
val user = getUserFromDb(userId)
val user = getUserFromDb(profileUserId)
callback(user)
} catch (e: Exception) {
Log.e(Constants.TAG, "retrieveProfile:failure", e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ package com.isanz.inmomarket.ui.profile
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.firebase.auth.FirebaseAuth
import com.isanz.inmomarket.ui.profile.tabs.favorites.FavoritesProfileFragment
import com.isanz.inmomarket.ui.profile.tabs.uploads.YourUploadsProfileFragment

class ViewPagerAdapter(fragmentActivity: FragmentActivity) :
class ViewPagerAdapter(fragmentActivity: FragmentActivity, private val userId: String = FirebaseAuth.getInstance().currentUser!!.uid) :
FragmentStateAdapter(fragmentActivity) {
override fun getItemCount(): Int {
return 2
}

override fun createFragment(position: Int): Fragment {
return when (position) {
0 -> FavoritesProfileFragment()
1 -> YourUploadsProfileFragment()
0 -> FavoritesProfileFragment(userId)
1 -> YourUploadsProfileFragment(userId)
else -> error("Invalid position")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ import com.isanz.inmomarket.utils.entities.Property
import com.isanz.inmomarket.utils.interfaces.OnItemClickListener


class FavoritesProfileFragment : Fragment(), OnItemClickListener {
class FavoritesProfileFragment(userId: String ): Fragment(), OnItemClickListener {


private lateinit var mBinging: FragmentFavoritesProfileBinding
private val favoritesProfileViewModel: FavoritesProfileViewModel by lazy {
ViewModelProvider(this)[FavoritesProfileViewModel::class.java]
val factory = FavoritesProfileViewModelFactory(userId)
ViewModelProvider(this, factory)[FavoritesProfileViewModel::class.java]
}

override fun onCreateView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.firestore.DocumentSnapshot
import com.google.firebase.firestore.QuerySnapshot
import com.isanz.inmomarket.InmoMarket
Expand All @@ -15,7 +16,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class FavoritesProfileViewModel(private val dispatcher: CoroutineDispatcher = Dispatchers.IO) : ViewModel() {
class FavoritesProfileViewModel(userId: String = FirebaseAuth.getInstance().currentUser!!.uid , private val dispatcher: CoroutineDispatcher = Dispatchers.IO) : ViewModel() {

val db = InmoMarket.getDb()
private val _listfavorites = MutableLiveData<MutableList<Property>>()
Expand All @@ -24,14 +25,14 @@ class FavoritesProfileViewModel(private val dispatcher: CoroutineDispatcher = Di
init {
viewModelScope.launch {
try {
listenForFavorites()
listenForFavorites(userId)
} catch (e: Exception) {
Log.w(ContentValues.TAG, "listenFavorites:failure.", e)
}
}
}

private suspend fun listenForFavorites() = withContext(dispatcher) {
private suspend fun listenForFavorites(userId: String = FirebaseAuth.getInstance().currentUser!!.uid) = withContext(dispatcher) {
db.collection("properties").addSnapshotListener { snapshot, e ->
if (e != null) {
logFailure(e)
Expand All @@ -40,7 +41,7 @@ class FavoritesProfileViewModel(private val dispatcher: CoroutineDispatcher = Di

try {
snapshot?.let {
handleSnapshot(it)
handleSnapshot(it, userId)
} ?: logNullData()
} catch (e: Exception) {
logFailure(e)
Expand All @@ -56,12 +57,11 @@ class FavoritesProfileViewModel(private val dispatcher: CoroutineDispatcher = Di
Log.d(ContentValues.TAG, "Current data: null")
}

private fun handleSnapshot(snapshot: QuerySnapshot) {
private fun handleSnapshot(snapshot: QuerySnapshot, userId: String = InmoMarket.getAuth().currentUser!!.uid) {
val properties = mutableListOf<Property>()
val currentUserId = InmoMarket.getAuth().currentUser!!.uid
for (document in snapshot.documents) {
if (document.exists()) {
handleDocument(document, currentUserId, properties)
handleDocument(document, userId, properties)
}
}
_listfavorites.postValue(properties)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.isanz.inmomarket.ui.profile.tabs.favorites

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class FavoritesProfileViewModelFactory(private val userId: String) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(FavoritesProfileViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return FavoritesProfileViewModel(userId) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.isanz.inmomarket.ui.profile.tabs.favorites

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.isanz.inmomarket.ui.profile.tabs.uploads.YourUploadsProfileViewModel

class UploadsProfileViewModelFactory(private val userId: String) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(YourUploadsProfileViewModel::class.java)) {
@Suppress("UNCHECKED_CAST")
return YourUploadsProfileViewModel(userId) as T
}
throw IllegalArgumentException("Unknown ViewModel class")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.isanz.inmomarket.R
import com.isanz.inmomarket.databinding.FragmentYourUploadsProfileBinding
import com.isanz.inmomarket.rv.propertyItem.PropertyItemListAdapter
import com.isanz.inmomarket.ui.profile.tabs.favorites.UploadsProfileViewModelFactory
import com.isanz.inmomarket.utils.entities.Property
import com.isanz.inmomarket.utils.interfaces.OnItemClickListener


class YourUploadsProfileFragment : Fragment(), OnItemClickListener {
class YourUploadsProfileFragment(userId: String) : Fragment(), OnItemClickListener {


private lateinit var mBinging: FragmentYourUploadsProfileBinding
private val yourUploadsProfileViewModel: YourUploadsProfileViewModel by lazy {
ViewModelProvider(this)[YourUploadsProfileViewModel::class.java]
val factory = UploadsProfileViewModelFactory(userId)
ViewModelProvider(this, factory)[YourUploadsProfileViewModel::class.java]
}

override fun onCreateView(
Expand Down
Loading

0 comments on commit 9bb02fa

Please sign in to comment.