Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class LastChatsRecyclerViewAdapter :
- RecyclerView.Adapter<LastChatsRecyclerViewAdapter.LastChatsHolder?>() {
- private lateinit var theUserList: List<LinkedHashMap<String, Any?>>
- private lateinit var listener: LastChatsRecyclerViewAdapter.OnClickedUser
- private var mainRepository = MainActivityRepository()
- private lateinit var itemBinding: LastChatsRowBinding
- private lateinit var lifecycleOwner : LifecycleOwner
- inner class LastChatsHolder() :
- RecyclerView.ViewHolder(itemBinding.root) {
- fun bindUsers(usersHashMap: HashMap<String, Any?>) {
- val user: User = usersHashMap["Sender"] as User
- val messages: Int = usersHashMap["Messages"] as Int
- if (user.status == "Online") {
- itemBinding.lastChatsImageOffline.visibility = View.GONE
- itemBinding.lastChatsImageOnline.visibility = View.VISIBLE
- }else{
- itemBinding.lastChatsImageOffline.visibility = View.VISIBLE
- itemBinding.lastChatsImageOnline.visibility = View.GONE
- }
- Glide.with(itemBinding.root).load(user.profilePic)
- .into(itemBinding.lastChatsUsersProfilePicture)
- itemBinding.lastChatsUsersUserName.text = user.userName
- when {
- messages == 0 -> {
- itemBinding.lastChatsNumberOfNewMessages.visibility = View.GONE
- }
- messages == 1 -> {
- itemBinding.lastChatsNumberOfNewMessages.text =
- messages.toString() + " New Message"
- }
- messages > 1 -> {
- itemBinding.lastChatsNumberOfNewMessages.text =
- messages.toString() + " New Messages"
- }
- }
- itemBinding.root.setOnClickListener {
- listener.onClickedUser(user)
- }
- }
- }
- override fun onCreateViewHolder(
- parent: ViewGroup,
- viewType: Int
- ): LastChatsRecyclerViewAdapter.LastChatsHolder {
- if(!::lifecycleOwner.isInitialized){
- lifecycleOwner = parent.context as LifecycleOwner
- }
- itemBinding = LastChatsRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
- return LastChatsHolder()
- }
- override fun onBindViewHolder(
- holder: LastChatsRecyclerViewAdapter.LastChatsHolder,
- position: Int
- ) {
- holder.bindUsers(theUserList[position])
- val user : User = theUserList[position]["Sender"] as User
- Log.e("onBindViewHolder","Key $user")
- }
- override fun getItemCount(): Int {
- return theUserList.size
- }
- fun getUserList(): List<HashMap<String, Any?>> {
- return theUserList
- }
- fun setUserList(newList: List<LinkedHashMap<String, Any?>>?, context: Context) {
- if (this::theUserList.isInitialized) {
- (theUserList as ArrayList<LinkedHashMap<String, Any?>>).clear()
- }
- if(!::lifecycleOwner.isInitialized){
- lifecycleOwner = context as LifecycleOwner
- }
- if (newList != null) {
- // Remove Duplicates Users
- val hs = LinkedHashSet<LinkedHashMap<String, Any?>>()
- hs.addAll(newList)
- (newList as ArrayList<LinkedHashMap<String, Any?>>)
- newList.clear()
- newList.addAll(hs)
- val sortedList = newList.sortedByDescending { it["Messages"].toString().toInt() }
- theUserList = sortedList
- notifyDataSetChanged()
- if (theUserList != null) {
- for (user in theUserList.indices){
- val userPosition : Int = user
- val userHashMap : HashMap<String, Any?> = theUserList[userPosition]
- val userDetails: User = userHashMap["Sender"] as User
- mainRepository.checkUserStatus(userDetails.uid).observe(lifecycleOwner){
- Log.e("newList","RUN $it")
- updateUserStatus(userPosition,it)
- }
- }
- }
- }
- }
- interface OnClickedUser {
- fun onClickedUser(user: User)
- }
- fun setOnUserClickListener(listener: OnClickedUser) {
- this.listener = listener
- }
- private fun updateUserStatus(userPosition: Int, userHashMap: HashMap<String, Any>) {
- val userUID = userHashMap["userUID"] as String
- val userStatus = userHashMap["status"] as String
- val user : User = theUserList[userPosition]["Sender"] as User
- Log.e("HashMap","UID - $userUID")
- if (userStatus == "Online") {
- Log.e("HashMap","Online")
- itemBinding.lastChatsImageOffline.visibility = View.GONE
- itemBinding.lastChatsImageOnline.visibility = View.VISIBLE
- itemBinding.lastChatsUsersUserName.text = "Online"
- notifyItemChanged(userPosition)
- }else{
- Log.e("HashMap","Offline")
- itemBinding.lastChatsImageOffline.visibility = View.VISIBLE
- itemBinding.lastChatsImageOnline.visibility = View.GONE
- itemBinding.lastChatsUsersUserName.text = "Offline"
- notifyItemChanged(userPosition)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement