2023-08-12 00:59:02 -06:00
|
|
|
package com.isolaatti.followers.ui
|
|
|
|
|
|
|
|
|
|
import android.os.Bundle
|
2024-01-07 19:41:28 -06:00
|
|
|
import android.util.Log
|
2023-08-12 00:59:02 -06:00
|
|
|
import android.view.LayoutInflater
|
|
|
|
|
import android.view.View
|
|
|
|
|
import android.view.ViewGroup
|
|
|
|
|
import androidx.fragment.app.Fragment
|
|
|
|
|
import androidx.fragment.app.viewModels
|
2023-08-15 00:02:29 -06:00
|
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
|
|
|
import com.isolaatti.common.UserItemCallback
|
|
|
|
|
import com.isolaatti.common.UserListRecyclerViewAdapter
|
2023-08-12 00:59:02 -06:00
|
|
|
import com.isolaatti.databinding.FragmentFollowersBinding
|
|
|
|
|
import com.isolaatti.followers.presentation.FollowersViewModel
|
2023-08-15 00:02:29 -06:00
|
|
|
import com.isolaatti.profile.domain.entity.ProfileListItem
|
2024-01-07 19:41:28 -06:00
|
|
|
import com.isolaatti.profile.ui.ProfileActivity
|
2023-08-12 00:59:02 -06:00
|
|
|
|
2023-08-15 00:02:29 -06:00
|
|
|
class FollowersFragment : Fragment(), UserItemCallback {
|
2023-08-12 00:59:02 -06:00
|
|
|
private lateinit var binding: FragmentFollowersBinding
|
|
|
|
|
private val viewModel: FollowersViewModel by viewModels({ requireParentFragment() })
|
2023-08-15 00:02:29 -06:00
|
|
|
private lateinit var adapter: UserListRecyclerViewAdapter
|
2023-08-12 00:59:02 -06:00
|
|
|
|
|
|
|
|
override fun onCreateView(
|
|
|
|
|
inflater: LayoutInflater,
|
|
|
|
|
container: ViewGroup?,
|
|
|
|
|
savedInstanceState: Bundle?
|
|
|
|
|
): View {
|
|
|
|
|
binding = FragmentFollowersBinding.inflate(inflater)
|
|
|
|
|
return binding.root
|
|
|
|
|
}
|
2023-08-15 00:02:29 -06:00
|
|
|
|
|
|
|
|
private fun setObservers() {
|
2024-01-07 19:41:28 -06:00
|
|
|
viewModel.followers.observe(viewLifecycleOwner) { (list, updateEvent) ->
|
|
|
|
|
adapter.updateData(list, updateEvent)
|
|
|
|
|
binding.swipeToRefresh.isRefreshing = false
|
2023-08-15 00:02:29 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun bind() {
|
|
|
|
|
adapter = UserListRecyclerViewAdapter(this)
|
|
|
|
|
binding.recyclerUsers.adapter = adapter
|
|
|
|
|
binding.recyclerUsers.layoutManager = LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
|
2024-01-07 19:41:28 -06:00
|
|
|
|
|
|
|
|
binding.swipeToRefresh.setOnRefreshListener {
|
|
|
|
|
viewModel.fetchFollowers(refresh = true)
|
|
|
|
|
}
|
2023-08-15 00:02:29 -06:00
|
|
|
}
|
|
|
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
|
|
|
|
super.onViewCreated(view, savedInstanceState)
|
|
|
|
|
|
|
|
|
|
bind()
|
|
|
|
|
setObservers()
|
|
|
|
|
|
|
|
|
|
viewModel.fetchFollowers()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun itemClick(userId: Int) {
|
2024-01-07 19:41:28 -06:00
|
|
|
ProfileActivity.startActivity(requireContext(), userId)
|
2023-08-15 00:02:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override fun followButtonClick(user: ProfileListItem, action: UserItemCallback.FollowButtonAction) {
|
|
|
|
|
when(action) {
|
|
|
|
|
UserItemCallback.FollowButtonAction.Follow -> viewModel.followUser(user)
|
|
|
|
|
UserItemCallback.FollowButtonAction.Unfollow -> viewModel.unfollowUser(user)
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-12 00:59:02 -06:00
|
|
|
}
|