X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FCurrency.java;fp=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FCurrency.java;h=a4854c05219c146608326b1acf8f2556f5aebd25;hb=5545ddea3574103c2a7eea552fff0d43a0587fac;hp=0000000000000000000000000000000000000000;hpb=b1889d2e6ad5b59a4b896a979cdd5736449e2b9e;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Currency.java b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java new file mode 100644 index 00000000..a4854c05 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/model/Currency.java @@ -0,0 +1,124 @@ +/* + * Copyright © 2019 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.model; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.DiffUtil; + +import net.ktnx.mobileledger.App; + +public class Currency { + public static final DiffUtil.ItemCallback DIFF_CALLBACK = + new DiffUtil.ItemCallback() { + @Override + public boolean areItemsTheSame(@NonNull Currency oldItem, + @NonNull Currency newItem) { + return oldItem.id == newItem.id; + } + @Override + public boolean areContentsTheSame(@NonNull Currency oldItem, + @NonNull Currency newItem) { + return oldItem.name.equals(newItem.name) && + oldItem.position.equals(newItem.position) && + (oldItem.hasGap == newItem.hasGap); + } + }; + private int id; + private String name; + private Position position; + private boolean hasGap; + public Currency(int id, String name) { + this.id = id; + this.name = name; + position = Position.after; + hasGap = true; + } + public Currency(int id, String name, Position position, boolean hasGap) { + this.id = id; + this.name = name; + this.position = position; + this.hasGap = hasGap; + } + public Currency(MobileLedgerProfile profile, String name, Position position, boolean hasGap) { + SQLiteDatabase db = App.getDatabase(); + int attempts = 0; + while (true) { + if (++attempts > 10) + throw new RuntimeException("Giving up getting next ID after 10 attempts"); + + try (Cursor c = db.rawQuery("select max(rowid) from currencies", null)) { + c.moveToNext(); + int nextId = c.getInt(0) + 1; + db.execSQL("insert into currencies(id, name, position, has_gap) values(?, ?, ?, ?)", + new Object[]{nextId, name, position.toString(), hasGap}); + + this.id = nextId; + break; + } + } + + this.name = name; + this.position = position; + this.hasGap = hasGap; + } + public int getId() { + return id; + } + public String getName() { + return name; + } + public void setName(String name) { + this.name = name; + } + public Position getPosition() { + return position; + } + public void setPosition(Position position) { + this.position = position; + } + public boolean hasGap() { + return hasGap; + } + public void setHasGap(boolean hasGap) { + this.hasGap = hasGap; + } + public enum Position { + before(-1), after(1), unknown(0), none(-2); + private int value; + Position(int value) { + this.value = value; + } + static Position valueOf(int value) { + switch (value) { + case -1: + return before; + case +1: + return after; + case 0: + return unknown; + case -2: + return none; + default: + throw new IllegalStateException(String.format("Unexpected value (%d)", value)); + } + } + } +}