Skip to content
This repository has been archived by the owner on Aug 11, 2024. It is now read-only.

Commit

Permalink
Make the video player scrollable
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Aug 15, 2023
1 parent 7f514e6 commit 47ee7db
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 133 deletions.
12 changes: 6 additions & 6 deletions app/src/main/java/app/suhasdissa/memerize/Destination.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ sealed class Destination(val route: String) {
object Subreddits : Destination("subreddits")
object Communities : Destination("communities")
object About : Destination("about")
object PhotoView : Destination("memescreen") {
val routeWithArgs = "$route/{url}"
val arguments = listOf(navArgument("url") { type = NavType.StringType })
object RedditFeed : Destination("reddit_feed") {
val routeWithArgs = "$route/{id}"
val arguments = listOf(navArgument("id") { type = NavType.IntType })
}

object VideoPlayer : Destination("videoplayer") {
val routeWithArgs = "$route/{url}"
val arguments = listOf(navArgument("url") { type = NavType.StringType })
object LemmyFeed : Destination("lemmy_feed") {
val routeWithArgs = "$route/{id}"
val arguments = listOf(navArgument("id") { type = NavType.IntType })
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class MemerizeApplication : Application(), ImageLoaderFactory {
.respectCacheHeaders(false)
.diskCache(
DiskCache.Builder()
.directory(cacheDir.resolve("coil"))
.directory(cacheDir.resolve("image_cache"))
.maxSizeBytes(
preferences.getInt(imageCacheKey, defaultImageCacheSize) * 1024 * 1024L
)
Expand Down
51 changes: 17 additions & 34 deletions app/src/main/java/app/suhasdissa/memerize/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,16 @@ package app.suhasdissa.memerize

import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import app.suhasdissa.memerize.backend.viewmodels.LemmyViewModel
import app.suhasdissa.memerize.backend.viewmodels.PlayerViewModel
import app.suhasdissa.memerize.backend.viewmodels.RedditViewModel
import app.suhasdissa.memerize.ui.screens.home.CommunityScreen
import app.suhasdissa.memerize.ui.screens.home.HomeScreen
import app.suhasdissa.memerize.ui.screens.home.SubredditScreen
import app.suhasdissa.memerize.ui.screens.primary.LemmyMemeScreen
import app.suhasdissa.memerize.ui.screens.primary.RedditMemeScreen
import app.suhasdissa.memerize.ui.screens.secondary.PhotoView
import app.suhasdissa.memerize.ui.screens.secondary.VideoView
import app.suhasdissa.memerize.ui.screens.secondary.LemmyMemeFeed
import app.suhasdissa.memerize.ui.screens.secondary.RedditMemeFeed
import app.suhasdissa.memerize.ui.screens.settings.AboutScreen
import app.suhasdissa.memerize.ui.screens.settings.SettingsScreen

Expand All @@ -32,9 +28,6 @@ fun AppNavHost(
onDrawerOpen: () -> Unit,
modifier: Modifier = Modifier
) {
val redditViewModel: RedditViewModel = viewModel(factory = RedditViewModel.Factory)
val lemmyViewModel: LemmyViewModel = viewModel(factory = LemmyViewModel.Factory)
val playerViewModel: PlayerViewModel = viewModel(factory = PlayerViewModel.Factory)
NavHost(
navController = navController,
startDestination = Destination.Home.route,
Expand All @@ -45,9 +38,7 @@ fun AppNavHost(
onNavigate = { destination ->
navController.navigateTo(destination.route)
},
onDrawerOpen,
redditViewModel = redditViewModel,
lemmyViewModel = lemmyViewModel
onDrawerOpen
)
}
composable(route = Destination.Settings.route) {
Expand All @@ -71,45 +62,37 @@ fun AppNavHost(
route = Destination.RedditMemeView.route
) {
RedditMemeScreen(
redditViewModel = redditViewModel,
onClickMeme = { url ->
navController.navigateTo("${Destination.PhotoView.route}/$url")
},
onClickVideo = { url ->
navController.navigateTo("${Destination.VideoPlayer.route}/$url")
onClickCard = { id ->
navController.navigateTo("${Destination.RedditFeed.route}/$id")
}
)
}
composable(
route = Destination.LemmyMemeView.route
) {
LemmyMemeScreen(
lemmyViewModel = lemmyViewModel,
onClickMeme = { url ->
navController.navigateTo("${Destination.PhotoView.route}/$url")
},
onClickVideo = { url ->
navController.navigateTo("${Destination.VideoPlayer.route}/$url")
onClickCard = { id ->
navController.navigateTo("${Destination.LemmyFeed.route}/$id")
}
)
}
composable(
route = Destination.PhotoView.routeWithArgs,
arguments = Destination.PhotoView.arguments
route = Destination.RedditFeed.routeWithArgs,
arguments = Destination.RedditFeed.arguments
) {
val imgurl = it.arguments?.getString("url")
if (imgurl != null) {
PhotoView(imgurl)
val id = it.arguments?.getInt("id")
if (id != null) {
RedditMemeFeed(initialPage = id)
}
}

composable(
route = Destination.VideoPlayer.routeWithArgs,
arguments = Destination.VideoPlayer.arguments
route = Destination.LemmyFeed.routeWithArgs,
arguments = Destination.LemmyFeed.arguments
) {
val url = it.arguments?.getString("url")
if (url != null) {
VideoView(url = url, playerViewModel = playerViewModel)
val id = it.arguments?.getInt("id")
if (id != null) {
LemmyMemeFeed(initialPage = id)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,16 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import androidx.media3.common.Player
import androidx.media3.common.util.UnstableApi
import androidx.media3.exoplayer.ExoPlayer
import app.suhasdissa.memerize.MemerizeApplication
import app.suhasdissa.memerize.utils.RedditVideoDownloader
import kotlinx.coroutines.launch

@UnstableApi
class PlayerViewModel(appContext: Context) : ViewModel() {
val player: ExoPlayer = ExoPlayer.Builder(appContext).build()

class PlayerViewModel() : ViewModel() {
var downloadState: DownloadState by mutableStateOf(DownloadState.NotStarted)

fun playPause() {
if (player.isPlaying) player.pause() else player.play()
}

fun seekTo(to: Long) {
player.seekTo(to)
}

@RequiresApi(Build.VERSION_CODES.O)
fun downloadVideo(context: Context, url: String) {
viewModelScope.launch {
Expand All @@ -54,14 +40,8 @@ class PlayerViewModel(appContext: Context) : ViewModel() {
}
}
}
}

companion object {
val Factory: ViewModelProvider.Factory = viewModelFactory {
initializer {
val application =
(this[ViewModelProvider.AndroidViewModelFactory.APPLICATION_KEY] as MemerizeApplication) // ktlint-disable max-line-length
PlayerViewModel(application)
}
}
}
fun Player.playPause() {
if (isPlaying) pause() else play()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ All Rights Reserved
package app.suhasdissa.memerize.ui.components

import androidx.compose.runtime.Composable
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

@Composable
fun MemeCard(
onClickMeme: (url: String) -> Unit,
onClickMeme: () -> Unit,
photo: String,
title: String
) {
val encodedImg = URLEncoder.encode(photo, StandardCharsets.UTF_8.toString())

ImageCard({ onClickMeme(encodedImg) }, photo, title)
ImageCard({ onClickMeme.invoke() }, photo, title)
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.foundation.lazy.grid.itemsIndexed
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
Expand All @@ -29,8 +29,7 @@ import app.suhasdissa.memerize.backend.database.entity.Meme
@Composable
fun MemeGrid(
memes: List<Meme>,
onClickMeme: (url: String) -> Unit,
onClickVideo: (url: String) -> Unit
onClickCard: (id: Int) -> Unit
) {
Column(
modifier = Modifier
Expand All @@ -44,11 +43,15 @@ fun MemeGrid(
modifier = Modifier.fillMaxWidth(),
contentPadding = PaddingValues(8.dp)
) {
items(items = memes) { meme ->
itemsIndexed(items = memes) { index, meme ->
if (meme.isVideo) {
VideoCard(onClickVideo, meme.url, meme.title, meme.preview, Modifier)
VideoCard(onClickVideo = {
onClickCard.invoke(index)
}, meme.title, meme.preview, Modifier)
} else {
MemeCard(onClickMeme, meme.url, meme.title)
MemeCard(onClickMeme = {
onClickCard.invoke(index)
}, meme.url, meme.title)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,21 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import java.net.URLEncoder
import java.nio.charset.StandardCharsets

@Composable
fun VideoCard(
onClickVideo: (url: String) -> Unit,
vidlink: String,
onClickVideo: () -> Unit,
title: String,
preview: String,
modifier: Modifier = Modifier
) {
val encodedLink = URLEncoder.encode(vidlink, StandardCharsets.UTF_8.toString())
Box(
contentAlignment = Alignment.Center,
modifier = modifier
.fillMaxSize()
) {
ImageCard({ onClickVideo(encodedLink) }, preview, title)
Card(modifier.clickable(onClick = { onClickVideo(encodedLink) }), shape = CircleShape) {
ImageCard({ onClickVideo.invoke() }, preview, title)
Card(modifier.clickable(onClick = { onClickVideo.invoke() }), shape = CircleShape) {
Icon(
modifier = modifier.size(64.dp),
imageVector = Icons.Default.PlayCircle,
Expand All @@ -55,5 +51,5 @@ fun VideoCard(
@Preview
@Composable
fun VideoCardPreview() {
VideoCard(onClickVideo = {}, vidlink = "", title = "Preview", preview = "")
VideoCard(onClickVideo = {}, title = "Preview", preview = "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All Rights Reserved

package app.suhasdissa.memerize.ui.screens.home

import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
Expand All @@ -27,6 +28,7 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
Expand All @@ -50,8 +52,14 @@ fun HomeScreen(
lemmyCommunityViewModel: LemmyCommunityViewModel = viewModel(
factory = LemmyCommunityViewModel.Factory
),
redditViewModel: RedditViewModel,
lemmyViewModel: LemmyViewModel
redditViewModel: RedditViewModel = viewModel(
LocalContext.current as ComponentActivity,
factory = RedditViewModel.Factory
),
lemmyViewModel: LemmyViewModel = viewModel(
LocalContext.current as ComponentActivity,
factory = LemmyViewModel.Factory
)
) {
val subreddits by redditCommunityViewModel.communities.collectAsState()
val communities by lemmyCommunityViewModel.communities.collectAsState()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All Rights Reserved

package app.suhasdissa.memerize.ui.screens.primary

import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -37,6 +38,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import app.suhasdissa.memerize.R
import app.suhasdissa.memerize.backend.viewmodels.LemmyViewModel
import app.suhasdissa.memerize.backend.viewmodels.state.MemeUiState
Expand All @@ -51,9 +53,11 @@ import coil.request.ImageRequest
@Composable
fun LemmyMemeScreen(
modifier: Modifier = Modifier,
lemmyViewModel: LemmyViewModel,
onClickMeme: (url: String) -> Unit,
onClickVideo: (url: String) -> Unit
lemmyViewModel: LemmyViewModel = viewModel(
LocalContext.current as ComponentActivity,
factory = LemmyViewModel.Factory
),
onClickCard: (Int) -> Unit
) {
var showFilterButtons by remember { mutableStateOf(false) }
Scaffold(
Expand Down Expand Up @@ -107,8 +111,7 @@ fun LemmyMemeScreen(

is MemeUiState.Success -> MemeGrid(
memeDataState.memes,
onClickMeme,
onClickVideo
onClickCard
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All Rights Reserved

package app.suhasdissa.memerize.ui.screens.primary

import androidx.activity.ComponentActivity
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
Expand Down Expand Up @@ -37,6 +38,7 @@ import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import app.suhasdissa.memerize.R
import app.suhasdissa.memerize.backend.viewmodels.RedditViewModel
import app.suhasdissa.memerize.backend.viewmodels.state.MemeUiState
Expand All @@ -51,9 +53,11 @@ import coil.request.ImageRequest
@Composable
fun RedditMemeScreen(
modifier: Modifier = Modifier,
redditViewModel: RedditViewModel,
onClickMeme: (url: String) -> Unit,
onClickVideo: (url: String) -> Unit
redditViewModel: RedditViewModel = viewModel(
LocalContext.current as ComponentActivity,
factory = RedditViewModel.Factory
),
onClickCard: (Int) -> Unit
) {
var showFilterButtons by remember { mutableStateOf(false) }
Scaffold(
Expand Down Expand Up @@ -107,8 +111,7 @@ fun RedditMemeScreen(

is MemeUiState.Success -> MemeGrid(
memeDataState.memes,
onClickMeme,
onClickVideo
onClickCard
)
}
}
Expand Down
Loading

0 comments on commit 47ee7db

Please sign in to comment.