summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/SwitchSetting.kt
blob: b793012ccc837ac576906fb0e5c6c4edcc0a6a23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// SPDX-FileCopyrightText: 2023 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package org.yuzu.yuzu_emu.features.settings.model.view

import org.yuzu.yuzu_emu.R
import org.yuzu.yuzu_emu.YuzuApplication
import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting
import org.yuzu.yuzu_emu.features.settings.model.IntSetting
import org.yuzu.yuzu_emu.features.settings.model.Setting
import org.yuzu.yuzu_emu.features.settings.ui.SettingsFragmentView

class SwitchSetting : SettingsItem {
    override val type = TYPE_SWITCH

    private var defaultValue: Boolean
    private var showPerformanceWarning: Boolean
    private var fragmentView: SettingsFragmentView? = null

    constructor(
        key: String,
        section: String,
        setting: Setting?,
        titleId: Int,
        descriptionId: Int,
        defaultValue: Boolean
    ) : super(key, section, setting, titleId, descriptionId) {
        this.defaultValue = defaultValue
        showPerformanceWarning = false
    }

    constructor(
        key: String,
        section: String,
        titleId: Int,
        descriptionId: Int,
        defaultValue: Boolean,
        setting: Setting,
        show_performance_warning: Boolean,
        view: SettingsFragmentView
    ) : super(key, section, setting, titleId, descriptionId) {
        this.defaultValue = defaultValue
        fragmentView = view
        showPerformanceWarning = show_performance_warning
    }

    val isChecked: Boolean
        get() {
            if (setting == null) {
                return defaultValue
            }

            // Try integer setting
            try {
                val setting = setting as IntSetting
                return setting.value == 1
            } catch (_: ClassCastException) {
            }

            // Try boolean setting
            try {
                val setting = setting as BooleanSetting
                return setting.value
            } catch (_: ClassCastException) {
            }
            return defaultValue
        }

    /**
     * Write a value to the backing boolean. If that boolean was previously null,
     * initializes a new one and returns it, so it can be added to the Hashmap.
     *
     * @param checked Pretty self explanatory.
     * @return null if overwritten successfully; otherwise, a newly created BooleanSetting.
     */
    fun setChecked(checked: Boolean): IntSetting? {
        // Show a performance warning if the setting has been disabled
        if (showPerformanceWarning && !checked) {
            fragmentView!!.showToastMessage(
                YuzuApplication.appContext.getString(R.string.performance_warning), true
            )
        }

        return if (setting == null) {
            val newSetting = IntSetting(key!!, section!!, if (checked) 1 else 0)
            setting = newSetting
            newSetting
        } else {
            val newSetting = setting as IntSetting
            newSetting.value = if (checked) 1 else 0
            null
        }
    }
}