diff --git a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/VideoPlayerState.kt b/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/VideoPlayerState.kt index ddb0abe..7b063ae 100644 --- a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/VideoPlayerState.kt +++ b/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/VideoPlayerState.kt @@ -1,10 +1,10 @@ package com.jik.lib.videoplayer -enum class VideoPlayerState { +sealed interface VideoPlayerState { - INITIAL, - LOADING, - GET_ERROR, - NO_VIDEO, - CAN_PLAY, + object Initial : VideoPlayerState + object Loading : VideoPlayerState + class GetError(val errorMessage: String) : VideoPlayerState + object NoVideo : VideoPlayerState + object CanPlay : VideoPlayerState } \ No newline at end of file diff --git a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/component/error/NoVideoFound.kt b/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/component/error/NoVideoFound.kt deleted file mode 100644 index 0a2437e..0000000 --- a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/component/error/NoVideoFound.kt +++ /dev/null @@ -1,15 +0,0 @@ -package com.jik.lib.videoplayer.component.error - -import androidx.compose.material3.MaterialTheme -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.graphics.Color - -@Composable -fun NoVideoFound() { - Text( - text = "No video found.", - style = MaterialTheme.typography.bodyLarge, - color = Color.White, - ) -} \ No newline at end of file diff --git a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/component/error/NetworkError.kt b/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/ui/ErrorScreen.kt similarity index 67% rename from lib-videoplayer/src/main/java/com/jik/lib/videoplayer/component/error/NetworkError.kt rename to lib-videoplayer/src/main/java/com/jik/lib/videoplayer/ui/ErrorScreen.kt index 77c2062..94ef630 100644 --- a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/component/error/NetworkError.kt +++ b/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/ui/ErrorScreen.kt @@ -1,4 +1,4 @@ -package com.jik.lib.videoplayer.component.error +package com.jik.lib.videoplayer.ui import androidx.compose.foundation.background import androidx.compose.foundation.layout.Arrangement @@ -15,9 +15,10 @@ import androidx.compose.ui.graphics.Color import com.jik.lib.videoplayer.component.VideoPlayerIcons.Refresh @Composable -fun NetworkError( +fun ErrorScreen( modifier: Modifier = Modifier, - onRefreshClick: () -> Unit + errorMessage: String, + onRefreshClick: (() -> Unit)? = null, ) { Column( modifier = modifier @@ -27,20 +28,22 @@ fun NetworkError( horizontalAlignment = Alignment.CenterHorizontally, ) { Text( - text = "Please check network connection.", + text = errorMessage, style = MaterialTheme.typography.bodySmall, color = Color.White, ) - IconButton( - onClick = { - onRefreshClick() - }, - ) { - Icon( - imageVector = Refresh, - contentDescription = "Refresh", - tint = Color.White, - ) + if (onRefreshClick != null) { + IconButton( + onClick = { + onRefreshClick() + }, + ) { + Icon( + imageVector = Refresh, + contentDescription = "Refresh", + tint = Color.White, + ) + } } } } \ No newline at end of file diff --git a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/ui/VideoPlayer.kt b/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/ui/VideoPlayer.kt index afcb6d2..ffd07e4 100644 --- a/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/ui/VideoPlayer.kt +++ b/lib-videoplayer/src/main/java/com/jik/lib/videoplayer/ui/VideoPlayer.kt @@ -13,8 +13,6 @@ import androidx.media3.exoplayer.ExoPlayer import com.jik.lib.videoplayer.VideoPlayerState import com.jik.lib.videoplayer.VideoPlayerUtil import com.jik.lib.videoplayer.VideoPlayerUtil.toStreamUrlOfYouTube -import com.jik.lib.videoplayer.component.error.NetworkError -import com.jik.lib.videoplayer.component.error.NoVideoFound import com.jik.lib.videoplayer.component.thumnail.ThumbnailLoadingWheel import com.jik.lib.videoplayer.component.thumnail.ThumbnailPlayIcon import kotlinx.coroutines.launch @@ -28,14 +26,14 @@ fun VideoPlayer( ) { val context = LocalContext.current - var videoPlayerState by remember { mutableStateOf(VideoPlayerState.INITIAL) } + var videoPlayerState: VideoPlayerState by remember { mutableStateOf(VideoPlayerState.Initial) } val coroutineScope = rememberCoroutineScope() var player: ExoPlayer? by remember { mutableStateOf(null) } val renderListener = VideoPlayerUtil.renderListener { player!!.play() } fun initializePlayer() { if (videoUrl == null) { - videoPlayerState = VideoPlayerState.NO_VIDEO + videoPlayerState = VideoPlayerState.NoVideo return } coroutineScope.launch { @@ -46,7 +44,7 @@ fun VideoPlayer( prepare() } } catch (e: Exception) { - videoPlayerState = VideoPlayerState.GET_ERROR + videoPlayerState = VideoPlayerState.GetError(e.message ?: "Unknown Error") } } } @@ -86,32 +84,33 @@ fun VideoPlayer( contentAlignment = Alignment.Center ) { when (videoPlayerState) { - VideoPlayerState.INITIAL -> { + is VideoPlayerState.Initial -> { Thumbnail() ThumbnailPlayIcon { - videoPlayerState = VideoPlayerState.LOADING + videoPlayerState = VideoPlayerState.Loading } } - VideoPlayerState.LOADING -> { + is VideoPlayerState.Loading -> { Thumbnail() ThumbnailLoadingWheel() if (player != null) { - videoPlayerState = VideoPlayerState.CAN_PLAY + videoPlayerState = VideoPlayerState.CanPlay } } - VideoPlayerState.CAN_PLAY -> { + is VideoPlayerState.CanPlay -> { VideoPlayerScreen(player = player ?: return) } - VideoPlayerState.GET_ERROR -> { - NetworkError( + is VideoPlayerState.GetError -> { + ErrorScreen( + errorMessage = (videoPlayerState as VideoPlayerState.GetError).errorMessage, onRefreshClick = { initializePlayer() - videoPlayerState = VideoPlayerState.LOADING + videoPlayerState = VideoPlayerState.Loading } ) } - VideoPlayerState.NO_VIDEO -> { - NoVideoFound() + is VideoPlayerState.NoVideo -> { + ErrorScreen(errorMessage = "No Video Found") } } }