# BundleBooleanDelegate.kt

    /*
     * The MIT License
     *
     * Original work sponsored and donated by The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     * Copyright (C) 2021 The Danish Health Data Authority (http://www.sundhedsdatastyrelsen.dk)
     *
     *
     * Permission is hereby granted, free of charge, to any person obtaining a copy of
     * this software and associated documentation files (the "Software"), to deal in
     * the Software without restriction, including without limitation the rights to
     * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
     * of the Software, and to permit persons to whom the Software is furnished to do
     * so, subject to the following conditions:
     *
     *
     * The above copyright notice and this permission notice shall be included in all
     * copies or substantial portions of the Software.
     *
     *
     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
     * SOFTWARE.
     */

    package dk.nsp.mingraviditet.util

    import android.os.Bundle
    import kotlin.reflect.KFunction0
    import kotlin.reflect.KProperty

    ///**
    // * A delegate for boolean values into a [Bundle].
    // *
    // * Since using a direct [Bundle] (causes the init to capture the variable, and in android terms,
    // * that is null at construction time) we instead use a getter, to retrieve the bundle.
    // * @property getArgs [Function0R]<[Bundle]?> the getter of the bundle (eg on a [androidx.fragment.app.Fragment] or [android.app.Activity])
    // */
    class BundleBooleanDelegate(val getArgs: KFunction0<Bundle?>) {

        operator fun getValue(thisRef: Any?, property: KProperty<*>): Boolean? {
            return getArgs.invoke()?.getBoolean(property.name)
        }

        operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Boolean?) {
            val args = getArgs.invoke()
            if (value == null) {
                args?.remove(property.name)
            } else {
                args?.putBoolean(property.name, value)
            }
        }
    }

    /**
     * A delegate for boolean values into a [Bundle].
     *
     * Since using a direct [Bundle] (causes the init to capture the variable, and in android terms,
     * that is null at construction time) we instead use a getter, to retrieve the bundle.
     * @property getArgs [Function0R]<[Bundle]?> the getter of the bundle (eg on a [androidx.fragment.app.Fragment] or [android.app.Activity])
     */
    class BundleStringDelegate(val getArgs: () -> Bundle?) {

        operator fun getValue(thisRef: Any?, property: KProperty<*>): String? {
            return getArgs.invoke()?.getString(property.name)
        }

        operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) {
            val args = getArgs()
            if (value == null) {
                args?.remove(property.name)
            } else {
                args?.putString(property.name, value)
            }
        }
    }