-
-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b92b799
commit a0842a6
Showing
7 changed files
with
104 additions
and
28 deletions.
There are no files selected for viewing
14 changes: 10 additions & 4 deletions
14
voyager-androidx/src/main/java/cafe/adriel/voyager/androidx/AndroidScreen.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,13 +1,19 @@ | ||
package cafe.adriel.voyager.androidx | ||
|
||
import androidx.lifecycle.ViewModelStoreOwner | ||
import cafe.adriel.voyager.core.hook.HookableScreen | ||
import cafe.adriel.voyager.core.hook.ScreenHookHandler | ||
import cafe.adriel.voyager.core.screen.Screen | ||
import cafe.adriel.voyager.core.screen.ScreenHook | ||
import cafe.adriel.voyager.core.screen.uniqueScreenKey | ||
|
||
public abstract class AndroidScreen : Screen, ViewModelStoreOwner by ScreenViewModelStoreOwner() { | ||
public abstract class AndroidScreen : | ||
Screen, | ||
HookableScreen by ScreenHookHandler(), | ||
ViewModelStoreOwner by ScreenViewModelStoreOwner() { | ||
|
||
override val key: String = uniqueScreenKey | ||
init { | ||
addHooks(viewModelScreenHooks) | ||
} | ||
|
||
override val hooks: List<ScreenHook> = viewModelScreenHooks | ||
override val key: String = uniqueScreenKey | ||
} |
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
20 changes: 20 additions & 0 deletions
20
voyager-core/src/main/java/cafe/adriel/voyager/core/hook/Hookable.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,20 @@ | ||
package cafe.adriel.voyager.core.hook | ||
|
||
public inline fun <reified T> Hookable<T>.addHooks(vararg hooks: T) { | ||
addHooks(hooks.toList()) | ||
} | ||
|
||
public inline fun <reified T> Hookable<T>.removeHooks(vararg hooks: T) { | ||
removeHooks(hooks.toList()) | ||
} | ||
|
||
public interface Hookable<T> { | ||
|
||
public val hooks: List<T> | ||
|
||
public fun addHooks(hooks: List<T>) | ||
|
||
public fun removeHooks(hooks: List<T>) | ||
|
||
public fun clearHooks() | ||
} |
51 changes: 51 additions & 0 deletions
51
voyager-core/src/main/java/cafe/adriel/voyager/core/hook/ScreenHook.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,51 @@ | ||
package cafe.adriel.voyager.core.hook | ||
|
||
import androidx.compose.runtime.ProvidedValue | ||
import cafe.adriel.voyager.core.screen.Screen | ||
|
||
public typealias HookableScreen = Hookable<ScreenHook> | ||
|
||
public val Screen.hooks: ScreenHooks | ||
get() = when (this) { | ||
is Hookable<*> -> ScreenHooks( | ||
providers = hooks.filterIsInstance<ScreenHook.OnProvide<*>>(), | ||
disposers = hooks.filterIsInstance<ScreenHook.OnDispose>(), | ||
) | ||
else -> ScreenHooks() | ||
} | ||
|
||
public fun Screen.clearHooks() { | ||
if (this is Hookable<*>) { | ||
clearHooks() | ||
} | ||
} | ||
|
||
public sealed class ScreenHook { | ||
public data class OnProvide<T>(val provide: () -> ProvidedValue<T>) : ScreenHook() | ||
public data class OnDispose(val dispose: () -> Unit) : ScreenHook() | ||
} | ||
|
||
public data class ScreenHooks( | ||
val providers: List<ScreenHook.OnProvide<*>> = emptyList(), | ||
val disposers: List<ScreenHook.OnDispose> = emptyList() | ||
) | ||
|
||
public class ScreenHookHandler : HookableScreen { | ||
|
||
private val mutableHooks = mutableListOf<ScreenHook>() | ||
|
||
override val hooks: List<ScreenHook> | ||
get() = mutableHooks | ||
|
||
override fun addHooks(hooks: List<ScreenHook>) { | ||
mutableHooks += hooks | ||
} | ||
|
||
override fun removeHooks(hooks: List<ScreenHook>) { | ||
mutableHooks -= hooks | ||
} | ||
|
||
override fun clearHooks() { | ||
mutableHooks.clear() | ||
} | ||
} |
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
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
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