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.
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.
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/>.
18 package net.ktnx.mobileledger.model;
20 import android.database.Cursor;
21 import android.database.sqlite.SQLiteDatabase;
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.DiffUtil;
26 import net.ktnx.mobileledger.App;
27 import net.ktnx.mobileledger.utils.Misc;
29 public class Currency {
30 public static final DiffUtil.ItemCallback<Currency> DIFF_CALLBACK =
31 new DiffUtil.ItemCallback<Currency>() {
33 public boolean areItemsTheSame(@NonNull Currency oldItem,
34 @NonNull Currency newItem) {
35 return oldItem.id == newItem.id;
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);
47 private Position position;
48 private boolean hasGap;
49 public Currency(int id, String name) {
52 position = Position.after;
55 public Currency(int id, String name, Position position, boolean hasGap) {
58 this.position = position;
61 public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) {
62 SQLiteDatabase db = App.getDatabase();
64 try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) {
66 this.id = c.getInt(0) + 1;
68 db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)",
69 new Object[]{this.id, name, position.toString(), hasGap});
72 this.position = position;
75 public static Currency loadByName(String name) {
76 MobileLedgerProfile profile = Data.getProfile();
77 return profile.loadCurrencyByName(name);
79 static public boolean equal(Currency left, Currency right) {
84 return left.equals(right);
86 static public boolean equal(Currency left, String right) {
87 right = Misc.emptyIsNull(right);
92 String leftName = Misc.emptyIsNull(left.getName());
93 if (leftName == null) {
97 return leftName.equals(right);
103 public String getName() {
106 public void setName(String name) {
109 public Position getPosition() {
112 public void setPosition(Position position) {
113 this.position = position;
115 public boolean hasGap() {
118 public void setHasGap(boolean hasGap) {
119 this.hasGap = hasGap;
121 public enum Position {
122 before, after, unknown, none