From 8098a8b37a4331b9faf6cf50a51a0d7aa9677421 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 4 Apr 2021 12:27:21 +0300 Subject: [PATCH] adopt Room for displaying account lists --- app/build.gradle | 16 +- .../async/RetrieveTransactionsTask.java | 7 +- .../net/ktnx/mobileledger/dao/AccountDAO.java | 24 +- .../mobileledger/dao/AccountValueDAO.java | 48 +++ .../net/ktnx/mobileledger/dao/OptionDAO.java | 8 + .../ktnx/mobileledger/dao/TransactionDAO.java | 6 + .../mobileledger/db/AccountWithAmounts.java | 30 ++ .../java/net/ktnx/mobileledger/db/DB.java | 3 + .../json/ParsedLedgerAccount.java | 4 +- .../mobileledger/model/AccountListItem.java | 26 +- .../net/ktnx/mobileledger/model/Data.java | 7 +- .../mobileledger/model/LedgerAccount.java | 65 ++-- .../ktnx/mobileledger/model/LedgerAmount.java | 25 +- .../model/MobileLedgerProfile.java | 39 +- .../net/ktnx/mobileledger/ui/MainModel.java | 97 +---- .../AccountSummaryAdapter.java | 355 ++++++++---------- .../AccountSummaryFragment.java | 45 ++- .../ui/activity/MainActivity.java | 9 +- .../TransactionListAdapter.java | 4 +- .../TransactionRowHolder.java | 21 +- ...t_summary_row.xml => account_list_row.xml} | 3 +- .../res/layout/account_list_summary_row.xml | 46 +++ build.gradle | 4 +- 23 files changed, 496 insertions(+), 396 deletions(-) create mode 100644 app/src/main/java/net/ktnx/mobileledger/dao/AccountValueDAO.java create mode 100644 app/src/main/java/net/ktnx/mobileledger/db/AccountWithAmounts.java rename app/src/main/res/layout/{account_summary_row.xml => account_list_row.xml} (97%) create mode 100644 app/src/main/res/layout/account_list_summary_row.xml diff --git a/app/build.gradle b/app/build.gradle index d5c8a9c8..6cbd93bc 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -59,26 +59,26 @@ android { } dependencies { - implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.0' - implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.0' + implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1' + implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1' def room_version = '2.2.6' implementation "androidx.room:room-runtime:$room_version" annotationProcessor "androidx.room:room-compiler:$room_version" - def nav_version = '2.3.3' + def nav_version = '2.3.4' implementation fileTree(include: ['*.jar'], dir: 'libs') implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation 'com.google.android.material:material:1.3.0' implementation 'androidx.constraintlayout:constraintlayout:2.0.4' implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' implementation 'androidx.recyclerview:recyclerview:1.1.0' - testImplementation 'junit:junit:4.13.1' - androidTestImplementation 'androidx.test:runner:1.4.0-alpha04' - androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha04' + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test:runner:1.4.0-alpha05' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0-alpha05' implementation 'org.jetbrains:annotations:15.0' - implementation 'com.fasterxml.jackson.module:jackson-modules-java8:2.11.3' + implementation 'com.fasterxml.jackson.module:jackson-modules-java8:2.12.2' implementation "androidx.navigation:navigation-fragment:$nav_version" implementation "androidx.navigation:navigation-ui:$nav_version" - implementation 'androidx.appcompat:appcompat:1.3.0-beta01' + implementation 'androidx.appcompat:appcompat:1.3.0-rc01' } allprojects { diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index e0b4e2eb..aa47d73d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -121,9 +121,6 @@ public class RetrieveTransactionsTask extends return null; } } - public MobileLedgerProfile getProfile() { - return profile; - } @Override protected void onProgressUpdate(Progress... values) { super.onProgressUpdate(values); @@ -214,7 +211,7 @@ public class RetrieveTransactionsTask extends else { parentAccount = null; } - lastAccount = new LedgerAccount(profile, accName, parentAccount); + lastAccount = new LedgerAccount(accName, parentAccount); accounts.add(lastAccount); map.put(accName, lastAccount); @@ -378,7 +375,7 @@ public class RetrieveTransactionsTask extends parentAccount = null; } - acc = new LedgerAccount(profile, accountName, parentAccount); + acc = new LedgerAccount(accountName, parentAccount); createdAccounts.add(acc); return acc; } diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java index bb8d4ec2..3b401b66 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/AccountDAO.java @@ -24,9 +24,11 @@ import androidx.room.Dao; import androidx.room.Delete; import androidx.room.Insert; import androidx.room.Query; +import androidx.room.Transaction; import androidx.room.Update; import net.ktnx.mobileledger.db.Account; +import net.ktnx.mobileledger.db.AccountWithAmounts; import java.util.ArrayList; import java.util.List; @@ -50,8 +52,18 @@ public abstract class AccountDAO extends BaseDAO { @Delete public abstract void deleteSync(Account item); - @Query("SELECT * FROM accounts") - public abstract LiveData> getAll(); + @Delete + public abstract void deleteSync(List items); + + @Query("SELECT * FROM accounts WHERE profile_id=:profileId") + public abstract LiveData> getAll(long profileId); + + @Transaction + @Query("SELECT * FROM accounts WHERE profile_id = :profileId") + public abstract LiveData> getAllWithAmounts(long profileId); + + @Query("SELECT * FROM accounts WHERE id=:id") + public abstract Account getByIdSync(long id); // not useful for now // @Transaction @@ -60,6 +72,11 @@ public abstract class AccountDAO extends BaseDAO { @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName") public abstract LiveData getByName(long profileId, @NonNull String accountName); + @Transaction + @Query("SELECT * FROM accounts WHERE profile_id = :profileId AND name = :accountName") + public abstract LiveData getByNameWithAmounts(long profileId, + @NonNull String accountName); + @Query("SELECT name, CASE WHEN name_upper LIKE :term||'%%' THEN 1 " + " WHEN name_upper LIKE '%%:'||:term||'%%' THEN 2 " + " WHEN name_upper LIKE '%% '||:term||'%%' THEN 3 " + @@ -93,6 +110,9 @@ public abstract class AccountDAO extends BaseDAO { "WHERE name_upper LIKE '%%'||:term||'%%' " + "ORDER BY ordering, name_upper, rowid ") public abstract List lookupByNameSync(@NonNull String term); + @Query("SELECT * FROM accounts WHERE profile_id = :profileId") + public abstract List allForProfileSync(long profileId); + static public class AccountNameContainer { @ColumnInfo public String name; diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/AccountValueDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/AccountValueDAO.java new file mode 100644 index 00000000..5ac559d6 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/dao/AccountValueDAO.java @@ -0,0 +1,48 @@ +/* + * Copyright © 2021 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.dao; + +import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; +import androidx.room.Dao; +import androidx.room.Delete; +import androidx.room.Insert; +import androidx.room.Query; +import androidx.room.Update; + +import net.ktnx.mobileledger.db.AccountValue; + +import java.util.List; + +@Dao +public abstract class AccountValueDAO extends BaseDAO { + @Insert + public abstract long insertSync(AccountValue item); + + @Update + public abstract void updateSync(AccountValue item); + + @Delete + public abstract void deleteSync(AccountValue item); + + @Query("SELECT * FROM account_values WHERE account_id=:accountId") + public abstract LiveData> getAll(long accountId); + + @Query("SELECT * FROM account_values WHERE account_id = :accountId AND currency = :currency") + public abstract AccountValue getByCurrencySync(long accountId, @NonNull String currency); +} diff --git a/app/src/main/java/net/ktnx/mobileledger/dao/OptionDAO.java b/app/src/main/java/net/ktnx/mobileledger/dao/OptionDAO.java index f6ee035b..5540b233 100644 --- a/app/src/main/java/net/ktnx/mobileledger/dao/OptionDAO.java +++ b/app/src/main/java/net/ktnx/mobileledger/dao/OptionDAO.java @@ -27,6 +27,8 @@ import androidx.room.Update; import net.ktnx.mobileledger.db.Option; +import java.util.List; + @Dao public abstract class OptionDAO extends BaseDAO