Skip to content

Commit

Permalink
Lift the folding logic out of the FoldingText module
Browse files Browse the repository at this point in the history
  • Loading branch information
MrApplejuice committed Jan 8, 2025
1 parent db03973 commit f6fd5f0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package eu.pkgsoftware.babybuddywidgets.widgets

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.view.View
import android.view.ViewGroup
import androidx.core.view.children
import androidx.transition.Scene
import androidx.transition.TransitionManager
import androidx.transition.ChangeBounds

class FoldingLogic(val container: ViewGroup, val foldingArrow: View?) {
var animationDuration = 200L
var folded: Boolean = true
set(value) {
field = value
updateFolding()
}

private val unfoldedScene: Scene
private val foldedScene: Scene
private var arrowAnimation: Animator? = null

init {
unfoldedScene = Scene(container)
unfoldedScene.setEnterAction {
var first = true
for (child in container.children) {
child.visibility = if (first) View.GONE else View.VISIBLE
first = false
}
}
foldedScene = Scene(container)
foldedScene.setEnterAction {
var first = true
for (child in container.children) {
child.visibility = if (first) View.VISIBLE else View.GONE
first = false
}
}

updateFolding()
}

fun updateFolding() {
val transition = ChangeBounds()
transition.duration = animationDuration

TransitionManager.endTransitions(container)
if (folded) {
TransitionManager.go(foldedScene, transition)
} else {
TransitionManager.go(unfoldedScene, transition)
}

foldingArrow?.let { arrow ->
arrowAnimation?.cancel()

arrowAnimation = AnimatorSet().apply {
if (folded) {
play(ObjectAnimator.ofFloat(arrow, View.ROTATION, 90f, 0f))
} else {
play(ObjectAnimator.ofFloat(arrow, View.ROTATION, 0f, 90f))
}
duration = animationDuration
start()
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,78 +1,26 @@
package eu.pkgsoftware.babybuddywidgets.widgets

import android.animation.Animator
import android.animation.AnimatorSet
import android.animation.LayoutTransition
import android.animation.ObjectAnimator
import android.animation.TimeInterpolator
import android.animation.ValueAnimator
import android.app.Activity
import androidx.transition.ChangeBounds
import android.transition.Transition
import android.view.View
import androidx.transition.Scene
import androidx.transition.TransitionManager
import eu.pkgsoftware.babybuddywidgets.databinding.FoldingTextBinding

class FoldingText(activity: Activity, shortText: String, longText: String) {
val binding: FoldingTextBinding

val view: View get() = binding.root
var folded = true
val foldedScene: Scene
val unfoldedScene: Scene

var arrowAnimation: Animator? = null
private val foldingLogic: FoldingLogic

init {
binding = FoldingTextBinding.inflate(activity.layoutInflater)
binding.shortText.text = shortText
binding.longText.text = longText

unfoldedScene = Scene(binding.textContainer)
unfoldedScene.setEnterAction {
binding.shortText.visibility = View.GONE
binding.longText.visibility = View.VISIBLE
}
foldedScene = Scene(binding.textContainer)
foldedScene.setEnterAction {
binding.shortText.visibility = View.VISIBLE
binding.longText.visibility = View.GONE
}

updateFolding()

foldingLogic = FoldingLogic(binding.textContainer, binding.foldingArrow)
binding.root.setOnClickListener { onClick() }
}

private fun updateFolding() {
binding.shortText.visibility = if (!folded) View.GONE else View.VISIBLE
binding.longText.visibility = if (folded) View.GONE else View.VISIBLE

arrowAnimation?.cancel()
arrowAnimation = AnimatorSet().apply {
if (folded) {
play(ObjectAnimator.ofFloat(binding.foldingArrow, View.ROTATION, 90f, 0f))
} else {
play(ObjectAnimator.ofFloat(binding.foldingArrow, View.ROTATION, 0f, 90f))
}
duration = 200
start()
}

val transition = ChangeBounds()
transition.duration = 200

TransitionManager.endTransitions(binding.textContainer)
if (folded) {
TransitionManager.go(foldedScene, transition)
} else {
TransitionManager.go(unfoldedScene, transition)
}
}

private fun onClick() {
folded = !folded
updateFolding()
foldingLogic.folded = !foldingLogic.folded
}
}

0 comments on commit f6fd5f0

Please sign in to comment.