Skip to content

Commit

Permalink
Fix blinking Favourite checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
maxravework committed Mar 26, 2024
1 parent dbcba65 commit 975c455
Show file tree
Hide file tree
Showing 15 changed files with 101 additions and 88 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

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

9 changes: 0 additions & 9 deletions .idea/deploymentTargetDropDown.xml

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

4 changes: 1 addition & 3 deletions .idea/gradle.xml

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

3 changes: 2 additions & 1 deletion .idea/misc.xml

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

2 changes: 2 additions & 0 deletions app/src/main/java/com/maxrave/simpmusic/common/Config.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ object Config {
const val RECOVER_TRACK_QUEUE = "RECOVER_TRACK_QUEUE"

val REMOVED_SONG_DATE_TIME = LocalDateTime.of(2003, Month.AUGUST, 26, 3, 0)

const val OPEN_NOTIFICATION_ACTION = "com.maxrave.simpmusic.service.test.notification.NOTIFICATION"
}

object DownloadState {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,9 @@ class SimpleMediaServiceHandler(
player.currentMediaItem?.mediaId ?: "",
if (!(_liked.value)) 1 else 0,
)
delay(500)
updateNotification()
}
_liked.value = !(_liked.value)
updateNotification()
}

fun like(liked: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,20 @@ import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.drawable.BitmapDrawable
import android.os.Bundle
import androidx.annotation.OptIn
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.graphics.drawable.toBitmap
import androidx.media3.common.util.UnstableApi
import androidx.navigation.NavDeepLinkBuilder
import coil.ImageLoader
import coil.request.ImageRequest
import coil.request.SuccessResult
import com.maxrave.simpmusic.R
import com.maxrave.simpmusic.common.Config
import com.maxrave.simpmusic.ui.MainActivity
import kotlinx.coroutines.runBlocking

Expand All @@ -31,18 +34,11 @@ object NotificationHandler {
noti: NotificationModel,
) {
// No back-stack when launched
val intent =
Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
}
intent.action = "com.maxrave.simpmusic.service.test.notification.NOTIFICATION"
val pendingIntent =
PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_IMMUTABLE,
)
val pendingIntent = NavDeepLinkBuilder(context)
.setGraph(R.navigation.nav_bottom_navigation)
.setDestination(R.id.notificationFragment)
.setComponentName(MainActivity::class.java)
.createPendingIntent()

val bitmap =
runBlocking {
Expand Down
40 changes: 17 additions & 23 deletions app/src/main/java/com/maxrave/simpmusic/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,6 @@ class MainActivity : AppCompatActivity() {
binding.progressBar.progress = (it * 100).toInt()
}
}

val job6 =
launch {
viewModel.simpleMediaServiceHandler?.liked?.collect { liked ->
binding.cbFavorite.isChecked = liked
}
}
val job3 =
launch {
viewModel.isPlaying.collect {
Expand Down Expand Up @@ -294,13 +287,14 @@ class MainActivity : AppCompatActivity() {
val likedJob =
launch {
viewModel.liked.collect {
binding.cbFavorite.isChecked = it
if (binding.cbFavorite.isChecked != it) {
binding.cbFavorite.isChecked = it
}
}
}
job2.join()
job3.join()
job5.join()
job6.join()
job4.join()
likedJob.join()
}
Expand Down Expand Up @@ -588,9 +582,6 @@ class MainActivity : AppCompatActivity() {
launch {
viewModel.intent.collectLatest { intent ->
if (intent != null) {
if (intent.action == "com.maxrave.simpmusic.service.test.notification.NOTIFICATION") {
navController.navigateSafe(R.id.action_global_notificationFragment)
}
data = intent.data ?: intent.getStringExtra(Intent.EXTRA_TEXT)?.toUri()
Log.d("MainActivity", "onCreate: $data")
if (data != null) {
Expand Down Expand Up @@ -736,18 +727,21 @@ class MainActivity : AppCompatActivity() {
}
binding.card.animation = AnimationUtils.loadAnimation(this, R.anim.bottom_to_top)
binding.cbFavorite.setOnCheckedChangeListener { _, isChecked ->
if (!isChecked) {
Log.d("cbFavorite", "onCheckedChanged: $isChecked")
viewModel.nowPlayingMediaItem.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(nowPlayingSong.mediaId, false)
viewModel.updateLikeInNotification(false)
}
} else {
Log.d("cbFavorite", "onCheckedChanged: $isChecked")
viewModel.nowPlayingMediaItem.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(nowPlayingSong.mediaId, true)
viewModel.updateLikeInNotification(true)
if (isChecked != runBlocking { viewModel.liked.first() }) {
if (!isChecked) {
Log.d("cbFavorite", "onCheckedChanged: $isChecked")
viewModel.nowPlayingMediaItem.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(nowPlayingSong.mediaId, false)
viewModel.updateLikeInNotification(false)
}
} else {
Log.d("cbFavorite", "onCheckedChanged: $isChecked")
viewModel.nowPlayingMediaItem.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(nowPlayingSong.mediaId, true)
viewModel.updateLikeInNotification(true)
}
}
Log.w("Where like", "Like in MainActivity listener")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,12 @@ class NowPlayingFragment : Fragment() {
val index = viewModel.getActiveLyrics(it)
if (index != null) {
if (lyrics?.lines?.get(0)?.words == "Lyrics not found") {
binding.lyricsLayout.visibility = View.GONE
binding.lyricsTextLayout.visibility = View.GONE
if (binding.lyricsLayout.visibility != View.GONE) {
binding.lyricsLayout.visibility = View.GONE
}
if (binding.lyricsTextLayout.visibility != View.GONE) {
binding.lyricsTextLayout.visibility = View.GONE
}
} else {
viewModel._lyrics.value!!.data?.let { it1 ->
lyricsAdapter.updateOriginalLyrics(
Expand All @@ -740,9 +744,13 @@ class NowPlayingFragment : Fragment() {
}

if (binding.btFull.text == getString(R.string.show)) {
binding.lyricsTextLayout.visibility = View.VISIBLE
if (binding.lyricsTextLayout.visibility != View.VISIBLE) {
binding.lyricsTextLayout.visibility = View.VISIBLE
}
}
if (binding.lyricsLayout.visibility != View.VISIBLE) {
binding.lyricsLayout.visibility = View.VISIBLE
}
binding.lyricsLayout.visibility = View.VISIBLE
// if (temp.nowLyric != null) {
// binding.tvNowLyrics.visibility = View.VISIBLE
// binding.tvNowLyrics.text = temp.nowLyric
Expand Down Expand Up @@ -774,8 +782,12 @@ class NowPlayingFragment : Fragment() {
}
}
} else {
binding.lyricsLayout.visibility = View.GONE
binding.lyricsTextLayout.visibility = View.GONE
if (binding.lyricsLayout.visibility != View.GONE) {
binding.lyricsLayout.visibility = View.GONE
}
if (binding.lyricsTextLayout.visibility != View.GONE) {
binding.lyricsTextLayout.visibility = View.GONE
}
}
}
}
Expand Down Expand Up @@ -814,7 +826,9 @@ class NowPlayingFragment : Fragment() {
val job10 =
launch {
viewModel.liked.collect { liked ->
binding.cbFavorite.isChecked = liked
if (binding.cbFavorite.isChecked != liked) {
binding.cbFavorite.isChecked = liked
}
}
}
val job11 =
Expand Down Expand Up @@ -1381,21 +1395,23 @@ class NowPlayingFragment : Fragment() {
findNavController().navigateSafe(R.id.action_global_infoFragment)
}
binding.cbFavorite.setOnCheckedChangeListener { _, isChecked ->
if (!isChecked) {
viewModel.songDB.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(
nowPlayingSong.videoId,
false,
)
viewModel.updateLikeInNotification(false)
}
} else {
viewModel.songDB.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(
nowPlayingSong.videoId,
true,
)
viewModel.updateLikeInNotification(true)
Log.w("Where Like", isChecked.toString() + "Now Playing Listener")
if (isChecked != runBlocking { viewModel.liked.first() }) {
viewModel.updateLikeInNotification(isChecked)
if (!isChecked) {
viewModel.nowPlayingMediaItem.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(
nowPlayingSong.mediaId,
false,
)
}
} else {
viewModel.nowPlayingMediaItem.value?.let { nowPlayingSong ->
viewModel.updateLikeStatus(
nowPlayingSong.mediaId,
true,
)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ class NotificationViewModel

init {
viewModelScope.launch {
mainRepository.getAllNotifications().collect {
_listNotification.value = it
mainRepository.getAllNotifications().collect { notificationEntities ->
_listNotification.value = notificationEntities?.sortedByDescending {
it.time
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class SharedViewModel
private var _downloadList: MutableStateFlow<ArrayList<SongEntity>?> = MutableStateFlow(null)
val downloadList: SharedFlow<ArrayList<SongEntity>?> = _downloadList.asSharedFlow()

protected val context
private val context
get() = getApplication<Application>()

val isServiceRunning = MutableLiveData<Boolean>(false)
Expand Down Expand Up @@ -363,7 +363,7 @@ class SharedViewModel
ImageLoader(context).execute(imageRequest)
}
}
if (nowPlaying != null && getCurrentMediaItemIndex() > 0) {
if (nowPlaying != null) {
_nowPlayingMediaItem.postValue(nowPlaying)
var downloaded = false
val tempSong =
Expand All @@ -381,7 +381,6 @@ class SharedViewModel
_songDB.value = songEntity
if (songEntity != null) {
_liked.value = songEntity.liked
simpleMediaServiceHandler!!.like(songEntity.liked)
downloaded =
songEntity.downloadState == DownloadState.STATE_DOWNLOADED
Log.d("Check like", songEntity.toString())
Expand Down Expand Up @@ -429,8 +428,7 @@ class SharedViewModel
launch {
simpleMediaServiceHandler!!.liked.collect { liked ->
if (liked != _liked.value) {
simpleMediaServiceHandler!!.nowPlaying.first()
?.let { updateLikeStatus(it.mediaId, liked) }
refreshSongDB()
}
}
}
Expand Down Expand Up @@ -1199,6 +1197,9 @@ class SharedViewModel
} else {
mainRepository.updateLikeStatus(videoId, 0)
}
delay(500)
refreshSongDB()
updateLikeInNotification(likeStatus)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/res/drawable/circle_ripple.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android" android:color="@android:color/transparent">
<item>
<shape android:shape="oval">
<solid android:color="@android:color/transparent"/>
</shape>
</item>
</ripple>
10 changes: 5 additions & 5 deletions app/src/main/res/layout/fragment_now_playing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -382,15 +382,15 @@
android:layout_width="@dimen/_60sdp"
android:layout_height="@dimen/_60sdp"
android:layout_centerInParent="true"
android:background="?attr/actionBarItemBackground"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:scaleType="fitCenter"
android:src="@drawable/baseline_play_circle_24" />

<ImageButton
android:id="@+id/btNext"
android:layout_width="@dimen/_40sdp"
android:layout_height="@dimen/_40sdp"
android:background="?attr/actionBarItemBackground"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:layout_centerVertical="true"
android:layout_marginStart="20sp"
android:scaleType="fitCenter"
Expand All @@ -403,7 +403,7 @@
android:id="@+id/btRepeat"
android:layout_width="@dimen/_25sdp"
android:layout_height="@dimen/_25sdp"
android:background="?attr/actionBarItemBackground"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:layout_centerVertical="true"
android:layout_marginStart="20sp"
android:scaleType="fitCenter"
Expand All @@ -416,7 +416,7 @@
android:id="@+id/btPrevious"
android:layout_width="@dimen/_40sdp"
android:layout_height="@dimen/_40sdp"
android:background="?attr/actionBarItemBackground"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:layout_centerVertical="true"
android:layout_marginEnd="20sp"
android:scaleType="fitCenter"
Expand All @@ -429,7 +429,7 @@
android:id="@+id/btShuffle"
android:layout_width="@dimen/_25sdp"
android:layout_height="@dimen/_25sdp"
android:background="?attr/actionBarItemBackground"
android:background="?android:attr/selectableItemBackgroundBorderless"
android:layout_centerVertical="true"
android:layout_marginEnd="20sp"
android:scaleType="fitCenter"
Expand Down
Loading

0 comments on commit 975c455

Please sign in to comment.