Skip to content

Commit

Permalink
Add Lrclib as Musixmatch Backup (Fixed #375), Fix PlaylistScreen
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed May 19, 2024
1 parent 2d9d344 commit d9da544
Show file tree
Hide file tree
Showing 15 changed files with 790 additions and 562 deletions.
3 changes: 0 additions & 3 deletions .idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/ktlint-plugin.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -596,9 +596,10 @@ class MainRepository
0,
)?.tabRenderer?.content?.sectionListRenderer?.contents?.get(
0,
)?.musicCarouselShelfRenderer?.header?.musicCarouselShelfBasicHeaderRenderer?.thumbnail?.musicThumbnailRenderer?.thumbnail?.thumbnails?.get(
0,
)?.url?.replace("s88", "s352") ?: ""
)?.musicCarouselShelfRenderer?.header?.musicCarouselShelfBasicHeaderRenderer
?.thumbnail?.musicThumbnailRenderer?.thumbnail?.thumbnails?.get(
0,
)?.url?.replace("s88", "s352") ?: ""
if (accountName != "" && accountThumbUrl != "") {
dataStoreManager.putString("AccountName", accountName)
dataStoreManager.putString("AccountThumbUrl", accountThumbUrl)
Expand Down Expand Up @@ -993,29 +994,40 @@ class MainRepository
val thumbnail =
result.background?.musicThumbnailRenderer?.thumbnail?.thumbnails?.toListThumbnail()
val title =
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()?.musicResponsiveHeaderRenderer?.title?.runs?.firstOrNull()?.text
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()
?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()
?.musicResponsiveHeaderRenderer?.title?.runs?.firstOrNull()?.text
val author =
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()?.musicResponsiveHeaderRenderer?.let {
Artist(
id = it.straplineTextOne?.runs?.firstOrNull()?.navigationEndpoint?.browseEndpoint?.browseId,
name = it.straplineTextOne?.runs?.firstOrNull()?.text ?: "",
)
}
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()
?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()
?.musicResponsiveHeaderRenderer?.let {
Artist(
id = it.straplineTextOne?.runs?.firstOrNull()?.navigationEndpoint?.browseEndpoint?.browseId,
name = it.straplineTextOne?.runs?.firstOrNull()?.text ?: "",
)
}
val authorThumbnail =
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()?.musicResponsiveHeaderRenderer?.let {
it.straplineThumbnail?.musicThumbnailRenderer?.thumbnail?.thumbnails?.lastOrNull()?.url
}
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()
?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()
?.musicResponsiveHeaderRenderer?.let {
it.straplineThumbnail?.musicThumbnailRenderer?.thumbnail?.thumbnails?.lastOrNull()?.url
}
val description =
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()?.tabRenderer?.content?.sectionListRenderer?.contents?.firstOrNull()?.musicResponsiveHeaderRenderer?.description?.musicDescriptionShelfRenderer?.description?.runs?.mapNotNull {
it.text
}?.joinToString("")
result.contents?.twoColumnBrowseResultsRenderer?.tabs?.firstOrNull()?.tabRenderer?.content?.sectionListRenderer
?.contents?.firstOrNull()?.musicResponsiveHeaderRenderer
?.description?.musicDescriptionShelfRenderer?.description?.runs?.map {
it.text
}?.joinToString("")
val data =
result.contents?.twoColumnBrowseResultsRenderer?.secondaryContents?.sectionListRenderer?.contents?.firstOrNull()?.musicShelfRenderer?.contents
result.contents?.twoColumnBrowseResultsRenderer?.secondaryContents?.sectionListRenderer?.contents?.firstOrNull()
?.musicShelfRenderer?.contents
parsePodcastData(data, author).let {
listEpisode.addAll(it)
}
var continueParam =
result.contents?.twoColumnBrowseResultsRenderer?.secondaryContents?.sectionListRenderer?.contents?.firstOrNull()?.musicShelfRenderer?.continuations?.firstOrNull()?.nextContinuationData?.continuation
result.contents?.twoColumnBrowseResultsRenderer?.secondaryContents
?.sectionListRenderer?.contents?.firstOrNull()
?.musicShelfRenderer?.continuations?.firstOrNull()?.nextContinuationData?.continuation
while (continueParam != null) {
YouTube.customQuery(continuation = continueParam, browseId = "")
.onSuccess { continueData ->
Expand All @@ -1026,7 +1038,8 @@ class MainRepository
listEpisode.addAll(it)
}
continueParam =
continueData.continuationContents?.musicShelfContinuation?.continuations?.firstOrNull()?.nextContinuationData?.continuation
continueData.continuationContents?.musicShelfContinuation?.continuations?.firstOrNull()
?.nextContinuationData?.continuation
}
.onFailure {
it.printStackTrace()
Expand Down Expand Up @@ -1462,7 +1475,9 @@ class MainRepository
.replace(" ", " ")
var spotifyPersonalToken = ""
if (dataStoreManager.spotifyPersonalToken.first()
.isNotEmpty() && dataStoreManager.spotifyPersonalTokenExpires.first() > System.currentTimeMillis() && dataStoreManager.spotifyPersonalTokenExpires.first() != 0L
.isNotEmpty() &&
dataStoreManager.spotifyPersonalTokenExpires.first() > System.currentTimeMillis() &&
dataStoreManager.spotifyPersonalTokenExpires.first() != 0L
) {
spotifyPersonalToken = dataStoreManager.spotifyPersonalToken.first()
Log.d("Lyrics", "spotifyPersonalToken: $spotifyPersonalToken")
Expand Down Expand Up @@ -1681,15 +1696,7 @@ class MainRepository
Regex("( và | & | и | e | und |, |和| dan)"),
" ",
).replace(" ", " ").replace(Regex("([()])"), "").replace(".", " ")
val query = "$qtrack $qartist"
val q =
query.replace(
Regex("\\((feat\\.|ft.|cùng với|con|mukana|com|avec|合作音乐人: ) "),
" ",
).replace(
Regex("( và | & | и | e | und |, |和| dan)"),
" ",
).replace(" ", " ").replace(Regex("([()])"), "").replace(".", " ")
val q = "$qtrack $qartist"
Log.d("Lyrics", "query: $q")
var musixMatchUserToken = YouTube.musixmatchUserToken
if (musixMatchUserToken == null) {
Expand Down Expand Up @@ -1749,7 +1756,7 @@ class MainRepository
bestMatchingIndex,
).track.track_name,
) &&
query.contains(
q.contains(
trackList.get(
bestMatchingIndex,
).track.track_name,
Expand Down Expand Up @@ -1778,7 +1785,7 @@ class MainRepository
bestMatchingIndex,
).track.track_name,
) &&
query.contains(
q.contains(
trackList.get(
bestMatchingIndex,
).track.track_name,
Expand Down Expand Up @@ -1822,7 +1829,12 @@ class MainRepository
}
}.onFailure { throwable ->
throwable.printStackTrace()
emit(Pair(id, Resource.Error<Lyrics>("Not found")))
YouTube.getLrclibLyrics(qtrack, qartist, durationInt).onSuccess {
it?.let { emit(Pair(id, Resource.Success<Lyrics>(it.toLyrics()))) }
}.onFailure {
it.printStackTrace()
emit(Pair(id, Resource.Error<Lyrics>("Not found")))
}
}
} else {
YouTube.fixSearchMusixmatch(
Expand All @@ -1844,16 +1856,29 @@ class MainRepository
)
} else {
Log.w("Lyrics", "Error: Lỗi getLyrics $it")
YouTube.getLrclibLyrics(qtrack, qartist, durationInt)
emit(Pair(id, Resource.Error<Lyrics>("Not found")))
}
}.onFailure {
it.printStackTrace()
emit(Pair(id, Resource.Error<Lyrics>("Not found")))
}
} else {
YouTube.getLrclibLyrics(qtrack, qartist, durationInt).onSuccess {
it?.let { emit(Pair(trackX?.track_id.toString(), Resource.Success<Lyrics>(it.toLyrics()))) }
}.onFailure {
it.printStackTrace()
emit(Pair(id, Resource.Error<Lyrics>("Not found")))
}
}
}
.onFailure {
emit(Pair("", Resource.Error<Lyrics>("Not found")))
YouTube.getLrclibLyrics(qtrack, qartist, durationInt).onSuccess {
it?.let { emit(Pair(id, Resource.Success<Lyrics>(it.toLyrics()))) }
}.onFailure {
it.printStackTrace()
emit(Pair(id, Resource.Error<Lyrics>("Not found")))
}
}
}
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package com.maxrave.simpmusic.ui.fragment.other

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.TransitionDrawable
import android.os.Build.ID
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.material3.Scaffold
import androidx.compose.ui.platform.ComposeView
import androidx.core.content.ContextCompat
Expand Down Expand Up @@ -42,6 +39,8 @@ import dagger.hilt.android.AndroidEntryPoint
import java.time.format.DateTimeFormatter

@AndroidEntryPoint
@UnstableApi
@ExperimentalFoundationApi
class LocalPlaylistFragment : Fragment() {
private var _binding: FragmentLocalPlaylistBinding? = null
private val binding get() = _binding!!
Expand All @@ -55,7 +54,7 @@ class LocalPlaylistFragment : Fragment() {
lateinit var listSuggestTrack: ArrayList<Track>
private lateinit var suggestAdapter: SuggestItemAdapter

private var id: Long? = null
private var playlistId: Long? = null
private lateinit var composeView: ComposeView

@UnstableApi
Expand All @@ -71,48 +70,20 @@ class LocalPlaylistFragment : Fragment() {
}
}

private var resultLauncher =
registerForActivityResult(
ActivityResultContracts.StartActivityForResult(),
) { activityResult ->
if (activityResult.resultCode == Activity.RESULT_OK) {
Log.d("ID", ID.toString())
val intentRef = activityResult.data
val data = intentRef?.data
if (data != null) {
val contentResolver = context?.contentResolver

val takeFlags: Int =
Intent.FLAG_GRANT_READ_URI_PERMISSION or
Intent.FLAG_GRANT_WRITE_URI_PERMISSION
// Check for the freshest data.
requireActivity().grantUriPermission(
requireActivity().packageName,
data,
takeFlags,
)
contentResolver?.takePersistableUriPermission(data, takeFlags)
val uri = data.toString()
viewModel.updatePlaylistThumbnail(uri, id!!)
loadImage(uri)
}
}
}

@UnstableApi
override fun onViewCreated(
view: View,
savedInstanceState: Bundle?,
) {
super.onViewCreated(view, savedInstanceState)

id = arguments?.getLong("id")
playlistId = arguments?.getLong("id")
composeView.apply {
setContent {
AppTheme {
Scaffold {
PlaylistScreen(
id = requireArguments().getLong("id"),
id = playlistId,
sharedViewModel = sharedViewModel,
viewModel = viewModel,
findNavController(),
Expand Down Expand Up @@ -1258,7 +1229,5 @@ class LocalPlaylistFragment : Fragment() {

override fun onDestroy() {
super.onDestroy()
viewModel.removeListSuggestion()
viewModel.removeData()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class PlaylistFragment : Fragment() {
}

binding.btPlayPause.setOnClickListener {
if (viewModel.isRadio.value == false) {
if (viewModel.isRadio.value != true) {
Queue.setContinuation(Queue.getPlaylistId() ?: "", null)
if (viewModel.playlistBrowse.value is Resource.Success && viewModel.playlistBrowse.value?.data != null) {
val args = Bundle()
Expand Down Expand Up @@ -448,7 +448,7 @@ class PlaylistFragment : Fragment() {
playlistItemAdapter.setOnClickListener(
object : PlaylistItemAdapter.OnItemClickListener {
override fun onItemClick(position: Int) {
if (viewModel.isRadio.value == false) {
if (viewModel.isRadio.value != true) {
Queue.setContinuation(Queue.getPlaylistId() ?: "", null)
if (viewModel.playlistBrowse.value is Resource.Success && viewModel.playlistBrowse.value?.data != null) {
val args = Bundle()
Expand Down
Loading

0 comments on commit d9da544

Please sign in to comment.