Skip to content

Commit

Permalink
Various fixes and TODOs (remote)
Browse files Browse the repository at this point in the history
Add alt style for MediaItemGrid (#7)
Fix false onCreate() call on MediaPlayerService
Show time until/since song plays in long press menu (#8)
Add long press to NowPlayingMainTab artist button (closes #8)
Remove PlatformContext.mainThread and PlatformContext.networkThread
Add prefs item for Settings.KEY_LYRICS_ENABLE_WORD_SYNC
Add Settings.KEY_LYRICS_FONT_SIZE (closes #28)
  • Loading branch information
toasterofbread committed Jun 12, 2023
1 parent cffaa16 commit 44f82e7
Show file tree
Hide file tree
Showing 22 changed files with 148 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,9 @@ actual open class MediaPlayerService {
val controller = instance ?: cls.newInstance()
controller.player = controller_future.get()
controller.context = PlatformContext(ctx)
controller.onCreate()
if (instance == null) {
controller.onCreate()
}
onConnected(controller)
},
MoreExecutors.directExecutor()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class MediaPlayerServiceSession: MediaSessionService() {
private inner class PlayerListener: Player.Listener {
private fun onSongLikeStatusChanged(status: SongLikeStatus?) {
if (status?.loading == false) {
runInMainThread {
coroutine_scope.launch {
updatePlayerCustomActions(status.status)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,6 @@ actual class PlatformContext(private val context: Context) {
}
}

// TODO Remove
actual fun mainThread(block: () -> Unit) = runInMainThread(block)
actual fun networkThread(block: () -> Unit): Thread = runInNetworkThread(block)

actual fun isConnectionMetered(): Boolean = ctx.isConnectionMetered()

@Composable
Expand Down Expand Up @@ -329,24 +325,6 @@ fun Context.isConnectionMetered(): Boolean {
return false
}

@Synchronized
fun runInMainThread(block: () -> Unit) {
val main_looper = Looper.getMainLooper()
if (main_looper.thread == Thread.currentThread()) {
block()
return
}
Handler(main_looper).post(block)
}

fun runInNetworkThread(block: () -> Unit): Thread {
// if (Looper.getMainLooper().thread != Thread.currentThread()) {
// block()
// return Thread.currentThread()
// }
return thread(block = block)
}

fun <T> Settings.get(context: Context): T {
return Settings.get(this, ProjectPreferences.getInstance(context))
}
15 changes: 5 additions & 10 deletions shared/src/commonMain/kotlin/com/spectre7/spmp/PlayerService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,7 @@ class PlayerService : MediaPlayerService() {
SpMp.error_manager.onError("continueRadio", result.exceptionOrNull()!!)
}
else {
context.mainThread {
addMultipleToQueue(result.getOrThrowHere(), song_count, false, skip_existing = true)
}
addMultipleToQueue(result.getOrThrowHere(), song_count, false, skip_existing = true)
}
}
}
Expand Down Expand Up @@ -220,9 +218,7 @@ class PlayerService : MediaPlayerService() {
}
}
else {
context.mainThread {
addMultipleToQueue(result.getOrThrowHere(), added_index + 1, save = save, skip_existing = true)
}
addMultipleToQueue(result.getOrThrowHere(), added_index + 1, save = save, skip_existing = true)
}
}
}
Expand Down Expand Up @@ -358,7 +354,6 @@ class PlayerService : MediaPlayerService() {
val song_artist_listener: (Artist?) -> Unit = {
updateDiscordStatus(current_song)
}
// TODO | Thumb listener for song and artist

override fun onSongTransition(song: Song?) {
current_song?.apply {
Expand Down Expand Up @@ -694,13 +689,13 @@ class PlayerService : MediaPlayerService() {
scheduleAtFixedRate(
object : TimerTask() {
override fun run() {
context.mainThread {
coroutine_scope.launch(Dispatchers.IO) {
savePersistentQueue()
markWatched()
}
}

fun markWatched() {
suspend fun markWatched() {
if (
!song_marked_as_watched
&& is_playing
Expand All @@ -714,7 +709,7 @@ class PlayerService : MediaPlayerService() {
}

if (Settings.KEY_ADD_SONGS_TO_HISTORY.get(context)) {
context.networkThread {
withContext(Dispatchers.IO) {
val result = markSongAsWatched(song.id)
if (result.isFailure) {
SpMp.error_manager.onError("autoMarkSongAsWatched", result.exceptionOrNull()!!)
Expand Down
14 changes: 10 additions & 4 deletions shared/src/commonMain/kotlin/com/spectre7/spmp/api/HomeFeed.kt
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ private fun processRows(rows: List<YoutubeiShelf>, hl: String): List<MediaItemLa
MediaItemLayout.ThumbnailSource(null, url = it.url)
},
media_item_type: MediaItemType? = null,
view_more: MediaItemLayout.ViewMore? = null
view_more: MediaItemLayout.ViewMore? = null,
type: MediaItemLayout.Type? = null
) {
val items = row.getMediaItems(hl).toMutableList()

Expand All @@ -151,7 +152,8 @@ private fun processRows(rows: List<YoutubeiShelf>, hl: String): List<MediaItemLa
items = items,
thumbnail_source = thumbnail_source,
view_more = view_more,
thumbnail_item_type = media_item_type
thumbnail_item_type = media_item_type,
type = type
))
}

Expand All @@ -177,7 +179,11 @@ private fun processRows(rows: List<YoutubeiShelf>, hl: String): List<MediaItemLa
add(
LocalisedYoutubeString.app(view_more_page_title_key),
null,
view_more = MediaItemLayout.ViewMore(list_page_browse_id = browse_endpoint.browseId)
view_more = MediaItemLayout.ViewMore(list_page_browse_id = browse_endpoint.browseId),
type = when(browse_endpoint.browseId) {
"FEmusic_listen_again" -> MediaItemlayout.Type.GRID_ALT
else -> null
}
)
continue
}
Expand Down Expand Up @@ -519,7 +525,7 @@ data class ContentsItem(val musicTwoRowItemRenderer: MusicTwoRowItemRenderer? =
val first_thumbnail = renderer.thumbnailRenderer.musicThumbnailRenderer.thumbnail.thumbnails.first()
return Pair(
Song.fromId(renderer.navigationEndpoint.watchEndpoint.videoId).editSongData {
// TODO | Is this the best way of checking?
// Is this the best way of checking?
supplySongType(if (first_thumbnail.height == first_thumbnail.width) SongType.SONG else SongType.VIDEO)
supplyTitle(renderer.title.first_text)
supplyArtist(renderer.getArtist(data_item))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ import kotlinx.coroutines.withContext
import okhttp3.Request
import okhttp3.Response

// TODO Organise and split

data class PlayerData(
val videoDetails: VideoDetails? = null,
// val streamingData: StreamingData? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import java.time.Duration

private const val HOUR: Long = 3600000L

fun durationToString(duration: Long, hl: String, short: Boolean): String {
fun durationToString(duration: Long, short: Boolean = false, hl: String = SpMp.ui_language): String {
if (short) {
return DurationFormatUtils.formatDuration(
duration,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ enum class Settings {
KEY_LYRICS_TEXT_ALIGNMENT,
KEY_LYRICS_EXTRA_PADDING,
KEY_LYRICS_ENABLE_WORD_SYNC,
KEY_LYRICS_FONT_SIZE,

// Audio & Video
KEY_STREAM_AUDIO_QUALITY,
Expand Down Expand Up @@ -238,6 +239,7 @@ enum class Settings {
KEY_LYRICS_TEXT_ALIGNMENT -> 0 // Left, center, right
KEY_LYRICS_EXTRA_PADDING -> true
KEY_LYRICS_ENABLE_WORD_SYNC -> false
KEY_LYRICS_FONT_SIZE -> 0.5f

KEY_STREAM_AUDIO_QUALITY -> SongAudioQuality.MEDIUM.ordinal
KEY_DOWNLOAD_AUDIO_QUALITY -> SongAudioQuality.MEDIUM.ordinal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ data class MediaItemPreviewParams(
val contentColour: (() -> Color)? = null,
val enable_long_press_menu: Boolean = true,
val show_type: Boolean = true,
val multiselect_context: MediaItemMultiSelectContext? = null
val multiselect_context: MediaItemMultiSelectContext? = null,
val getInfoText: (@Composable () -> String?)? = null
)
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ expect class PlatformContext {

fun loadFontFromFile(path: String): Font

fun mainThread(block: () -> Unit)
fun networkThread(block: () -> Unit): Thread

fun isConnectionMetered(): Boolean

@Composable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ data class LongPressMenuData(
val thumb_shape: Shape? = null,
val infoContent: (@Composable ColumnScope.(accent: Color) -> Unit)? = null,
val info_title: String? = null,
val getInitialInfoTitle: (@Composable () -> String?)? = null,
val multiselect_context: MediaItemMultiSelectContext? = null,
val multiselect_key: Int? = null,
val sideButton: (@Composable (Modifier, background: Color, accent: Color) -> Unit)? = null,
Expand Down Expand Up @@ -290,12 +291,19 @@ private fun MenuContent(
.weight(1f)
.onSizeChanged { box_width = it.width }
) {
if (data.info_title != null) {
Text(
data.info_title,
Modifier.offset(y = (-10).dp).onSizeChanged { info_title_width = it.width },
overflow = TextOverflow.Visible
)
Crossfade(show_info) { info ->
val text = if (info) data.info_title else data.getInitialInfoTitle?.invoke()
val current = info == show_info
if (text != null) {
Text(
text,
Modifier.offset(y = (-10).dp).onSizeChanged { if (current) info_title_width = it.width },
overflow = TextOverflow.Visible
)
}
else if (current) {
info_title_width = 0.dp
}
}

Box(
Expand All @@ -304,7 +312,7 @@ private fun MenuContent(
if (box_width < 0) fillMaxWidth()
else width(animateDpAsState(
with(LocalDensity.current) {
if (show_info) (box_width - info_title_width).toDp() - 15.dp else box_width.toDp()
if (info_title_width == 0.dp) box_width.toDp() else (box_width - info_title_width).toDp() - 15.dp
}
).value)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ data class MediaItemLayout(

enum class Type {
GRID,
GRID_ALT,
ROW,
LIST,
NUMBERED_LIST,
Expand All @@ -81,6 +82,7 @@ data class MediaItemLayout(
when (this) {
// TODO multiselect_context
GRID -> MediaItemGrid(layout, modifier, multiselect_context = multiselect_context)
GRID_ALT -> MediaItemGrid(layout, modifier, alt_style = true, multiselect_context = multiselect_context)
ROW -> MediaItemGrid(layout, modifier, 1, multiselect_context = multiselect_context)
LIST -> MediaItemList(layout, modifier, false, multiselect_context = multiselect_context)
NUMBERED_LIST -> MediaItemList(layout, modifier, true, multiselect_context = multiselect_context)
Expand Down Expand Up @@ -580,6 +582,7 @@ fun MediaItemGrid(
layout: MediaItemLayout,
modifier: Modifier = Modifier,
rows: Int? = null,
alt_style: Boolean = false,
multiselect_context: MediaItemMultiSelectContext? = null,
startContent: (LazyGridScope.() -> Unit)? = null
) {
Expand All @@ -590,6 +593,7 @@ fun MediaItemGrid(
layout.title,
layout.subtitle,
layout.view_more,
alt_style = alt_style,
itemSizeProvider = layout.itemSizeProvider,
multiselect_context = multiselect_context,
startContent = startContent
Expand All @@ -605,14 +609,15 @@ fun MediaItemGrid(
title: LocalisedYoutubeString? = null,
subtitle: LocalisedYoutubeString? = null,
view_more: MediaItemLayout.ViewMore? = null,
alt_style: Boolean = false,
content_padding: PaddingValues = PaddingValues(),
itemSizeProvider: @Composable () -> DpSize = { getDefaultMediaItemPreviewSize() },
multiselect_context: MediaItemMultiSelectContext? = null,
startContent: (LazyGridScope.() -> Unit)? = null
) {
val row_count = rows ?: if (items.size <= 3) 1 else 2
val row_count = (rows ?: if (items.size <= 3) 1 else 2) * (if (alt_style) 2 else 1)
val item_spacing = Arrangement.spacedBy(15.dp)
val item_size = itemSizeProvider()
val item_size = itemSizeProvider() * (if (alt_style) 0.5f else 1)

Column(modifier, verticalArrangement = Arrangement.spacedBy(10.dp)) {
TitleBar(
Expand All @@ -623,24 +628,34 @@ fun MediaItemGrid(
multiselect_context = multiselect_context
)

Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.CenterEnd) {
BoxWithConstraints(Modifier.fillMaxWidth(), contentAlignment = Alignment.CenterEnd) {
LazyHorizontalGrid(
rows = GridCells.Fixed(row_count),
modifier = Modifier.height(item_size.height * row_count).fillMaxWidth(),
modifier = Modifier
.height(item_size.height * row_count)
.fillMaxWidth(),
horizontalArrangement = item_spacing,
verticalArrangement = item_spacing,
contentPadding = content_padding
verticalArrangement = item_spacing
) {
startContent?.invoke(this)

items(items.size, { items[it].item?.id ?: "" }) {
items[it].item?.PreviewSquare(
MediaItemPreviewParams(
Modifier.size(item_size).animateItemPlacement(),
contentColour = Theme.current.on_background_provider,
multiselect_context = multiselect_context
)
items(items.size, { items[it].item?.id ?: "" }) { i ->
val item = items[it].item ?: return@items
val params = MediaItemPreviewParams(
Modifier.animateItemPlacement().then(
if (alt_style) Modifier.size(maxWidth, item_size)
else Modifier.size(item_height)
),
contentColour = Theme.current.on_background_provider,
multiselect_context = multiselect_context
)

if (alt_style) {
item.PreviewLong(params)
}
else {
item.PreviewSquare(params)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ fun SongPreviewSquare(
getSongLongPressMenuData(
song,
multiselect_key = queue_index,
multiselect_context = params.multiselect_context
multiselect_context = params.multiselect_context,
getInfoText = params.getInfoText
)
}

Expand Down Expand Up @@ -93,7 +94,8 @@ fun SongPreviewLong(
getSongLongPressMenuData(
song,
multiselect_key = queue_index,
multiselect_context = params.multiselect_context
multiselect_context = params.multiselect_context,
getInfoText = params.getInfoText
)
}

Expand Down Expand Up @@ -163,13 +165,15 @@ fun getSongLongPressMenuData(
song: Song,
thumb_shape: Shape? = RoundedCornerShape(SONG_THUMB_CORNER_ROUNDING),
multiselect_key: Int? = null,
multiselect_context: MediaItemMultiSelectContext? = null
multiselect_context: MediaItemMultiSelectContext? = null,
getInfoText: (@Composable () -> String?)? = null
): LongPressMenuData {
return LongPressMenuData(
song,
thumb_shape,
{ SongLongPressMenuInfo(song, multiselect_key, it) },
getString("lpm_long_press_actions"),
getInitialInfoTitle = getInfoText,
multiselect_context = multiselect_context,
multiselect_key = multiselect_key,
sideButton = { modifier, background, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ private fun LazyListScope.PlaylistItems(
)

val duration_text = remember(item.duration) {
item.duration?.let { duration -> durationToString(duration, SpMp.ui_language, true) }
item.duration?.let { duration -> durationToString(duration, true, hl = SpMp.ui_language) }
}
duration_text?.also { text ->
Text(text, style = MaterialTheme.typography.labelSmall)
Expand All @@ -326,7 +326,7 @@ private fun PlaylistInfoText(playlist: Playlist, items: List<MediaItem>) {
if (item_count > 0) {
val total_duration_text = remember(playlist.total_duration) {
if (playlist.total_duration == null) ""
else durationToString(playlist.total_duration!!, SpMp.ui_language, false)
else durationToString(playlist.total_duration!!, hl = SpMp.ui_language)
}
if (total_duration_text.isNotBlank()) {
Text(total_duration_text)
Expand Down
Loading

0 comments on commit 44f82e7

Please sign in to comment.