]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/Currency.java
fix a crash when returning to the new transaction activity with the date picker dialo...
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / Currency.java
1 /*
2  * Copyright © 2019 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.model;
19
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
22
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.DiffUtil;
25
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.utils.Misc;
28
29 public class Currency {
30     public static final DiffUtil.ItemCallback<Currency> DIFF_CALLBACK =
31             new DiffUtil.ItemCallback<Currency>() {
32                 @Override
33                 public boolean areItemsTheSame(@NonNull Currency oldItem,
34                                                @NonNull Currency newItem) {
35                     return oldItem.id == newItem.id;
36                 }
37                 @Override
38                 public boolean areContentsTheSame(@NonNull Currency oldItem,
39                                                   @NonNull Currency newItem) {
40                     return oldItem.name.equals(newItem.name) &&
41                            oldItem.position.equals(newItem.position) &&
42                            (oldItem.hasGap == newItem.hasGap);
43                 }
44             };
45     private int id;
46     private String name;
47     private Position position;
48     private boolean hasGap;
49     public Currency(int id, String name) {
50         this.id = id;
51         this.name = name;
52         position = Position.after;
53         hasGap = true;
54     }
55     public Currency(int id, String name, Position position, boolean hasGap) {
56         this.id = id;
57         this.name = name;
58         this.position = position;
59         this.hasGap = hasGap;
60     }
61     public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) {
62         SQLiteDatabase db = App.getDatabase();
63         int attempts = 0;
64         while (true) {
65             if (++attempts > 10)
66                 throw new RuntimeException("Giving up getting next ID after 10 attempts");
67
68             try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
69                 c.moveToNext();
70                 int nextId = c.getInt(0) + 1;
71                 db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
72                         new Object[]{nextId, name, position.toString(), hasGap});
73
74                 this.id = nextId;
75                 break;
76             }
77         }
78
79         this.name = name;
80         this.position = position;
81         this.hasGap = hasGap;
82     }
83     public static Currency loadByName(String name) {
84         MobileLedgerProfile profile = Data.profile.getValue();
85         return profile.loadCurrencyByName(name);
86     }
87     public int getId() {
88         return id;
89     }
90     public String getName() {
91         return name;
92     }
93     public void setName(String name) {
94         this.name = name;
95     }
96     public Position getPosition() {
97         return position;
98     }
99     public void setPosition(Position position) {
100         this.position = position;
101     }
102     public boolean hasGap() {
103         return hasGap;
104     }
105     public void setHasGap(boolean hasGap) {
106         this.hasGap = hasGap;
107     }
108     static public boolean equal(Currency left, Currency right) {
109         if (left == null) {
110             return right == null;
111         }
112         else
113             return left.equals(right);
114     }
115     static public boolean equal(Currency left, String right) {
116         right = Misc.emptyIsNull(right);
117         if (left == null) {
118             return right == null;
119         }
120         else {
121             String leftName = Misc.emptyIsNull(left.getName());
122             if (leftName == null) {
123                 return right == null;
124             }
125             else
126                 return leftName.equals(right);
127         }
128     }
129     public enum Position {
130         before(-1), after(1), unknown(0), none(-2);
131         private int value;
132         Position(int value) {
133             this.value = value;
134         }
135         static Position valueOf(int value) {
136             switch (value) {
137                 case -1:
138                     return before;
139                 case +1:
140                     return after;
141                 case 0:
142                     return unknown;
143                 case -2:
144                     return none;
145                 default:
146                     throw new IllegalStateException(String.format("Unexpected value (%d)", value));
147             }
148         }
149     }
150 }