Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
greenart7c3 committed Sep 27, 2023
2 parents e762ad5 + 9b1bd21 commit 40dae87
Show file tree
Hide file tree
Showing 11 changed files with 152 additions and 90 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ android {
applicationId "com.vitorpamplona.amethyst"
minSdk 26
targetSdk 34
versionCode 306
versionName "0.78.0"
versionCode 308
versionName "0.78.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/vitorpamplona/amethyst/VideoCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import java.io.File

@UnstableApi object VideoCache {

var exoPlayerCacheSize: Long = 90 * 1024 * 1024 // 90MB
var exoPlayerCacheSize: Long = 150 * 1024 * 1024 // 90MB

var leastRecentlyUsedCacheEvictor = LeastRecentlyUsedCacheEvictor(exoPlayerCacheSize)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,10 @@ fun NewPostView(
PostButton(
onPost = {
postViewModel.sendPost(relayList = relayList)
onClose()
scope.launch {
delay(100)
onClose()
}
},
isActive = postViewModel.canPost()
)
Expand All @@ -224,7 +227,10 @@ fun NewPostView(
Spacer(modifier = StdHorzSpacer)
CloseButton(onPress = {
postViewModel.cancel()
onClose()
scope.launch {
delay(100)
onClose()
}
})
},
backgroundColor = MaterialTheme.colors.surface,
Expand Down Expand Up @@ -1220,9 +1226,7 @@ private fun MarkAsSensitive(
@Composable
fun CloseButton(onPress: () -> Unit) {
Button(
onClick = {
onPress()
},
onClick = onPress,
shape = ButtonBorder,
colors = ButtonDefaults
.buttonColors(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.vitorpamplona.amethyst.ui.components

import android.content.Context
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.net.Uri
Expand Down Expand Up @@ -206,34 +207,36 @@ fun VideoViewInner(
)
}

val mediaItem = remember(videoUri) {
MediaItem.Builder()
.setMediaId(videoUri)
.setUri(videoUri)
.setMediaMetadata(
MediaMetadata.Builder()
.setArtist(authorName?.ifBlank { null })
.setTitle(title?.ifBlank { null } ?: videoUri)
.setArtworkUri(
try {
if (artworkUri != null) {
Uri.parse(artworkUri)
} else {
null
}
} catch (e: Exception) {
null
}
)
.build()
)
.build()
}

if (!automaticallyStartPlayback.value) {
ImageUrlWithDownloadButton(url = videoUri, showImage = automaticallyStartPlayback)
} else {
VideoPlayerActiveMutex(videoUri) { activeOnScreen ->
val mediaItem = remember(videoUri) {
mutableStateOf(
MediaItem.Builder()
.setMediaId(videoUri)
.setUri(videoUri)
.setMediaMetadata(
MediaMetadata.Builder()
.setArtist(authorName?.ifBlank { null })
.setTitle(title?.ifBlank { null } ?: videoUri)
.setArtworkUri(
try {
if (artworkUri != null) {
Uri.parse(artworkUri)
} else {
null
}
} catch (e: Exception) {
null
}
)
.build()
)
.build()
)
}

GetVideoController(
mediaItem = mediaItem,
videoUri = videoUri,
Expand All @@ -258,7 +261,7 @@ fun VideoViewInner(
@Composable
@OptIn(UnstableApi::class)
fun GetVideoController(
mediaItem: MediaItem,
mediaItem: MutableState<MediaItem>,
videoUri: String,
defaultToStart: Boolean = false,
nostrUriCallback: String? = null,
Expand Down Expand Up @@ -288,14 +291,16 @@ fun GetVideoController(
DisposableEffect(key1 = videoUri) {
// If it is not null, the user might have come back from a playing video, like clicking on
// the notification of the video player.
scope.launch(Dispatchers.IO) {
if (controller.value == null) {
if (controller.value == null) {
scope.launch(Dispatchers.IO) {
PlaybackClientController.prepareController(
uid,
videoUri,
nostrUriCallback,
context
) {
// REQUIRED TO BE RUN IN THE MAIN THREAD

// checks again because of race conditions.
if (controller.value == null) { // still prone to race conditions.
controller.value = it
Expand All @@ -311,7 +316,7 @@ fun GetVideoController(
}
}

controller.value?.setMediaItem(mediaItem)
controller.value?.setMediaItem(mediaItem.value)
controller.value?.prepare()
} else if (controller.value != it) {
// discards the new controller because there is an existing one
Expand All @@ -329,27 +334,27 @@ fun GetVideoController(
it.volume = if (defaultToStart) 0f else 1f
}

it.setMediaItem(mediaItem)
it.setMediaItem(mediaItem.value)
it.prepare()
}
}
}
}
} else {
controller.value?.let {
if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) {
if (it.isPlaying) {
// There is a video playing, start this one on mute.
it.volume = 0f
} else {
// There is no other video playing. Use the default mute state to
// decide if sound is on or not.
it.volume = if (defaultToStart) 0f else 1f
}

it.setMediaItem(mediaItem)
it.prepare()
}
} else {
controller.value?.let {
if (it.playbackState == Player.STATE_IDLE || it.playbackState == Player.STATE_ENDED) {
if (it.isPlaying) {
// There is a video playing, start this one on mute.
it.volume = 0f
} else {
// There is no other video playing. Use the default mute state to
// decide if sound is on or not.
it.volume = if (defaultToStart) 0f else 1f
}

it.setMediaItem(mediaItem.value)
it.prepare()
}
}
}
Expand Down Expand Up @@ -379,6 +384,8 @@ fun GetVideoController(
nostrUriCallback,
context
) {
// REQUIRED TO BE RUN IN THE MAIN THREAD

// checks again to make sure no other thread has created a controller.
if (controller.value == null) {
controller.value = it
Expand All @@ -394,7 +401,7 @@ fun GetVideoController(
}
}

controller.value?.setMediaItem(mediaItem)
controller.value?.setMediaItem(mediaItem.value)
controller.value?.prepare()
} else if (controller.value != it) {
// discards the new controller because there is an existing one
Expand Down Expand Up @@ -460,7 +467,7 @@ fun VideoPlayerActiveMutex(videoUri: String, inner: @Composable (MutableState<Bo
}
}

Box(
val myModifier = remember(videoUri) {
Modifier
.fillMaxWidth()
.defaultMinSize(minHeight = 70.dp)
Expand Down Expand Up @@ -489,7 +496,9 @@ fun VideoPlayerActiveMutex(videoUri: String, inner: @Composable (MutableState<Bo
}
}
}
) {
}

Box(modifier = myModifier) {
inner(active)
}
}
Expand All @@ -511,27 +520,30 @@ private fun RenderVideoPlayer(
activeOnScreen: MutableState<Boolean>,
onDialog: ((Boolean) -> Unit)?
) {
val context = LocalContext.current

ControlWhenPlayerIsActive(controller, keepPlaying, automaticallyStartPlayback, activeOnScreen)

val controllerVisible = remember(controller) {
mutableStateOf(false)
}

BoxWithConstraints() {
AndroidView(
modifier = if (roundedCorner) {
MaterialTheme.colors.imageModifier
val borders = MaterialTheme.colors.imageModifier

val myModifier = remember {
if (roundedCorner) {
borders
.defaultMinSize(minHeight = 100.dp)
.align(Alignment.Center)
} else {
Modifier
.fillMaxWidth()
.defaultMinSize(minHeight = 100.dp)
.align(Alignment.Center)
},
factory = {
}
}

val factory = remember(controller) {
{ context: Context ->
PlayerView(context).apply {
player = controller
layoutParams = FrameLayout.LayoutParams(
Expand All @@ -556,10 +568,15 @@ private fun RenderVideoPlayer(
)
}
}
}

AndroidView(
modifier = myModifier,
factory = factory
)

waveform?.let {
Waveform(it, controller, Modifier.align(Alignment.Center))
Waveform(it, controller, remember { Modifier.align(Alignment.Center) })
}

val startingMuteState = remember(controller) {
Expand All @@ -580,7 +597,7 @@ private fun RenderVideoPlayer(
controller.volume = if (mute) 0f else 1f
}

KeepPlayingButton(keepPlaying, controllerVisible, Modifier.align(Alignment.TopEnd)) { newKeepPlaying: Boolean ->
KeepPlayingButton(keepPlaying, controllerVisible, remember { Modifier.align(Alignment.TopEnd) }) { newKeepPlaying: Boolean ->
// If something else is playing and the user marks this video to keep playing, stops the other one.
if (newKeepPlaying) {
if (keepPlayingMutex != null && keepPlayingMutex != controller) {
Expand Down
41 changes: 16 additions & 25 deletions app/src/main/java/com/vitorpamplona/amethyst/ui/note/NoteCompose.kt
Original file line number Diff line number Diff line change
Expand Up @@ -840,41 +840,32 @@ private fun CheckNewAndRenderNote(
val backgroundColor = remember { mutableStateOf<Color>(defaultBackgroundColor) }

LaunchedEffect(key1 = routeForLastRead, key2 = parentBackgroundColor?.value) {
launch(Dispatchers.IO) {
routeForLastRead?.let {
val lastTime = accountViewModel.account.loadLastRead(it)

val createdAt = baseNote.createdAt()
if (createdAt != null) {
accountViewModel.account.markAsRead(it, createdAt)

val isNew = createdAt > lastTime

val newBackgroundColor = if (isNew) {
if (parentBackgroundColor != null) {
newItemColor.compositeOver(parentBackgroundColor.value)
} else {
newItemColor.compositeOver(defaultBackgroundColor)
}
routeForLastRead?.let {
accountViewModel.loadAndMarkAsRead(it, baseNote.createdAt()) { isNew ->
val newBackgroundColor = if (isNew) {
if (parentBackgroundColor != null) {
newItemColor.compositeOver(parentBackgroundColor.value)
} else {
parentBackgroundColor?.value ?: defaultBackgroundColor
}

if (newBackgroundColor != backgroundColor.value) {
launch(Dispatchers.Main) {
backgroundColor.value = newBackgroundColor
}
newItemColor.compositeOver(defaultBackgroundColor)
}
} else {
parentBackgroundColor?.value ?: defaultBackgroundColor
}
} ?: run {
val newBackgroundColor = parentBackgroundColor?.value ?: defaultBackgroundColor

if (newBackgroundColor != backgroundColor.value) {
launch(Dispatchers.Main) {
backgroundColor.value = newBackgroundColor
}
}
}
} ?: run {
val newBackgroundColor = parentBackgroundColor?.value ?: defaultBackgroundColor

if (newBackgroundColor != backgroundColor.value) {
launch(Dispatchers.Main) {
backgroundColor.value = newBackgroundColor
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class NostrChatroomFeedViewModel(val user: ChatroomKey, val account: Account) :
}
}

@Stable
class NostrVideoFeedViewModel(val account: Account) : FeedViewModel(VideoFeedFilter(account)) {
class Factory(val account: Account) : ViewModelProvider.Factory {
override fun <NostrVideoFeedViewModel : ViewModel> create(modelClass: Class<NostrVideoFeedViewModel>): NostrVideoFeedViewModel {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,19 @@ class AccountViewModel(val account: Account) : ViewModel(), Dao {
}
}

fun loadAndMarkAsRead(routeForLastRead: String, baseNoteCreatedAt: Long?, onIsNew: (Boolean) -> Unit) {
viewModelScope.launch(Dispatchers.IO) {
val lastTime = account.loadLastRead(routeForLastRead)

if (baseNoteCreatedAt != null) {
account.markAsRead(routeForLastRead, baseNoteCreatedAt)
onIsNew(baseNoteCreatedAt > lastTime)
} else {
onIsNew(false)
}
}
}

class Factory(val account: Account) : ViewModelProvider.Factory {
override fun <AccountViewModel : ViewModel> create(modelClass: Class<AccountViewModel>): AccountViewModel {
return AccountViewModel(account) as AccountViewModel
Expand Down
Loading

0 comments on commit 40dae87

Please sign in to comment.