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