Redo controls view

Redo the controls view to support a slide enter/exit animation, along with fixes to elevation problems on certain devices.
This commit is contained in:
OxygenCobalt
2021-02-13 14:49:57 -07:00
parent 2b24d6661e
commit a4801bdf2f
6 changed files with 164 additions and 65 deletions
@@ -1,5 +1,6 @@
package org.oxycblt.auxio
import android.animation.LayoutTransition
import android.content.res.ColorStateList
import android.os.Bundle
import android.view.LayoutInflater
@@ -9,6 +10,7 @@ import android.view.ViewGroup
import androidx.core.graphics.ColorUtils
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.interpolator.view.animation.FastOutSlowInInterpolator
import androidx.navigation.NavController
import androidx.navigation.NavOptions
import androidx.navigation.fragment.NavHostFragment
@@ -69,6 +71,13 @@ class MainFragment : Fragment() {
binding.lifecycleOwner = this
// Apply custom interpolation effects
// The container layout types differ in the land/port layouts so it has to be cast to their parent instead.
(binding.controlsContainer as ViewGroup).layoutTransition = LayoutTransition().apply {
setInterpolator(LayoutTransition.APPEARING, FastOutSlowInInterpolator())
setInterpolator(LayoutTransition.DISAPPEARING, FastOutSlowInInterpolator())
}
binding.navBar.apply {
itemIconTintList = navTints
itemTextColor = navTints
@@ -150,7 +159,9 @@ class MainFragment : Fragment() {
if (song == null) {
logD("Hiding CompactPlaybackFragment since no song is being played.")
if (!isLandscape(resources)) {
if (isLandscape(resources)) {
binding.compactPlayback.visibility = View.INVISIBLE
} else {
binding.compactPlayback.visibility = View.GONE
}
@@ -57,10 +57,6 @@ class CompactPlaybackFragment : Fragment() {
}
}
if (playbackModel.song.value == null) {
setInvisible(true)
}
// --- VIEWMODEL SETUP ---
playbackModel.song.observe(viewLifecycleOwner) {
@@ -69,9 +65,6 @@ class CompactPlaybackFragment : Fragment() {
binding.song = it
binding.playbackProgress.max = it.seconds.toInt()
setInvisible(false)
} else {
setInvisible(true)
}
}
@@ -117,20 +110,4 @@ class CompactPlaybackFragment : Fragment() {
}
}
}
/**
* Set this fragment to be invisible, if needed. Only runs in landscape mode.
*/
private fun setInvisible(invisible: Boolean) {
// Does not run in landscape
if (!isLandscape(resources)) return
val visibility = if (invisible) View.INVISIBLE else View.VISIBLE
binding.playbackLayout.children.forEach {
it.visibility = visibility
}
binding.root.isEnabled = !invisible
}
}
@@ -0,0 +1,98 @@
package org.oxycblt.auxio.ui
import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import org.oxycblt.auxio.logD
import org.oxycblt.auxio.logE
import java.lang.reflect.Field
/**
* Hack layout that fixes an issue where disappearing views would draw over non-disappearing
* views when animated with a stock LayoutTransition. Adapted from this StackOverflow answer:
* https://stackoverflow.com/a/35087229
*/
class SlideLinearLayout @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = -1
) : LinearLayout(context, attrs, defStyleAttr) {
private val disappearingChildrenField: Field? = try {
ViewGroup::class.java.getDeclaredField("mDisappearingChildren").also {
it.isAccessible = true
}
} catch (e: NoSuchFieldException) {
logE("Could not get mDisappearingChildren.")
null
}
private var disappearingChildren: List<View>? = null
private var dumpView: View? = null
private var doDrawingTrick: Boolean = false
init {
if (disappearingChildrenField != null) {
// Create a junk view and add it, which makes all the magic happen [I think??].
dumpView = View(context)
addView(dumpView, 0, 0)
}
}
override fun dispatchDraw(canvas: Canvas?) {
doDrawingTrick = beforeDispatchDraw()
super.dispatchDraw(canvas)
if (doDrawingTrick) {
doDrawingTrick = false
}
}
override fun drawChild(canvas: Canvas?, child: View?, drawingTime: Long): Boolean {
val children = getDisappearingChildren()
// I have no idea what this code does.
if (doDrawingTrick && children != null) {
if (child == dumpView) {
var more = false
children.forEach {
more = more or super.drawChild(canvas, it, drawingTime)
}
return more
} else if (children.contains(child)) {
return false
}
}
return super.drawChild(canvas, child, drawingTime)
}
private fun beforeDispatchDraw(): Boolean {
val children = getDisappearingChildren()
if (children == null || children.isEmpty() || childCount <= 1) { // Junk view included
return false
}
return true
}
@Suppress("UNCHECKED_CAST")
private fun getDisappearingChildren(): List<View>? {
if (disappearingChildrenField == null || disappearingChildren != null) {
return disappearingChildren
}
// If there is no list of disappearing children yet, attempt to get it.
try {
disappearingChildren = disappearingChildrenField.get(this) as List<View>?
} catch (e: IllegalAccessException) {
logD("Could not get list of disappearing children.")
}
return disappearingChildren
}
}