]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/profiles/ProfileDetailModel.java
2a655dccd224b11d537ab24c9a4d89c0bded23b2
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / profiles / ProfileDetailModel.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.profiles;
19
20 import androidx.lifecycle.LifecycleOwner;
21 import androidx.lifecycle.MutableLiveData;
22 import androidx.lifecycle.Observer;
23 import androidx.lifecycle.ViewModel;
24
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;
29
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() {
48     }
49     String getProfileName() {
50         return profileName.getValue();
51     }
52     void setProfileName(String newValue) {
53         if (!Misc.nullIsEmpty(newValue)
54                  .equals(Misc.nullIsEmpty(profileName.getValue())))
55             profileName.setValue(newValue);
56     }
57     void setProfileName(CharSequence newValue) {
58         setProfileName(String.valueOf(newValue));
59     }
60     void observeProfileName(LifecycleOwner lfo, Observer<String> o) {
61         profileName.observe(lfo, o);
62     }
63     Boolean getPostingPermitted() {
64         return postingPermitted.getValue();
65     }
66     void setPostingPermitted(boolean newValue) {
67         if (newValue != postingPermitted.getValue())
68             postingPermitted.setValue(newValue);
69     }
70     void observePostingPermitted(LifecycleOwner lfo, Observer<Boolean> o) {
71         postingPermitted.observe(lfo, o);
72     }
73     public void setShowCommentsByDefault(boolean newValue) {
74         if (newValue != showCommentsByDefault.getValue())
75             showCommentsByDefault.setValue(newValue);
76     }
77     void observeShowCommentsByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
78         showCommentsByDefault.observe(lfo, o);
79     }
80     MobileLedgerProfile.FutureDates getFutureDates() {
81         return futureDates.getValue();
82     }
83     void setFutureDates(MobileLedgerProfile.FutureDates newValue) {
84         if (newValue != futureDates.getValue())
85             futureDates.setValue(newValue);
86     }
87     void observeFutureDates(LifecycleOwner lfo, Observer<MobileLedgerProfile.FutureDates> o) {
88         futureDates.observe(lfo, o);
89     }
90     Currency getDefaultCommodity() {
91         return defaultCommodity.getValue();
92     }
93     void setDefaultCommodity(Currency newValue) {
94         if (newValue != defaultCommodity.getValue())
95             defaultCommodity.setValue(newValue);
96     }
97     void observeDefaultCommodity(LifecycleOwner lfo, Observer<Currency> o) {
98         defaultCommodity.observe(lfo, o);
99     }
100     Boolean getShowCommodityByDefault() {
101         return showCommodityByDefault.getValue();
102     }
103     void setShowCommodityByDefault(boolean newValue) {
104         if (newValue != showCommodityByDefault.getValue())
105             showCommodityByDefault.setValue(newValue);
106     }
107     void observeShowCommodityByDefault(LifecycleOwner lfo, Observer<Boolean> o) {
108         showCommodityByDefault.observe(lfo, o);
109     }
110     Boolean getUseAuthentication() {
111         return useAuthentication.getValue();
112     }
113     void setUseAuthentication(boolean newValue) {
114         if (newValue != useAuthentication.getValue())
115             useAuthentication.setValue(newValue);
116     }
117     void observeUseAuthentication(LifecycleOwner lfo, Observer<Boolean> o) {
118         useAuthentication.observe(lfo, o);
119     }
120     SendTransactionTask.API getApiVersion() {
121         return apiVersion.getValue();
122     }
123     void setApiVersion(SendTransactionTask.API newValue) {
124         if (newValue != apiVersion.getValue())
125             apiVersion.setValue(newValue);
126     }
127     void observeApiVersion(LifecycleOwner lfo, Observer<SendTransactionTask.API> o) {
128         apiVersion.observe(lfo, o);
129     }
130     String getUrl() {
131         return url.getValue();
132     }
133     void setUrl(String newValue) {
134         if (!Misc.nullIsEmpty(newValue)
135                  .equals(Misc.nullIsEmpty(url.getValue())))
136             url.setValue(newValue);
137     }
138     void setUrl(CharSequence newValue) {
139         setUrl(String.valueOf(newValue));
140     }
141     void observeUrl(LifecycleOwner lfo, Observer<String> o) {
142         url.observe(lfo, o);
143     }
144     String getAuthUserName() {
145         return authUserName.getValue();
146     }
147     void setAuthUserName(String newValue) {
148         if (!Misc.nullIsEmpty(newValue)
149                  .equals(Misc.nullIsEmpty(authUserName.getValue())))
150             authUserName.setValue(newValue);
151     }
152     void setAuthUserName(CharSequence newValue) {
153         setAuthUserName(String.valueOf(newValue));
154     }
155     void observeUserName(LifecycleOwner lfo, Observer<String> o) {
156         authUserName.observe(lfo, o);
157     }
158     String getAuthPassword() {
159         return authPassword.getValue();
160     }
161     void setAuthPassword(String newValue) {
162         if (!Misc.nullIsEmpty(newValue)
163                  .equals(Misc.nullIsEmpty(authPassword.getValue())))
164             authPassword.setValue(newValue);
165     }
166     void setAuthPassword(CharSequence newValue) {
167         setAuthPassword(String.valueOf(newValue));
168     }
169     void observePassword(LifecycleOwner lfo, Observer<String> o) {
170         authPassword.observe(lfo, o);
171     }
172     String getPreferredAccountsFilter() {
173         return preferredAccountsFilter.getValue();
174     }
175     void setPreferredAccountsFilter(String newValue) {
176         if (!Misc.nullIsEmpty(newValue)
177                  .equals(Misc.nullIsEmpty(preferredAccountsFilter.getValue())))
178             preferredAccountsFilter.setValue(newValue);
179     }
180     void setPreferredAccountsFilter(CharSequence newValue) {
181         setPreferredAccountsFilter(String.valueOf(newValue));
182     }
183     void observePreferredAccountsFilter(LifecycleOwner lfo, Observer<String> o) {
184         preferredAccountsFilter.observe(lfo, o);
185     }
186     int getThemeId() {
187         return themeId.getValue();
188     }
189     void setThemeId(int newValue) {
190         themeId.setValue(newValue);
191     }
192     void observeThemeId(LifecycleOwner lfo, Observer<Integer> o) {
193         themeId.observe(lfo, o);
194     }
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());
202             {
203                 String comm = mProfile.getDefaultCommodity();
204                 if (Misc.isEmptyOrNull(comm))
205                     setDefaultCommodity(null);
206                 else
207                     setDefaultCommodity(new Currency(-1, comm));
208             }
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());
217         }
218         else {
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);
231         }
232
233
234     }
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());
250     }
251 }