-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lift the folding logic out of the FoldingText module
- Loading branch information
1 parent
db03973
commit f6fd5f0
Showing
2 changed files
with
74 additions
and
55 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
app/src/main/java/eu/pkgsoftware/babybuddywidgets/widgets/FoldingLogic.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} |
58 changes: 3 additions & 55 deletions
58
app/src/main/java/eu/pkgsoftware/babybuddywidgets/widgets/FoldingText.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |