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