summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/citra/citra_emu/features/cheats/model/CheatsViewModel.java
blob: 66f4202d83bcae1f8b3b12bf5ee0d214b37c2b48 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package org.citra.citra_emu.features.cheats.model;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

public class CheatsViewModel extends ViewModel {
    private int mSelectedCheatPosition = -1;
    private final MutableLiveData<Cheat> mSelectedCheat = new MutableLiveData<>(null);
    private final MutableLiveData<Boolean> mIsAdding = new MutableLiveData<>(false);
    private final MutableLiveData<Boolean> mIsEditing = new MutableLiveData<>(false);

    private final MutableLiveData<Integer> mCheatAddedEvent = new MutableLiveData<>(null);
    private final MutableLiveData<Integer> mCheatChangedEvent = new MutableLiveData<>(null);
    private final MutableLiveData<Integer> mCheatDeletedEvent = new MutableLiveData<>(null);
    private final MutableLiveData<Boolean> mOpenDetailsViewEvent = new MutableLiveData<>(false);

    private Cheat[] mCheats;
    private boolean mCheatsNeedSaving = false;

    public void load() {
        mCheats = CheatEngine.getCheats();

        for (int i = 0; i < mCheats.length; i++) {
            int position = i;
            mCheats[i].setEnabledChangedCallback(() -> {
                mCheatsNeedSaving = true;
                notifyCheatUpdated(position);
            });
        }
    }

    public void saveIfNeeded() {
        if (mCheatsNeedSaving) {
            CheatEngine.saveCheatFile();
            mCheatsNeedSaving = false;
        }
    }

    public Cheat[] getCheats() {
        return mCheats;
    }

    public LiveData<Cheat> getSelectedCheat() {
        return mSelectedCheat;
    }

    public void setSelectedCheat(Cheat cheat, int position) {
        if (mIsEditing.getValue()) {
            setIsEditing(false);
        }

        mSelectedCheat.setValue(cheat);
        mSelectedCheatPosition = position;
    }

    public LiveData<Boolean> getIsAdding() {
        return mIsAdding;
    }

    public LiveData<Boolean> getIsEditing() {
        return mIsEditing;
    }

    public void setIsEditing(boolean isEditing) {
        mIsEditing.setValue(isEditing);

        if (mIsAdding.getValue() && !isEditing) {
            mIsAdding.setValue(false);
            setSelectedCheat(null, -1);
        }
    }

    /**
     * When a cheat is added, the integer stored in the returned LiveData
     * changes to the position of that cheat, then changes back to null.
     */
    public LiveData<Integer> getCheatAddedEvent() {
        return mCheatAddedEvent;
    }

    private void notifyCheatAdded(int position) {
        mCheatAddedEvent.setValue(position);
        mCheatAddedEvent.setValue(null);
    }

    public void startAddingCheat() {
        mSelectedCheat.setValue(null);
        mSelectedCheatPosition = -1;

        mIsAdding.setValue(true);
        mIsEditing.setValue(true);
    }

    public void finishAddingCheat(Cheat cheat) {
        if (!mIsAdding.getValue()) {
            throw new IllegalStateException();
        }

        mIsAdding.setValue(false);
        mIsEditing.setValue(false);

        int position = mCheats.length;

        CheatEngine.addCheat(cheat);

        mCheatsNeedSaving = true;
        load();

        notifyCheatAdded(position);
        setSelectedCheat(mCheats[position], position);
    }

    /**
     * When a cheat is edited, the integer stored in the returned LiveData
     * changes to the position of that cheat, then changes back to null.
     */
    public LiveData<Integer> getCheatUpdatedEvent() {
        return mCheatChangedEvent;
    }

    /**
     * Notifies that an edit has been made to the contents of the cheat at the given position.
     */
    private void notifyCheatUpdated(int position) {
        mCheatChangedEvent.setValue(position);
        mCheatChangedEvent.setValue(null);
    }

    public void updateSelectedCheat(Cheat newCheat) {
        CheatEngine.updateCheat(mSelectedCheatPosition, newCheat);

        mCheatsNeedSaving = true;
        load();

        notifyCheatUpdated(mSelectedCheatPosition);
        setSelectedCheat(mCheats[mSelectedCheatPosition], mSelectedCheatPosition);
    }

    /**
     * When a cheat is deleted, the integer stored in the returned LiveData
     * changes to the position of that cheat, then changes back to null.
     */
    public LiveData<Integer> getCheatDeletedEvent() {
        return mCheatDeletedEvent;
    }

    /**
     * Notifies that the cheat at the given position has been deleted.
     */
    private void notifyCheatDeleted(int position) {
        mCheatDeletedEvent.setValue(position);
        mCheatDeletedEvent.setValue(null);
    }

    public void deleteSelectedCheat() {
        int position = mSelectedCheatPosition;

        setSelectedCheat(null, -1);

        CheatEngine.removeCheat(position);

        mCheatsNeedSaving = true;
        load();

        notifyCheatDeleted(position);
    }

    public LiveData<Boolean> getOpenDetailsViewEvent() {
        return mOpenDetailsViewEvent;
    }

    public void openDetailsView() {
        mOpenDetailsViewEvent.setValue(true);
        mOpenDetailsViewEvent.setValue(false);
    }
}