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.Colors;
29 import net.ktnx.mobileledger.utils.Misc;
31 public class ProfileDetailModel extends ViewModel {
32 private static final String HTTPS_URL_START = "https://";
33 private final MutableLiveData<String> profileName = new MutableLiveData<>();
34 private final MutableLiveData<Boolean> postingPermitted = new MutableLiveData<>(true);
35 private final MutableLiveData<Currency> defaultCommodity = new MutableLiveData<>(null);
36 private final MutableLiveData<MobileLedgerProfile.FutureDates> futureDates =
37 new MutableLiveData<>(MobileLedgerProfile.FutureDates.None);
38 private final MutableLiveData<Boolean> showCommodityByDefault = new MutableLiveData<>(false);
39 private final MutableLiveData<Boolean> showCommentsByDefault = new MutableLiveData<>(true);
40 private final MutableLiveData<Boolean> useAuthentication = new MutableLiveData<>(false);
41 private final MutableLiveData<SendTransactionTask.API> apiVersion =
42 new MutableLiveData<>(SendTransactionTask.API.auto);
43 private final MutableLiveData<String> url = new MutableLiveData<>(null);
44 private final MutableLiveData<String> authUserName = new MutableLiveData<>(null);
45 private final MutableLiveData<String> authPassword = new MutableLiveData<>(null);
46 private final MutableLiveData<String> preferredAccountsFilter = new MutableLiveData<>(null);
47 private final MutableLiveData<Integer> themeId = new MutableLiveData<>(-1);
48 public int initialThemeHue = Colors.DEFAULT_HUE_DEG;
49 public ProfileDetailModel() {
51 String getProfileName() {
52 return profileName.getValue();
54 void setProfileName(String newValue) {
55 if (!Misc.nullIsEmpty(newValue)
56 .equals(Misc.nullIsEmpty(profileName.getValue())))
57 profileName.setValue(newValue);
59 void setProfileName(CharSequence newValue) {
60 setProfileName(String.valueOf(newValue));
62 void observeProfileName(LifecycleOwner lfo, Observer<String> o) {
63 profileName.observe(lfo, o);
65 Boolean getPostingPermitted() {
66 return postingPermitted.getValue();
68 void setPostingPermitted(boolean newValue) {
69 if (newValue != postingPermitted.getValue())
70 postingPermitted.setValue(newValue);
72 void observePostingPermitted(LifecycleOwner lfo, Observer<Boolean> o) {
73 postingPermitted.observe(lfo, o);
75 public void setShowCommentsByDefault(boolean newValue) {
76 if (newValue != showCommentsByDefault.getValue())
77 showCommentsByDefault.setValue(newValue);
79 void observeShowCommentsByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
80 showCommentsByDefault.observe(lfo, o);
82 MobileLedgerProfile.FutureDates getFutureDates() {
83 return futureDates.getValue();
85 void setFutureDates(MobileLedgerProfile.FutureDates newValue) {
86 if (newValue != futureDates.getValue())
87 futureDates.setValue(newValue);
89 void observeFutureDates(LifecycleOwner lfo, Observer<MobileLedgerProfile.FutureDates> o) {
90 futureDates.observe(lfo, o);
92 Currency getDefaultCommodity() {
93 return defaultCommodity.getValue();
95 void setDefaultCommodity(Currency newValue) {
96 if (newValue != defaultCommodity.getValue())
97 defaultCommodity.setValue(newValue);
99 void observeDefaultCommodity(LifecycleOwner lfo, Observer<Currency> o) {
100 defaultCommodity.observe(lfo, o);
102 Boolean getShowCommodityByDefault() {
103 return showCommodityByDefault.getValue();
105 void setShowCommodityByDefault(boolean newValue) {
106 if (newValue != showCommodityByDefault.getValue())
107 showCommodityByDefault.setValue(newValue);
109 void observeShowCommodityByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
110 showCommodityByDefault.observe(lfo, o);
112 Boolean getUseAuthentication() {
113 return useAuthentication.getValue();
115 void setUseAuthentication(boolean newValue) {
116 if (newValue != useAuthentication.getValue())
117 useAuthentication.setValue(newValue);
119 void observeUseAuthentication(LifecycleOwner lfo, Observer<Boolean> o) {
120 useAuthentication.observe(lfo, o);
122 SendTransactionTask.API getApiVersion() {
123 return apiVersion.getValue();
125 void setApiVersion(SendTransactionTask.API newValue) {
126 if (newValue != apiVersion.getValue())
127 apiVersion.setValue(newValue);
129 void observeApiVersion(LifecycleOwner lfo, Observer<SendTransactionTask.API> o) {
130 apiVersion.observe(lfo, o);
133 return url.getValue();
135 void setUrl(String newValue) {
136 if (!Misc.nullIsEmpty(newValue)
137 .equals(Misc.nullIsEmpty(url.getValue())))
138 url.setValue(newValue);
140 void setUrl(CharSequence newValue) {
141 setUrl(String.valueOf(newValue));
143 void observeUrl(LifecycleOwner lfo, Observer<String> o) {
146 String getAuthUserName() {
147 return authUserName.getValue();
149 void setAuthUserName(String newValue) {
150 if (!Misc.nullIsEmpty(newValue)
151 .equals(Misc.nullIsEmpty(authUserName.getValue())))
152 authUserName.setValue(newValue);
154 void setAuthUserName(CharSequence newValue) {
155 setAuthUserName(String.valueOf(newValue));
157 void observeUserName(LifecycleOwner lfo, Observer<String> o) {
158 authUserName.observe(lfo, o);
160 String getAuthPassword() {
161 return authPassword.getValue();
163 void setAuthPassword(String newValue) {
164 if (!Misc.nullIsEmpty(newValue)
165 .equals(Misc.nullIsEmpty(authPassword.getValue())))
166 authPassword.setValue(newValue);
168 void setAuthPassword(CharSequence newValue) {
169 setAuthPassword(String.valueOf(newValue));
171 void observePassword(LifecycleOwner lfo, Observer<String> o) {
172 authPassword.observe(lfo, o);
174 String getPreferredAccountsFilter() {
175 return preferredAccountsFilter.getValue();
177 void setPreferredAccountsFilter(String newValue) {
178 if (!Misc.nullIsEmpty(newValue)
179 .equals(Misc.nullIsEmpty(preferredAccountsFilter.getValue())))
180 preferredAccountsFilter.setValue(newValue);
182 void setPreferredAccountsFilter(CharSequence newValue) {
183 setPreferredAccountsFilter(String.valueOf(newValue));
185 void observePreferredAccountsFilter(LifecycleOwner lfo, Observer<String> o) {
186 preferredAccountsFilter.observe(lfo, o);
189 return themeId.getValue();
191 void setThemeId(int newValue) {
192 themeId.setValue(newValue);
194 void observeThemeId(LifecycleOwner lfo, Observer<Integer> o) {
195 themeId.observe(lfo, o);
197 void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) {
198 final int profileThemeId;
199 if (mProfile != null) {
200 profileName.setValue(mProfile.getName());
201 postingPermitted.setValue(mProfile.isPostingPermitted());
202 showCommentsByDefault.setValue(mProfile.getShowCommentsByDefault());
203 showCommodityByDefault.setValue(mProfile.getShowCommodityByDefault());
205 String comm = mProfile.getDefaultCommodity();
206 if (Misc.isEmptyOrNull(comm))
207 setDefaultCommodity(null);
209 setDefaultCommodity(new Currency(-1, comm));
211 futureDates.setValue(mProfile.getFutureDates());
212 apiVersion.setValue(mProfile.getApiVersion());
213 url.setValue(mProfile.getUrl());
214 useAuthentication.setValue(mProfile.isAuthEnabled());
215 authUserName.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
216 authPassword.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
217 preferredAccountsFilter.setValue(mProfile.getPreferredAccountsFilter());
218 themeId.setValue(mProfile.getThemeHue());
221 profileName.setValue(null);
222 url.setValue(HTTPS_URL_START);
223 postingPermitted.setValue(true);
224 showCommentsByDefault.setValue(true);
225 showCommodityByDefault.setValue(false);
226 setFutureDates(MobileLedgerProfile.FutureDates.None);
227 setApiVersion(SendTransactionTask.API.auto);
228 useAuthentication.setValue(false);
229 authUserName.setValue("");
230 authPassword.setValue("");
231 preferredAccountsFilter.setValue(null);
232 themeId.setValue(newProfileHue);
237 void updateProfile(MobileLedgerProfile mProfile) {
238 mProfile.setName(profileName.getValue());
239 mProfile.setUrl(url.getValue());
240 mProfile.setPostingPermitted(postingPermitted.getValue());
241 mProfile.setShowCommentsByDefault(showCommentsByDefault.getValue());
242 Currency c = defaultCommodity.getValue();
243 mProfile.setDefaultCommodity((c == null) ? null : c.getName());
244 mProfile.setShowCommodityByDefault(showCommodityByDefault.getValue());
245 mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getValue());
246 mProfile.setAuthEnabled(useAuthentication.getValue());
247 mProfile.setAuthUserName(authUserName.getValue());
248 mProfile.setAuthPassword(authPassword.getValue());
249 mProfile.setThemeHue(themeId.getValue());
250 mProfile.setFutureDates(futureDates.getValue());
251 mProfile.setApiVersion(apiVersion.getValue());