|
| 1 | +import 'package:drift/drift.dart'; |
| 2 | +import 'package:drift_flutter/drift_flutter.dart'; |
| 3 | +import 'package:path/path.dart' as path; |
| 4 | + |
| 5 | +import '../../../models/shopinbit/shopinbit_order_model.dart' |
| 6 | + show ShopInBitCategory, ShopInBitOrderStatus; |
| 7 | +import '../../../utilities/stack_file_system.dart'; |
| 8 | +import 'tables/cakepay_orders.dart'; |
| 9 | +import 'tables/shopin_bit_settings.dart'; |
| 10 | +import 'tables/shopin_bit_tickets.dart'; |
| 11 | + |
| 12 | +part 'shared_database.g.dart'; |
| 13 | + |
| 14 | +abstract final class SharedDrift { |
| 15 | + static bool _didInit = false; |
| 16 | + |
| 17 | + static SharedDatabase? _db; |
| 18 | + |
| 19 | + static SharedDatabase get() { |
| 20 | + if (!_didInit) { |
| 21 | + driftRuntimeOptions.dontWarnAboutMultipleDatabases = true; |
| 22 | + _didInit = true; |
| 23 | + } |
| 24 | + |
| 25 | + return _db ??= SharedDatabase._(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +@DriftDatabase( |
| 30 | + tables: [CakepayOrders, ShopinBitSettings, ShopInBitTickets], |
| 31 | + daos: [ShopinBitSettingsDao], |
| 32 | +) |
| 33 | +final class SharedDatabase extends _$SharedDatabase { |
| 34 | + SharedDatabase._([QueryExecutor? executor]) |
| 35 | + : super(executor ?? _openConnection()); |
| 36 | + |
| 37 | + @override |
| 38 | + int get schemaVersion => 2; |
| 39 | + |
| 40 | + @override |
| 41 | + MigrationStrategy get migration => MigrationStrategy( |
| 42 | + onUpgrade: (m, from, to) async { |
| 43 | + if (from == 1 && to == 2) { |
| 44 | + await m.createTable(shopinBitSettings); |
| 45 | + await m.createTable(shopInBitTickets); |
| 46 | + } |
| 47 | + }, |
| 48 | + ); |
| 49 | + |
| 50 | + static QueryExecutor _openConnection() { |
| 51 | + return driftDatabase( |
| 52 | + name: "shared", |
| 53 | + native: DriftNativeOptions( |
| 54 | + shareAcrossIsolates: true, |
| 55 | + databasePath: () async { |
| 56 | + final dir = await StackFileSystem.applicationDriftDirectory(); |
| 57 | + return path.join(dir.path, "shared", "shared.db"); |
| 58 | + }, |
| 59 | + ), |
| 60 | + ); |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +@DriftAccessor(tables: [ShopinBitSettings]) |
| 65 | +class ShopinBitSettingsDao extends DatabaseAccessor<SharedDatabase> |
| 66 | + with _$ShopinBitSettingsDaoMixin { |
| 67 | + ShopinBitSettingsDao(super.db); |
| 68 | + |
| 69 | + Future<ShopinBitSetting> getSettings() async { |
| 70 | + final ShopinBitSetting? row = await (select( |
| 71 | + shopinBitSettings, |
| 72 | + )..where((t) => t.id.equals(0))).getSingleOrNull(); |
| 73 | + if (row != null) return row; |
| 74 | + |
| 75 | + return into( |
| 76 | + shopinBitSettings, |
| 77 | + ).insertReturning(ShopinBitSettingsCompanion.insert(id: const Value(0))); |
| 78 | + } |
| 79 | + |
| 80 | + Future<void> setGuidelinesAccepted(bool accepted) => |
| 81 | + _update(ShopinBitSettingsCompanion(guidelinesAccepted: Value(accepted))); |
| 82 | + |
| 83 | + Future<void> setSetupComplete(bool complete) => |
| 84 | + _update(ShopinBitSettingsCompanion(setupComplete: Value(complete))); |
| 85 | + |
| 86 | + Future<void> setDisplayName(String name) => |
| 87 | + _update(ShopinBitSettingsCompanion(displayName: Value(name))); |
| 88 | + |
| 89 | + Future<void> _update(ShopinBitSettingsCompanion changes) async { |
| 90 | + await getSettings(); // ensure row exists |
| 91 | + await (update( |
| 92 | + shopinBitSettings, |
| 93 | + )..where((t) => t.id.equals(0))).write(changes); |
| 94 | + } |
| 95 | +} |
0 commit comments