Skip to content

Commit

Permalink
Fixed SponsorBlock and fixed navigation bar color
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Jul 28, 2024
1 parent 15ee3c8 commit f76bad3
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 106 deletions.
132 changes: 68 additions & 64 deletions app/src/main/java/com/maxrave/simpmusic/data/parser/PlaylistParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,77 +31,81 @@ fun parsePlaylistData(
var description = ""
val listThumbnails: ArrayList<Thumbnail> = arrayListOf()
var year = ""
if (header is BrowseResponse.Header.MusicDetailHeaderRenderer) {
title += header.title.runs?.get(0)?.text
Log.d("PlaylistParser", "title: $title")
if (!header.subtitle.runs.isNullOrEmpty() && header.subtitle.runs?.size!! > 2) {
val author = Author(id = header.subtitle.runs?.get(2)?.navigationEndpoint?.browseEndpoint?.browseId ?: "", name = header.subtitle.runs?.get(2)?.text ?: "")
when (header) {
is BrowseResponse.Header.MusicDetailHeaderRenderer -> {
title += header.title.runs?.get(0)?.text
Log.d("PlaylistParser", "title: $title")
if (!header.subtitle.runs.isNullOrEmpty() && header.subtitle.runs?.size!! > 2) {
val author = Author(id = header.subtitle.runs?.get(2)?.navigationEndpoint?.browseEndpoint?.browseId ?: "", name = header.subtitle.runs?.get(2)?.text ?: "")
listAuthor.add(author)
Log.d("PlaylistParser", "author: $author")
}
val secondSubtitle = header.secondSubtitle.runs
Log.w("PlaylistParser", "secondSubtitle: $secondSubtitle")
if (!secondSubtitle.isNullOrEmpty() && secondSubtitle.size > 2) {
duration += secondSubtitle.getOrNull(2)?.text
}
Log.d("PlaylistParser", "duration: $duration")
if (!header.description?.runs.isNullOrEmpty()) {
for (run in header.description?.runs!!) {
description += (run.text)
}
}
if (!header.subtitle.runs.isNullOrEmpty() && header.subtitle.runs?.size!! > 4) {
year += header.subtitle.runs?.get(4)?.text
}
header.thumbnail.croppedSquareThumbnailRenderer?.thumbnail?.thumbnails?.toListThumbnail()
?.let { listThumbnails.addAll(it) }
}

is BrowseResponse.Header.MusicEditablePlaylistDetailHeaderRenderer? -> {
title += header.header.musicDetailHeaderRenderer?.title?.runs?.get(0)?.text
Log.d("PlaylistParser", "title: $title")
val author = Author(
id = header.header.musicDetailHeaderRenderer?.subtitle?.runs?.get(2)?.navigationEndpoint?.browseEndpoint?.browseId
?: "",
name = header.header.musicDetailHeaderRenderer?.subtitle?.runs?.get(2)?.text ?: "")
listAuthor.add(author)
Log.d("PlaylistParser", "author: $author")
}
val secondSubtitle = header.secondSubtitle.runs
Log.w("PlaylistParser", "secondSubtitle: $secondSubtitle")
if (!secondSubtitle.isNullOrEmpty() && secondSubtitle.size > 2) {
duration += secondSubtitle.getOrNull(2)?.text
}
Log.d("PlaylistParser", "duration: $duration")
if (!header.description?.runs.isNullOrEmpty()) {
for (run in header.description?.runs!!) {
description += (run.text)
if (header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.size!! > 4) {
duration += header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.get(4)?.text
}
}
if (!header.subtitle.runs.isNullOrEmpty() && header.subtitle.runs?.size!! > 4) {
year += header.subtitle.runs?.get(4)?.text
}
header.thumbnail.croppedSquareThumbnailRenderer?.thumbnail?.thumbnails?.toListThumbnail()
?.let { listThumbnails.addAll(it) }
}
else if (header is BrowseResponse.Header.MusicEditablePlaylistDetailHeaderRenderer?) {
title += header.header.musicDetailHeaderRenderer?.title?.runs?.get(0)?.text
Log.d("PlaylistParser", "title: $title")
val author = Author(
id = header.header.musicDetailHeaderRenderer?.subtitle?.runs?.get(2)?.navigationEndpoint?.browseEndpoint?.browseId
?: "",
name = header.header.musicDetailHeaderRenderer?.subtitle?.runs?.get(2)?.text ?: "")
listAuthor.add(author)
Log.d("PlaylistParser", "author: $author")
if (header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.size!! > 4) {
duration += header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.get(4)?.text
}
else if (header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.size!! == 3) {
duration += header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.get(2)?.text
}
Log.d("PlaylistParser", "duration: $duration")
if (!header.header.musicDetailHeaderRenderer?.description?.runs.isNullOrEmpty()) {
for (run in header.header.musicDetailHeaderRenderer?.description?.runs!!) {
description += (run.text)
else if (header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.size!! == 3) {
duration += header.header.musicDetailHeaderRenderer?.secondSubtitle?.runs?.get(2)?.text
}
Log.d("PlaylistParser", "duration: $duration")
if (!header.header.musicDetailHeaderRenderer?.description?.runs.isNullOrEmpty()) {
for (run in header.header.musicDetailHeaderRenderer?.description?.runs!!) {
description += (run.text)
}
}
header.header.musicDetailHeaderRenderer?.thumbnail?.croppedSquareThumbnailRenderer?.thumbnail?.thumbnails?.toListThumbnail()
?.let { listThumbnails.addAll(it) }
}
header.header.musicDetailHeaderRenderer?.thumbnail?.croppedSquareThumbnailRenderer?.thumbnail?.thumbnails?.toListThumbnail()
?.let { listThumbnails.addAll(it) }
}
else if (header is SectionListRenderer.Content.MusicResponsiveHeaderRenderer?) {
title += header.title?.runs?.firstOrNull()?.text
Log.d("PlaylistParser", "title: $title")
val author = Author(id = header.straplineTextOne?.runs?.get(0)?.navigationEndpoint?.browseEndpoint?.browseId ?: "", name = header.straplineTextOne?.runs?.get(0)?.text ?: "")
listAuthor.add(author)
Log.d("PlaylistParser", "author: $author")
val secondSubtitle = header.secondSubtitle?.runs
Log.w("PlaylistParser", "secondSubtitle: $secondSubtitle")
if (!secondSubtitle.isNullOrEmpty() && secondSubtitle.size > 4) {
duration += secondSubtitle.getOrNull(4)?.text
}
else if (!secondSubtitle.isNullOrEmpty() && secondSubtitle.size == 3) {
duration += secondSubtitle.getOrNull(2)?.text
}
Log.d("PlaylistParser", "duration: $duration")
if (!header.description?.musicDescriptionShelfRenderer?.description?.runs.isNullOrEmpty()) {
for (run in header.description?.musicDescriptionShelfRenderer?.description?.runs!!) {
description += (run.text)

is SectionListRenderer.Content.MusicResponsiveHeaderRenderer? -> {
title += header.title?.runs?.firstOrNull()?.text
Log.d("PlaylistParser", "title: $title")
val author = Author(id = header.straplineTextOne?.runs?.get(0)?.navigationEndpoint?.browseEndpoint?.browseId ?: "", name = header.straplineTextOne?.runs?.get(0)?.text ?: "")
listAuthor.add(author)
Log.d("PlaylistParser", "author: $author")
val secondSubtitle = header.secondSubtitle?.runs
Log.w("PlaylistParser", "secondSubtitle: $secondSubtitle")
if (!secondSubtitle.isNullOrEmpty() && secondSubtitle.size > 4) {
duration += secondSubtitle.getOrNull(4)?.text
}
else if (!secondSubtitle.isNullOrEmpty() && secondSubtitle.size == 3) {
duration += secondSubtitle.getOrNull(2)?.text
}
Log.d("PlaylistParser", "duration: $duration")
if (!header.description?.musicDescriptionShelfRenderer?.description?.runs.isNullOrEmpty()) {
for (run in header.description?.musicDescriptionShelfRenderer?.description?.runs!!) {
description += (run.text)
}
}
header.thumbnail?.musicThumbnailRenderer?.thumbnail?.thumbnails?.toListThumbnail()
?.let { listThumbnails.addAll(it) }
}
header.thumbnail?.musicThumbnailRenderer?.thumbnail?.thumbnails?.toListThumbnail()
?.let { listThumbnails.addAll(it) }
}
Log.d("PlaylistParser", "description: $description")
val listTrack: MutableList<Track> = arrayListOf()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,12 @@ import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.cancellable
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.filterNotNull
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.single
import kotlinx.coroutines.flow.singleOrNull
import kotlinx.coroutines.flow.update
Expand Down Expand Up @@ -209,43 +212,54 @@ class SimpleMediaServiceHandler(
mayBeRestoreQueue()
coroutineScope.launch {
val skipSegmentsJob = launch {
simpleMediaState.collect { state ->
if (state is SimpleMediaState.Progress && dataStoreManager.sponsorBlockEnabled.first() == TRUE) {
val progress = (state.progress / player.duration).toFloat()
if (player.duration > 0L) {
val skipSegments = skipSegments.value
val listCategory = dataStoreManager.getSponsorBlockCategories()
if (skipSegments != null) {
for (skip in skipSegments) {
if (listCategory.contains(skip.category)) {
val firstPart = (skip.segment[0] / skip.videoDuration).toFloat()
val secondPart =
(skip.segment[1] / skip.videoDuration).toFloat()
if (progress in firstPart..secondPart) {
Log.w(
"Seek to",
(skip.segment[1] / skip.videoDuration).toFloat()
.toString(),
)
skipSegment((skip.segment[1] * 1000).toLong())
Toast.makeText(
context,
context.getString(
R.string.sponsorblock_skip_segment,
simpleMediaState.filter { it is SimpleMediaState.Progress }.map {
val current = (it as SimpleMediaState.Progress).progress
val duration = player.duration
if (duration > 0L) {
(current.toFloat() / player.duration)*100
}
else {
-1f
}
}.filter { it >= 0f }
.distinctUntilChanged()
.collect { current ->
Log.d("Seek to", "Current $current")
if (dataStoreManager.sponsorBlockEnabled.first() == TRUE) {
if (player.duration > 0L) {
val skipSegments = skipSegments.value
val listCategory = dataStoreManager.getSponsorBlockCategories()
if (skipSegments != null) {
for (skip in skipSegments) {
if (listCategory.contains(skip.category)) {
val firstPart = ((skip.segment[0] / skip.videoDuration)*100).toFloat()
val secondPart =
((skip.segment[1] / skip.videoDuration)*100).toFloat()
if (current in firstPart..secondPart) {
Log.w(
"Seek to",
secondPart.toString(),
)
Log.d("Seek to", "Cr: $current, First: $firstPart, Second: $secondPart")
skipSegment((secondPart * player.duration).toLong()/100)
Toast.makeText(
context,
context.getString(
SPONSOR_BLOCK.listName.get(
SPONSOR_BLOCK.list.indexOf(skip.category),
),
).lowercase(),
),
Toast.LENGTH_SHORT,
).show()
R.string.sponsorblock_skip_segment,
context.getString(
SPONSOR_BLOCK.listName.get(
SPONSOR_BLOCK.list.indexOf(skip.category),
),
).lowercase(),
),
Toast.LENGTH_SHORT,
).show()
}
}
}
}
}
}
}
}
}
val playbackJob =
Expand Down Expand Up @@ -1120,12 +1134,12 @@ class SimpleMediaServiceHandler(
normalizeVolume = normalize
}

fun seekTo(position: String) {
private fun seekTo(position: String) {
player.seekTo(position.toLong())
Log.d("Check seek", "seekTo: ${player.currentPosition}")
}

fun skipSegment(position: Long) {
private fun skipSegment(position: Long) {
if (position in 0..player.duration) {
player.seekTo(position)
} else if (position > player.duration) {
Expand Down
14 changes: 12 additions & 2 deletions app/src/main/java/com/maxrave/simpmusic/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ import android.util.Log
import android.view.View
import android.view.animation.AnimationUtils
import android.widget.Toast
import androidx.activity.SystemBarStyle
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.app.AppCompatDelegate
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.core.content.res.ResourcesCompat
import androidx.core.net.toUri
import androidx.core.os.LocaleListCompat
Expand Down Expand Up @@ -207,7 +210,12 @@ class MainActivity : AppCompatActivity() {
//
// if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
// WindowCompat.setDecorFitsSystemWindows(window, false)
enableEdgeToEdge()
enableEdgeToEdge(
navigationBarStyle = SystemBarStyle.auto(
lightScrim = Color.Transparent.toArgb(),
darkScrim = Color.Transparent.toArgb()
)
)
viewModel.checkIsRestoring()
viewModel.runWorker()
// } else {
Expand Down Expand Up @@ -620,7 +628,9 @@ class MainActivity : AppCompatActivity() {
dataStoreManager.translucentBottomBar.distinctUntilChanged().collectLatest {
if (it == DataStoreManager.TRUE) {
binding.bottomNavigationView.background =
ResourcesCompat.getDrawable(resources, R.drawable.transparent_rect, null)
ResourcesCompat.getDrawable(resources, R.drawable.transparent_rect, null)?.apply {
this.setDither(true)
}
}
else if (it == DataStoreManager.FALSE) {
binding.bottomNavigationView.background =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.maxrave.simpmusic.ui.fragment.player

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -161,8 +160,6 @@ class NowPlayingFragment : Fragment() {

composeView.apply {
setContent {
val activity = requireActivity()
activity.window.navigationBarColor = Color.TRANSPARENT
AppTheme {
Scaffold { paddingValues ->
NowPlayingScreen(sharedViewModel = viewModel, navController = findNavController())
Expand Down Expand Up @@ -2295,8 +2292,6 @@ class NowPlayingFragment : Fragment() {
super.onDestroyView()
arguments?.putString("type", null)
arguments?.putString("videoId", null)
val activity = requireActivity()
activity.window.navigationBarColor = Color.parseColor("#CB0B0A0A")
// val bottom = activity.findViewById<BottomNavigationView>(R.id.bottom_navigation_view)
// val miniplayer = activity.findViewById<ComposeView>(R.id.miniplayer)
// bottom.animation = AnimationUtils.loadAnimation(requireContext(), R.anim.bottom_to_top)
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/drawable/transparent_rect.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
android:shape="rectangle" >
<gradient
android:startColor="#00100000"
android:centerColor="#A40B0A0A"
android:endColor="#E80B0A0A"
android:centerColor="#C30B0A0A"
android:endColor="#0B0A0A"
android:angle="270"/>
</shape>

0 comments on commit f76bad3

Please sign in to comment.