/*
 * Copyright (c) 2018. Louis Cognault Ayeva Derman
 *
 * 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 splitties.preferences.experimental

import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.withContext
import splitties.preferences.Preferences
import splitties.uithread.isUiThread

/**
 * **Requires** you have the following dependency in your project:
 * `org.jetbrains.kotlinx:kotlinx-coroutines-android:0.25.3`
 */
abstract class SuspendPrefsAccessor<out Prefs : Preferences>(prefsConstructorRef: () -> Prefs) {
    suspend operator fun invoke(): Prefs {
        return if (prefDelegate.isInitialized()) prefs else withContext(IO) {
            check(!isUiThread) {
                "Instantiating SharedPreferences performs I/O, which is not allowed on UI thread."
            }
            prefs
        }
    }

    private val prefDelegate = lazy(prefsConstructorRef)
    private val prefs by prefDelegate
}

/*
 * Copyright (c) 2018. Louis Cognault Ayeva Derman
 *
 * 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 splitties.preferences.experimental

import kotlinx.coroutines.experimental.IO
import kotlinx.coroutines.experimental.withContext
import splitties.preferences.Preferences

/**
 * **Requires** you have the following dependency in your project:
 * `org.jetbrains.kotlinx:kotlinx-coroutines-android:0.25.3`
 */
abstract class SuspendPrefsAccessor<out Prefs : Preferences>(prefsConstructorRef: () -> Prefs) {
    suspend operator fun invoke(): Prefs {
        return if (prefDelegate.isInitialized()) prefs else withContext(IO) { prefs }
    }

    private val prefDelegate = lazy(prefsConstructorRef)
    private val prefs by prefDelegate
}

Remove no longer needed not UI thread check
Remove no longer needed not UI thread check

LouisCAD/Splitties,LouisCAD/Splitties,LouisCAD/Splitties