Skip to content

Fixed: Real-time updates for participants list when view is open #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -240,16 +240,44 @@ class CallActivity : AppCompatActivity() {
}
}
}

lifecycleScope.launchWhenCreated {
viewModel.participants.collect { participants ->
if (bottomSheet == null) {
bottomSheet = ParticipantsBottomSheetFragment.newInstance(participants.toMutableList(), viewModel.participantColors)
} else {
bottomSheet?.setParticipants(participants.toMutableList(), viewModel.participantColors)
}
lifecycleScope.launchWhenCreated {
viewModel.participants.collect { participants ->
// Update or create bottom sheet
if (bottomSheet == null) {
bottomSheet = ParticipantsBottomSheetFragment.newInstance(
participants.toMutableList(),
viewModel.participantColors
).apply {
// Set up the fragment to handle updates
this.participantsList = participants.toMutableList()
this.colorsMap = viewModel.participantColors.toMutableMap()
}
}

// Update existing bottom sheet
bottomSheet?.let { sheet ->
sheet.participantsList?.clear()
sheet.participantsList?.addAll(participants)
sheet.colorsMap?.clear()
sheet.colorsMap?.putAll(viewModel.participantColors)

// Force UI update
sheet.view?.let { view ->
val recyclerView = view.findViewById<RecyclerView>(R.id.recyclerView)
recyclerView?.adapter?.notifyDataSetChanged()

// Update count if available
val countView = view.findViewById<TextView>(R.id.participantCount)
countView?.text = participants.size.toString()
}
}

// Update the badge in activity
val participantCountBadge = findViewById<TextView>(R.id.participantCountBadge)
participantCountBadge.visibility = if (participants.isNotEmpty()) View.VISIBLE else View.GONE
participantCountBadge.text = participants.size.toString()
}
}

// Controls setup
viewModel.cameraEnabled.observe(this) { enabled ->
Expand Down Expand Up @@ -545,4 +573,21 @@ class CallActivity : AppCompatActivity() {
val videoEnabled: Boolean,
val roomName: String? = null
) : Parcelable
}
}

// Extensions to access fragment properties directly
var ParticipantsBottomSheetFragment.participantsList: MutableList<Participant>?
get() = arguments?.getParcelableArrayList<Participant>("participants")
set(value) {
arguments = (arguments ?: Bundle()).apply {
putParcelableArrayList("participants", ArrayList(value ?: emptyList()))
}
}

var ParticipantsBottomSheetFragment.colorsMap: MutableMap<String, Int>?
get() = arguments?.getSerializable("colors") as? MutableMap<String, Int>
set(value) {
arguments = (arguments ?: Bundle()).apply {
putSerializable("colors", HashMap(value ?: emptyMap()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,21 @@ class ParticipantsBottomSheetFragment : BottomSheetDialogFragment() {
_binding = null // Avoid memory leaks
}

fun setParticipants(
newParticipants: MutableList<Participant>,
newParticipantColors: MutableMap<String, Int>
) {
this.participants = newParticipants
this.participantColors = newParticipantColors

if (_binding != null) { // Ensure the view exists
updateParticipantCount()
adapter.setParticipants(participants, participantColors)
}
fun setParticipants(
newParticipants: MutableList<Participant>,
newParticipantColors: MutableMap<String, Int>
) {
this.participants.clear()
this.participants.addAll(newParticipants)
this.participantColors.clear()
this.participantColors.putAll(newParticipantColors)

if (isAdded && _binding != null) { // Check both isAdded and binding
updateParticipantCount()
adapter.setParticipants(participants, participantColors)
binding.listView.invalidateViews()
}
}

private fun updateParticipantCount() {
binding.participantCountText.text = when (participants.size) {
Expand All @@ -107,6 +110,10 @@ class ParticipantsBottomSheetFragment : BottomSheetDialogFragment() {

private val moshi: Moshi = Moshi.Builder().build() // Reuse instead of recreating in getView()

override fun getItemId(position: Int): Long {
return participants.getOrNull(position)?.sid?.hashCode()?.toLong() ?: super.getItemId(position)
}

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val viewHolder: ViewHolder
val view: View
Expand Down Expand Up @@ -164,14 +171,17 @@ class ParticipantsBottomSheetFragment : BottomSheetDialogFragment() {
return view
}

fun setParticipants(
participants: MutableList<Participant>,
participantColors: MutableMap<String, Int>
) {
this.participants = participants
this.participantColors = participantColors
notifyDataSetChanged()
}
fun setParticipants(
participants: MutableList<Participant>,
participantColors: MutableMap<String, Int>
) {
clear()
addAll(participants)
this.participantColors.clear()
this.participantColors.putAll(participantColors)
notifyDataSetChanged()
notifyAll()
}

// ViewHolder to optimize findViewById calls
private class ViewHolder(view: View) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,18 @@
</LinearLayout>


<ListView
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxHeight="400dp"
android:divider="@color/blackSemiTransparent"
android:dividerHeight="1dp"/>
android:dividerHeight="1dp"
android:drawSelectorOnTop="true"
android:fastScrollEnabled="false"
android:scrollingCache="false"
android:animationCache="false"
android:smoothScrollbar="true"/>

<View
android:layout_width="match_parent"
Expand Down