<commit_before>/*
 * Copyright 2018 Google LLC
 *
 * 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
 *
 *     https://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.google.samples.apps.iosched.shared.data.prefs

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.support.annotation.WorkerThread
import androidx.content.edit
import javax.inject.Inject

/**
 * Storage for app and user preferences.
 */
interface PreferenceStorage {
    var onboardingCompleted: Boolean
    var scheduleUiHintsShown: Boolean
    var notificationsPreferenceShown: Boolean
    var preferToReceiveNotifications: Boolean
    var snackbarIsStopped: Boolean
    var observableSnackbarIsStopped: LiveData<Boolean>
}

/**
 * [PreferenceStorage] impl backed by [android.content.SharedPreferences].
 */
class SharedPreferenceStorage @Inject constructor(context: Context) :
    PreferenceStorage {

    private val prefs = context.applicationContext.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)

    private val observableShowSnackbarResult = MutableLiveData<Boolean>()
    private val changeListener = OnSharedPreferenceChangeListener { _, key ->
        if (key == PREF_SNACKBAR_IS_STOPPED) {
            observableShowSnackbarResult.value = snackbarIsStopped
        }
    }

    init {
        prefs.registerOnSharedPreferenceChangeListener(changeListener)
    }

    @get:WorkerThread
    override var onboardingCompleted
        get() = prefs.getBoolean(PREF_ONBOARDING, false)
        set(value) = prefs.edit {
            putBoolean(PREF_ONBOARDING, value)
        }

    @get:WorkerThread
    override var scheduleUiHintsShown: Boolean
        get() = prefs.getBoolean(PREF_SCHED_UI_HINTS_SHOWN, false)
        set(value) = prefs.edit {
            putBoolean(PREF_SCHED_UI_HINTS_SHOWN, value)
        }

    @get:WorkerThread
    override var notificationsPreferenceShown: Boolean
        get() = prefs.getBoolean(PREF_NOTIFICATIONS_SHOWN, false)
        set(value) = prefs.edit {
            putBoolean(PREF_NOTIFICATIONS_SHOWN, value)
        }

    @get:WorkerThread
    override var preferToReceiveNotifications: Boolean
        get() = prefs.getBoolean(PREF_RECEIVE_NOTIFICATIONS, true)
        set(value) = prefs.edit {
            putBoolean(PREF_RECEIVE_NOTIFICATIONS, value)
        }

    @get:WorkerThread
    override var snackbarIsStopped: Boolean
        get() = prefs.getBoolean(PREF_SNACKBAR_IS_STOPPED, false)
        set(value) = prefs.edit { putBoolean(PREF_SNACKBAR_IS_STOPPED, value) }

    override var observableSnackbarIsStopped: LiveData<Boolean>
        get() {
            observableShowSnackbarResult.value = snackbarIsStopped
            return observableShowSnackbarResult
        }
        set(value) = throw IllegalAccessException("This property can't be changed")

    companion object {
        private const val PREFS_NAME = "iosched"
        private const val PREF_ONBOARDING = "pref_onboarding"
        private const val PREF_SCHED_UI_HINTS_SHOWN = "pref_sched_ui_hints_shown"
        private const val PREF_NOTIFICATIONS_SHOWN = "pref_notifications_shown"
        private const val PREF_RECEIVE_NOTIFICATIONS = "pref_receive_notifications"
        private const val PREF_SNACKBAR_IS_STOPPED = "pref_snackbar_is_stopped"
    }
}<commit_msg>Simpify the PreferenceStorage using delegated properties<commit_after>/*
 * Copyright 2018 Google LLC
 *
 * 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
 *
 *     https://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.google.samples.apps.iosched.shared.data.prefs

import android.arch.lifecycle.LiveData
import android.arch.lifecycle.MutableLiveData
import android.content.Context
import android.content.Context.MODE_PRIVATE
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.content.SharedPreferences
import android.support.annotation.WorkerThread
import androidx.content.edit
import javax.inject.Inject
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty

/**
 * Storage for app and user preferences.
 */
interface PreferenceStorage {
    var onboardingCompleted: Boolean
    var scheduleUiHintsShown: Boolean
    var notificationsPreferenceShown: Boolean
    var preferToReceiveNotifications: Boolean
    var snackbarIsStopped: Boolean
    var observableSnackbarIsStopped: LiveData<Boolean>
}

/**
 * [PreferenceStorage] impl backed by [android.content.SharedPreferences].
 */
class SharedPreferenceStorage @Inject constructor(context: Context) :
    PreferenceStorage {

    private val prefs = context.applicationContext.getSharedPreferences(PREFS_NAME, MODE_PRIVATE)

    private val observableShowSnackbarResult = MutableLiveData<Boolean>()
    private val changeListener = OnSharedPreferenceChangeListener { _, key ->
        if (key == PREF_SNACKBAR_IS_STOPPED) {
            observableShowSnackbarResult.value = snackbarIsStopped
        }
    }

    init {
        prefs.registerOnSharedPreferenceChangeListener(changeListener)
    }

    override var onboardingCompleted
            by BooleanPreference(prefs, PREF_ONBOARDING, false)

    override var scheduleUiHintsShown: Boolean
            by BooleanPreference(prefs, PREF_SCHED_UI_HINTS_SHOWN, false)

    override var notificationsPreferenceShown
            by BooleanPreference(prefs, PREF_NOTIFICATIONS_SHOWN, false)

    override var preferToReceiveNotifications
            by BooleanPreference(prefs, PREF_RECEIVE_NOTIFICATIONS, true)

    override var snackbarIsStopped: Boolean
            by BooleanPreference(prefs, PREF_SNACKBAR_IS_STOPPED, false)

    override var observableSnackbarIsStopped: LiveData<Boolean>
        get() {
            observableShowSnackbarResult.value = snackbarIsStopped
            return observableShowSnackbarResult
        }
        set(value) = throw IllegalAccessException("This property can't be changed")

    companion object {
        private const val PREFS_NAME = "iosched"
        private const val PREF_ONBOARDING = "pref_onboarding"
        private const val PREF_SCHED_UI_HINTS_SHOWN = "pref_sched_ui_hints_shown"
        private const val PREF_NOTIFICATIONS_SHOWN = "pref_notifications_shown"
        private const val PREF_RECEIVE_NOTIFICATIONS = "pref_receive_notifications"
        private const val PREF_SNACKBAR_IS_STOPPED = "pref_snackbar_is_stopped"
    }
}

class BooleanPreference(private val preferences: SharedPreferences,
                        private val name: String,
                        private val defaultValue: Boolean): ReadWriteProperty<Any, Boolean> {

    @WorkerThread
    override fun getValue(thisRef: Any, property: KProperty<*>): Boolean {
        return preferences.getBoolean(name, defaultValue)
    }

    override fun setValue(thisRef: Any, property: KProperty<*>, value: Boolean) {
        preferences.edit { putBoolean(name, value) }
    }
}
{'fraction_non_alphanumeric': 0.06564193020032758, 'fraction_numerical': 0.0020158750157490235, 'mean_word_length': 3.822600243013366, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1798231', 'n_tokens_mistral': 2272, 'n_tokens_neox': 2181, 'n_words': 558}