| 1 | import 'package:cw_core/card_design.dart'; |
| 2 | import 'package:cw_core/db/sqlite.dart'; |
| 3 | import 'package:sqflite/sqflite.dart'; |
| 4 | |
| 5 | class BalanceCardStyleSettings { |
| 6 | final int walletInfoId; |
| 7 | final int accountIndex; |
| 8 | final int gradientIndex; |
| 9 | final bool useSpecialDesign; |
| 10 | final String backgroundImagePath; |
| 11 | final int iconStyleIndex; |
| 12 | final bool isGradientOnly; |
| 13 | final int cardOrder; |
| 14 | |
| 15 | BalanceCardStyleSettings( |
| 16 | {required this.walletInfoId, |
| 17 | required this.accountIndex, |
| 18 | required this.gradientIndex, |
| 19 | required this.useSpecialDesign, |
| 20 | required this.backgroundImagePath, |
| 21 | this.iconStyleIndex = 0, |
| 22 | this.isGradientOnly = false, |
| 23 | required this.cardOrder}); |
| 24 | |
| 25 | static const tableName = "BalanceCardStyleSettings"; |
| 26 | |
| 27 | Map<String, dynamic> toJson() { |
| 28 | final ret = { |
| 29 | "walletInfoId": walletInfoId, |
| 30 | "accountIndex": accountIndex, |
| 31 | "gradientIndex": gradientIndex, |
| 32 | "useSpecialDesign": useSpecialDesign ? 1 : 0, |
| 33 | "backgroundImagePath": backgroundImagePath, |
| 34 | "iconStyleIndex": iconStyleIndex, |
| 35 | "isGradientOnly": isGradientOnly ? 1 : 0, |
| 36 | "cardOrder": cardOrder, |
| 37 | }; |
| 38 | return ret; |
| 39 | } |
| 40 | |
| 41 | static BalanceCardStyleSettings fromJson(Map<String, dynamic> json) { |
| 42 | return BalanceCardStyleSettings( |
| 43 | walletInfoId: json["walletInfoId"] as int, |
| 44 | accountIndex: json["accountIndex"] as int, |
| 45 | gradientIndex: json["gradientIndex"] as int, |
| 46 | useSpecialDesign: json["useSpecialDesign"] == 1, |
| 47 | backgroundImagePath: json["backgroundImagePath"] as String? ?? "", |
| 48 | iconStyleIndex: json["iconStyleIndex"] as int? ?? 0, |
| 49 | isGradientOnly: json["isGradientOnly"] == 1, |
| 50 | cardOrder: json["cardOrder"] as int? ?? -1, |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | static BalanceCardStyleSettings fromCardDesign({ |
| 55 | required int walletInfoId, |
| 56 | required int accountIndex, |
| 57 | required int cardOrder, |
| 58 | required CardDesign design, |
| 59 | int iconStyleIndex = 0, |
| 60 | int? gradientIndexOverride, |
| 61 | }) { |
| 62 | final int gradientIndex = |
| 63 | gradientIndexOverride ?? CardDesign.allGradients.indexOf(design.gradient); |
| 64 | return BalanceCardStyleSettings( |
| 65 | walletInfoId: walletInfoId, |
| 66 | accountIndex: accountIndex, |
| 67 | gradientIndex: gradientIndex, |
| 68 | useSpecialDesign: design.backgroundType == CardDesignBackgroundTypes.svgFull, |
| 69 | backgroundImagePath: |
| 70 | design.backgroundType == CardDesignBackgroundTypes.image ? design.imagePath : "", |
| 71 | iconStyleIndex: iconStyleIndex, |
| 72 | isGradientOnly: design.backgroundType == CardDesignBackgroundTypes.gradientOnly, |
| 73 | cardOrder: cardOrder, |
| 74 | ); |
| 75 | } |
| 76 | |
| 77 | static Future<BalanceCardStyleSettings?> get(int walletInfoId, int accountIndex) async { |
| 78 | final json = await db!.query( |
| 79 | tableName, |
| 80 | where: "walletInfoId = ? AND accountIndex = ?", |
| 81 | whereArgs: [walletInfoId, accountIndex], |
| 82 | ); |
| 83 | |
| 84 | if (json.isEmpty) { |
| 85 | return null; |
| 86 | } |
| 87 | |
| 88 | return fromJson(json.first); |
| 89 | } |
| 90 | |
| 91 | static Future<List<BalanceCardStyleSettings>> getAll(int walletInfoId) async { |
| 92 | final json = await db!.query( |
| 93 | tableName, |
| 94 | where: "walletInfoId = ?", |
| 95 | whereArgs: [walletInfoId], |
| 96 | ); |
| 97 | return List.generate(json.length, (index) => BalanceCardStyleSettings.fromJson(json[index])); |
| 98 | } |
| 99 | |
| 100 | Future<void> insert() async { |
| 101 | db!.insert(tableName, toJson(), conflictAlgorithm: ConflictAlgorithm.replace); |
| 102 | } |
| 103 | } |