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> useAuthentication = new MutableLiveData<>(false);
39 private final MutableLiveData<SendTransactionTask.API> apiVersion =
40 new MutableLiveData<>(SendTransactionTask.API.auto);
41 private final MutableLiveData<String> url = new MutableLiveData<>(null);
42 private final MutableLiveData<String> authUserName = new MutableLiveData<>(null);
43 private final MutableLiveData<String> authPassword = new MutableLiveData<>(null);
44 private final MutableLiveData<String> preferredAccountsFilter = new MutableLiveData<>(null);
45 private final MutableLiveData<Integer> themeId = new MutableLiveData<>(-1);
46 public ProfileDetailModel() {
48 String getProfileName() {
49 return profileName.getValue();
51 void setProfileName(String newValue) {
52 if (!Misc.nullIsEmpty(newValue)
53 .equals(Misc.nullIsEmpty(profileName.getValue())))
54 profileName.setValue(newValue);
56 void setProfileName(CharSequence newValue) {
57 setProfileName(String.valueOf(newValue));
59 void observeProfileName(LifecycleOwner lfo, Observer<String> o) {
60 profileName.observe(lfo, o);
62 Boolean getPostingPermitted() {
63 return postingPermitted.getValue();
65 void setPostingPermitted(boolean newValue) {
66 if (newValue != postingPermitted.getValue())
67 postingPermitted.setValue(newValue);
69 void observePostingPermitted(LifecycleOwner lfo, Observer<Boolean> o) {
70 postingPermitted.observe(lfo, o);
72 MobileLedgerProfile.FutureDates getFutureDates() {
73 return futureDates.getValue();
75 void setFutureDates(MobileLedgerProfile.FutureDates newValue) {
76 if (newValue != futureDates.getValue())
77 futureDates.setValue(newValue);
79 void observeFutureDates(LifecycleOwner lfo, Observer<MobileLedgerProfile.FutureDates> o) {
80 futureDates.observe(lfo, o);
82 Currency getDefaultCommodity() {
83 return defaultCommodity.getValue();
85 void setDefaultCommodity(Currency newValue) {
86 if (newValue != defaultCommodity.getValue())
87 defaultCommodity.setValue(newValue);
89 void observeDefaultCommodity(LifecycleOwner lfo, Observer<Currency> o) {
90 defaultCommodity.observe(lfo, o);
92 Boolean getShowCommodityByDefault() {
93 return showCommodityByDefault.getValue();
95 void setShowCommodityByDefault(boolean newValue) {
96 if (newValue != showCommodityByDefault.getValue())
97 showCommodityByDefault.setValue(newValue);
99 void observeShowCommodityByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
100 showCommodityByDefault.observe(lfo, o);
102 Boolean getUseAuthentication() {
103 return useAuthentication.getValue();
105 void setUseAuthentication(boolean newValue) {
106 if (newValue != useAuthentication.getValue())
107 useAuthentication.setValue(newValue);
109 void observeUseAuthentication(LifecycleOwner lfo, Observer<Boolean> o) {
110 useAuthentication.observe(lfo, o);
112 SendTransactionTask.API getApiVersion() {
113 return apiVersion.getValue();
115 void setApiVersion(SendTransactionTask.API newValue) {
116 if (newValue != apiVersion.getValue())
117 apiVersion.setValue(newValue);
119 void observeApiVersion(LifecycleOwner lfo, Observer<SendTransactionTask.API> o) {
120 apiVersion.observe(lfo, o);
123 return url.getValue();
125 void setUrl(String newValue) {
126 if (!Misc.nullIsEmpty(newValue)
127 .equals(Misc.nullIsEmpty(url.getValue())))
128 url.setValue(newValue);
130 void setUrl(CharSequence newValue) {
131 setUrl(String.valueOf(newValue));
133 void observeUrl(LifecycleOwner lfo, Observer<String> o) {
136 String getAuthUserName() {
137 return authUserName.getValue();
139 void setAuthUserName(String newValue) {
140 if (!Misc.nullIsEmpty(newValue)
141 .equals(Misc.nullIsEmpty(authUserName.getValue())))
142 authUserName.setValue(newValue);
144 void setAuthUserName(CharSequence newValue) {
145 setAuthUserName(String.valueOf(newValue));
147 void observeUserName(LifecycleOwner lfo, Observer<String> o) {
148 authUserName.observe(lfo, o);
150 String getAuthPassword() {
151 return authPassword.getValue();
153 void setAuthPassword(String newValue) {
154 if (!Misc.nullIsEmpty(newValue)
155 .equals(Misc.nullIsEmpty(authPassword.getValue())))
156 authPassword.setValue(newValue);
158 void setAuthPassword(CharSequence newValue) {
159 setAuthPassword(String.valueOf(newValue));
161 void observePassword(LifecycleOwner lfo, Observer<String> o) {
162 authPassword.observe(lfo, o);
164 String getPreferredAccountsFilter() {
165 return preferredAccountsFilter.getValue();
167 void setPreferredAccountsFilter(String newValue) {
168 if (!Misc.nullIsEmpty(newValue)
169 .equals(Misc.nullIsEmpty(preferredAccountsFilter.getValue())))
170 preferredAccountsFilter.setValue(newValue);
172 void setPreferredAccountsFilter(CharSequence newValue) {
173 setPreferredAccountsFilter(String.valueOf(newValue));
175 void observePreferredAccountsFilter(LifecycleOwner lfo, Observer<String> o) {
176 preferredAccountsFilter.observe(lfo, o);
179 return themeId.getValue();
181 void setThemeId(int newValue) {
182 themeId.setValue(newValue);
184 void observeThemeId(LifecycleOwner lfo, Observer<Integer> o) {
185 themeId.observe(lfo, o);
187 void setValuesFromProfile(MobileLedgerProfile mProfile, int newProfileHue) {
188 final int profileThemeId;
189 if (mProfile != null) {
190 profileName.setValue(mProfile.getName());
191 postingPermitted.setValue(mProfile.isPostingPermitted());
192 showCommodityByDefault.setValue(mProfile.getShowCommodityByDefault());
194 String comm = mProfile.getDefaultCommodity();
195 if (Misc.isEmptyOrNull(comm))
196 setDefaultCommodity(null);
198 setDefaultCommodity(new Currency(-1, comm));
200 futureDates.setValue(mProfile.getFutureDates());
201 apiVersion.setValue(mProfile.getApiVersion());
202 url.setValue(mProfile.getUrl());
203 useAuthentication.setValue(mProfile.isAuthEnabled());
204 authUserName.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthUserName() : "");
205 authPassword.setValue(mProfile.isAuthEnabled() ? mProfile.getAuthPassword() : "");
206 preferredAccountsFilter.setValue(mProfile.getPreferredAccountsFilter());
207 themeId.setValue(mProfile.getThemeHue());
210 profileName.setValue(null);
211 url.setValue(HTTPS_URL_START);
212 postingPermitted.setValue(true);
213 showCommodityByDefault.setValue(false);
214 setFutureDates(MobileLedgerProfile.FutureDates.None);
215 setApiVersion(SendTransactionTask.API.auto);
216 useAuthentication.setValue(false);
217 authUserName.setValue("");
218 authPassword.setValue("");
219 preferredAccountsFilter.setValue(null);
220 themeId.setValue(newProfileHue);
225 void updateProfile(MobileLedgerProfile mProfile) {
226 mProfile.setName(profileName.getValue());
227 mProfile.setUrl(url.getValue());
228 mProfile.setPostingPermitted(postingPermitted.getValue());
229 Currency c = defaultCommodity.getValue();
230 mProfile.setDefaultCommodity((c == null) ? null : c.getName());
231 mProfile.setShowCommodityByDefault(showCommodityByDefault.getValue());
232 mProfile.setPreferredAccountsFilter(preferredAccountsFilter.getValue());
233 mProfile.setAuthEnabled(useAuthentication.getValue());
234 mProfile.setAuthUserName(authUserName.getValue());
235 mProfile.setAuthPassword(authPassword.getValue());
236 mProfile.setThemeHue(themeId.getValue());
237 mProfile.setFutureDates(futureDates.getValue());
238 mProfile.setApiVersion(apiVersion.getValue());