Skip to content

Commit

Permalink
fix(Player): SDK 35 adaptation
Browse files Browse the repository at this point in the history
  • Loading branch information
urFate committed Sep 13, 2024
1 parent e9e92b1 commit 86ff1de
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package org.shirabox.app.ui.activity.player
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.annotation.OptIn
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.media3.common.util.UnstableApi
Expand All @@ -34,6 +34,7 @@ class PlayerActivity : ComponentActivity() {

setContent {
ShiraBoxTheme(
transparentStatusBar = true,
darkTheme = false
) {
// A surface container using the 'background' color from the theme
Expand All @@ -45,10 +46,6 @@ class PlayerActivity : ComponentActivity() {
val context = LocalContext.current

rememberSystemUiController().apply {
setStatusBarColor(
color = Color.Transparent,
darkIcons = false
)
Util.hideSystemUi(this)
}

Expand Down Expand Up @@ -81,6 +78,8 @@ class PlayerActivity : ComponentActivity() {
}
}
}

enableEdgeToEdge()
}

override fun onPause() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.material3.BottomSheetDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.ListItem
import androidx.compose.material3.ListItemDefaults
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.SheetState
Expand All @@ -32,17 +33,16 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.media3.exoplayer.ExoPlayer
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.shirabox.app.R
import org.shirabox.core.entity.EpisodeEntity
import org.shirabox.core.model.Quality
import org.shirabox.core.util.Util

@Composable
fun SettingsBottomSheet(exoPlayer: ExoPlayer, playlist: List<EpisodeEntity>, model: PlayerViewModel) {
Expand Down Expand Up @@ -74,8 +74,8 @@ fun SettingsBottomSheet(exoPlayer: ExoPlayer, playlist: List<EpisodeEntity>, mod
* fact that the BottomSheet shows it when opening
*/

val systemUiController = rememberSystemUiController()
Util.hideSystemUi(systemUiController)
// val systemUiController = rememberSystemUiController()
// Util.hideSystemUi(systemUiController)
}

}
Expand All @@ -100,7 +100,7 @@ private fun SettingsOptions(
model.bottomSheetVisibilityState = false
}
},
contentWindowInsets = { BottomSheetDefaults.windowInsets.only(WindowInsetsSides.Bottom) }
contentWindowInsets = { BottomSheetDefaults.windowInsets.only(WindowInsetsSides.Bottom).only(WindowInsetsSides.Top) }
) {
Column(
modifier = Modifier.padding(0.dp, 0.dp, 0.dp, 16.dp)
Expand Down Expand Up @@ -134,7 +134,8 @@ private fun SettingsOptions(
tint = MaterialTheme.colorScheme.primary,
contentDescription = model.currentQuality.quality.toString()
)
}
},
colors = ListItemDefaults.colors(containerColor = Color.Transparent)
)

/*
Expand Down Expand Up @@ -162,7 +163,8 @@ private fun SettingsOptions(
tint = MaterialTheme.colorScheme.primary,
contentDescription = null
)
}
},
colors = ListItemDefaults.colors(containerColor = Color.Transparent)
)

/*
Expand All @@ -183,7 +185,8 @@ private fun SettingsOptions(
tint = MaterialTheme.colorScheme.primary,
contentDescription = null
)
}
},
colors = ListItemDefaults.colors(containerColor = Color.Transparent)
)
}
}
Expand Down Expand Up @@ -237,7 +240,8 @@ fun QualityBottomSheet(
contentDescription = "${it.quality}"
)
},
supportingContent = { Text("${it.quality}p") }
supportingContent = { Text("${it.quality}p") },
colors = ListItemDefaults.colors(containerColor = Color.Transparent)
)
}
}
Expand Down Expand Up @@ -303,7 +307,8 @@ fun PlaybackSpeedBottomSheet(
tint = MaterialTheme.colorScheme.primary,
contentDescription = "${it}x"
)
}
},
colors = ListItemDefaults.colors(containerColor = Color.Transparent)
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import android.app.Activity
import android.content.pm.ActivityInfo
import android.content.res.Configuration
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand Down Expand Up @@ -71,6 +74,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpSize
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
Expand All @@ -89,6 +93,7 @@ import org.shirabox.core.util.Values
fun ControlsScaffold(exoPlayer: ExoPlayer, playlist: List<EpisodeEntity>, model: PlayerViewModel) {

val context = LocalContext.current
val activity = context as Activity
val coroutineScope = rememberCoroutineScope()

var isPlaying by remember { mutableStateOf(exoPlayer.isPlaying) }
Expand Down Expand Up @@ -147,10 +152,6 @@ fun ControlsScaffold(exoPlayer: ExoPlayer, playlist: List<EpisodeEntity>, model:
}
}

val activity = LocalContext.current as Activity

activity.requestedOrientation = model.orientationState

LaunchedEffect(currentMediaItemIndex) {
model.fetchAnimeSkipIntroTimestamps(context = context, episode = currentEpisode)
}
Expand Down Expand Up @@ -273,12 +274,14 @@ fun ControlsScaffold(exoPlayer: ExoPlayer, playlist: List<EpisodeEntity>, model:
@Composable
fun PlayerTopBar(title: String, episode: Int, onBackClick: () -> Unit, onSettingsClick: () -> Unit) {
TopAppBar(
modifier = Modifier.fillMaxWidth(),
modifier = Modifier
.padding(4.dp, 16.dp)
.fillMaxWidth(),
title = {
Column(
modifier = Modifier
.fillMaxWidth()
.padding(0.dp, 8.dp),
.padding(0.dp, 6.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Expand Down Expand Up @@ -499,6 +502,7 @@ fun InstantSeekZones(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PlayerBottomBar(
currentPosition: Long,
Expand All @@ -509,9 +513,15 @@ fun PlayerBottomBar(
) {
var isValueChanging by remember { mutableStateOf(false) }
var currentValue by remember { mutableFloatStateOf(0F) }
var mutablePosition by remember {
mutableLongStateOf(currentPosition)
}
var mutablePosition by remember { mutableLongStateOf(currentPosition) }

val animatedValue by animateFloatAsState(if (isValueChanging) currentValue else (currentPosition.toFloat() / duration.toFloat()),
label = ""
)

val thumbGapSize by animateDpAsState(if (isValueChanging) 8.dp else 0.dp, label = "")
val cornerSize by animateDpAsState(if (isValueChanging) 4.dp else 0.dp, label = "")
val thumbHeight by animateDpAsState(if (isValueChanging) 36.dp else 16.dp, label = "")

Box(
contentAlignment = Alignment.BottomCenter
Expand Down Expand Up @@ -541,10 +551,16 @@ fun PlayerBottomBar(
horizontalArrangement = Arrangement.spacedBy(12.dp),
verticalAlignment = Alignment.CenterVertically
) {
val colors = SliderDefaults.colors(
thumbColor = MaterialTheme.colorScheme.onPrimary,
inactiveTrackColor = Color.Gray.copy(0.4F),
disabledInactiveTrackColor = Color.Gray.copy(0.4F)
)
val interactionSource = remember(::MutableInteractionSource)

Slider(
modifier = Modifier.weight(1f, false),
value = if (isValueChanging) currentValue else (currentPosition.toFloat() / duration.toFloat()),
value = animatedValue,
enabled = duration != C.TIME_UNSET, // Prevent seeking while content is loading
onValueChange = { value ->
mutablePosition = value.times(duration).toLong()
Expand All @@ -556,28 +572,52 @@ fun PlayerBottomBar(
onSliderValueChangeFinish((currentValue * duration).toLong())
isValueChanging = false
},
colors = SliderDefaults.colors(
thumbColor = MaterialTheme.colorScheme.onPrimary,
inactiveTrackColor = Color.Gray.copy(0.6F),
disabledInactiveTrackColor = Color.Gray.copy(0.6F)
)
track = {
SliderDefaults.Track(
colors = colors,
enabled = duration != C.TIME_UNSET,
sliderState = it,
thumbTrackGapSize = thumbGapSize,
trackInsideCornerSize = cornerSize
)
},
thumb = {
SliderDefaults.Thumb(
interactionSource = interactionSource,
colors = colors,
enabled = true,
thumbSize = DpSize(6.dp, thumbHeight)
)
},
colors = colors
)

val activity = LocalContext.current as Activity

val isPortrait = when (activity.requestedOrientation) {
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED,
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT,
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT -> true

else -> false
}

IconButton(
onClick = {
model.orientationState =
if (model.orientationState == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
activity.requestedOrientation =
if (isPortrait) {
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
} else {
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
) {
Icon(
imageVector = if (model.orientationState ==
ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
) Icons.Rounded.Fullscreen
else Icons.Rounded.FullscreenExit,
imageVector = if (isPortrait) {
Icons.Rounded.Fullscreen
} else {
Icons.Rounded.FullscreenExit
},
contentDescription = null,
tint = MaterialTheme.colorScheme.inverseOnSurface
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.shirabox.app.ui.activity.player

import android.content.Context
import android.content.pm.ActivityInfo
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
Expand Down Expand Up @@ -52,7 +50,6 @@ class PlayerViewModel @AssistedInject constructor(
val episodesPositions = mutableStateMapOf<Int, Long>()
var controlsVisibilityState by mutableStateOf(true)
var bottomSheetVisibilityState by mutableStateOf(false)
var orientationState by mutableIntStateOf(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
var currentQuality by mutableStateOf(Quality.HD)
var playbackSpeed by mutableFloatStateOf(1F)
var coldStartSeekApplied by mutableStateOf(false)
Expand Down

0 comments on commit 86ff1de

Please sign in to comment.