Skip to content

Commit

Permalink
Tidy code and split arrays-related utilities from Collections.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
Reco1I committed Sep 25, 2024
1 parent 4a9aca2 commit 7ca06b8
Show file tree
Hide file tree
Showing 19 changed files with 508 additions and 407 deletions.
24 changes: 9 additions & 15 deletions Toolkt/src/main/java/com/reco1l/toolkt/Observables.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package com.reco1l.toolkt
/**
* Intended to be used in manager classes where many events happens at once that must be listened.
*/
interface IObservable<T>
{
interface IObservable<T> {

/**
* The list of observers
Expand All @@ -16,8 +15,7 @@ interface IObservable<T>
* @param index The index where the observer should be bind, by default last index.
* @param observer The observer to be bind.
*/
fun bindObserver(index: Int = observers.size, observer: T): Boolean
{
fun bindObserver(index: Int = observers.size, observer: T): Boolean {
observers.add(index, observer)
return index == observers.indexOf(observer)
}
Expand All @@ -31,8 +29,7 @@ interface IObservable<T>
/**
* Iterate over all observers.
*/
inline fun <T> IObservable<T>.forEachObserver(action: (T) -> Unit)
{
inline fun <T> IObservable<T>.forEachObserver(action: (T) -> Unit) {
for (i in 0..<observers.size)
action(observers[i])
}
Expand All @@ -41,8 +38,7 @@ inline fun <T> IObservable<T>.forEachObserver(action: (T) -> Unit)
/**
* Intended to be used in manager classes where many events happens at once that must be listened.
*/
interface IMapObservable<K, T>
{
interface IMapObservable<K, T> {

/**
* The list of observers
Expand All @@ -54,8 +50,7 @@ interface IMapObservable<K, T>
* @param index The index where the observer should be bind, by default last index.
* @param observer The observer to be bind.
*/
fun bindObserver(key: K, index: Int = observers[key]?.size ?: 0, observer: T): Boolean
{
fun bindObserver(key: K, index: Int = observers[key]?.size ?: 0, observer: T): Boolean {
val list = observers.getOrPut(key) { mutableListOf() }
list.add(index, observer)
return index == list.indexOf(observer)
Expand All @@ -69,8 +64,7 @@ interface IMapObservable<K, T>
/**
* Unbind and existent observer.
*/
fun unbindObserver(key: K, observer: T): Boolean
{
fun unbindObserver(key: K, observer: T): Boolean {
val list = observers[key] ?: return false
val result = list.remove(observer)

Expand All @@ -84,10 +78,10 @@ interface IMapObservable<K, T>
/**
* Iterate over all observers for a key.
*/
fun <K, T> IMapObservable<K, T>.forEachObserver(key: K, action: (T) -> Unit)
{
fun <K, T> IMapObservable<K, T>.forEachObserver(key: K, action: (T) -> Unit) {
val list = observers[key] ?: return

for (i in 0..<list.size)
for (i in 0..<list.size) {
action(list[i])
}
}
5 changes: 5 additions & 0 deletions Toolkt/src/main/java/com/reco1l/toolkt/android/Groups.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


/**
* The orientation of the [RecyclerView] if it's using a [LinearLayoutManager].
*
* If the [RecyclerView] is not using a [LinearLayoutManager] then it'll set one.
*/
var RecyclerView.orientation
get() = (layoutManager as? LinearLayoutManager)?.orientation
set(value) {
Expand Down
58 changes: 0 additions & 58 deletions Toolkt/src/main/java/com/reco1l/toolkt/animation/Interpolators.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.reco1l.toolkt.animation

import android.animation.TimeInterpolator
import android.animation.ValueAnimator
import android.view.animation.LinearInterpolator
import kotlin.reflect.KFunction1
import kotlin.reflect.KMutableProperty0

Expand All @@ -21,7 +22,10 @@ private fun animateFloat(

duration = end
startDelay = delay
interpolator = ease ?: TimeEasing.LINEAR

if (ease != null) {
interpolator = ease
}

addUpdateListener { onApply(it.animatedValue as Float) }

Expand All @@ -43,7 +47,11 @@ private fun animateInt(

duration = end
startDelay = delay
interpolator = ease ?: TimeEasing.LINEAR

if (ease != null) {
interpolator = ease
}


addUpdateListener { onApply(it.animatedValue as Int) }

Expand Down
28 changes: 16 additions & 12 deletions Toolkt/src/main/java/com/reco1l/toolkt/animation/ViewAnimations.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.reco1l.toolkt.animation

import android.animation.TimeInterpolator
import android.view.View
import android.view.ViewPropertyAnimator

Expand All @@ -10,7 +11,7 @@ private fun <T> View.animateTo(
value: T,
time: Long,
delay: Long,
timeInterpolator: EasingFunction?,
timeInterpolator: TimeInterpolator?,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -24,7 +25,10 @@ private fun <T> View.animateTo(

duration = time
startDelay = delay
interpolator = timeInterpolator

if (timeInterpolator != null) {
interpolator = timeInterpolator
}

start()
}
Expand All @@ -44,7 +48,7 @@ fun View.toAlpha(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -55,7 +59,7 @@ fun View.toScaleX(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -66,7 +70,7 @@ fun View.toScaleY(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -77,7 +81,7 @@ fun View.toTranslationX(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -88,7 +92,7 @@ fun View.toTranslationY(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -99,7 +103,7 @@ fun View.toX(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -110,7 +114,7 @@ fun View.toY(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -121,7 +125,7 @@ fun View.toRotation(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -132,7 +136,7 @@ fun View.toRotationX(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand All @@ -143,7 +147,7 @@ fun View.toRotationY(
value: Float,
time: Long = 0L,
delay: Long = 0L,
ease: EasingFunction? = null,
ease: TimeInterpolator? = null,
onStart: (() -> Unit)? = null,
onEnd: (() -> Unit)? = null

Expand Down
Loading

0 comments on commit 7ca06b8

Please sign in to comment.