Skip to content

Commit

Permalink
chore: update streamer's last live time after going offline.
Browse files Browse the repository at this point in the history
  • Loading branch information
hua0512 committed Apr 26, 2024
1 parent 8a40f08 commit 6b3d404
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,6 @@ class StreamerDownloadManager(

private var updateLiveStatusCallback: suspend (id: Long, isLive: Boolean) -> Unit = { _, _ -> }
private var updateStreamerLastLiveTime: suspend (id: Long, lastLiveTime: Long) -> Unit = { _, _ -> }
private var checkShouldUpdateStreamerLastLiveTime: suspend (id: Long, lastLiveTime: Long, now: Long) -> Boolean =
{ _, _, _ -> false }
private var onSavedToDb: suspend (stream: StreamData) -> Unit = {}
private var avatarUpdateCallback: (id: Long, avatarUrl: String) -> Unit = { _, _ -> }
private var onDescriptionUpdateCallback: (id: Long, description: String) -> Unit = { _, _ -> }
Expand Down Expand Up @@ -157,6 +155,8 @@ class StreamerDownloadManager(
dataList.toList()
)
)
// update last live time
updateLastLiveTime()
// call onStreamingFinished callback with the copy of the list
launch {
bindOnStreamingEndActions(streamer, dataList.toList())
Expand Down Expand Up @@ -246,10 +246,8 @@ class StreamerDownloadManager(

private suspend fun updateLastLiveTime() {
val now = Clock.System.now()
if (checkShouldUpdateStreamerLastLiveTime(streamer.id, streamer.lastLiveTime ?: 0, now.epochSeconds)) {
updateStreamerLastLiveTime(streamer.id, now.epochSeconds)
streamer.lastLiveTime = now.epochSeconds
}
updateStreamerLastLiveTime(streamer.id, now.epochSeconds)
streamer.lastLiveTime = now.epochSeconds
}

private suspend fun saveStreamData(stream: StreamData) = coroutineScope {
Expand Down Expand Up @@ -389,10 +387,6 @@ class StreamerDownloadManager(
this.updateStreamerLastLiveTime = updateStreamerLastLiveTime
}

fun onCheckLastLiveTime(checkShouldUpdateStreamerLastLiveTime: suspend (id: Long, lastLiveTime: Long, now: Long) -> Boolean) {
this.checkShouldUpdateStreamerLastLiveTime = checkShouldUpdateStreamerLastLiveTime
}

fun onSavedToDb(onSavedToDb: suspend (stream: StreamData) -> Unit) {
this.onSavedToDb = onSavedToDb
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ interface StreamerRepo {
suspend fun updateStreamerLiveStatus(id: Long, status: Boolean)

suspend fun updateStreamerStreamTitle(id: Long, streamTitle: String?)

suspend fun shouldUpdateStreamerLastLiveTime(id: Long, lastLiveTime: Long, currentLiveTime: Long): Boolean
suspend fun updateStreamerLastLiveTime(id: Long, lastLiveTime: Long)
suspend fun updateStreamerAvatar(id: Long, avatar: String?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,6 @@ class StreamerRepository(val dao: StreamerDao, val json: Json) : StreamerRepo {
}
}

override suspend fun shouldUpdateStreamerLastLiveTime(id: Long, lastLiveTime: Long, currentLiveTime: Long): Boolean {
if (lastLiveTime == 0L) return true
val lastStream = Instant.fromEpochSeconds(lastLiveTime).toLocalDateTime(TimeZone.currentSystemDefault())
val currentStream = Instant.fromEpochSeconds(currentLiveTime).toLocalDateTime(TimeZone.currentSystemDefault())
// if current live time is in the same day, no need to update
// otherwise, update the last live time
return lastStream.date != currentStream.date
}

override suspend fun updateStreamerLastLiveTime(id: Long, lastLiveTime: Long) {
return withIOContext {
dao.updateLastStream(StreamerId(id), lastLiveTime)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,6 @@ class DownloadService(
repo.updateStreamerLastLiveTime(id, lastLiveTime)
}

onCheckLastLiveTime { id, lastLiveTime, now ->
repo.shouldUpdateStreamerLastLiveTime(id, lastLiveTime, now)
}
onSavedToDb {
streamDataRepository.saveStreamData(it)
}
Expand Down

0 comments on commit 6b3d404

Please sign in to comment.