Advertisement
Guest User

Untitled

a guest
Feb 3rd, 2022
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 5.38 KB | None | 0 0
  1. class LastChatsRecyclerViewAdapter :
  2.     RecyclerView.Adapter<LastChatsRecyclerViewAdapter.LastChatsHolder?>() {
  3.  
  4.     private lateinit var theUserList: List<LinkedHashMap<String, Any?>>
  5.     private lateinit var listener: LastChatsRecyclerViewAdapter.OnClickedUser
  6.  
  7.     private var mainRepository = MainActivityRepository()
  8.     private lateinit var itemBinding: LastChatsRowBinding
  9.     private lateinit var lifecycleOwner : LifecycleOwner
  10.  
  11.     inner class LastChatsHolder() :
  12.         RecyclerView.ViewHolder(itemBinding.root) {
  13.  
  14.         fun bindUsers(usersHashMap: HashMap<String, Any?>) {
  15.  
  16.             val user: User = usersHashMap["Sender"] as User
  17.             val messages: Int = usersHashMap["Messages"] as Int
  18.  
  19.             if (user.status == "Online") {
  20.                 itemBinding.lastChatsImageOffline.visibility = View.GONE
  21.                 itemBinding.lastChatsImageOnline.visibility = View.VISIBLE
  22.             }else{
  23.                 itemBinding.lastChatsImageOffline.visibility = View.VISIBLE
  24.                 itemBinding.lastChatsImageOnline.visibility = View.GONE
  25.             }
  26.  
  27.             Glide.with(itemBinding.root).load(user.profilePic)
  28.                 .into(itemBinding.lastChatsUsersProfilePicture)
  29.  
  30.  
  31.             itemBinding.lastChatsUsersUserName.text = user.userName
  32.  
  33.             when {
  34.                 messages == 0 -> {
  35.                     itemBinding.lastChatsNumberOfNewMessages.visibility = View.GONE
  36.                 }
  37.                 messages == 1 -> {
  38.                     itemBinding.lastChatsNumberOfNewMessages.text =
  39.                         messages.toString() + " New Message"
  40.                 }
  41.                 messages > 1 -> {
  42.                     itemBinding.lastChatsNumberOfNewMessages.text =
  43.                         messages.toString() + " New Messages"
  44.                 }
  45.             }
  46.  
  47.             itemBinding.root.setOnClickListener {
  48.                 listener.onClickedUser(user)
  49.             }
  50.  
  51.         }
  52.  
  53.     }
  54.  
  55.     override fun onCreateViewHolder(
  56.         parent: ViewGroup,
  57.         viewType: Int
  58.     ): LastChatsRecyclerViewAdapter.LastChatsHolder {
  59.  
  60.         if(!::lifecycleOwner.isInitialized){
  61.             lifecycleOwner = parent.context as LifecycleOwner
  62.         }
  63.         itemBinding = LastChatsRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
  64.  
  65.         return LastChatsHolder()
  66.     }
  67.  
  68.     override fun onBindViewHolder(
  69.         holder: LastChatsRecyclerViewAdapter.LastChatsHolder,
  70.         position: Int
  71.     ) {
  72.  
  73.         holder.bindUsers(theUserList[position])
  74.         val user : User = theUserList[position]["Sender"] as User
  75.         Log.e("onBindViewHolder","Key $user")
  76.  
  77.  
  78.     }
  79.  
  80.     override fun getItemCount(): Int {
  81.         return theUserList.size
  82.     }
  83.  
  84.     fun getUserList(): List<HashMap<String, Any?>> {
  85.         return theUserList
  86.     }
  87.  
  88.     fun setUserList(newList: List<LinkedHashMap<String, Any?>>?, context: Context) {
  89.  
  90.         if (this::theUserList.isInitialized) {
  91.          (theUserList as ArrayList<LinkedHashMap<String, Any?>>).clear()
  92.         }
  93.  
  94.         if(!::lifecycleOwner.isInitialized){
  95.             lifecycleOwner = context as LifecycleOwner
  96.         }
  97.  
  98.         if (newList != null) {
  99.  
  100.             // Remove Duplicates Users
  101.             val hs = LinkedHashSet<LinkedHashMap<String, Any?>>()
  102.             hs.addAll(newList)
  103.             (newList as ArrayList<LinkedHashMap<String, Any?>>)
  104.             newList.clear()
  105.             newList.addAll(hs)
  106.  
  107.  
  108.             val sortedList = newList.sortedByDescending { it["Messages"].toString().toInt() }
  109.  
  110.             theUserList = sortedList
  111.             notifyDataSetChanged()
  112.  
  113.             if (theUserList != null) {
  114.  
  115.                 for (user in theUserList.indices){
  116.  
  117.  
  118.                     val userPosition : Int = user
  119.                     val userHashMap : HashMap<String, Any?> = theUserList[userPosition]
  120.                     val userDetails: User = userHashMap["Sender"] as User
  121.  
  122.                         mainRepository.checkUserStatus(userDetails.uid).observe(lifecycleOwner){
  123.  
  124.  
  125.                             Log.e("newList","RUN $it")
  126.                             updateUserStatus(userPosition,it)
  127.  
  128.                         }
  129.                 }
  130.  
  131.             }
  132.  
  133.         }
  134.     }
  135.  
  136.     interface OnClickedUser {
  137.         fun onClickedUser(user: User)
  138.     }
  139.  
  140.     fun setOnUserClickListener(listener: OnClickedUser) {
  141.         this.listener = listener
  142.     }
  143.  
  144.     private fun updateUserStatus(userPosition: Int, userHashMap: HashMap<String, Any>) {
  145.  
  146.         val userUID = userHashMap["userUID"] as String
  147.         val userStatus = userHashMap["status"] as String
  148.         val user : User = theUserList[userPosition]["Sender"] as User
  149.  
  150.         Log.e("HashMap","UID - $userUID")
  151.  
  152.         if (userStatus == "Online") {
  153.             Log.e("HashMap","Online")
  154.             itemBinding.lastChatsImageOffline.visibility = View.GONE
  155.             itemBinding.lastChatsImageOnline.visibility = View.VISIBLE
  156.             itemBinding.lastChatsUsersUserName.text = "Online"
  157.             notifyItemChanged(userPosition)
  158.         }else{
  159.             Log.e("HashMap","Offline")
  160.             itemBinding.lastChatsImageOffline.visibility = View.VISIBLE
  161.             itemBinding.lastChatsImageOnline.visibility = View.GONE
  162.  
  163.             itemBinding.lastChatsUsersUserName.text = "Offline"
  164.             notifyItemChanged(userPosition)
  165.         }
  166.  
  167.     }
  168.  
  169.  
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement