mirror of
https://github.com/JustinZeus/Wholphin.git
synced 2026-07-08 23:51:21 +02:00
Refactor slideshow to use new player creation
This commit is contained in:
parent
271cb0ca20
commit
fc5babac45
2 changed files with 464 additions and 435 deletions
|
|
@ -67,6 +67,7 @@ import com.github.damontecres.wholphin.ui.playback.isDirectionalDpad
|
|||
import com.github.damontecres.wholphin.ui.playback.isDpad
|
||||
import com.github.damontecres.wholphin.ui.playback.isEnterKey
|
||||
import com.github.damontecres.wholphin.ui.tryRequestFocus
|
||||
import com.github.damontecres.wholphin.util.LoadingState
|
||||
import org.jellyfin.sdk.model.api.MediaType
|
||||
import timber.log.Timber
|
||||
import kotlin.math.abs
|
||||
|
|
@ -88,413 +89,429 @@ fun SlideshowPage(
|
|||
),
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val loading by viewModel.loading.collectAsState()
|
||||
|
||||
val loadingState by viewModel.loadingState.observeAsState(ImageLoadingState.Loading)
|
||||
val imageFilter by viewModel.imageFilter.observeAsState(VideoFilter())
|
||||
val position by viewModel.position.observeAsState(0)
|
||||
val pager by viewModel.pager.observeAsState()
|
||||
val imageState by viewModel.image.observeAsState()
|
||||
|
||||
var zoomFactor by rememberSaveable { mutableFloatStateOf(1f) }
|
||||
val isZoomed = zoomFactor * 100 > 102
|
||||
var rotation by rememberSaveable { mutableFloatStateOf(0f) }
|
||||
var showOverlay by rememberSaveable { mutableStateOf(false) }
|
||||
var showFilterDialog by rememberSaveable { mutableStateOf(false) }
|
||||
var panX by rememberSaveable { mutableFloatStateOf(0f) }
|
||||
var panY by rememberSaveable { mutableFloatStateOf(0f) }
|
||||
|
||||
val slideshowControls =
|
||||
object : SlideshowControls {
|
||||
override fun startSlideshow() {
|
||||
showOverlay = false
|
||||
viewModel.startSlideshow()
|
||||
}
|
||||
|
||||
override fun stopSlideshow() {
|
||||
viewModel.stopSlideshow()
|
||||
}
|
||||
when (val st = loading) {
|
||||
is LoadingState.Error -> {
|
||||
ErrorMessage(st, modifier)
|
||||
}
|
||||
|
||||
val rotateAnimation: Float by animateFloatAsState(
|
||||
targetValue = rotation,
|
||||
label = "image_rotation",
|
||||
)
|
||||
val zoomAnimation: Float by animateFloatAsState(
|
||||
targetValue = zoomFactor,
|
||||
label = "image_zoom",
|
||||
)
|
||||
val panXAnimation: Float by animateFloatAsState(
|
||||
targetValue = panX,
|
||||
label = "image_panX",
|
||||
)
|
||||
val panYAnimation: Float by animateFloatAsState(
|
||||
targetValue = panY,
|
||||
label = "image_panY",
|
||||
)
|
||||
|
||||
val slideshowState by viewModel.slideshow.collectAsState()
|
||||
val slideshowActive by viewModel.slideshowActive.collectAsState(false)
|
||||
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
|
||||
LaunchedEffect(Unit) {
|
||||
focusRequester.tryRequestFocus()
|
||||
}
|
||||
|
||||
val density = LocalDensity.current
|
||||
val screenHeight = LocalWindowInfo.current.containerSize.height
|
||||
val screenWidth = LocalWindowInfo.current.containerSize.width
|
||||
|
||||
val maxPanX = screenWidth * .75f
|
||||
val maxPanY = screenHeight * .75f
|
||||
|
||||
fun reset(resetRotate: Boolean) {
|
||||
zoomFactor = 1f
|
||||
panX = 0f
|
||||
panY = 0f
|
||||
if (resetRotate) rotation = 0f
|
||||
}
|
||||
|
||||
fun pan(
|
||||
xFactor: Int,
|
||||
yFactor: Int,
|
||||
) {
|
||||
if (xFactor != 0) {
|
||||
panX = (panX + with(density) { xFactor.dp.toPx() }).coerceIn(-maxPanX, maxPanX)
|
||||
LoadingState.Loading,
|
||||
LoadingState.Pending,
|
||||
-> {
|
||||
LoadingPage(modifier)
|
||||
}
|
||||
if (yFactor != 0) {
|
||||
panY = (panY + with(density) { yFactor.dp.toPx() }).coerceIn(-maxPanY, maxPanY)
|
||||
}
|
||||
}
|
||||
|
||||
fun zoom(factor: Float) {
|
||||
if (factor < 0) {
|
||||
val diffFactor = factor / (zoomFactor - 1f)
|
||||
// zooming out
|
||||
val panXDiff = abs(panX * diffFactor)
|
||||
val panYDiff = abs(panY * diffFactor)
|
||||
if (DEBUG) {
|
||||
Timber.d(
|
||||
"zoomFactor=$zoomFactor, factor=$factor, panX=$panX, panY=$panY, panXDiff=$panXDiff, panYDiff=$panYDiff",
|
||||
)
|
||||
}
|
||||
if (panX > 0f) {
|
||||
panX -= panXDiff
|
||||
} else if (panX < 0f) {
|
||||
panX += panXDiff
|
||||
}
|
||||
if (panY > 0f) {
|
||||
panY -= panYDiff
|
||||
} else if (panY < 0f) {
|
||||
panY += panYDiff
|
||||
}
|
||||
}
|
||||
zoomFactor = (zoomFactor + factor).coerceIn(1f, 5f)
|
||||
if (!isZoomed) {
|
||||
// Always reset if not zoomed
|
||||
panX = 0f
|
||||
panY = 0f
|
||||
}
|
||||
}
|
||||
LoadingState.Success -> {
|
||||
val loadingState by viewModel.loadingState.observeAsState(ImageLoadingState.Loading)
|
||||
val imageFilter by viewModel.imageFilter.observeAsState(VideoFilter())
|
||||
val position by viewModel.position.observeAsState(0)
|
||||
val pager by viewModel.pager.observeAsState()
|
||||
val imageState by viewModel.image.observeAsState()
|
||||
|
||||
LaunchedEffect(imageState) {
|
||||
reset(true)
|
||||
}
|
||||
val player = viewModel.player
|
||||
val presentationState = rememberPresentationState(player)
|
||||
LaunchedEffect(slideshowActive) {
|
||||
player.repeatMode =
|
||||
if (slideshowState.enabled) Player.REPEAT_MODE_OFF else Player.REPEAT_MODE_ONE
|
||||
}
|
||||
var zoomFactor by rememberSaveable { mutableFloatStateOf(1f) }
|
||||
val isZoomed = zoomFactor * 100 > 102
|
||||
var rotation by rememberSaveable { mutableFloatStateOf(0f) }
|
||||
var showOverlay by rememberSaveable { mutableStateOf(false) }
|
||||
var showFilterDialog by rememberSaveable { mutableStateOf(false) }
|
||||
var panX by rememberSaveable { mutableFloatStateOf(0f) }
|
||||
var panY by rememberSaveable { mutableFloatStateOf(0f) }
|
||||
|
||||
var longPressing by remember { mutableStateOf(false) }
|
||||
val slideshowControls =
|
||||
object : SlideshowControls {
|
||||
override fun startSlideshow() {
|
||||
showOverlay = false
|
||||
viewModel.startSlideshow()
|
||||
}
|
||||
|
||||
val contentModifier =
|
||||
Modifier
|
||||
.clickable(
|
||||
interactionSource = null,
|
||||
indication = null,
|
||||
onClick = {
|
||||
showOverlay = !showOverlay
|
||||
},
|
||||
override fun stopSlideshow() {
|
||||
viewModel.stopSlideshow()
|
||||
}
|
||||
}
|
||||
|
||||
val rotateAnimation: Float by animateFloatAsState(
|
||||
targetValue = rotation,
|
||||
label = "image_rotation",
|
||||
)
|
||||
val zoomAnimation: Float by animateFloatAsState(
|
||||
targetValue = zoomFactor,
|
||||
label = "image_zoom",
|
||||
)
|
||||
val panXAnimation: Float by animateFloatAsState(
|
||||
targetValue = panX,
|
||||
label = "image_panX",
|
||||
)
|
||||
val panYAnimation: Float by animateFloatAsState(
|
||||
targetValue = panY,
|
||||
label = "image_panY",
|
||||
)
|
||||
|
||||
Box(
|
||||
modifier =
|
||||
modifier
|
||||
.background(Color.Black)
|
||||
.focusRequester(focusRequester)
|
||||
.focusable()
|
||||
.onKeyEvent {
|
||||
val isOverlayShowing = showOverlay || showFilterDialog
|
||||
var result = false
|
||||
if (!isOverlayShowing) {
|
||||
if (longPressing && it.type == KeyEventType.KeyUp) {
|
||||
// User stopped long pressing, so cancel the zooming action, but still consume the event so it doesn't move the image
|
||||
longPressing = false
|
||||
return@onKeyEvent true
|
||||
}
|
||||
longPressing =
|
||||
it.nativeKeyEvent.isLongPress ||
|
||||
it.nativeKeyEvent.repeatCount > 0
|
||||
if (longPressing) {
|
||||
when (it.key) {
|
||||
Key.DirectionUp -> zoom(.05f)
|
||||
Key.DirectionDown -> zoom(-.05f)
|
||||
val slideshowState by viewModel.slideshow.collectAsState()
|
||||
val slideshowActive by viewModel.slideshowActive.collectAsState(false)
|
||||
|
||||
// These work, but feel awkward because Up/Down zoom, so you can't long press them to pan
|
||||
// Key.DirectionLeft -> panX += with(density) { 15.dp.toPx() }
|
||||
// Key.DirectionRight -> panX -= with(density) { 15.dp.toPx() }
|
||||
}
|
||||
return@onKeyEvent true
|
||||
}
|
||||
}
|
||||
if (it.type != KeyEventType.KeyUp) {
|
||||
result = false
|
||||
} else if (!isOverlayShowing && isZoomed && isDirectionalDpad(it)) {
|
||||
// Image is zoomed in
|
||||
when (it.key) {
|
||||
Key.DirectionLeft -> pan(30, 0)
|
||||
Key.DirectionRight -> pan(-30, 0)
|
||||
Key.DirectionUp -> pan(0, 30)
|
||||
Key.DirectionDown -> pan(0, -30)
|
||||
}
|
||||
result = true
|
||||
} else if (!isOverlayShowing && isZoomed && it.key == Key.Back) {
|
||||
reset(false)
|
||||
result = true
|
||||
} else if (!isOverlayShowing && (it.key == Key.DirectionLeft || it.key == Key.DirectionRight)) {
|
||||
when (it.key) {
|
||||
Key.DirectionLeft, Key.DirectionUpLeft, Key.DirectionDownLeft -> {
|
||||
if (!viewModel.previousImage()) {
|
||||
Toast
|
||||
.makeText(
|
||||
context,
|
||||
R.string.slideshow_at_beginning,
|
||||
Toast.LENGTH_SHORT,
|
||||
).show()
|
||||
}
|
||||
}
|
||||
val focusRequester = remember { FocusRequester() }
|
||||
|
||||
Key.DirectionRight, Key.DirectionUpRight, Key.DirectionDownRight -> {
|
||||
if (!viewModel.nextImage()) {
|
||||
Toast
|
||||
.makeText(
|
||||
context,
|
||||
R.string.no_more_images,
|
||||
Toast.LENGTH_SHORT,
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isOverlayShowing && it.key == Key.Back) {
|
||||
showOverlay = false
|
||||
viewModel.unpauseSlideshow()
|
||||
result = true
|
||||
} else if (!isOverlayShowing && (isDpad(it) || isEnterKey(it))) {
|
||||
showOverlay = true
|
||||
viewModel.pauseSlideshow()
|
||||
result = true
|
||||
}
|
||||
if (result) {
|
||||
// Handled the key, so reset the slideshow timer
|
||||
viewModel.pulseSlideshow()
|
||||
}
|
||||
result
|
||||
},
|
||||
) {
|
||||
when (loadingState) {
|
||||
ImageLoadingState.Error -> {
|
||||
ErrorMessage("Error loading image", null, modifier)
|
||||
LaunchedEffect(Unit) {
|
||||
focusRequester.tryRequestFocus()
|
||||
}
|
||||
|
||||
ImageLoadingState.Loading -> {
|
||||
LoadingPage(modifier)
|
||||
val density = LocalDensity.current
|
||||
val screenHeight = LocalWindowInfo.current.containerSize.height
|
||||
val screenWidth = LocalWindowInfo.current.containerSize.width
|
||||
|
||||
val maxPanX = screenWidth * .75f
|
||||
val maxPanY = screenHeight * .75f
|
||||
|
||||
fun reset(resetRotate: Boolean) {
|
||||
zoomFactor = 1f
|
||||
panX = 0f
|
||||
panY = 0f
|
||||
if (resetRotate) rotation = 0f
|
||||
}
|
||||
|
||||
is ImageLoadingState.Success -> {
|
||||
imageState?.let { imageState ->
|
||||
if (imageState.image.data.mediaType == MediaType.VIDEO) {
|
||||
LaunchedEffect(imageState.id) {
|
||||
val mediaItem =
|
||||
MediaItem
|
||||
.Builder()
|
||||
.setUri(imageState.url)
|
||||
.build()
|
||||
player.setMediaItem(mediaItem)
|
||||
player.repeatMode =
|
||||
if (slideshowState.enabled) {
|
||||
Player.REPEAT_MODE_OFF
|
||||
} else {
|
||||
Player.REPEAT_MODE_ONE
|
||||
}
|
||||
player.prepare()
|
||||
player.play()
|
||||
viewModel.pulseSlideshow(Long.MAX_VALUE)
|
||||
}
|
||||
LifecycleStartEffect(Unit) {
|
||||
onStopOrDispose {
|
||||
player.stop()
|
||||
}
|
||||
}
|
||||
val contentScale = ContentScale.Fit
|
||||
val scaledModifier =
|
||||
contentModifier.resizeWithContentScale(
|
||||
contentScale,
|
||||
presentationState.videoSizeDp,
|
||||
)
|
||||
PlayerSurface(
|
||||
player = player,
|
||||
surfaceType = SURFACE_TYPE_SURFACE_VIEW,
|
||||
modifier =
|
||||
scaledModifier
|
||||
.fillMaxSize()
|
||||
.graphicsLayer {
|
||||
scaleX = zoomAnimation
|
||||
scaleY = zoomAnimation
|
||||
translationX = panXAnimation
|
||||
translationY = panYAnimation
|
||||
}.rotate(rotateAnimation),
|
||||
fun pan(
|
||||
xFactor: Int,
|
||||
yFactor: Int,
|
||||
) {
|
||||
if (xFactor != 0) {
|
||||
panX = (panX + with(density) { xFactor.dp.toPx() }).coerceIn(-maxPanX, maxPanX)
|
||||
}
|
||||
if (yFactor != 0) {
|
||||
panY = (panY + with(density) { yFactor.dp.toPx() }).coerceIn(-maxPanY, maxPanY)
|
||||
}
|
||||
}
|
||||
|
||||
fun zoom(factor: Float) {
|
||||
if (factor < 0) {
|
||||
val diffFactor = factor / (zoomFactor - 1f)
|
||||
// zooming out
|
||||
val panXDiff = abs(panX * diffFactor)
|
||||
val panYDiff = abs(panY * diffFactor)
|
||||
if (DEBUG) {
|
||||
Timber.d(
|
||||
"zoomFactor=$zoomFactor, factor=$factor, panX=$panX, panY=$panY, panXDiff=$panXDiff, panYDiff=$panYDiff",
|
||||
)
|
||||
if (presentationState.coverSurface) {
|
||||
Box(
|
||||
Modifier
|
||||
.matchParentSize()
|
||||
.background(Color.Black),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
val colorFilter =
|
||||
remember(imageState.id, imageFilter) {
|
||||
if (imageFilter.hasImageFilter()) {
|
||||
ColorMatrixColorFilter(imageFilter.colorMatrix)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
if (panX > 0f) {
|
||||
panX -= panXDiff
|
||||
} else if (panX < 0f) {
|
||||
panX += panXDiff
|
||||
}
|
||||
if (panY > 0f) {
|
||||
panY -= panYDiff
|
||||
} else if (panY < 0f) {
|
||||
panY += panYDiff
|
||||
}
|
||||
}
|
||||
zoomFactor = (zoomFactor + factor).coerceIn(1f, 5f)
|
||||
if (!isZoomed) {
|
||||
// Always reset if not zoomed
|
||||
panX = 0f
|
||||
panY = 0f
|
||||
}
|
||||
}
|
||||
|
||||
LaunchedEffect(imageState) {
|
||||
reset(true)
|
||||
}
|
||||
val player = viewModel.player
|
||||
val presentationState = rememberPresentationState(player)
|
||||
LaunchedEffect(slideshowActive) {
|
||||
player.repeatMode =
|
||||
if (slideshowState.enabled) Player.REPEAT_MODE_OFF else Player.REPEAT_MODE_ONE
|
||||
}
|
||||
|
||||
var longPressing by remember { mutableStateOf(false) }
|
||||
|
||||
val contentModifier =
|
||||
Modifier
|
||||
.clickable(
|
||||
interactionSource = null,
|
||||
indication = null,
|
||||
onClick = {
|
||||
showOverlay = !showOverlay
|
||||
},
|
||||
)
|
||||
|
||||
Box(
|
||||
modifier =
|
||||
modifier
|
||||
.background(Color.Black)
|
||||
.focusRequester(focusRequester)
|
||||
.focusable()
|
||||
.onKeyEvent {
|
||||
val isOverlayShowing = showOverlay || showFilterDialog
|
||||
var result = false
|
||||
if (!isOverlayShowing) {
|
||||
if (longPressing && it.type == KeyEventType.KeyUp) {
|
||||
// User stopped long pressing, so cancel the zooming action, but still consume the event so it doesn't move the image
|
||||
longPressing = false
|
||||
return@onKeyEvent true
|
||||
}
|
||||
longPressing =
|
||||
it.nativeKeyEvent.isLongPress ||
|
||||
it.nativeKeyEvent.repeatCount > 0
|
||||
if (longPressing) {
|
||||
when (it.key) {
|
||||
Key.DirectionUp -> zoom(.05f)
|
||||
Key.DirectionDown -> zoom(-.05f)
|
||||
|
||||
// These work, but feel awkward because Up/Down zoom, so you can't long press them to pan
|
||||
// Key.DirectionLeft -> panX += with(density) { 15.dp.toPx() }
|
||||
// Key.DirectionRight -> panX -= with(density) { 15.dp.toPx() }
|
||||
}
|
||||
return@onKeyEvent true
|
||||
}
|
||||
}
|
||||
// If the image loading is large, show the thumbnail while waiting
|
||||
// TODO
|
||||
val showLoadingThumbnail = true
|
||||
SubcomposeAsyncImage(
|
||||
if (it.type != KeyEventType.KeyUp) {
|
||||
result = false
|
||||
} else if (!isOverlayShowing && isZoomed && isDirectionalDpad(it)) {
|
||||
// Image is zoomed in
|
||||
when (it.key) {
|
||||
Key.DirectionLeft -> pan(30, 0)
|
||||
Key.DirectionRight -> pan(-30, 0)
|
||||
Key.DirectionUp -> pan(0, 30)
|
||||
Key.DirectionDown -> pan(0, -30)
|
||||
}
|
||||
result = true
|
||||
} else if (!isOverlayShowing && isZoomed && it.key == Key.Back) {
|
||||
reset(false)
|
||||
result = true
|
||||
} else if (!isOverlayShowing && (it.key == Key.DirectionLeft || it.key == Key.DirectionRight)) {
|
||||
when (it.key) {
|
||||
Key.DirectionLeft, Key.DirectionUpLeft, Key.DirectionDownLeft -> {
|
||||
if (!viewModel.previousImage()) {
|
||||
Toast
|
||||
.makeText(
|
||||
context,
|
||||
R.string.slideshow_at_beginning,
|
||||
Toast.LENGTH_SHORT,
|
||||
).show()
|
||||
}
|
||||
}
|
||||
|
||||
Key.DirectionRight, Key.DirectionUpRight, Key.DirectionDownRight -> {
|
||||
if (!viewModel.nextImage()) {
|
||||
Toast
|
||||
.makeText(
|
||||
context,
|
||||
R.string.no_more_images,
|
||||
Toast.LENGTH_SHORT,
|
||||
).show()
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (isOverlayShowing && it.key == Key.Back) {
|
||||
showOverlay = false
|
||||
viewModel.unpauseSlideshow()
|
||||
result = true
|
||||
} else if (!isOverlayShowing && (isDpad(it) || isEnterKey(it))) {
|
||||
showOverlay = true
|
||||
viewModel.pauseSlideshow()
|
||||
result = true
|
||||
}
|
||||
if (result) {
|
||||
// Handled the key, so reset the slideshow timer
|
||||
viewModel.pulseSlideshow()
|
||||
}
|
||||
result
|
||||
},
|
||||
) {
|
||||
when (loadingState) {
|
||||
ImageLoadingState.Error -> {
|
||||
ErrorMessage("Error loading image", null, modifier)
|
||||
}
|
||||
|
||||
ImageLoadingState.Loading -> {
|
||||
LoadingPage(modifier)
|
||||
}
|
||||
|
||||
is ImageLoadingState.Success -> {
|
||||
imageState?.let { imageState ->
|
||||
if (imageState.image.data.mediaType == MediaType.VIDEO) {
|
||||
LaunchedEffect(imageState.id) {
|
||||
val mediaItem =
|
||||
MediaItem
|
||||
.Builder()
|
||||
.setUri(imageState.url)
|
||||
.build()
|
||||
player.setMediaItem(mediaItem)
|
||||
player.repeatMode =
|
||||
if (slideshowState.enabled) {
|
||||
Player.REPEAT_MODE_OFF
|
||||
} else {
|
||||
Player.REPEAT_MODE_ONE
|
||||
}
|
||||
player.prepare()
|
||||
player.play()
|
||||
viewModel.pulseSlideshow(Long.MAX_VALUE)
|
||||
}
|
||||
LifecycleStartEffect(Unit) {
|
||||
onStopOrDispose {
|
||||
player.stop()
|
||||
}
|
||||
}
|
||||
val contentScale = ContentScale.Fit
|
||||
val scaledModifier =
|
||||
contentModifier.resizeWithContentScale(
|
||||
contentScale,
|
||||
presentationState.videoSizeDp,
|
||||
)
|
||||
PlayerSurface(
|
||||
player = player,
|
||||
surfaceType = SURFACE_TYPE_SURFACE_VIEW,
|
||||
modifier =
|
||||
scaledModifier
|
||||
.fillMaxSize()
|
||||
.graphicsLayer {
|
||||
scaleX = zoomAnimation
|
||||
scaleY = zoomAnimation
|
||||
translationX = panXAnimation
|
||||
translationY = panYAnimation
|
||||
}.rotate(rotateAnimation),
|
||||
)
|
||||
if (presentationState.coverSurface) {
|
||||
Box(
|
||||
Modifier
|
||||
.matchParentSize()
|
||||
.background(Color.Black),
|
||||
)
|
||||
}
|
||||
} else {
|
||||
val colorFilter =
|
||||
remember(imageState.id, imageFilter) {
|
||||
if (imageFilter.hasImageFilter()) {
|
||||
ColorMatrixColorFilter(imageFilter.colorMatrix)
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
// If the image loading is large, show the thumbnail while waiting
|
||||
// TODO
|
||||
val showLoadingThumbnail = true
|
||||
SubcomposeAsyncImage(
|
||||
modifier =
|
||||
contentModifier
|
||||
.fillMaxSize()
|
||||
.graphicsLayer {
|
||||
scaleX = zoomAnimation
|
||||
scaleY = zoomAnimation
|
||||
translationX = panXAnimation
|
||||
translationY = panYAnimation
|
||||
|
||||
val xTransform =
|
||||
(screenWidth - panXAnimation) / (screenWidth * 2)
|
||||
val yTransform =
|
||||
(screenHeight - panYAnimation) / (screenHeight * 2)
|
||||
if (DEBUG) {
|
||||
Timber.d(
|
||||
"graphicsLayer: xTransform=$xTransform, yTransform=$yTransform",
|
||||
)
|
||||
}
|
||||
|
||||
transformOrigin =
|
||||
TransformOrigin(xTransform, yTransform)
|
||||
}.rotate(rotateAnimation),
|
||||
model =
|
||||
ImageRequest
|
||||
.Builder(LocalContext.current)
|
||||
.data(imageState.url)
|
||||
.size(Size.ORIGINAL)
|
||||
.crossfade(!showLoadingThumbnail)
|
||||
.build(),
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Fit,
|
||||
colorFilter = colorFilter,
|
||||
error = {
|
||||
Text(
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center),
|
||||
text = "Error loading image",
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
)
|
||||
},
|
||||
loading = {
|
||||
ImageLoadingPlaceholder(
|
||||
thumbnailUrl = imageState.thumbnailUrl,
|
||||
showThumbnail = showLoadingThumbnail,
|
||||
colorFilter = colorFilter,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
)
|
||||
},
|
||||
// Ensure that if an image takes a long time to load, it won't be skipped
|
||||
onLoading = {
|
||||
viewModel.pulseSlideshow(Long.MAX_VALUE)
|
||||
},
|
||||
onSuccess = {
|
||||
viewModel.pulseSlideshow()
|
||||
},
|
||||
onError = {
|
||||
Timber.e(
|
||||
it.result.throwable,
|
||||
"Error loading image ${imageState.id}",
|
||||
)
|
||||
Toast
|
||||
.makeText(
|
||||
context,
|
||||
"Error loading image: ${it.result.throwable.localizedMessage}",
|
||||
Toast.LENGTH_LONG,
|
||||
).show()
|
||||
viewModel.pulseSlideshow()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
AnimatedVisibility(
|
||||
showOverlay,
|
||||
enter = slideInVertically { it },
|
||||
exit = slideOutVertically { it },
|
||||
modifier = Modifier.align(Alignment.BottomStart),
|
||||
) {
|
||||
imageState?.let { imageState ->
|
||||
ImageOverlay(
|
||||
modifier =
|
||||
contentModifier
|
||||
.fillMaxSize()
|
||||
.graphicsLayer {
|
||||
scaleX = zoomAnimation
|
||||
scaleY = zoomAnimation
|
||||
translationX = panXAnimation
|
||||
translationY = panYAnimation
|
||||
|
||||
val xTransform =
|
||||
(screenWidth - panXAnimation) / (screenWidth * 2)
|
||||
val yTransform =
|
||||
(screenHeight - panYAnimation) / (screenHeight * 2)
|
||||
if (DEBUG) {
|
||||
Timber.d(
|
||||
"graphicsLayer: xTransform=$xTransform, yTransform=$yTransform",
|
||||
)
|
||||
}
|
||||
|
||||
transformOrigin = TransformOrigin(xTransform, yTransform)
|
||||
}.rotate(rotateAnimation),
|
||||
model =
|
||||
ImageRequest
|
||||
.Builder(LocalContext.current)
|
||||
.data(imageState.url)
|
||||
.size(Size.ORIGINAL)
|
||||
.crossfade(!showLoadingThumbnail)
|
||||
.build(),
|
||||
contentDescription = null,
|
||||
contentScale = ContentScale.Fit,
|
||||
colorFilter = colorFilter,
|
||||
error = {
|
||||
Text(
|
||||
modifier =
|
||||
Modifier
|
||||
.align(Alignment.Center),
|
||||
text = "Error loading image",
|
||||
color = MaterialTheme.colorScheme.onBackground,
|
||||
)
|
||||
},
|
||||
loading = {
|
||||
ImageLoadingPlaceholder(
|
||||
thumbnailUrl = imageState.thumbnailUrl,
|
||||
showThumbnail = showLoadingThumbnail,
|
||||
colorFilter = colorFilter,
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
)
|
||||
},
|
||||
// Ensure that if an image takes a long time to load, it won't be skipped
|
||||
onLoading = {
|
||||
viewModel.pulseSlideshow(Long.MAX_VALUE)
|
||||
},
|
||||
onSuccess = {
|
||||
viewModel.pulseSlideshow()
|
||||
},
|
||||
onError = {
|
||||
Timber.e(
|
||||
it.result.throwable,
|
||||
"Error loading image ${imageState.id}",
|
||||
)
|
||||
Toast
|
||||
.makeText(
|
||||
context,
|
||||
"Error loading image: ${it.result.throwable.localizedMessage}",
|
||||
Toast.LENGTH_LONG,
|
||||
).show()
|
||||
viewModel.pulseSlideshow()
|
||||
.fillMaxWidth()
|
||||
.background(AppColors.TransparentBlack50),
|
||||
onDismiss = { showOverlay = false },
|
||||
player = player,
|
||||
slideshowControls = slideshowControls,
|
||||
slideshowEnabled = slideshowState.enabled,
|
||||
image = imageState,
|
||||
position = position,
|
||||
count = pager?.size ?: -1,
|
||||
onClickItem = {},
|
||||
onLongClickItem = {},
|
||||
onZoom = ::zoom,
|
||||
onRotate = { rotation += it },
|
||||
onReset = { reset(true) },
|
||||
onShowFilterDialogClick = {
|
||||
showFilterDialog = true
|
||||
showOverlay = false
|
||||
viewModel.pauseSlideshow()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
AnimatedVisibility(showFilterDialog) {
|
||||
ImageFilterDialog(
|
||||
filter = imageFilter,
|
||||
showVideoOptions = false,
|
||||
showSaveGalleryButton = true,
|
||||
onChange = viewModel::updateImageFilter,
|
||||
onClickSave = viewModel::saveImageFilter,
|
||||
onClickSaveGallery = viewModel::saveGalleryFilter,
|
||||
onDismissRequest = {
|
||||
showFilterDialog = false
|
||||
viewModel.unpauseSlideshow()
|
||||
viewModel.pulseSlideshow()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
AnimatedVisibility(
|
||||
showOverlay,
|
||||
enter = slideInVertically { it },
|
||||
exit = slideOutVertically { it },
|
||||
modifier = Modifier.align(Alignment.BottomStart),
|
||||
) {
|
||||
imageState?.let { imageState ->
|
||||
ImageOverlay(
|
||||
modifier =
|
||||
contentModifier
|
||||
.fillMaxWidth()
|
||||
.background(AppColors.TransparentBlack50),
|
||||
onDismiss = { showOverlay = false },
|
||||
player = player,
|
||||
slideshowControls = slideshowControls,
|
||||
slideshowEnabled = slideshowState.enabled,
|
||||
image = imageState,
|
||||
position = position,
|
||||
count = pager?.size ?: -1,
|
||||
onClickItem = {},
|
||||
onLongClickItem = {},
|
||||
onZoom = ::zoom,
|
||||
onRotate = { rotation += it },
|
||||
onReset = { reset(true) },
|
||||
onShowFilterDialogClick = {
|
||||
showFilterDialog = true
|
||||
showOverlay = false
|
||||
viewModel.pauseSlideshow()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
AnimatedVisibility(showFilterDialog) {
|
||||
ImageFilterDialog(
|
||||
filter = imageFilter,
|
||||
showVideoOptions = false,
|
||||
showSaveGalleryButton = true,
|
||||
onChange = viewModel::updateImageFilter,
|
||||
onClickSave = viewModel::saveImageFilter,
|
||||
onClickSaveGallery = viewModel::saveGalleryFilter,
|
||||
onDismissRequest = {
|
||||
showFilterDialog = false
|
||||
viewModel.unpauseSlideshow()
|
||||
viewModel.pulseSlideshow()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import com.github.damontecres.wholphin.data.model.BaseItem
|
|||
import com.github.damontecres.wholphin.data.model.PlaybackEffect
|
||||
import com.github.damontecres.wholphin.data.model.VideoFilter
|
||||
import com.github.damontecres.wholphin.preferences.AppPreference
|
||||
import com.github.damontecres.wholphin.preferences.PlayerBackend
|
||||
import com.github.damontecres.wholphin.services.ImageUrlService
|
||||
import com.github.damontecres.wholphin.services.PlayerFactory
|
||||
import com.github.damontecres.wholphin.services.ScreensaverService
|
||||
|
|
@ -30,6 +31,7 @@ import com.github.damontecres.wholphin.ui.util.ThrottledLiveData
|
|||
import com.github.damontecres.wholphin.util.ApiRequestPager
|
||||
import com.github.damontecres.wholphin.util.ExceptionHandler
|
||||
import com.github.damontecres.wholphin.util.GetItemsRequestHandler
|
||||
import com.github.damontecres.wholphin.util.LoadingState
|
||||
import dagger.assisted.Assisted
|
||||
import dagger.assisted.AssistedFactory
|
||||
import dagger.assisted.AssistedInject
|
||||
|
|
@ -77,9 +79,8 @@ class SlideshowViewModel
|
|||
fun create(slideshow: Destination.Slideshow): SlideshowViewModel
|
||||
}
|
||||
|
||||
val player by lazy {
|
||||
playerFactory.createVideoPlayer()
|
||||
}
|
||||
lateinit var player: Player
|
||||
private set
|
||||
|
||||
private var saveFilters = true
|
||||
|
||||
|
|
@ -104,6 +105,8 @@ class SlideshowViewModel
|
|||
private val _image = MutableLiveData<ImageState>()
|
||||
val image: LiveData<ImageState> = _image
|
||||
|
||||
val loading = MutableStateFlow<LoadingState>(LoadingState.Pending)
|
||||
|
||||
val loadingState = MutableLiveData<ImageLoadingState>(ImageLoadingState.Loading)
|
||||
private val _imageFilter = MutableLiveData(VideoFilter())
|
||||
val imageFilter = ThrottledLiveData(_imageFilter, 500L)
|
||||
|
|
@ -113,58 +116,67 @@ class SlideshowViewModel
|
|||
init {
|
||||
addCloseable {
|
||||
screensaverService.keepScreenOn(false)
|
||||
player.removeListener(this@SlideshowViewModel)
|
||||
player.release()
|
||||
}
|
||||
player.addListener(this@SlideshowViewModel)
|
||||
viewModelScope.launchIO {
|
||||
val photoPrefs = userPreferencesService.getCurrent().appPreferences.photoPreferences
|
||||
slideshowDelay =
|
||||
photoPrefs.slideshowDuration.takeIf { it >= AppPreference.SlideshowDuration.min }
|
||||
?: AppPreference.SlideshowDuration.defaultValue
|
||||
// val album =
|
||||
// api.userLibraryApi
|
||||
// .getItem(
|
||||
// itemId = slideshowSettings.parentId,
|
||||
// ).content
|
||||
// .let { BaseItem(it, false) }
|
||||
// this@SlideshowViewModel.album.setValueOnMain(album)
|
||||
val includeItemTypes =
|
||||
if (photoPrefs.slideshowPlayVideos) {
|
||||
listOf(BaseItemKind.PHOTO, BaseItemKind.VIDEO)
|
||||
} else {
|
||||
listOf(BaseItemKind.PHOTO)
|
||||
}
|
||||
val request =
|
||||
slideshowSettings.filter.filter.applyTo(
|
||||
GetItemsRequest(
|
||||
parentId = slideshowSettings.parentId,
|
||||
includeItemTypes = includeItemTypes,
|
||||
fields = PhotoItemFields,
|
||||
recursive = true,
|
||||
sortBy = listOf(slideshowSettings.sortAndDirection.sort),
|
||||
sortOrder = listOf(slideshowSettings.sortAndDirection.direction),
|
||||
),
|
||||
)
|
||||
serverRepository.currentUser.value?.let { user ->
|
||||
val filter =
|
||||
playbackEffectDao
|
||||
.getPlaybackEffect(
|
||||
user.rowId,
|
||||
slideshowSettings.parentId,
|
||||
BaseItemKind.PHOTO_ALBUM,
|
||||
)?.videoFilter
|
||||
if (filter != null) {
|
||||
Timber.v("Got filter for album %s", slideshowSettings.parentId)
|
||||
albumImageFilter = filter
|
||||
}
|
||||
if (this@SlideshowViewModel::player.isInitialized) {
|
||||
player.removeListener(this@SlideshowViewModel)
|
||||
player.release()
|
||||
}
|
||||
}
|
||||
viewModelScope.launchIO {
|
||||
try {
|
||||
val appPreferences = userPreferencesService.getCurrent().appPreferences
|
||||
val playerCreation =
|
||||
playerFactory.createVideoPlayer(
|
||||
backend = PlayerBackend.EXO_PLAYER,
|
||||
appPreferences.playbackPreferences,
|
||||
)
|
||||
player = playerCreation.player
|
||||
player.addListener(this@SlideshowViewModel)
|
||||
|
||||
val photoPrefs = appPreferences.photoPreferences
|
||||
slideshowDelay =
|
||||
photoPrefs.slideshowDuration.takeIf { it >= AppPreference.SlideshowDuration.min }
|
||||
?: AppPreference.SlideshowDuration.defaultValue
|
||||
val includeItemTypes =
|
||||
if (photoPrefs.slideshowPlayVideos) {
|
||||
listOf(BaseItemKind.PHOTO, BaseItemKind.VIDEO)
|
||||
} else {
|
||||
listOf(BaseItemKind.PHOTO)
|
||||
}
|
||||
val request =
|
||||
slideshowSettings.filter.filter.applyTo(
|
||||
GetItemsRequest(
|
||||
parentId = slideshowSettings.parentId,
|
||||
includeItemTypes = includeItemTypes,
|
||||
fields = PhotoItemFields,
|
||||
recursive = true,
|
||||
sortBy = listOf(slideshowSettings.sortAndDirection.sort),
|
||||
sortOrder = listOf(slideshowSettings.sortAndDirection.direction),
|
||||
),
|
||||
)
|
||||
serverRepository.currentUser.value?.let { user ->
|
||||
val filter =
|
||||
playbackEffectDao
|
||||
.getPlaybackEffect(
|
||||
user.rowId,
|
||||
slideshowSettings.parentId,
|
||||
BaseItemKind.PHOTO_ALBUM,
|
||||
)?.videoFilter
|
||||
if (filter != null) {
|
||||
Timber.v("Got filter for album %s", slideshowSettings.parentId)
|
||||
albumImageFilter = filter
|
||||
}
|
||||
}
|
||||
val pager =
|
||||
ApiRequestPager(api, request, GetItemsRequestHandler, viewModelScope)
|
||||
.init(slideshowSettings.index)
|
||||
this@SlideshowViewModel._pager.setValueOnMain(pager)
|
||||
loading.update { LoadingState.Success }
|
||||
updatePosition(slideshowSettings.index)?.join()
|
||||
if (slideshowSettings.startSlideshow) onMain { startSlideshow() }
|
||||
} catch (ex: Exception) {
|
||||
Timber.e(ex, "Error")
|
||||
loading.update { LoadingState.Error(ex) }
|
||||
}
|
||||
val pager =
|
||||
ApiRequestPager(api, request, GetItemsRequestHandler, viewModelScope)
|
||||
.init(slideshowSettings.index)
|
||||
this@SlideshowViewModel._pager.setValueOnMain(pager)
|
||||
updatePosition(slideshowSettings.index)?.join()
|
||||
if (slideshowSettings.startSlideshow) onMain { startSlideshow() }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue