summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/features/settings/model/view/CheckBoxSetting.java
blob: c5c4e14f35afa24ccd6ccb9e8d0c3241d2718864 (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
package org.yuzu.yuzu_emu.features.settings.model.view;

import org.yuzu.yuzu_emu.YuzuApplication;
import org.yuzu.yuzu_emu.R;
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;

public final class CheckBoxSetting extends SettingsItem {
    private boolean mDefaultValue;
    private boolean mShowPerformanceWarning;
    private SettingsFragmentView mView;

    public CheckBoxSetting(String key, String section, int titleId, int descriptionId,
                           boolean defaultValue, Setting setting) {
        super(key, section, setting, titleId, descriptionId);
        mDefaultValue = defaultValue;
        mShowPerformanceWarning = false;
    }

    public CheckBoxSetting(String key, String section, int titleId, int descriptionId,
                           boolean defaultValue, Setting setting, boolean show_performance_warning, SettingsFragmentView view) {
        super(key, section, setting, titleId, descriptionId);
        mDefaultValue = defaultValue;
        mView = view;
        mShowPerformanceWarning = show_performance_warning;
    }

    public boolean isChecked() {
        if (getSetting() == null) {
            return mDefaultValue;
        }

        // Try integer setting
        try {
            IntSetting setting = (IntSetting) getSetting();
            return setting.getValue() == 1;
        } catch (ClassCastException exception) {
        }

        // Try boolean setting
        try {
            BooleanSetting setting = (BooleanSetting) getSetting();
            return setting.getValue() == true;
        } catch (ClassCastException exception) {
        }

        return mDefaultValue;
    }

    /**
     * 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.
     */
    public IntSetting setChecked(boolean checked) {
        // Show a performance warning if the setting has been disabled
        if (mShowPerformanceWarning && !checked) {
            mView.showToastMessage(YuzuApplication.getAppContext().getString(R.string.performance_warning), true);
        }

        if (getSetting() == null) {
            IntSetting setting = new IntSetting(getKey(), getSection(), checked ? 1 : 0);
            setSetting(setting);
            return setting;
        } else {
            IntSetting setting = (IntSetting) getSetting();
            setting.setValue(checked ? 1 : 0);
            return null;
        }
    }

    @Override
    public int getType() {
        return TYPE_CHECKBOX;
    }
}