]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/Currency.java
helper method for loading a currency by name
[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 static Currency loadByName(String name) {
83         MobileLedgerProfile profile = Data.profile.getValue();
84         return profile.loadCurrencyByName(name);
85     }
86     public int getId() {
87         return id;
88     }
89     public String getName() {
90         return name;
91     }
92     public void setName(String name) {
93         this.name = name;
94     }
95     public Position getPosition() {
96         return position;
97     }
98     public void setPosition(Position position) {
99         this.position = position;
100     }
101     public boolean hasGap() {
102         return hasGap;
103     }
104     public void setHasGap(boolean hasGap) {
105         this.hasGap = hasGap;
106     }
107     public enum Position {
108         before(-1), after(1), unknown(0), none(-2);
109         private int value;
110         Position(int value) {
111             this.value = value;
112         }
113         static Position valueOf(int value) {
114             switch (value) {
115                 case -1:
116                     return before;
117                 case +1:
118                     return after;
119                 case 0:
120                     return unknown;
121                 case -2:
122                     return none;
123                 default:
124                     throw new IllegalStateException(String.format("Unexpected value (%d)", value));
125             }
126         }
127     }
128 }