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 android.text.TextUtils;
22 import androidx.lifecycle.LifecycleOwner;
23 import androidx.lifecycle.MutableLiveData;
24 import androidx.lifecycle.Observer;
25 import androidx.lifecycle.ViewModel;
27 import net.ktnx.mobileledger.async.SendTransactionTask;
28 import net.ktnx.mobileledger.model.Currency;
29 import net.ktnx.mobileledger.model.MobileLedgerProfile;
30 import net.ktnx.mobileledger.utils.Colors;
31 import net.ktnx.mobileledger.utils.Misc;
33 public class ProfileDetailModel extends ViewModel {
34 private static final String HTTPS_URL_START = "https://";
35 private final MutableLiveData<String> profileName = new MutableLiveData<>();
36 private final MutableLiveData<Boolean> postingPermitted = new MutableLiveData<>(true);
37 private final MutableLiveData<Currency> defaultCommodity = new MutableLiveData<>(null);
38 private final MutableLiveData<MobileLedgerProfile.FutureDates> futureDates =
39 new MutableLiveData<>(MobileLedgerProfile.FutureDates.None);
40 private final MutableLiveData<Boolean> showCommodityByDefault = new MutableLiveData<>(false);
41 private final MutableLiveData<Boolean> showCommentsByDefault = new MutableLiveData<>(true);
42 private final MutableLiveData<Boolean> useAuthentication = new MutableLiveData<>(false);
43 private final MutableLiveData<SendTransactionTask.API> apiVersion =
44 new MutableLiveData<>(SendTransactionTask.API.auto);
45 private final MutableLiveData<String> url = new MutableLiveData<>(null);
46 private final MutableLiveData<String> authUserName = new MutableLiveData<>(null);
47 private final MutableLiveData<String> authPassword = new MutableLiveData<>(null);
48 private final MutableLiveData<String> preferredAccountsFilter = new MutableLiveData<>(null);
49 private final MutableLiveData<Integer> themeId = new MutableLiveData<>(-1);
50 public int initialThemeHue = Colors.DEFAULT_HUE_DEG;
51 public ProfileDetailModel() {
53 String getProfileName() {
54 return profileName.getValue();
56 void setProfileName(String newValue) {
57 if (!Misc.nullIsEmpty(newValue)
58 .equals(Misc.nullIsEmpty(profileName.getValue())))
59 profileName.setValue(newValue);
61 void setProfileName(CharSequence newValue) {
62 setProfileName(String.valueOf(newValue));
64 void observeProfileName(LifecycleOwner lfo, Observer<String> o) {
65 profileName.observe(lfo, o);
67 Boolean getPostingPermitted() {
68 return postingPermitted.getValue();
70 void setPostingPermitted(boolean newValue) {
71 if (newValue != postingPermitted.getValue())
72 postingPermitted.setValue(newValue);
74 void observePostingPermitted(LifecycleOwner lfo, Observer<Boolean> o) {
75 postingPermitted.observe(lfo, o);
77 public void setShowCommentsByDefault(boolean newValue) {
78 if (newValue != showCommentsByDefault.getValue())
79 showCommentsByDefault.setValue(newValue);
81 void observeShowCommentsByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
82 showCommentsByDefault.observe(lfo, o);
84 MobileLedgerProfile.FutureDates getFutureDates() {
85 return futureDates.getValue();
87 void setFutureDates(MobileLedgerProfile.FutureDates newValue) {
88 if (newValue != futureDates.getValue())
89 futureDates.setValue(newValue);
91 void observeFutureDates(LifecycleOwner lfo, Observer<MobileLedgerProfile.FutureDates> o) {
92 futureDates.observe(lfo, o);
94 Currency getDefaultCommodity() {
95 return defaultCommodity.getValue();
97 void setDefaultCommodity(Currency newValue) {
98 if (newValue != defaultCommodity.getValue())
99 defaultCommodity.setValue(newValue);
101 void observeDefaultCommodity(LifecycleOwner lfo, Observer<Currency> o) {
102 defaultCommodity.observe(lfo, o);
104 Boolean getShowCommodityByDefault() {
105 return showCommodityByDefault.getValue();
107 void setShowCommodityByDefault(boolean newValue) {
108 if (newValue != showCommodityByDefault.getValue())
109 showCommodityByDefault.setValue(newValue);
111 void observeShowCommodityByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
112 showCommodityByDefault.observe(lfo, o);
114 Boolean getUseAuthentication() {
115 return useAuthentication.getValue();
117 void setUseAuthentication(boolean newValue) {
118 if (newValue != useAuthentication.getValue())
119 useAuthentication.setValue(newValue);
121 void observeUseAuthentication(LifecycleOwner lfo, Observer<Boolean> o) {
122 useAuthentication.observe(lfo, o);
124 SendTransactionTask.API getApiVersion() {
125 return apiVersion.getValue();
127 void setApiVersion(SendTransactionTask.API newValue) {
128 if (newValue != apiVersion.getValue())
129 apiVersion.setValue(newValue);
131 void observeApiVersion(LifecycleOwner lfo, Observer<SendTransactionTask.API> o) {
132 apiVersion.observe(lfo, o);
135 return url.getValue();
137 void setUrl(String newValue) {
138 if (!Misc.nullIsEmpty(newValue)
139 .equals(Misc.nullIsEmpty(url.getValue())))
140 url.setValue(newValue);
142 void setUrl(CharSequence newValue) {
143 setUrl(String.valueOf(newValue));
145 void observeUrl(LifecycleOwner lfo, Observer<String> o) {
148 String getAuthUserName() {
149 return authUserName.getValue();
151 void setAuthUserName(String newValue) {
152 if (!Misc.nullIsEmpty(newValue)
153 .equals(Misc.nullIsEmpty(authUserName.getValue())))
154 authUserName.setValue(newValue);
156 void setAuthUserName(CharSequence newValue) {
157 setAuthUserName(String.valueOf(newValue));
159 void observeUserName(LifecycleOwner lfo, Observer<String> o) {
160 authUserName.observe(lfo, o);
162 String getAuthPassword() {
163 return authPassword.getValue();
165 void setAuthPassword(String newValue) {
166 if (!Misc.nullIsEmpty(newValue)
167 .equals(Misc.nullIsEmpty(authPassword.getValue())))
168 authPassword.setValue(newValue);
170 void setAuthPassword(CharSequence newValue) {
171 setAuthPassword(String.valueOf(newValue));
173 void observePassword(LifecycleOwner lfo, Observer<String> o) {
174 authPassword.observe(lfo, o);
176 String getPreferredAccountsFilter() {
177 return preferredAccountsFilter.getValue();
179 void setPreferredAccountsFilter(String newValue) {
180 if (!Misc.nullIsEmpty(newValue)
181 .equals(Misc.nullIsEmpty(preferredAccountsFilter.getValue())))
182 preferredAccountsFilter.setValue(newValue);
184 void setPreferredAccountsFilter(CharSequence newValue) {
185 setPreferredAccountsFilter(String.valueOf(newValue));
187 void observePreferredAccountsFilter(LifecycleOwner lfo, Observer<String> o) {
188 preferredAccountsFilter.observe(lfo, o);
191 return themeId.getValue();
193 void setThemeId(int newValue) {
194 themeId.setValue(newValue);
196 void observeThemeId(LifecycleOwner lfo, Observer<Integer> o) {
197 themeId.observe(lfo, o);
199 void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) {
200 final int profileThemeId;
201 if (mProfile != null) {
202 profileName.setValue(mProfile.getName());
203 postingPermitted.setValue(mProfile.isPostingPermitted());
204 showCommentsByDefault.setValue(mProfile.getShowCommentsByDefault());
205 showCommodityByDefault.setValue(mProfile.getShowCommodityByDefault());
207 String comm = mProfile.getDefaultCommodity();
208 if (TextUtils.isEmpty(comm))
209 setDefaultCommodity(null);
211 setDefaultCommodity(new Currency(-1, comm));
213 futureDates.setValue(mProfile.getFutureDates());
214 apiVersion.setValue(mProfile.getApiVersion());
215 url.setValue(mProfile.getUrl());
216 useAuthentication.setValue(mProfile.isAuthEnabled());
217 authUserName.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
218 authPassword.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
219 preferredAccountsFilter.setValue(mProfile.getPreferredAccountsFilter());
220 themeId.setValue(mProfile.getThemeHue());
223 profileName.setValue(null);
224 url.setValue(HTTPS_URL_START);
225 postingPermitted.setValue(true);
226 showCommentsByDefault.setValue(true);
227 showCommodityByDefault.setValue(false);
228 setFutureDates(MobileLedgerProfile.FutureDates.None);
229 setApiVersion(SendTransactionTask.API.auto);
230 useAuthentication.setValue(false);
231 authUserName.setValue("");
232 authPassword.setValue("");
233 preferredAccountsFilter.setValue(null);
234 themeId.setValue(newProfileHue);
239 void updateProfile(MobileLedgerProfile mProfile) {
240 mProfile.setName(profileName.getValue());
241 mProfile.setUrl(url.getValue());
242 mProfile.setPostingPermitted(postingPermitted.getValue());
243 mProfile.setShowCommentsByDefault(showCommentsByDefault.getValue());
244 Currency c = defaultCommodity.getValue();
245 mProfile.setDefaultCommodity((c == null) ? null : c.getName());
246 mProfile.setShowCommodityByDefault(showCommodityByDefault.getValue());
247 mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getValue());
248 mProfile.setAuthEnabled(useAuthentication.getValue());
249 mProfile.setAuthUserName(authUserName.getValue());
250 mProfile.setAuthPassword(authPassword.getValue());
251 mProfile.setThemeHue(themeId.getValue());
252 mProfile.setFutureDates(futureDates.getValue());
253 mProfile.setApiVersion(apiVersion.getValue());