Skip to content

Commit

Permalink
fix downloaded chat double messages
Browse files Browse the repository at this point in the history
(cherry picked from commit 8118c82)
  • Loading branch information
crackededed committed Jan 1, 2025
1 parent e390124 commit 373fa11
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class ChatFragment : BaseNetworkFragment(), LifecycleListener, MessageClickedDia
if (!requireContext().prefs().getBoolean(C.CHAT_DISABLE, false)) {
val args = requireArguments()
val channelId = args.getString(KEY_CHANNEL_ID)
val channelLogin = args.getString(KEY_CHANNEL_LOGIN)
val isLive = args.getBoolean(KEY_IS_LIVE)
val accountLogin = requireContext().tokenPrefs().getString(C.USERNAME, null)
val isLoggedIn = !accountLogin.isNullOrBlank() &&
Expand All @@ -81,7 +82,6 @@ class ChatFragment : BaseNetworkFragment(), LifecycleListener, MessageClickedDia
personalEmoteSets = viewModel.personalEmoteSets,
personalEmoteSetUsers = viewModel.personalEmoteSetUsers
)
val channelLogin = args.getString(KEY_CHANNEL_LOGIN)
val helixHeaders = TwitchApiHelper.getHelixHeaders(requireContext())
val gqlHeaders = TwitchApiHelper.getGQLHeaders(requireContext(), true)
val accountId = requireContext().tokenPrefs().getString(C.USER_ID, null)
Expand Down Expand Up @@ -302,7 +302,7 @@ class ChatFragment : BaseNetworkFragment(), LifecycleListener, MessageClickedDia
viewModel.playbackMessage.collectLatest {
if (it != null) {
if (it.live != null) {
(parentFragment as? StreamPlayerFragment)?.updateLiveStatus(it.live, it.serverTime, args.getString(KEY_CHANNEL_LOGIN))
(parentFragment as? StreamPlayerFragment)?.updateLiveStatus(it.live, it.serverTime, channelLogin)
}
(parentFragment as? StreamPlayerFragment)?.updateViewerCount(it.viewers)
}
Expand Down Expand Up @@ -379,7 +379,13 @@ class ChatFragment : BaseNetworkFragment(), LifecycleListener, MessageClickedDia
}
}
if (chatUrl != null) {
initialize()
viewModel.startReplay(
channelId = channelId,
channelLogin = channelLogin,
chatUrl = chatUrl,
getCurrentPosition = (parentFragment as BasePlayerFragment)::getCurrentPosition,
getCurrentSpeed = (parentFragment as BasePlayerFragment)::getCurrentSpeed
)
}
}
}
Expand All @@ -394,13 +400,11 @@ class ChatFragment : BaseNetworkFragment(), LifecycleListener, MessageClickedDia
if (args.getBoolean(KEY_IS_LIVE)) {
viewModel.startLive(channelId, channelLogin, args.getString(KEY_CHANNEL_NAME), args.getString(KEY_STREAM_ID))
} else {
val chatUrl = args.getString(KEY_CHAT_URL)
val videoId = args.getString(KEY_VIDEO_ID)
if (chatUrl != null || (videoId != null && !args.getBoolean(KEY_START_TIME_EMPTY))) {
if (videoId != null && !args.getBoolean(KEY_START_TIME_EMPTY)) {
viewModel.startReplay(
channelId = channelId,
channelLogin = channelLogin,
chatUrl = chatUrl,
videoId = videoId,
startTime = args.getInt(KEY_START_TIME),
getCurrentPosition = (parentFragment as BasePlayerFragment)::getCurrentPosition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,25 @@ import javax.inject.Inject
import kotlin.math.max

class ChatReplayManagerLocal @Inject constructor(
private val messages: List<ChatMessage>,
private val startTime: Long,
private val getCurrentPosition: () -> Long?,
private val getCurrentSpeed: () -> Float?,
private val onMessage: (ChatMessage) -> Unit,
private val clearMessages: () -> Unit,
private val coroutineScope: CoroutineScope,
) {

private var messages: List<ChatMessage> = emptyList()
private var startTime = 0L
private val list = mutableListOf<ChatMessage>()
private var isLoading = false
private var loadJob: Job? = null
private var messageJob: Job? = null
private var lastCheckedPosition = 0L
private var playbackSpeed: Float? = null

fun start() {
fun start(newMessages: List<ChatMessage>, newStartTime: Long) {
messages = newMessages
startTime = newStartTime
val currentPosition = getCurrentPosition() ?: 0
lastCheckedPosition = currentPosition
playbackSpeed = getCurrentSpeed()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ class ChatViewModel @Inject constructor(
}
}

fun startReplay(channelId: String?, channelLogin: String?, chatUrl: String?, videoId: String?, startTime: Int, getCurrentPosition: () -> Long?, getCurrentSpeed: () -> Float?) {
fun startReplay(channelId: String?, channelLogin: String?, chatUrl: String? = null, videoId: String? = null, startTime: Int = 0, getCurrentPosition: () -> Long?, getCurrentSpeed: () -> Float?) {
if (chatReplayManager == null && chatReplayManagerLocal == null) {
messageLimit = applicationContext.prefs().getInt(C.CHAT_LIMIT, 600)
startReplayChat(videoId, startTime, chatUrl, getCurrentPosition, getCurrentSpeed, channelId, channelLogin)
Expand Down Expand Up @@ -1518,7 +1518,14 @@ class ChatViewModel @Inject constructor(
fun startReplayChat(videoId: String?, startTime: Int, chatUrl: String?, getCurrentPosition: () -> Long?, getCurrentSpeed: () -> Float?, channelId: String?, channelLogin: String?) {
stopReplayChat()
if (!chatUrl.isNullOrBlank()) {
readChatFile(chatUrl, getCurrentPosition, getCurrentSpeed, channelId, channelLogin)
chatReplayManagerLocal = ChatReplayManagerLocal(
getCurrentPosition = getCurrentPosition,
getCurrentSpeed = getCurrentSpeed,
onMessage = { onMessage(it) },
clearMessages = { _chatMessages.value = ArrayList() },
coroutineScope = viewModelScope
)
readChatFile(chatUrl, channelId, channelLogin)
} else {
if (!videoId.isNullOrBlank()) {
chatReplayManager = ChatReplayManager(
Expand Down Expand Up @@ -1549,7 +1556,7 @@ class ChatViewModel @Inject constructor(
chatReplayManager?.updateSpeed(speed) ?: chatReplayManagerLocal?.updateSpeed(speed)
}

private fun readChatFile(url: String, getCurrentPosition: () -> Long?, getCurrentSpeed: () -> Float?, channelId: String?, channelLogin: String?) {
private fun readChatFile(url: String, channelId: String?, channelLogin: String?) {
viewModelScope.launch(Dispatchers.IO) {
try {
val nameDisplay = applicationContext.prefs().getString(C.UI_NAME_DISPLAY, "0")
Expand Down Expand Up @@ -1930,15 +1937,7 @@ class ChatViewModel @Inject constructor(
}
if (messages.isNotEmpty()) {
viewModelScope.launch {
chatReplayManagerLocal = ChatReplayManagerLocal(
messages = messages,
startTime = startTimeMs,
getCurrentPosition = getCurrentPosition,
getCurrentSpeed = getCurrentSpeed,
onMessage = { onMessage(it) },
clearMessages = { _chatMessages.value = ArrayList() },
coroutineScope = viewModelScope
).apply { start() }
chatReplayManagerLocal?.start(messages, startTimeMs)
}
}
} catch (e: Exception) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ class MessageClickedDialog : BottomSheetDialogFragment(), IntegrityDialog.Callba
}
}
}
viewProfile.setOnClickListener {
listener.onViewProfileClicked(selectedMessage.userId, selectedMessage.userLogin, selectedMessage.userName, null)
dismiss()
}
}
}
}
Expand Down

0 comments on commit 373fa11

Please sign in to comment.