Skip to content

Commit

Permalink
Fix crash when closing player
Browse files Browse the repository at this point in the history
  • Loading branch information
arkon committed Jun 24, 2021
1 parent 82fef3b commit 2075a3c
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.navArgument
Expand All @@ -26,7 +25,6 @@ import com.livetl.android.vm.StreamViewModel
fun MainNavHost(
setKeepScreenOn: (Boolean) -> Unit,
setFullscreen: (Boolean) -> Unit,
streamViewModel: StreamViewModel = viewModel(),
) {
val navController = rememberNavController()

Expand Down Expand Up @@ -60,9 +58,11 @@ fun MainNavHost(
"${Route.Player.id}?urlOrId={urlOrId}",
arguments = listOf(navArgument("urlOrId") { defaultValue = "" })
) { backStackEntry ->
val streamViewModel = hiltViewModel<StreamViewModel>()
val playerViewModel = hiltViewModel<PlayerViewModel>()

val urlOrId = backStackEntry.arguments?.getString("urlOrId")!!
val videoId = streamViewModel.getVideoId(urlOrId)
val playerViewModel = hiltViewModel<PlayerViewModel>()

PlayerScreen(videoId, setKeepScreenOn, setFullscreen, playerViewModel)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ fun Chat(
.clickable { onClickMessage(message) }

when (minimalMode) {
true -> MinimalMessage(baseMessageModifier, message)
false -> Message(baseMessageModifier, message, showTimestamp, debugTimestamp)
true -> MinimalMessage(baseMessageModifier, message, playerViewModel.emojiCache)
false -> Message(baseMessageModifier, message, showTimestamp, debugTimestamp, playerViewModel.emojiCache)
}
}
}
Expand Down Expand Up @@ -140,6 +140,7 @@ private fun RegularChatPreviews() {
timestamp = 1615001105,
),
showTimestamp = true,
emojiCache = EmojiCache(),
)

Message(
Expand All @@ -149,6 +150,7 @@ private fun RegularChatPreviews() {
timestamp = 1615001105,
),
showTimestamp = true,
emojiCache = EmojiCache(),
)

Message(
Expand All @@ -158,6 +160,7 @@ private fun RegularChatPreviews() {
timestamp = 1615001105,
),
showTimestamp = true,
emojiCache = EmojiCache(),
)

Message(
Expand All @@ -167,6 +170,7 @@ private fun RegularChatPreviews() {
timestamp = 1615001105,
),
showTimestamp = true,
emojiCache = EmojiCache(),
)
}
}
Expand All @@ -186,5 +190,6 @@ private fun SuperChatPreview() {
level = ChatMessage.SuperChat.Level.RED,
),
showTimestamp = true,
emojiCache = EmojiCache(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ fun MessageActionsDialog(
AlertDialog(
onDismissRequest = { onDismiss() },
text = {
message?.let { Message(message = it) }
message?.let { Message(message = it, emojiCache = playerViewModel.emojiCache) }
},
buttons = {
TextButton(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import com.google.accompanist.coil.rememberCoilPainter
import com.livetl.android.data.chat.ChatMessage
import com.livetl.android.data.chat.ChatMessageContent
Expand All @@ -37,13 +36,12 @@ import com.livetl.android.ui.common.SymbolAnnotationType
import com.livetl.android.ui.common.textParser
import com.livetl.android.util.toDebugTimestampString
import com.livetl.android.util.toTimestampString
import com.livetl.android.vm.PlayerViewModel

@Composable
fun MinimalMessage(
modifier: Modifier = Modifier,
message: ChatMessage,
playerViewModel: PlayerViewModel = viewModel(),
emojiCache: EmojiCache,
) {
val text = buildAnnotatedString {
CompositionLocalProvider(LocalAuthorNameColor provides LocalContentColor.current) {
Expand All @@ -56,7 +54,7 @@ fun MinimalMessage(
modifier = modifier.chatPadding(),
text = text,
style = MaterialTheme.typography.body1.copy(color = LocalContentColor.current),
inlineContent = message.getEmoteInlineContent(playerViewModel.getEmojiCache())
inlineContent = message.getEmoteInlineContent(emojiCache)
)
}

Expand All @@ -66,7 +64,7 @@ fun Message(
message: ChatMessage,
showTimestamp: Boolean = false,
debugTimestamp: Boolean = false,
playerViewModel: PlayerViewModel = viewModel(),
emojiCache: EmojiCache,
) {
val textColor = when (message) {
is ChatMessage.RegularChat -> LocalContentColor.current
Expand Down Expand Up @@ -170,7 +168,7 @@ fun Message(
},
text = text,
style = MaterialTheme.typography.body1.copy(color = textColor),
inlineContent = authorPicInlineContent + authorBadgeInlineContent + message.getEmoteInlineContent(playerViewModel.getEmojiCache())
inlineContent = authorPicInlineContent + authorBadgeInlineContent + message.getEmoteInlineContent(emojiCache)
)
}

Expand Down
4 changes: 1 addition & 3 deletions app/src/main/kotlin/com/livetl/android/vm/PlayerViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class PlayerViewModel @Inject constructor(
private val streamService: StreamService,
private val chatService: ChatService,
private val chatFilterService: ChatFilterService,
private val emojiCache: EmojiCache,
val emojiCache: EmojiCache,
val prefs: PreferencesHelper,
) : ViewModel() {

Expand All @@ -28,8 +28,6 @@ class PlayerViewModel @Inject constructor(
var videoAttemptedRetries = 0
var currentSecond: Float = 0f

fun getEmojiCache() = emojiCache

fun clearEmojiCache() {
emojiCache.cache.evictAll()
}
Expand Down

0 comments on commit 2075a3c

Please sign in to comment.