diff --git a/lib/wallets/isar/models/wallet_info.dart b/lib/wallets/isar/models/wallet_info.dart index 7f88f5e8d..d14944a1e 100644 --- a/lib/wallets/isar/models/wallet_info.dart +++ b/lib/wallets/isar/models/wallet_info.dart @@ -32,6 +32,16 @@ class WalletInfo implements IsarId { String? get cachedBalanceString => _cachedBalanceString; String? _cachedBalanceString; + /// Only exposed for Isar. Use the [cachedBalanceSecondary] getter. + // Only exposed for isar as Amount cannot be stored in isar easily + String? get cachedBalanceSecondaryString => _cachedBalanceSecondaryString; + String? _cachedBalanceSecondaryString; + + /// Only exposed for Isar. Use the [cachedBalanceTertiary] getter. + // Only exposed for isar as Amount cannot be stored in isar easily + String? get cachedBalanceTertiaryString => _cachedBalanceTertiaryString; + String? _cachedBalanceTertiaryString; + /// Only exposed for Isar. Use the [coin] getter. // Only exposed for isar to avoid dealing with storing enums as Coin can change String get coinName => _coinName; @@ -82,16 +92,23 @@ class WalletInfo implements IsarId { } } - /// Special case for coins such as firo + /// Special case for coins such as firo lelantus @ignore - Balance get cachedSecondaryBalance { - try { - return Balance.fromJson( - otherData[WalletInfoKeys.cachedSecondaryBalance] as String? ?? "", - coin.decimals, - ); - } catch (_) { + Balance get cachedBalanceSecondary { + if (cachedBalanceSecondaryString == null) { return Balance.zeroForCoin(coin: coin); + } else { + return Balance.fromJson(cachedBalanceSecondaryString!, coin.decimals); + } + } + + /// Special case for coins such as firo spark + @ignore + Balance get cachedBalanceTertiary { + if (cachedBalanceTertiaryString == null) { + return Balance.zeroForCoin(coin: coin); + } else { + return Balance.fromJson(cachedBalanceTertiaryString!, coin.decimals); } } @@ -113,9 +130,8 @@ class WalletInfo implements IsarId { : Map.from(jsonDecode(otherDataJsonString!) as Map); //============================================================================ - //============= Updaters ================================================ + //============= Updaters ================================================ - /// copies this with a new balance and updates the db Future updateBalance({ required Balance newBalance, required Isar isar, @@ -133,6 +149,40 @@ class WalletInfo implements IsarId { } } + Future updateBalanceSecondary({ + required Balance newBalance, + required Isar isar, + }) async { + final newEncoded = newBalance.toJsonIgnoreCoin(); + + // only update if there were changes to the balance + if (cachedBalanceSecondaryString != newEncoded) { + _cachedBalanceSecondaryString = newEncoded; + + await isar.writeTxn(() async { + await isar.walletInfo.deleteByWalletId(walletId); + await isar.walletInfo.put(this); + }); + } + } + + Future updateBalanceTertiary({ + required Balance newBalance, + required Isar isar, + }) async { + final newEncoded = newBalance.toJsonIgnoreCoin(); + + // only update if there were changes to the balance + if (cachedBalanceTertiaryString != newEncoded) { + _cachedBalanceTertiaryString = newEncoded; + + await isar.writeTxn(() async { + await isar.walletInfo.deleteByWalletId(walletId); + await isar.walletInfo.put(this); + }); + } + } + /// copies this with a new chain height and updates the db Future updateCachedChainHeight({ required int newHeight, diff --git a/lib/wallets/isar/models/wallet_info.g.dart b/lib/wallets/isar/models/wallet_info.g.dart index 490e5ffbe..c6227d79f 100644 --- a/lib/wallets/isar/models/wallet_info.g.dart +++ b/lib/wallets/isar/models/wallet_info.g.dart @@ -17,69 +17,79 @@ const WalletInfoSchema = CollectionSchema( name: r'WalletInfo', id: -2861501434900022153, properties: { - r'cachedBalanceString': PropertySchema( + r'cachedBalanceSecondaryString': PropertySchema( id: 0, + name: r'cachedBalanceSecondaryString', + type: IsarType.string, + ), + r'cachedBalanceString': PropertySchema( + id: 1, name: r'cachedBalanceString', type: IsarType.string, ), + r'cachedBalanceTertiaryString': PropertySchema( + id: 2, + name: r'cachedBalanceTertiaryString', + type: IsarType.string, + ), r'cachedChainHeight': PropertySchema( - id: 1, + id: 3, name: r'cachedChainHeight', type: IsarType.long, ), r'cachedReceivingAddress': PropertySchema( - id: 2, + id: 4, name: r'cachedReceivingAddress', type: IsarType.string, ), r'coinName': PropertySchema( - id: 3, + id: 5, name: r'coinName', type: IsarType.string, ), r'favouriteOrderIndex': PropertySchema( - id: 4, + id: 6, name: r'favouriteOrderIndex', type: IsarType.long, ), r'isFavourite': PropertySchema( - id: 5, + id: 7, name: r'isFavourite', type: IsarType.bool, ), r'isMnemonicVerified': PropertySchema( - id: 6, + id: 8, name: r'isMnemonicVerified', type: IsarType.bool, ), r'mainAddressType': PropertySchema( - id: 7, + id: 9, name: r'mainAddressType', type: IsarType.byte, enumMap: _WalletInfomainAddressTypeEnumValueMap, ), r'name': PropertySchema( - id: 8, + id: 10, name: r'name', type: IsarType.string, ), r'otherDataJsonString': PropertySchema( - id: 9, + id: 11, name: r'otherDataJsonString', type: IsarType.string, ), r'restoreHeight': PropertySchema( - id: 10, + id: 12, name: r'restoreHeight', type: IsarType.long, ), r'tokenContractAddresses': PropertySchema( - id: 11, + id: 13, name: r'tokenContractAddresses', type: IsarType.stringList, ), r'walletId': PropertySchema( - id: 12, + id: 14, name: r'walletId', type: IsarType.string, ) @@ -118,12 +128,24 @@ int _walletInfoEstimateSize( Map> allOffsets, ) { var bytesCount = offsets.last; + { + final value = object.cachedBalanceSecondaryString; + if (value != null) { + bytesCount += 3 + value.length * 3; + } + } { final value = object.cachedBalanceString; if (value != null) { bytesCount += 3 + value.length * 3; } } + { + final value = object.cachedBalanceTertiaryString; + if (value != null) { + bytesCount += 3 + value.length * 3; + } + } bytesCount += 3 + object.cachedReceivingAddress.length * 3; bytesCount += 3 + object.coinName.length * 3; bytesCount += 3 + object.name.length * 3; @@ -150,19 +172,21 @@ void _walletInfoSerialize( List offsets, Map> allOffsets, ) { - writer.writeString(offsets[0], object.cachedBalanceString); - writer.writeLong(offsets[1], object.cachedChainHeight); - writer.writeString(offsets[2], object.cachedReceivingAddress); - writer.writeString(offsets[3], object.coinName); - writer.writeLong(offsets[4], object.favouriteOrderIndex); - writer.writeBool(offsets[5], object.isFavourite); - writer.writeBool(offsets[6], object.isMnemonicVerified); - writer.writeByte(offsets[7], object.mainAddressType.index); - writer.writeString(offsets[8], object.name); - writer.writeString(offsets[9], object.otherDataJsonString); - writer.writeLong(offsets[10], object.restoreHeight); - writer.writeStringList(offsets[11], object.tokenContractAddresses); - writer.writeString(offsets[12], object.walletId); + writer.writeString(offsets[0], object.cachedBalanceSecondaryString); + writer.writeString(offsets[1], object.cachedBalanceString); + writer.writeString(offsets[2], object.cachedBalanceTertiaryString); + writer.writeLong(offsets[3], object.cachedChainHeight); + writer.writeString(offsets[4], object.cachedReceivingAddress); + writer.writeString(offsets[5], object.coinName); + writer.writeLong(offsets[6], object.favouriteOrderIndex); + writer.writeBool(offsets[7], object.isFavourite); + writer.writeBool(offsets[8], object.isMnemonicVerified); + writer.writeByte(offsets[9], object.mainAddressType.index); + writer.writeString(offsets[10], object.name); + writer.writeString(offsets[11], object.otherDataJsonString); + writer.writeLong(offsets[12], object.restoreHeight); + writer.writeStringList(offsets[13], object.tokenContractAddresses); + writer.writeString(offsets[14], object.walletId); } WalletInfo _walletInfoDeserialize( @@ -172,19 +196,19 @@ WalletInfo _walletInfoDeserialize( Map> allOffsets, ) { final object = WalletInfo( - cachedBalanceString: reader.readStringOrNull(offsets[0]), - cachedChainHeight: reader.readLongOrNull(offsets[1]) ?? 0, - cachedReceivingAddress: reader.readStringOrNull(offsets[2]) ?? "", - coinName: reader.readString(offsets[3]), - favouriteOrderIndex: reader.readLongOrNull(offsets[4]) ?? -1, - isMnemonicVerified: reader.readBoolOrNull(offsets[6]) ?? false, + cachedBalanceString: reader.readStringOrNull(offsets[1]), + cachedChainHeight: reader.readLongOrNull(offsets[3]) ?? 0, + cachedReceivingAddress: reader.readStringOrNull(offsets[4]) ?? "", + coinName: reader.readString(offsets[5]), + favouriteOrderIndex: reader.readLongOrNull(offsets[6]) ?? -1, + isMnemonicVerified: reader.readBoolOrNull(offsets[8]) ?? false, mainAddressType: _WalletInfomainAddressTypeValueEnumMap[ - reader.readByteOrNull(offsets[7])] ?? + reader.readByteOrNull(offsets[9])] ?? AddressType.p2pkh, - name: reader.readString(offsets[8]), - otherDataJsonString: reader.readStringOrNull(offsets[9]), - restoreHeight: reader.readLongOrNull(offsets[10]) ?? 0, - walletId: reader.readString(offsets[12]), + name: reader.readString(offsets[10]), + otherDataJsonString: reader.readStringOrNull(offsets[11]), + restoreHeight: reader.readLongOrNull(offsets[12]) ?? 0, + walletId: reader.readString(offsets[14]), ); object.id = id; return object; @@ -200,30 +224,34 @@ P _walletInfoDeserializeProp

( case 0: return (reader.readStringOrNull(offset)) as P; case 1: - return (reader.readLongOrNull(offset) ?? 0) as P; + return (reader.readStringOrNull(offset)) as P; case 2: - return (reader.readStringOrNull(offset) ?? "") as P; + return (reader.readStringOrNull(offset)) as P; case 3: - return (reader.readString(offset)) as P; + return (reader.readLongOrNull(offset) ?? 0) as P; case 4: - return (reader.readLongOrNull(offset) ?? -1) as P; + return (reader.readStringOrNull(offset) ?? "") as P; case 5: - return (reader.readBool(offset)) as P; + return (reader.readString(offset)) as P; case 6: - return (reader.readBoolOrNull(offset) ?? false) as P; + return (reader.readLongOrNull(offset) ?? -1) as P; case 7: + return (reader.readBool(offset)) as P; + case 8: + return (reader.readBoolOrNull(offset) ?? false) as P; + case 9: return (_WalletInfomainAddressTypeValueEnumMap[ reader.readByteOrNull(offset)] ?? AddressType.p2pkh) as P; - case 8: - return (reader.readString(offset)) as P; - case 9: - return (reader.readStringOrNull(offset)) as P; case 10: - return (reader.readLongOrNull(offset) ?? 0) as P; + return (reader.readString(offset)) as P; case 11: - return (reader.readStringList(offset) ?? []) as P; + return (reader.readStringOrNull(offset)) as P; case 12: + return (reader.readLongOrNull(offset) ?? 0) as P; + case 13: + return (reader.readStringList(offset) ?? []) as P; + case 14: return (reader.readString(offset)) as P; default: throw IsarError('Unknown property with id $propertyId'); @@ -446,6 +474,162 @@ extension WalletInfoQueryWhere extension WalletInfoQueryFilter on QueryBuilder { + QueryBuilder + cachedBalanceSecondaryStringIsNull() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(const FilterCondition.isNull( + property: r'cachedBalanceSecondaryString', + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringIsNotNull() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(const FilterCondition.isNotNull( + property: r'cachedBalanceSecondaryString', + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringEqualTo( + String? value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.equalTo( + property: r'cachedBalanceSecondaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringGreaterThan( + String? value, { + bool include = false, + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.greaterThan( + include: include, + property: r'cachedBalanceSecondaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringLessThan( + String? value, { + bool include = false, + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.lessThan( + include: include, + property: r'cachedBalanceSecondaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringBetween( + String? lower, + String? upper, { + bool includeLower = true, + bool includeUpper = true, + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.between( + property: r'cachedBalanceSecondaryString', + lower: lower, + includeLower: includeLower, + upper: upper, + includeUpper: includeUpper, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringStartsWith( + String value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.startsWith( + property: r'cachedBalanceSecondaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringEndsWith( + String value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.endsWith( + property: r'cachedBalanceSecondaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringContains(String value, + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.contains( + property: r'cachedBalanceSecondaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringMatches(String pattern, + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.matches( + property: r'cachedBalanceSecondaryString', + wildcard: pattern, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringIsEmpty() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.equalTo( + property: r'cachedBalanceSecondaryString', + value: '', + )); + }); + } + + QueryBuilder + cachedBalanceSecondaryStringIsNotEmpty() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.greaterThan( + property: r'cachedBalanceSecondaryString', + value: '', + )); + }); + } + QueryBuilder cachedBalanceStringIsNull() { return QueryBuilder.apply(this, (query) { @@ -600,6 +784,162 @@ extension WalletInfoQueryFilter }); } + QueryBuilder + cachedBalanceTertiaryStringIsNull() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(const FilterCondition.isNull( + property: r'cachedBalanceTertiaryString', + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringIsNotNull() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(const FilterCondition.isNotNull( + property: r'cachedBalanceTertiaryString', + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringEqualTo( + String? value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.equalTo( + property: r'cachedBalanceTertiaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringGreaterThan( + String? value, { + bool include = false, + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.greaterThan( + include: include, + property: r'cachedBalanceTertiaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringLessThan( + String? value, { + bool include = false, + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.lessThan( + include: include, + property: r'cachedBalanceTertiaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringBetween( + String? lower, + String? upper, { + bool includeLower = true, + bool includeUpper = true, + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.between( + property: r'cachedBalanceTertiaryString', + lower: lower, + includeLower: includeLower, + upper: upper, + includeUpper: includeUpper, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringStartsWith( + String value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.startsWith( + property: r'cachedBalanceTertiaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringEndsWith( + String value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.endsWith( + property: r'cachedBalanceTertiaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringContains(String value, + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.contains( + property: r'cachedBalanceTertiaryString', + value: value, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringMatches(String pattern, + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.matches( + property: r'cachedBalanceTertiaryString', + wildcard: pattern, + caseSensitive: caseSensitive, + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringIsEmpty() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.equalTo( + property: r'cachedBalanceTertiaryString', + value: '', + )); + }); + } + + QueryBuilder + cachedBalanceTertiaryStringIsNotEmpty() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(FilterCondition.greaterThan( + property: r'cachedBalanceTertiaryString', + value: '', + )); + }); + } + QueryBuilder cachedChainHeightEqualTo(int value) { return QueryBuilder.apply(this, (query) { @@ -1823,6 +2163,20 @@ extension WalletInfoQueryLinks extension WalletInfoQuerySortBy on QueryBuilder { + QueryBuilder + sortByCachedBalanceSecondaryString() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceSecondaryString', Sort.asc); + }); + } + + QueryBuilder + sortByCachedBalanceSecondaryStringDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceSecondaryString', Sort.desc); + }); + } + QueryBuilder sortByCachedBalanceString() { return QueryBuilder.apply(this, (query) { @@ -1837,6 +2191,20 @@ extension WalletInfoQuerySortBy }); } + QueryBuilder + sortByCachedBalanceTertiaryString() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceTertiaryString', Sort.asc); + }); + } + + QueryBuilder + sortByCachedBalanceTertiaryStringDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceTertiaryString', Sort.desc); + }); + } + QueryBuilder sortByCachedChainHeight() { return QueryBuilder.apply(this, (query) { return query.addSortBy(r'cachedChainHeight', Sort.asc); @@ -1982,6 +2350,20 @@ extension WalletInfoQuerySortBy extension WalletInfoQuerySortThenBy on QueryBuilder { + QueryBuilder + thenByCachedBalanceSecondaryString() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceSecondaryString', Sort.asc); + }); + } + + QueryBuilder + thenByCachedBalanceSecondaryStringDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceSecondaryString', Sort.desc); + }); + } + QueryBuilder thenByCachedBalanceString() { return QueryBuilder.apply(this, (query) { @@ -1996,6 +2378,20 @@ extension WalletInfoQuerySortThenBy }); } + QueryBuilder + thenByCachedBalanceTertiaryString() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceTertiaryString', Sort.asc); + }); + } + + QueryBuilder + thenByCachedBalanceTertiaryStringDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(r'cachedBalanceTertiaryString', Sort.desc); + }); + } + QueryBuilder thenByCachedChainHeight() { return QueryBuilder.apply(this, (query) { return query.addSortBy(r'cachedChainHeight', Sort.asc); @@ -2153,6 +2549,14 @@ extension WalletInfoQuerySortThenBy extension WalletInfoQueryWhereDistinct on QueryBuilder { + QueryBuilder + distinctByCachedBalanceSecondaryString({bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addDistinctBy(r'cachedBalanceSecondaryString', + caseSensitive: caseSensitive); + }); + } + QueryBuilder distinctByCachedBalanceString( {bool caseSensitive = true}) { return QueryBuilder.apply(this, (query) { @@ -2161,6 +2565,14 @@ extension WalletInfoQueryWhereDistinct }); } + QueryBuilder + distinctByCachedBalanceTertiaryString({bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addDistinctBy(r'cachedBalanceTertiaryString', + caseSensitive: caseSensitive); + }); + } + QueryBuilder distinctByCachedChainHeight() { return QueryBuilder.apply(this, (query) { @@ -2253,6 +2665,13 @@ extension WalletInfoQueryProperty }); } + QueryBuilder + cachedBalanceSecondaryStringProperty() { + return QueryBuilder.apply(this, (query) { + return query.addPropertyName(r'cachedBalanceSecondaryString'); + }); + } + QueryBuilder cachedBalanceStringProperty() { return QueryBuilder.apply(this, (query) { @@ -2260,6 +2679,13 @@ extension WalletInfoQueryProperty }); } + QueryBuilder + cachedBalanceTertiaryStringProperty() { + return QueryBuilder.apply(this, (query) { + return query.addPropertyName(r'cachedBalanceTertiaryString'); + }); + } + QueryBuilder cachedChainHeightProperty() { return QueryBuilder.apply(this, (query) { return query.addPropertyName(r'cachedChainHeight');