2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.profiles;
20 import androidx.lifecycle.LifecycleOwner;
21 import androidx.lifecycle.MutableLiveData;
22 import androidx.lifecycle.Observer;
23 import androidx.lifecycle.ViewModel;
25 import net.ktnx.mobileledger.async.SendTransactionTask;
26 import net.ktnx.mobileledger.model.Currency;
27 import net.ktnx.mobileledger.model.MobileLedgerProfile;
28 import net.ktnx.mobileledger.utils.Misc;
30 public class ProfileDetailModel extends ViewModel {
31 private static final String HTTPS_URL_START = "https://";
32 private final MutableLiveData<String> profileName = new MutableLiveData<>();
33 private final MutableLiveData<Boolean> postingPermitted = new MutableLiveData<>(true);
34 private final MutableLiveData<Currency> defaultCommodity = new MutableLiveData<>(null);
35 private final MutableLiveData<MobileLedgerProfile.FutureDates> futureDates =
36 new MutableLiveData<>(MobileLedgerProfile.FutureDates.None);
37 private final MutableLiveData<Boolean> showCommodityByDefault = new MutableLiveData<>(false);
38 private final MutableLiveData<Boolean> showCommentsByDefault = new MutableLiveData<>(true);
39 private final MutableLiveData<Boolean> useAuthentication = new MutableLiveData<>(false);
40 private final MutableLiveData<SendTransactionTask.API> apiVersion =
41 new MutableLiveData<>(SendTransactionTask.API.auto);
42 private final MutableLiveData<String> url = new MutableLiveData<>(null);
43 private final MutableLiveData<String> authUserName = new MutableLiveData<>(null);
44 private final MutableLiveData<String> authPassword = new MutableLiveData<>(null);
45 private final MutableLiveData<String> preferredAccountsFilter = new MutableLiveData<>(null);
46 private final MutableLiveData<Integer> themeId = new MutableLiveData<>(-1);
47 public ProfileDetailModel() {
49 String getProfileName() {
50 return profileName.getValue();
52 void setProfileName(String newValue) {
53 if (!Misc.nullIsEmpty(newValue)
54 .equals(Misc.nullIsEmpty(profileName.getValue())))
55 profileName.setValue(newValue);
57 void setProfileName(CharSequence newValue) {
58 setProfileName(String.valueOf(newValue));
60 void observeProfileName(LifecycleOwner lfo, Observer<String> o) {
61 profileName.observe(lfo, o);
63 Boolean getPostingPermitted() {
64 return postingPermitted.getValue();
66 void setPostingPermitted(boolean newValue) {
67 if (newValue != postingPermitted.getValue())
68 postingPermitted.setValue(newValue);
70 void observePostingPermitted(LifecycleOwner lfo, Observer<Boolean> o) {
71 postingPermitted.observe(lfo, o);
73 public void setShowCommentsByDefault(boolean newValue) {
74 if (newValue != showCommentsByDefault.getValue())
75 showCommentsByDefault.setValue(newValue);
77 void observeShowCommentsByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
78 showCommentsByDefault.observe(lfo, o);
80 MobileLedgerProfile.FutureDates getFutureDates() {
81 return futureDates.getValue();
83 void setFutureDates(MobileLedgerProfile.FutureDates newValue) {
84 if (newValue != futureDates.getValue())
85 futureDates.setValue(newValue);
87 void observeFutureDates(LifecycleOwner lfo, Observer<MobileLedgerProfile.FutureDates> o) {
88 futureDates.observe(lfo, o);
90 Currency getDefaultCommodity() {
91 return defaultCommodity.getValue();
93 void setDefaultCommodity(Currency newValue) {
94 if (newValue != defaultCommodity.getValue())
95 defaultCommodity.setValue(newValue);
97 void observeDefaultCommodity(LifecycleOwner lfo, Observer<Currency> o) {
98 defaultCommodity.observe(lfo, o);
100 Boolean getShowCommodityByDefault() {
101 return showCommodityByDefault.getValue();
103 void setShowCommodityByDefault(boolean newValue) {
104 if (newValue != showCommodityByDefault.getValue())
105 showCommodityByDefault.setValue(newValue);
107 void observeShowCommodityByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
108 showCommodityByDefault.observe(lfo, o);
110 Boolean getUseAuthentication() {
111 return useAuthentication.getValue();
113 void setUseAuthentication(boolean newValue) {
114 if (newValue != useAuthentication.getValue())
115 useAuthentication.setValue(newValue);
117 void observeUseAuthentication(LifecycleOwner lfo, Observer<Boolean> o) {
118 useAuthentication.observe(lfo, o);
120 SendTransactionTask.API getApiVersion() {
121 return apiVersion.getValue();
123 void setApiVersion(SendTransactionTask.API newValue) {
124 if (newValue != apiVersion.getValue())
125 apiVersion.setValue(newValue);
127 void observeApiVersion(LifecycleOwner lfo, Observer<SendTransactionTask.API> o) {
128 apiVersion.observe(lfo, o);
131 return url.getValue();
133 void setUrl(String newValue) {
134 if (!Misc.nullIsEmpty(newValue)
135 .equals(Misc.nullIsEmpty(url.getValue())))
136 url.setValue(newValue);
138 void setUrl(CharSequence newValue) {
139 setUrl(String.valueOf(newValue));
141 void observeUrl(LifecycleOwner lfo, Observer<String> o) {
144 String getAuthUserName() {
145 return authUserName.getValue();
147 void setAuthUserName(String newValue) {
148 if (!Misc.nullIsEmpty(newValue)
149 .equals(Misc.nullIsEmpty(authUserName.getValue())))
150 authUserName.setValue(newValue);
152 void setAuthUserName(CharSequence newValue) {
153 setAuthUserName(String.valueOf(newValue));
155 void observeUserName(LifecycleOwner lfo, Observer<String> o) {
156 authUserName.observe(lfo, o);
158 String getAuthPassword() {
159 return authPassword.getValue();
161 void setAuthPassword(String newValue) {
162 if (!Misc.nullIsEmpty(newValue)
163 .equals(Misc.nullIsEmpty(authPassword.getValue())))
164 authPassword.setValue(newValue);
166 void setAuthPassword(CharSequence newValue) {
167 setAuthPassword(String.valueOf(newValue));
169 void observePassword(LifecycleOwner lfo, Observer<String> o) {
170 authPassword.observe(lfo, o);
172 String getPreferredAccountsFilter() {
173 return preferredAccountsFilter.getValue();
175 void setPreferredAccountsFilter(String newValue) {
176 if (!Misc.nullIsEmpty(newValue)
177 .equals(Misc.nullIsEmpty(preferredAccountsFilter.getValue())))
178 preferredAccountsFilter.setValue(newValue);
180 void setPreferredAccountsFilter(CharSequence newValue) {
181 setPreferredAccountsFilter(String.valueOf(newValue));
183 void observePreferredAccountsFilter(LifecycleOwner lfo, Observer<String> o) {
184 preferredAccountsFilter.observe(lfo, o);
187 return themeId.getValue();
189 void setThemeId(int newValue) {
190 themeId.setValue(newValue);
192 void observeThemeId(LifecycleOwner lfo, Observer<Integer> o) {
193 themeId.observe(lfo, o);
195 void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) {
196 final int profileThemeId;
197 if (mProfile != null) {
198 profileName.setValue(mProfile.getName());
199 postingPermitted.setValue(mProfile.isPostingPermitted());
200 showCommentsByDefault.setValue(mProfile.getShowCommentsByDefault());
201 showCommodityByDefault.setValue(mProfile.getShowCommodityByDefault());
203 String comm = mProfile.getDefaultCommodity();
204 if (Misc.isEmptyOrNull(comm))
205 setDefaultCommodity(null);
207 setDefaultCommodity(new Currency(-1, comm));
209 futureDates.setValue(mProfile.getFutureDates());
210 apiVersion.setValue(mProfile.getApiVersion());
211 url.setValue(mProfile.getUrl());
212 useAuthentication.setValue(mProfile.isAuthEnabled());
213 authUserName.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
214 authPassword.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
215 preferredAccountsFilter.setValue(mProfile.getPreferredAccountsFilter());
216 themeId.setValue(mProfile.getThemeHue());
219 profileName.setValue(null);
220 url.setValue(HTTPS_URL_START);
221 postingPermitted.setValue(true);
222 showCommentsByDefault.setValue(true);
223 showCommodityByDefault.setValue(false);
224 setFutureDates(MobileLedgerProfile.FutureDates.None);
225 setApiVersion(SendTransactionTask.API.auto);
226 useAuthentication.setValue(false);
227 authUserName.setValue("");
228 authPassword.setValue("");
229 preferredAccountsFilter.setValue(null);
230 themeId.setValue(newProfileHue);
235 void updateProfile(MobileLedgerProfile mProfile) {
236 mProfile.setName(profileName.getValue());
237 mProfile.setUrl(url.getValue());
238 mProfile.setPostingPermitted(postingPermitted.getValue());
239 mProfile.setShowCommentsByDefault(showCommentsByDefault.getValue());
240 Currency c = defaultCommodity.getValue();
241 mProfile.setDefaultCommodity((c == null) ? null : c.getName());
242 mProfile.setShowCommodityByDefault(showCommodityByDefault.getValue());
243 mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getValue());
244 mProfile.setAuthEnabled(useAuthentication.getValue());
245 mProfile.setAuthUserName(authUserName.getValue());
246 mProfile.setAuthPassword(authPassword.getValue());
247 mProfile.setThemeHue(themeId.getValue());
248 mProfile.setFutureDates(futureDates.getValue());
249 mProfile.setApiVersion(apiVersion.getValue());