Skip to content

Commit

Permalink
Fixed #109, Fixed #249, Fixed #243, Fixed #169, Fixed #245, Fixed #244
Browse files Browse the repository at this point in the history
…Add "Add to YouTube Liked" function
  • Loading branch information
maxrave-dev committed Feb 12, 2024
1 parent 20bb771 commit 062ef9e
Show file tree
Hide file tree
Showing 26 changed files with 1,129 additions and 327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import com.maxrave.simpmusic.data.queue.Queue
import com.maxrave.simpmusic.databinding.ItemHomeBinding
import com.maxrave.simpmusic.extension.navigateSafe
import com.maxrave.simpmusic.extension.toTrack
import com.maxrave.simpmusic.viewModel.HomeViewModel

class HomeItemAdapter(
private var homeItemList: ArrayList<HomeItem>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,12 @@ class PlaylistItemAdapter(private var playlistItemList: ArrayList<Any>): Recycle
)
}
}

fun setLikedTrack(position: Int, like: Boolean) {
if (playlistItemList[position] is SongEntity) {
val track = playlistItemList[position] as SongEntity
playlistItemList[position] = track.copy(liked = like)
notifyItemChanged(position)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,18 @@ class DataStoreManager @Inject constructor(private val settingsDataStore: DataSt
}
}

val homeLimit: Flow<Int> = settingsDataStore.data.map { preferences ->
preferences[HOME_LIMIT] ?: 5
}

suspend fun setHomeLimit(limit: Int) {
withContext(Dispatchers.IO) {
settingsDataStore.edit { settings ->
settings[HOME_LIMIT] = limit
}
}
}

companion object Settings {
val COOKIE = stringPreferencesKey("cookie")
val LOGGED_IN = stringPreferencesKey("logged_in")
Expand Down Expand Up @@ -499,6 +511,7 @@ class DataStoreManager @Inject constructor(private val settingsDataStore: DataSt
val SPOTIFY_PERSONAL_TOKEN = stringPreferencesKey("spotify_personal_token")
val SPOTIFY_PERSONAL_TOKEN_EXPIRES = longPreferencesKey("spotify_personal_token_expires")
val SPOTIFY_CLIENT_TOKEN = stringPreferencesKey("spotify_client_token")
val HOME_LIMIT = intPreferencesKey("home_limit")
const val REPEAT_MODE_OFF = "REPEAT_MODE_OFF"
const val REPEAT_ONE = "REPEAT_ONE"
const val REPEAT_ALL = "REPEAT_ALL"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ class MainRepository @Inject constructor(private val localDataSource: LocalDataS

suspend fun getHomeData(): Flow<Resource<ArrayList<HomeItem>>> = flow {
runCatching {
val limit = dataStoreManager.homeLimit.first()
YouTube.customQuery(browseId = "FEmusic_home").onSuccess { result ->
val list: ArrayList<HomeItem> = arrayListOf()
if (result.contents?.singleColumnBrowseResultsRenderer?.tabs?.get(0)?.tabRenderer?.content?.sectionListRenderer?.contents?.get(
Expand Down Expand Up @@ -427,19 +428,20 @@ class MainRepository @Inject constructor(private val localDataSource: LocalDataS
result.contents?.singleColumnBrowseResultsRenderer?.tabs?.get(0)?.tabRenderer?.content?.sectionListRenderer?.contents
list.addAll(parseMixedContent(data, context))
var count = 0
while (count < 5 && continueParam != null) {
YouTube.customQuery(browseId = "", continuation = continueParam).onSuccess { response ->
continueParam =
response.continuationContents?.sectionListContinuation?.continuations?.get(
0
)?.nextContinuationData?.continuation
Log.d("Repository", "continueParam: $continueParam")
val dataContinue =
response.continuationContents?.sectionListContinuation?.contents
list.addAll(parseMixedContent(dataContinue, context))
count++
Log.d("Repository", "count: $count")
}.onFailure {
while (count < limit && continueParam != null) {
YouTube.customQuery(browseId = "", continuation = continueParam)
.onSuccess { response ->
continueParam =
response.continuationContents?.sectionListContinuation?.continuations?.get(
0
)?.nextContinuationData?.continuation
Log.d("Repository", "continueParam: $continueParam")
val dataContinue =
response.continuationContents?.sectionListContinuation?.contents
list.addAll(parseMixedContent(dataContinue, context))
count++
Log.d("Repository", "count: $count")
}.onFailure {
Log.e("Repository", "Error: ${it.message}")
count++
}
Expand Down Expand Up @@ -804,15 +806,18 @@ class MainRepository @Inject constructor(private val localDataSource: LocalDataS
suspend fun getPlaylistData(playlistId: String): Flow<Resource<PlaylistBrowse>> = flow {
runCatching {
var id = ""
if (!playlistId.startsWith("VL")) {
id += "VL$playlistId"
id += if (!playlistId.startsWith("VL")) {
"VL$playlistId"
} else {
id += playlistId
playlistId
}
Log.d("Repository", "playlist id: $id")
YouTube.customQuery(browseId = id, setLogin = true).onSuccess { result ->
val listContent: ArrayList<MusicShelfRenderer.Content> = arrayListOf()
val data: List<MusicShelfRenderer.Content>? = result.contents?.singleColumnBrowseResultsRenderer?.tabs?.get(0)?.tabRenderer?.content?.sectionListRenderer?.contents?.get(0)?.musicPlaylistShelfRenderer?.contents
val data: List<MusicShelfRenderer.Content>? =
result.contents?.singleColumnBrowseResultsRenderer?.tabs?.get(0)?.tabRenderer?.content?.sectionListRenderer?.contents?.get(
0
)?.musicPlaylistShelfRenderer?.contents
if (data != null) {
Log.d("Data", "data: $data")
Log.d("Data", "data size: ${data.size}")
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,17 @@ class MainActivity : AppCompatActivity() {
}
}
}
val likedJob = launch {
viewModel.liked.collect {
binding.cbFavorite.isChecked = it
}
}
job2.join()
job3.join()
job5.join()
job6.join()
job4.join()
likedJob.join()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ import com.maxrave.simpmusic.viewModel.SearchViewModel
import com.maxrave.simpmusic.viewModel.SharedViewModel
import dagger.hilt.android.AndroidEntryPoint
import dev.chrisbanes.insetter.applyInsetter
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import java.time.LocalDateTime
import javax.inject.Inject
Expand Down Expand Up @@ -467,6 +469,30 @@ class SearchFragment : Fragment() {
tvSongArtist.text = track.artists.toListName().connectArtists()
tvSongArtist.isSelected = true
ivThumbnail.load(track.thumbnails?.last()?.url)
if (track.album != null) {
setEnabledAll(btAlbum, true)
tvAlbum.text = track.album.name
} else {
tvAlbum.text = getString(R.string.no_album)
setEnabledAll(btAlbum, false)
}
btAlbum.setOnClickListener {
val albumId = track.album?.id
if (albumId != null) {
findNavController().navigateSafe(
R.id.action_global_albumFragment,
Bundle().apply {
putString("browseId", albumId)
})
dialog.dismiss()
} else {
Toast.makeText(
requireContext(),
getString(R.string.no_album),
Toast.LENGTH_SHORT
).show()
}
}
btAddQueue.setOnClickListener {
sharedViewModel.addToQueue(track)
}
Expand All @@ -487,16 +513,21 @@ class SearchFragment : Fragment() {
)
}
btLike.setOnClickListener {
if (cbFavorite.isChecked){
if (cbFavorite.isChecked) {
cbFavorite.isChecked = false
tvFavorite.text = getString(R.string.like)
viewModel.updateLikeStatus(track.videoId, false)
}
else {
} else {
cbFavorite.isChecked = true
tvFavorite.text = getString(R.string.liked)
viewModel.updateLikeStatus(track.videoId, true)
}
lifecycleScope.launch {
if (sharedViewModel.simpleMediaServiceHandler?.nowPlaying?.first()?.mediaId == track.videoId) {
delay(500)
sharedViewModel.refreshSongDB()
}
}
}

btSeeArtists.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ import com.maxrave.simpmusic.viewModel.HomeViewModel
import com.maxrave.simpmusic.viewModel.SharedViewModel
import dagger.hilt.android.AndroidEntryPoint
import dev.chrisbanes.insetter.applyInsetter
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import java.text.SimpleDateFormat
import java.time.LocalDateTime
Expand Down Expand Up @@ -132,6 +134,30 @@ class HomeFragment : Fragment() {
tvSongArtist.text = track.artists.toListName().connectArtists()
tvSongArtist.isSelected = true
ivThumbnail.load(track.thumbnails?.last()?.url)
if (track.album != null) {
setEnabledAll(btAlbum, true)
tvAlbum.text = track.album.name
} else {
tvAlbum.text = getString(R.string.no_album)
setEnabledAll(btAlbum, false)
}
btAlbum.setOnClickListener {
val albumId = track.album?.id
if (albumId != null) {
findNavController().navigateSafe(
R.id.action_global_albumFragment,
Bundle().apply {
putString("browseId", albumId)
})
dialog.dismiss()
} else {
Toast.makeText(
requireContext(),
getString(R.string.no_album),
Toast.LENGTH_SHORT
).show()
}
}
btAddQueue.setOnClickListener {
sharedViewModel.addToQueue(track)
}
Expand Down Expand Up @@ -161,6 +187,12 @@ class HomeFragment : Fragment() {
tvFavorite.text = getString(R.string.liked)
viewModel.updateLikeStatus(track.videoId, true)
}
lifecycleScope.launch {
if (sharedViewModel.simpleMediaServiceHandler?.nowPlaying?.first()?.mediaId == track.videoId) {
delay(500)
sharedViewModel.refreshSongDB()
}
}
}

btSeeArtists.setOnClickListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import coil.annotation.ExperimentalCoilApi
import com.google.android.flexbox.FlexboxLayout
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.android.material.slider.Slider
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.adapter.account.AccountAdapter
import com.maxrave.simpmusic.common.LIMIT_CACHE_SIZE
Expand Down Expand Up @@ -124,6 +125,7 @@ class SettingsFragment : Fragment() {
viewModel.getLyricsProvider() //
viewModel.getUseTranslation() //
viewModel.getMusixmatchLoggedIn() //
viewModel.getHomeLimit()
viewModel.getPlayVideoInsteadOfAudio()
viewModel.getVideoQuality()
viewModel.getThumbCacheSize()
Expand Down Expand Up @@ -398,7 +400,14 @@ class SettingsFragment : Fragment() {
}
}
}

val job26 = launch {
viewModel.homeLimit.collect {
binding.tvHomeLimit.text = it.toString()
if (it != null) {
binding.sliderHomeLimit.value = it.toFloat()
}
}
}
job1.join()
job2.join()
job3.join()
Expand All @@ -424,9 +433,18 @@ class SettingsFragment : Fragment() {
job23.join()
job24.join()
job25.join()
job26.join()
}
}
binding.sliderHomeLimit.addOnSliderTouchListener(object : Slider.OnSliderTouchListener {
override fun onStartTrackingTouch(slider: Slider) {
// Responds to when slider's touch event is being started
}

override fun onStopTrackingTouch(slider: Slider) {
viewModel.setHomeLimit(slider.value.toInt())
}
})
binding.btLimitPlayerCache.setOnClickListener {
var checkedIndex = -1
val dialog = MaterialAlertDialogBuilder(requireContext())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ import com.maxrave.simpmusic.viewModel.DownloadedViewModel
import com.maxrave.simpmusic.viewModel.SharedViewModel
import dagger.hilt.android.AndroidEntryPoint
import dev.chrisbanes.insetter.applyInsetter
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import java.time.LocalDateTime

Expand Down Expand Up @@ -158,6 +160,30 @@ class DownloadedFragment : Fragment() {
tvSongArtist.text = song.artistName?.connectArtists()
tvSongArtist.isSelected = true
ivThumbnail.load(song.thumbnails)
if (song.albumName != null) {
setEnabledAll(btAlbum, true)
tvAlbum.text = song.albumName
} else {
tvAlbum.text = getString(R.string.no_album)
setEnabledAll(btAlbum, false)
}
btAlbum.setOnClickListener {
val albumId = song.albumId
if (albumId != null) {
findNavController().navigateSafe(
R.id.action_global_albumFragment,
Bundle().apply {
putString("browseId", albumId)
})
dialog.dismiss()
} else {
Toast.makeText(
requireContext(),
getString(R.string.no_album),
Toast.LENGTH_SHORT
).show()
}
}
btAddQueue.setOnClickListener {
sharedViewModel.addToQueue(song.toTrack())
}
Expand Down Expand Up @@ -193,7 +219,7 @@ class DownloadedFragment : Fragment() {
cbFavorite.isChecked = true
tvFavorite.text = getString(R.string.liked)
viewModel.updateLikeStatus(song.videoId, 1)
viewModel.listDownloadedSong.observe(viewLifecycleOwner){ downloaded ->
viewModel.listDownloadedSong.observe(viewLifecycleOwner) { downloaded ->
listDownloaded.clear()
val tempDownloaded = mutableListOf<SongEntity>()
for (i in downloaded.size - 1 downTo 0) {
Expand All @@ -203,6 +229,12 @@ class DownloadedFragment : Fragment() {
downloadedAdapter.updateList(listDownloaded)
}
}
lifecycleScope.launch {
if (sharedViewModel.simpleMediaServiceHandler?.nowPlaying?.first()?.mediaId == song.videoId) {
delay(500)
sharedViewModel.refreshSongDB()
}
}
}
btAddPlaylist.setOnClickListener {
viewModel.getAllLocalPlaylist()
Expand Down
Loading

0 comments on commit 062ef9e

Please sign in to comment.