Skip to content

Commit 94fd422

Browse files
committed
Fix stream pill flicker when returning to the foreground
1 parent b1f6522 commit 94fd422

5 files changed

Lines changed: 40 additions & 1 deletion

File tree

app/src/main/kotlin/net/primal/android/user/subscriptions/SubscriptionsManager.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ class SubscriptionsManager @Inject constructor(
138138

139139
private fun launchStreamsFromFollowsSubscription(userId: String) =
140140
scope.launch {
141+
streamRepository.syncLiveEventsFromFollows(userId = userId)
141142
streamRepository.startLiveEventsFromFollowsSubscription(userId = userId)
142143
}
143144

data/caching/remote/src/commonMain/kotlin/net/primal/data/remote/api/stream/LiveStreamApi.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ interface LiveStreamApi {
1717

1818
suspend fun subscribeToLiveEventsFromFollows(userId: String): Flow<NostrEvent>
1919

20+
suspend fun getLiveEventsFromFollows(userId: String): List<NostrEvent>
21+
2022
suspend fun findLiveStream(body: FindLiveStreamRequestBody): FindLiveStreamResponse
2123
}

data/caching/remote/src/commonMain/kotlin/net/primal/data/remote/api/stream/LiveStreamApiImpl.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ class LiveStreamApiImpl(
7373
}
7474
}
7575

76+
override suspend fun getLiveEventsFromFollows(userId: String): List<NostrEvent> =
77+
primalApiClient.query(
78+
message = PrimalCacheFilter(
79+
primalVerb = PrimalVerb.LIVE_EVENTS_FROM_FOLLOWS.id,
80+
optionsJson = LiveEventsFromFollowsRequest(pubkey = userId).encodeToJsonString(),
81+
),
82+
).filterNostrEvents(NostrEventKind.LiveActivity)
83+
7684
override suspend fun findLiveStream(body: FindLiveStreamRequestBody): FindLiveStreamResponse {
7785
val queryResult = primalApiClient.query(
7886
message = PrimalCacheFilter(

data/caching/repository/src/commonMain/kotlin/net/primal/data/repository/streams/StreamRepositoryImpl.kt

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,35 @@ class StreamRepositoryImpl(
112112
return@withContext job
113113
}
114114

115+
override suspend fun syncLiveEventsFromFollows(userId: String) =
116+
withContext(dispatcherProvider.io()) {
117+
val currentLiveStreams = liveStreamApi.getLiveEventsFromFollows(userId = userId)
118+
.mapNotNull { it.asStreamData() }
119+
120+
currentLiveStreams.forEach { liveActivity ->
121+
scope.launch {
122+
profileRepository.fetchMissingProfiles(profileIds = listOf(liveActivity.mainHostId))
123+
}
124+
}
125+
126+
database.withTransaction {
127+
database.streams().upsertStreamData(data = currentLiveStreams)
128+
database.streamFollows().deleteAllByOwnerId(ownerId = userId)
129+
currentLiveStreams
130+
.filter { it.isLive() }
131+
.forEach { liveActivity ->
132+
database.streamFollows().upsert(
133+
data = StreamFollowsCrossRef(
134+
streamATag = liveActivity.aTag,
135+
ownerId = userId,
136+
),
137+
)
138+
}
139+
}
140+
}
141+
115142
override suspend fun startLiveEventsFromFollowsSubscription(userId: String) =
116143
withContext(dispatcherProvider.io()) {
117-
database.streamFollows().deleteAllByOwnerId(ownerId = userId)
118144
liveStreamApi.subscribeToLiveEventsFromFollows(userId = userId)
119145
.collect { liveActivityEvent ->
120146
val liveActivity = liveActivityEvent.asStreamData() ?: return@collect

domain/primal/src/commonMain/kotlin/net/primal/domain/streams/StreamRepository.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ interface StreamRepository {
3535
streamContentModerationMode: StreamContentModerationMode,
3636
): Job
3737

38+
suspend fun syncLiveEventsFromFollows(userId: String)
39+
3840
suspend fun startLiveEventsFromFollowsSubscription(userId: String)
3941

4042
fun observeLiveEventsFromFollows(userId: String): Flow<List<Stream>>

0 commit comments

Comments
 (0)