]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/Currency.java
a4854c05219c146608326b1acf8f2556f5aebd25
[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
28 public class Currency {
29     public static final DiffUtil.ItemCallback<Currency> DIFF_CALLBACK =
30             new DiffUtil.ItemCallback<Currency>() {
31                 @Override
32                 public boolean areItemsTheSame(@NonNull Currency oldItem,
33                                                @NonNull Currency newItem) {
34                     return oldItem.id == newItem.id;
35                 }
36                 @Override
37                 public boolean areContentsTheSame(@NonNull Currency oldItem,
38                                                   @NonNull Currency newItem) {
39                     return oldItem.name.equals(newItem.name) &&
40                            oldItem.position.equals(newItem.position) &&
41                            (oldItem.hasGap == newItem.hasGap);
42                 }
43             };
44     private int id;
45     private String name;
46     private Position position;
47     private boolean hasGap;
48     public Currency(int id, String name) {
49         this.id = id;
50         this.name = name;
51         position = Position.after;
52         hasGap = true;
53     }
54     public Currency(int id, String name, Position position, boolean hasGap) {
55         this.id = id;
56         this.name = name;
57         this.position = position;
58         this.hasGap = hasGap;
59     }
60     public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) {
61         SQLiteDatabase db = App.getDatabase();
62         int attempts = 0;
63         while (true) {
64             if (++attempts > 10)
65                 throw new RuntimeException("Giving up getting next ID after 10 attempts");
66
67             try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
68                 c.moveToNext();
69                 int nextId = c.getInt(0) + 1;
70                 db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
71                         new Object[]{nextId, name, position.toString(), hasGap});
72
73                 this.id = nextId;
74                 break;
75             }
76         }
77
78         this.name = name;
79         this.position = position;
80         this.hasGap = hasGap;
81     }
82     public int getId() {
83         return id;
84     }
85     public String getName() {
86         return name;
87     }
88     public void setName(String name) {
89         this.name = name;
90     }
91     public Position getPosition() {
92         return position;
93     }
94     public void setPosition(Position position) {
95         this.position = position;
96     }
97     public boolean hasGap() {
98         return hasGap;
99     }
100     public void setHasGap(boolean hasGap) {
101         this.hasGap = hasGap;
102     }
103     public enum Position {
104         before(-1), after(1), unknown(0), none(-2);
105         private int value;
106         Position(int value) {
107             this.value = value;
108         }
109         static Position valueOf(int value) {
110             switch (value) {
111                 case -1:
112                     return before;
113                 case +1:
114                     return after;
115                 case 0:
116                     return unknown;
117                 case -2:
118                     return none;
119                 default:
120                     throw new IllegalStateException(String.format("Unexpected value (%d)", value));
121             }
122         }
123     }
124 }