commonMain.com.github.panpf.zoomimage.compose.internal.ConvertorMutableState.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zoomimage-compose Show documentation
Show all versions of zoomimage-compose Show documentation
Android Zoom Image Component
/*
* Copyright (C) 2023 panpf
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.panpf.zoomimage.compose.internal
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.SnapshotMutationPolicy
import androidx.compose.runtime.snapshots.SnapshotMutableState
internal fun MutableState.convert(convertor: (T) -> T): MutableState {
return if (this is SnapshotMutableState) {
ConvertorSnapshotMutableState(this, convertor)
} else {
ConvertorMutableState(this, convertor)
}
}
internal class ConvertorMutableState(
private val state: MutableState,
private val convertor: (T) -> T
) : MutableState {
override var value: T
get() = convertor(state.value)
set(value) {
state.value = value
}
override fun component1(): T {
return state.component1()
}
override fun component2(): (T) -> Unit {
return state.component2()
}
}
internal class ConvertorSnapshotMutableState(
private val state: SnapshotMutableState,
private val convertor: (T) -> T
) : SnapshotMutableState {
override val policy: SnapshotMutationPolicy
get() = state.policy
override var value: T
get() = convertor(state.value)
set(value) {
state.value = value
}
override fun component1(): T {
return state.component1()
}
override fun component2(): (T) -> Unit {
return state.component2()
}
}