All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.com.github.panpf.zoomimage.compose.internal.ConvertorMutableState.kt Maven / Gradle / Ivy

There is a newer version: 1.1.0-beta01
Show newest version
/*
 * 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()
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy