]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/async/ConfigReader.java
use post..() instead of set..() from a background thread
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / ConfigReader.java
1 /*
2  * Copyright © 2021 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.async;
19
20 import android.content.Context;
21 import android.net.Uri;
22 import android.util.JsonReader;
23 import android.util.JsonToken;
24
25 import net.ktnx.mobileledger.dao.CurrencyDAO;
26 import net.ktnx.mobileledger.dao.ProfileDAO;
27 import net.ktnx.mobileledger.dao.TemplateHeaderDAO;
28 import net.ktnx.mobileledger.db.Currency;
29 import net.ktnx.mobileledger.db.DB;
30 import net.ktnx.mobileledger.db.Profile;
31 import net.ktnx.mobileledger.db.TemplateAccount;
32 import net.ktnx.mobileledger.db.TemplateHeader;
33 import net.ktnx.mobileledger.db.TemplateWithAccounts;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.utils.Misc;
36
37 import java.io.BufferedReader;
38 import java.io.FileInputStream;
39 import java.io.FileNotFoundException;
40 import java.io.IOException;
41 import java.io.InputStreamReader;
42 import java.util.ArrayList;
43 import java.util.List;
44
45 public class ConfigReader extends ConfigIO {
46     private final OnDoneListener onDoneListener;
47     private JsonReader r;
48     public ConfigReader(Context context, Uri uri, OnErrorListener onErrorListener,
49                         OnDoneListener onDoneListener) throws FileNotFoundException {
50         super(context, uri, onErrorListener);
51
52         this.onDoneListener = onDoneListener;
53     }
54     @Override
55     protected String getStreamMode() {
56         return "r";
57     }
58     @Override
59     protected void initStream() {
60         r = new JsonReader(new BufferedReader(
61                 new InputStreamReader(new FileInputStream(pfd.getFileDescriptor()))));
62     }
63     @Override
64     protected void processStream() throws IOException {
65         List<Currency> commodities = null;
66         List<Profile> profiles = null;
67         List<TemplateWithAccounts> templates = null;
68         String currentProfile = null;
69         r.beginObject();
70         while (r.hasNext()) {
71             String item = r.nextName();
72             switch (item) {
73                 case Keys.COMMODITIES:
74                     commodities = readCommodities(r);
75                     break;
76                 case Keys.PROFILES:
77                     profiles = readProfiles(r);
78                     break;
79                 case Keys.TEMPLATES:
80                     templates = readTemplates(r);
81                     break;
82                 case Keys.CURRENT_PROFILE:
83                     currentProfile = r.nextString();
84                     break;
85                 default:
86                     throw new RuntimeException("unexpected top-level item " + item);
87             }
88         }
89         r.endObject();
90
91         restoreCommodities(commodities);
92         restoreProfiles(profiles);
93         restoreTemplates(templates);
94
95         if (Data.getProfile() == null && currentProfile != null) {
96             Profile p = DB.get()
97                           .getProfileDAO()
98                           .getByUuidSync(currentProfile);
99             if (p != null)
100                 Data.postCurrentProfile(p);
101         }
102
103         if (onDoneListener != null)
104             Misc.onMainThread(onDoneListener::done);
105     }
106     private void restoreTemplates(List<TemplateWithAccounts> list) {
107         if (list == null)
108             return;
109
110         TemplateHeaderDAO dao = DB.get()
111                                   .getTemplateDAO();
112
113         for (TemplateWithAccounts t : list) {
114             if (dao.getTemplateWithAccountsByUuidSync(t.header.getUuid()) == null)
115                 dao.insertSync(t);
116         }
117     }
118     private void restoreProfiles(List<Profile> list) {
119         if (list == null)
120             return;
121
122         ProfileDAO dao = DB.get()
123                            .getProfileDAO();
124
125         for (Profile p : list) {
126             if (dao.getByUuidSync(p.getUuid()) == null)
127                 dao.insert(p);
128         }
129     }
130     private void restoreCommodities(List<Currency> list) {
131         if (list == null)
132             return;
133
134         CurrencyDAO dao = DB.get()
135                             .getCurrencyDAO();
136
137         for (Currency c : list) {
138             if (dao.getByNameSync(c.getName()) == null)
139                 dao.insert(c);
140         }
141     }
142     private TemplateAccount readTemplateAccount(JsonReader r) throws IOException {
143         r.beginObject();
144         TemplateAccount result = new TemplateAccount(0L, 0L, 0L);
145         while (r.peek() != JsonToken.END_OBJECT) {
146             String item = r.nextName();
147             switch (item) {
148                 case Keys.NAME:
149                     result.setAccountName(r.nextString());
150                     break;
151                 case Keys.NAME_GROUP:
152                     result.setAccountNameMatchGroup(r.nextInt());
153                     break;
154                 case Keys.COMMENT:
155                     result.setAccountComment(r.nextString());
156                     break;
157                 case Keys.COMMENT_GROUP:
158                     result.setAccountCommentMatchGroup(r.nextInt());
159                     break;
160                 case Keys.AMOUNT:
161                     result.setAmount((float) r.nextDouble());
162                     break;
163                 case Keys.AMOUNT_GROUP:
164                     result.setAmountMatchGroup(r.nextInt());
165                     break;
166                 case Keys.NEGATE_AMOUNT:
167                     result.setNegateAmount(r.nextBoolean());
168                     break;
169                 case Keys.CURRENCY:
170                     result.setCurrency(r.nextLong());
171                     break;
172                 case Keys.CURRENCY_GROUP:
173                     result.setCurrencyMatchGroup(r.nextInt());
174                     break;
175
176                 default:
177                     throw new IllegalStateException("Unexpected template account item: " + item);
178             }
179         }
180         r.endObject();
181
182         return result;
183     }
184     private TemplateWithAccounts readTemplate(JsonReader r) throws IOException {
185         r.beginObject();
186         String name = null;
187         TemplateHeader t = new TemplateHeader(0L, "", "");
188         List<TemplateAccount> accounts = new ArrayList<>();
189
190         while (r.peek() != JsonToken.END_OBJECT) {
191             String item = r.nextName();
192             switch (item) {
193                 case Keys.UUID:
194                     t.setUuid(r.nextString());
195                     break;
196                 case Keys.NAME:
197                     t.setName(r.nextString());
198                     break;
199                 case Keys.REGEX:
200                     t.setRegularExpression(r.nextString());
201                     break;
202                 case Keys.TEST_TEXT:
203                     t.setTestText(r.nextString());
204                     break;
205                 case Keys.DATE_YEAR:
206                     t.setDateYear(r.nextInt());
207                     break;
208                 case Keys.DATE_YEAR_GROUP:
209                     t.setDateYearMatchGroup(r.nextInt());
210                     break;
211                 case Keys.DATE_MONTH:
212                     t.setDateMonth(r.nextInt());
213                     break;
214                 case Keys.DATE_MONTH_GROUP:
215                     t.setDateMonthMatchGroup(r.nextInt());
216                     break;
217                 case Keys.DATE_DAY:
218                     t.setDateDay(r.nextInt());
219                     break;
220                 case Keys.DATE_DAY_GROUP:
221                     t.setDateDayMatchGroup(r.nextInt());
222                     break;
223                 case Keys.TRANSACTION:
224                     t.setTransactionDescription(r.nextString());
225                     break;
226                 case Keys.TRANSACTION_GROUP:
227                     t.setTransactionDescriptionMatchGroup(r.nextInt());
228                     break;
229                 case Keys.COMMENT:
230                     t.setTransactionComment(r.nextString());
231                     break;
232                 case Keys.COMMENT_GROUP:
233                     t.setTransactionCommentMatchGroup(r.nextInt());
234                     break;
235                 case Keys.IS_FALLBACK:
236                     t.setFallback(r.nextBoolean());
237                     break;
238                 case Keys.ACCOUNTS:
239                     r.beginArray();
240                     while (r.peek() == JsonToken.BEGIN_OBJECT) {
241                         accounts.add(readTemplateAccount(r));
242                     }
243                     r.endArray();
244                     break;
245                 default:
246                     throw new RuntimeException("Unknown template header item: " + item);
247             }
248         }
249         r.endObject();
250
251         TemplateWithAccounts result = new TemplateWithAccounts();
252         result.header = t;
253         result.accounts = accounts;
254         return result;
255     }
256     private List<TemplateWithAccounts> readTemplates(JsonReader r) throws IOException {
257         List<TemplateWithAccounts> list = new ArrayList<>();
258
259         r.beginArray();
260         while (r.peek() == JsonToken.BEGIN_OBJECT) {
261             list.add(readTemplate(r));
262         }
263         r.endArray();
264
265         return list;
266     }
267     private List<Currency> readCommodities(JsonReader r) throws IOException {
268         List<Currency> list = new ArrayList<>();
269
270         r.beginArray();
271         while (r.peek() == JsonToken.BEGIN_OBJECT) {
272             Currency c = new Currency();
273
274             r.beginObject();
275             while (r.peek() != JsonToken.END_OBJECT) {
276                 final String item = r.nextName();
277                 switch (item) {
278                     case Keys.NAME:
279                         c.setName(r.nextString());
280                         break;
281                     case Keys.POSITION:
282                         c.setPosition(r.nextString());
283                         break;
284                     case Keys.HAS_GAP:
285                         c.setHasGap(r.nextBoolean());
286                         break;
287                     default:
288                         throw new RuntimeException("Unknown commodity key: " + item);
289                 }
290             }
291             r.endObject();
292
293             if (c.getName()
294                  .isEmpty())
295                 throw new RuntimeException("Missing commodity name");
296
297             list.add(c);
298         }
299         r.endArray();
300
301         return list;
302     }
303     private List<Profile> readProfiles(JsonReader r) throws IOException {
304         List<Profile> list = new ArrayList<>();
305         r.beginArray();
306         while (r.peek() == JsonToken.BEGIN_OBJECT) {
307             Profile p = new Profile();
308             r.beginObject();
309             while (r.peek() != JsonToken.END_OBJECT) {
310                 String item = r.nextName();
311
312                 switch (item) {
313                     case Keys.UUID:
314                         p.setUuid(r.nextString());
315                         break;
316                     case Keys.NAME:
317                         p.setName(r.nextString());
318                         break;
319                     case Keys.URL:
320                         p.setUrl(r.nextString());
321                         break;
322                     case Keys.USE_AUTH:
323                         p.setUseAuthentication(r.nextBoolean());
324                         break;
325                     case Keys.AUTH_USER:
326                         p.setAuthUser(r.nextString());
327                         break;
328                     case Keys.AUTH_PASS:
329                         p.setAuthPassword(r.nextString());
330                         break;
331                     case Keys.API_VER:
332                         p.setApiVersion(r.nextInt());
333                         break;
334                     case Keys.CAN_POST:
335                         p.setPermitPosting(r.nextBoolean());
336                         break;
337                     case Keys.DEFAULT_COMMODITY:
338                         p.setDefaultCommodity(r.nextString());
339                         break;
340                     case Keys.SHOW_COMMODITY:
341                         p.setShowCommodityByDefault(r.nextBoolean());
342                         break;
343                     case Keys.SHOW_COMMENTS:
344                         p.setShowCommentsByDefault(r.nextBoolean());
345                         break;
346                     case Keys.FUTURE_DATES:
347                         p.setFutureDates(r.nextInt());
348                         break;
349                     case Keys.PREF_ACCOUNT:
350                         p.setPreferredAccountsFilter(r.nextString());
351                         break;
352                     case Keys.COLOUR:
353                         p.setTheme(r.nextInt());
354                         break;
355
356
357                     default:
358                         throw new IllegalStateException("Unexpected profile item: " + item);
359                 }
360             }
361             r.endObject();
362
363             list.add(p);
364         }
365         r.endArray();
366
367         return list;
368     }
369     abstract static public class OnDoneListener {
370         public abstract void done();
371     }
372 }