Skip to content

Commit

Permalink
调整过渡动画和手势逻辑,完善文档注释
Browse files Browse the repository at this point in the history
  • Loading branch information
ErolC committed Jun 21, 2024
1 parent b139c49 commit 70ea5df
Show file tree
Hide file tree
Showing 33 changed files with 190 additions and 123 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ actual class LifecycleOwnerDelegate private constructor(

private val savedState: Bundle? = null

actual constructor(delegate: LifecycleOwnerDelegate, arguments: Bundle?) : this(
internal actual constructor(delegate: LifecycleOwnerDelegate, arguments: Bundle?) : this(
delegate.viewModelStoreProvider,
delegate.hostLifecycleState,
arguments,
Expand Down Expand Up @@ -178,7 +178,7 @@ actual class LifecycleOwnerDelegate private constructor(
private class SavedStateViewModel(val handle: SavedStateHandle) : ViewModel()
}

actual fun createLifecycleOwnerDelegate(
internal actual fun createLifecycleOwnerDelegate(
viewModelStoreProvider: MRouterViewModelStoreProvider?,
hostLifecycleState: Lifecycle.State,
immutableArgs: Bundle?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import java.lang.ref.WeakReference
import java.util.UUID.randomUUID

@Composable
actual fun LifecycleOwnerDelegate.LocalOwnersProvider(
internal actual fun LifecycleOwnerDelegate.LocalOwnersProvider(
saveableStateHolder: SaveableStateHolder,
pageScope: PageScope,
content: @Composable () -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,21 @@ import androidx.savedstate.SavedStateRegistryOwner

/**
* 添加事件监听
* @param body 事件回调
*/
fun Lifecycle.addEventObserver(body: (source: LifecycleOwner, event: Lifecycle.Event) -> Unit) {
addObserver(LifecycleEventObserver { source, event ->
body(source, event)
})
}

/**
* 生命周期拥有者的代理,
*/
expect class LifecycleOwnerDelegate :
LifecycleOwner, ViewModelStoreOwner, SavedStateRegistryOwner,
HasDefaultViewModelProviderFactory {
constructor(delegate: LifecycleOwnerDelegate, arguments: Bundle?)
internal constructor(delegate: LifecycleOwnerDelegate, arguments: Bundle?)

override val lifecycle: Lifecycle

Expand Down Expand Up @@ -49,7 +53,7 @@ expect class LifecycleOwnerDelegate :
fun handleLifecycleEvent(event: Lifecycle.Event)
}

expect fun createLifecycleOwnerDelegate(
internal expect fun createLifecycleOwnerDelegate(
viewModelStoreProvider: MRouterViewModelStoreProvider?,
hostLifecycleState: Lifecycle.State = Lifecycle.State.CREATED,
immutableArgs: Bundle?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.compose.runtime.saveable.SaveableStateHolder
import com.erolc.mrouter.scope.PageScope

@Composable
expect fun LifecycleOwnerDelegate.LocalOwnersProvider(
internal expect fun LifecycleOwnerDelegate.LocalOwnersProvider(
saveableStateHolder: SaveableStateHolder,
pageScope: PageScope,
content: @Composable () -> Unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import com.erolc.mrouter.platform.getViewModelProvider
import com.erolc.mrouter.platform.isAndroid
import kotlin.reflect.KClass

interface MRouterViewModelStoreProvider {
internal interface MRouterViewModelStoreProvider {
fun getViewModelStore(entryId: String): ViewModelStore
}

Expand All @@ -32,7 +32,9 @@ internal expect class MRouterControllerViewModel : ViewModel, MRouterViewModelSt
override fun getViewModelStore(entryId: String): ViewModelStore
}


/**
* 包装获取泛型类型
*/
inline fun <reified T : Any> getKClassForGenericType(): KClass<T> = T::class

/**
Expand Down Expand Up @@ -69,8 +71,8 @@ inline fun <reified VM : ViewModel> viewModel(
@Composable
fun <T : ViewModel> viewModelImpl(
modelClass: KClass<T>,
emptyBlock: (() -> T)? = null,
block: ((SavedStateHandle) -> T)? = null,
emptyBlock: EmptyConstructor? = null,
block: SSHConstructor? = null,
key: String? = null
): T {
val viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
Expand All @@ -84,20 +86,24 @@ fun <T : ViewModel> viewModelImpl(
block?.let { extras[SavedStateHandleCreateKey] = it }
?: emptyBlock?.let { extras[EmptyCreateKey] = it }
return viewModelStoreOwner.createVM(modelClass, key, extras)

}

internal typealias EmptyConstructor = () -> ViewModel
internal typealias SSHConstructor = (SavedStateHandle) -> ViewModel

object EmptyCreateKey : CreationExtras.Key<EmptyConstructor>
object SavedStateHandleCreateKey : CreationExtras.Key<SSHConstructor>
/**
* 空构造函数的key
*/
internal object EmptyCreateKey : CreationExtras.Key<EmptyConstructor>

/**
* 用于构造简单的ViewModel的工厂
* 拥有SaveStateHandle构造函数的key
*/
internal object SavedStateHandleCreateKey : CreationExtras.Key<SSHConstructor>


/**
* 用于构造简单的ViewModel的工厂
*/
@Composable
fun <VM : ViewModel> ViewModelStoreOwner.createVM(
modelClass: KClass<VM>,
Expand Down
2 changes: 2 additions & 0 deletions core/src/commonMain/kotlin/com/erolc/mrouter/model/Address.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.erolc.mrouter.model

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import com.erolc.mrouter.register.emptyConfig

/**
Expand All @@ -10,6 +11,7 @@ import com.erolc.mrouter.register.emptyConfig
* @param content 页面内容本体
* @param matchKey 用于匹配的key
*/
@Immutable
data class Address(
val path: String,
val config: PageConfig = emptyConfig,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.erolc.mrouter.model

import androidx.compose.runtime.Immutable
import androidx.core.bundle.Bundle

/**
* @param key panel的key
* @param clearTask 是否清空panel的栈
*/
@Immutable
data class PanelOptions(
val key: String,
val clearTask: Boolean = true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.erolc.mrouter.model
import androidx.compose.animation.core.FiniteAnimationSpec
import androidx.compose.animation.core.Transition
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.geometry.Rect
Expand All @@ -14,11 +15,13 @@ import com.erolc.mrouter.utils.ShareState
* @param start 开始页面的共享元素
* @param end 结束页面的共享元素
*/
@Immutable
data class ShareElementGroup(val start: ShareElement, val end: ShareElement, val key: Any)

/**
* 共享条目
*/
@Immutable
data class ShareEntry(
val groups: List<ShareElementGroup>,
val shareAnimationSpec: FiniteAnimationSpec<Rect>,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.erolc.mrouter.model

import androidx.compose.foundation.gestures.Orientation
import com.erolc.mrouter.route.transform.GestureModel

data class ShareGesture(
val gestureModel: GestureModel,
val orientation: Orientation
)
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.erolc.mrouter.model

import androidx.compose.runtime.Immutable
import androidx.compose.ui.Alignment
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.unit.DpOffset
Expand All @@ -19,6 +20,7 @@ import com.erolc.mrouter.Constants
* @param position window位置
* @param alignment 对齐方式,比如说window居中:[Alignment.Center],请注意,在[alignment] 设置了值之后,[position]将失效
*/
@Immutable
data class WindowOptions(
val id: String,
val title: String = "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,35 @@ expect fun getPlatform(): Platform

expect fun safeAreaInsetsTop():Float

//判断ios是否有刘海
/**
* 判断ios是否有刘海
*/
val iosHasNotch get() = isIos && safeAreaInsetsTop() > 20

/**
* 是否是移动端
*/
val isMobile: Boolean
get() {
val platform = getPlatform()
return platform == Android || platform == Ios
}

/**
* 是否是桌面端
*/
val isDesktop: Boolean
get() {
val platform = getPlatform()
return platform == Windows || platform == Linux || platform == Mac
}

/**
* 是否是Android
*/
val isAndroid = getPlatform() == Android

/**
* 是否是ios
*/
val isIos = getPlatform() == Ios
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ fun routeBuild(route: String, optionsBuilder: RouteBuilder.() -> Unit = {}): Rou
* 路由构建类,用于构建路由到下一个页面所需的一些数据:参数,回调等。
*/
class RouteBuilder(currentWindowId: String = Constants.DEFAULT_WINDOW) {
internal var onResult: (Bundle) -> Unit = {}
private var onResult: (Bundle) -> Unit = {}
private var windowOptions: WindowOptions = WindowOptions(currentWindowId, "")

private val args = bundleOf()

var flag: RouteFlag = NormalFlag

var transform: Transform = normal(isMobile)
var transform: Transform = normal(if (isMobile) GestureModel.Both else GestureModel.None)

private var panelOptions: PanelOptions? = null

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.erolc.mrouter.route.shareelement

import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.updateTransition
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -32,7 +30,6 @@ import kotlinx.coroutines.flow.MutableStateFlow
* @param styles 样式列表,给共享元素内的元素使用
* @param content 共享元素的界面
*/
@OptIn(ExperimentalAnimationApi::class)
@Composable
fun Element(
key: Any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,23 @@ import com.erolc.mrouter.platform.safeAreaInsetsTop
/**
* 类ios的Modal手势实现,在页面route时设置[modal]即可使用
*/
class ModalTransformWrap(private val proportion: Float, private val hasGesture: Boolean) :
TransformWrap() {
class ModalTransformWrap(private val proportion: Float, gestureModel: GestureModel) :
TransformWrap(gestureModel) {
private val prevCorner = if (iosHasNotch) safeAreaInsetsTop() else 0f

@Composable
override fun Wrap(modifier: Modifier) {
val scope = LocalTransformWrapScope.current
val padding by scope.getGapSize(proportion)
Box(modifier = modifier.padding(top = with(LocalDensity.current) {
padding.toDp()
}) then if (hasGesture) rememberDraggableModifier(Orientation.Vertical) else Modifier) {
val gestureModifier = rememberDraggableModifier(Orientation.Vertical)
Box(
modifier = matchModifier(
modifier.padding(top = with(LocalDensity.current) { padding.toDp() }),
gestureModifier
)
) {
PageContent(Modifier.clip(RoundedCornerShape(10.dp)))
Gesture(gestureModifier)
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ package com.erolc.mrouter.route.transform

import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import com.erolc.mrouter.platform.GestureContent

/**
* 普通的默认手势实现,在页面route时设置[normal]即可使用
*/
class NormalTransformWrap : TransformWrap() {
class NormalTransformWrap(gestureModel: GestureModel) : TransformWrap(gestureModel) {
@Composable
override fun Wrap(modifier: Modifier) {
val gestureModifier = rememberDraggableModifier(Orientation.Horizontal)
Box(modifier = modifier then gestureModifier) {
Box(matchModifier(modifier, gestureModifier)) {
PageContent(Modifier) // 页面内容
// GestureContent(modifier = gestureModifier) // 手势触发部分
Gesture(gestureModifier) // 手势触发部分
}
}
}
Loading

0 comments on commit 70ea5df

Please sign in to comment.