Added auto hide control buttons

main
h4h13 2019-11-01 23:11:22 +05:30
parent df013ec904
commit 674c5de706
6 changed files with 92 additions and 36 deletions

View File

@ -0,0 +1,32 @@
/*
* Copyright (c) 2019 Hemanth Savarala.
*
* Licensed under the GNU General Public License v3
*
* This is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by
* the Free Software Foundation either version 3 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*/
package code.name.monkey.retromusic.extensions
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.model.Song
/**
* Created by hemanths on 2019-11-01.
*/
fun ArrayList<Song>.lastElement(): Boolean {
println("${this.size} ${this.indexOf(MusicPlayerRemote.currentSong)}")
return this.size - 1 == this.indexOf(MusicPlayerRemote.currentSong)
}
fun ArrayList<Song>.fistElement(): Boolean {
return 0 == this.indexOf(MusicPlayerRemote.currentSong)
}

View File

@ -36,6 +36,10 @@ fun View.hide() {
visibility = View.GONE
}
fun View.hidden() {
visibility = View.INVISIBLE
}
fun View.showOrHide(show: Boolean) = if (show) show() else hide()
fun EditText.appHandleColor(): EditText {

View File

@ -3,7 +3,6 @@ package code.name.monkey.retromusic.fragments
import android.animation.ObjectAnimator
import android.annotation.SuppressLint
import android.content.Context
import android.os.AsyncTask
import android.os.Bundle
import android.text.SpannableString
import android.text.SpannableStringBuilder
@ -12,12 +11,18 @@ import android.view.*
import android.view.animation.DecelerateInterpolator
import code.name.monkey.appthemehelper.ThemeStore
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.fistElement
import code.name.monkey.retromusic.extensions.hide
import code.name.monkey.retromusic.extensions.lastElement
import code.name.monkey.retromusic.extensions.show
import code.name.monkey.retromusic.fragments.base.AbsMusicServiceFragment
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.helper.MusicProgressViewUpdateHelper
import code.name.monkey.retromusic.helper.PlayPauseButtonOnClickHandler
import code.name.monkey.retromusic.model.Song
import code.name.monkey.retromusic.util.*
import code.name.monkey.retromusic.util.NavigationUtil
import code.name.monkey.retromusic.util.PreferenceUtil
import code.name.monkey.retromusic.util.RetroUtil
import code.name.monkey.retromusic.util.ViewUtil
import kotlinx.android.synthetic.main.fragment_mini_player.*
import kotlin.math.abs
@ -91,16 +96,30 @@ open class MiniPlayerFragment : AbsMusicServiceFragment(), MusicProgressViewUpda
override fun onServiceConnected() {
updateSongTitle()
updatePlayPauseDrawableState()
//updateIsFavorite()
updateButtons()
}
override fun onPlayingMetaChanged() {
updateSongTitle()
//updateIsFavorite()
}
override fun onPlayStateChanged() {
updatePlayPauseDrawableState()
updateButtons()
}
private fun updateButtons() {
if (MusicPlayerRemote.playingQueue.fistElement()) {
actionPrevious.hide()
} else {
actionPrevious.show()
}
if (MusicPlayerRemote.playingQueue.lastElement()) {
actionNext.hide()
} else {
actionNext.show()
}
}
override fun onUpdateProgressViews(progress: Int, total: Int) {
@ -157,34 +176,4 @@ open class MiniPlayerFragment : AbsMusicServiceFragment(), MusicProgressViewUpda
return flingPlayBackController.onTouchEvent(event)
}
}
fun toggleFavorite(song: Song) {
MusicUtil.toggleFavorite(requireActivity(), song)
if (song.id == MusicPlayerRemote.currentSong.id) {
updateIsFavorite()
}
}
private var updateIsFavoriteTask: AsyncTask<*, *, *>? = null
@SuppressLint("StaticFieldLeak")
fun updateIsFavorite() {
if (updateIsFavoriteTask != null) {
updateIsFavoriteTask!!.cancel(false)
}
updateIsFavoriteTask = object : AsyncTask<Song, Void, Boolean>() {
override fun doInBackground(vararg params: Song): Boolean {
return MusicUtil.isFavorite(requireActivity(), params[0])
}
override fun onPostExecute(isFavorite: Boolean) {
val res = if (isFavorite)
R.drawable.ic_favorite_white_24dp
else
R.drawable.ic_favorite_border_white_24dp
val drawable = RetroUtil.getTintedVectorDrawable(requireActivity(), res, ThemeStore.accentColor(requireActivity()))
miniPlayerImage.setImageDrawable(drawable)
}
}.execute(MusicPlayerRemote.currentSong)
}
}

View File

@ -5,9 +5,15 @@ import android.view.View
import android.view.animation.AccelerateInterpolator
import android.view.animation.DecelerateInterpolator
import code.name.monkey.retromusic.R
import code.name.monkey.retromusic.extensions.fistElement
import code.name.monkey.retromusic.extensions.hidden
import code.name.monkey.retromusic.extensions.lastElement
import code.name.monkey.retromusic.extensions.show
import code.name.monkey.retromusic.fragments.VolumeFragment
import code.name.monkey.retromusic.helper.MusicPlayerRemote
import code.name.monkey.retromusic.helper.MusicProgressViewUpdateHelper
import code.name.monkey.retromusic.util.PreferenceUtil
import kotlinx.android.synthetic.main.fragment_adaptive_player_playback_controls.*
/**
@ -71,4 +77,28 @@ abstract class AbsPlayerControlsFragment : AbsMusicServiceFragment(), MusicProgr
companion object {
const val SLIDER_ANIMATION_TIME: Long = 400
}
override fun onServiceConnected() {
super.onServiceConnected()
updateButtons()
}
override fun onPlayingMetaChanged() {
super.onPlayingMetaChanged()
updateButtons()
}
private fun updateButtons() {
if (MusicPlayerRemote.playingQueue.fistElement()) {
previousButton?.hidden()
} else {
previousButton?.show()
}
if (MusicPlayerRemote.playingQueue.lastElement()) {
nextButton?.hidden()
} else {
nextButton?.show()
}
}
}

View File

@ -2,6 +2,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/miniPlayerContent"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?colorSurface"

View File

@ -2,7 +2,7 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mini_player_content"
android:id="@+id/miniPlayerContent"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="?colorSecondary"