]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/Currency.java
1c7dbf7f81f014062c1282d55d2f9633d47abe9b
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / Currency.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.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 final 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
64         try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
65             c.moveToNext();
66             this.id = c.getInt(0) + 1;
67         }
68         db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
69                 new Object[]{this.id, name, position.toString(), hasGap});
70
71         this.name = name;
72         this.position = position;
73         this.hasGap = hasGap;
74     }
75     public static Currency loadByName(String name) {
76         MobileLedgerProfile profile = Data.getProfile();
77         return profile.loadCurrencyByName(name);
78     }
79     public static Currency loadById(int id) {
80         MobileLedgerProfile profile = Data.getProfile();
81         return profile.loadCurrencyById(id);
82     }
83     static public boolean equal(Currency left, Currency right) {
84         if (left == null) {
85             return right == null;
86         }
87         else
88             return left.equals(right);
89     }
90     static public boolean equal(Currency left, String right) {
91         right = Misc.emptyIsNull(right);
92         if (left == null) {
93             return right == null;
94         }
95         else {
96             String leftName = Misc.emptyIsNull(left.getName());
97             if (leftName == null) {
98                 return right == null;
99             }
100             else
101                 return leftName.equals(right);
102         }
103     }
104     public int getId() {
105         return id;
106     }
107     public String getName() {
108         return name;
109     }
110     public void setName(String name) {
111         this.name = name;
112     }
113     public Position getPosition() {
114         return position;
115     }
116     public void setPosition(Position position) {
117         this.position = position;
118     }
119     public boolean hasGap() {
120         return hasGap;
121     }
122     public void setHasGap(boolean hasGap) {
123         this.hasGap = hasGap;
124     }
125     public enum Position {
126         before, after, unknown, none
127     }
128 }