Skip to content

Commit 30d9ffa

Browse files
feat(discussion): Use TeamsRepository in DiscussionListFragment
Injects TeamsRepository into DiscussionListFragment to handle team data lookups. Replaces the direct Realm query with an asynchronous call to teamsRepository.getTeamById(teamId). Refactors the data loading logic to safely handle the asynchronous operation and prevent race conditions.
1 parent fb9fc10 commit 30d9ffa

File tree

1 file changed

+15
-21
lines changed

1 file changed

+15
-21
lines changed

app/src/main/java/org/ole/planet/myplanet/ui/teams/discussion/DiscussionListFragment.kt

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class DiscussionListFragment : BaseTeamFragment() {
3939
lateinit var userProfileDbHandler: UserProfileDbHandler
4040
@Inject
4141
lateinit var sharedPrefManager: SharedPrefManager
42+
@Inject
43+
lateinit var teamsRepository: TeamsRepository
4244

4345
private var filteredNewsList: List<RealmNews?> = listOf()
4446

@@ -103,22 +105,6 @@ class DiscussionListFragment : BaseTeamFragment() {
103105
}
104106
}
105107

106-
if (shouldQueryTeamFromRealm()) {
107-
team = try {
108-
mRealm.where(RealmMyTeam::class.java).equalTo("_id", teamId).findFirst()
109-
} catch (e: Exception) {
110-
e.printStackTrace()
111-
null
112-
}
113-
114-
if (team == null) {
115-
try {
116-
team = mRealm.where(RealmMyTeam::class.java).equalTo("teamId", teamId).findFirst()
117-
} catch (e: Exception) {
118-
e.printStackTrace()
119-
}
120-
}
121-
}
122108
binding.addMessage.isVisible = false
123109
return binding.root
124110
}
@@ -128,24 +114,32 @@ class DiscussionListFragment : BaseTeamFragment() {
128114
changeLayoutManager(resources.configuration.orientation, binding.rvDiscussion)
129115

130116
viewLifecycleOwner.lifecycleScope.launch {
117+
// Asynchronously load the team if necessary
118+
if (shouldQueryTeamFromRealm()) {
119+
team = teamsRepository.getTeamById(teamId)
120+
}
121+
122+
// Initialize the RecyclerView with an empty list first
123+
showRecyclerView(emptyList())
124+
125+
// Fetch and display the news list
131126
val realmNewsList = voicesRepository.getFilteredNews(getEffectiveTeamId())
132127
val count = realmNewsList.size
133128
voicesRepository.updateTeamNotification(getEffectiveTeamId(), count)
134129
showRecyclerView(realmNewsList)
135-
}
136130

137-
viewLifecycleOwner.lifecycleScope.launch {
131+
// Start collecting flows only after the team is potentially loaded
138132
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
139133
launch {
140134
voicesRepository.getDiscussionsByTeamIdFlow(getEffectiveTeamId()).collect {
141135
setData(it)
142136
}
143137
}
144138
combine(isMemberFlow, teamFlow) { isMember, teamData ->
145-
Pair(isMember, teamData?.isPublic == true)
146-
}.collectLatest { (isMember, isPublicTeamFromFlow) ->
139+
val isPublic = teamData?.isPublic ?: team?.isPublic ?: false
140+
Pair(isMember, isPublic)
141+
}.collectLatest { (isMember, isPublicTeam) ->
147142
val isGuest = user?.id?.startsWith("guest") == true
148-
val isPublicTeam = isPublicTeamFromFlow || team?.isPublic == true
149143
val canPost = !isGuest && (isMember || isPublicTeam)
150144
binding.addMessage.isVisible = canPost
151145
(binding.rvDiscussion.adapter as? NewsAdapter)?.setNonTeamMember(!isMember)

0 commit comments

Comments
 (0)