Skip to content

Commit fa5fa81

Browse files
committed
refactor(shopinbit): Store shop in bit settings using Drift, use providers for drift shared db and shopinbit service, and some general clean up and tweaks
1 parent 7ddaa90 commit fa5fa81

29 files changed

Lines changed: 1708 additions & 1266 deletions

lib/db/drift/shared_database.dart

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,72 @@ class CakepayOrders extends Table {
2828
Set<Column> get primaryKey => {orderId};
2929
}
3030

31-
@DriftDatabase(tables: [CakepayOrders])
31+
class ShopinBitSettings extends Table {
32+
// Single row table - always row 0
33+
IntColumn get id => integer().withDefault(const Constant(0))();
34+
35+
BoolColumn get guidelinesAccepted =>
36+
boolean().withDefault(const Constant(false))();
37+
BoolColumn get setupComplete =>
38+
boolean().withDefault(const Constant(false))();
39+
TextColumn get displayName => text().nullable()();
40+
41+
@override
42+
Set<Column> get primaryKey => {id};
43+
}
44+
45+
@DriftAccessor(tables: [ShopinBitSettings])
46+
class ShopinBitSettingsDao extends DatabaseAccessor<SharedDatabase>
47+
with _$ShopinBitSettingsDaoMixin {
48+
ShopinBitSettingsDao(super.db);
49+
50+
Future<ShopinBitSetting> getSettings() async {
51+
final ShopinBitSetting? row = await (select(
52+
shopinBitSettings,
53+
)..where((t) => t.id.equals(0))).getSingleOrNull();
54+
if (row != null) return row;
55+
56+
return into(
57+
shopinBitSettings,
58+
).insertReturning(ShopinBitSettingsCompanion.insert(id: const Value(0)));
59+
}
60+
61+
Future<void> setGuidelinesAccepted(bool accepted) =>
62+
_update(ShopinBitSettingsCompanion(guidelinesAccepted: Value(accepted)));
63+
64+
Future<void> setSetupComplete(bool complete) =>
65+
_update(ShopinBitSettingsCompanion(setupComplete: Value(complete)));
66+
67+
Future<void> setDisplayName(String name) =>
68+
_update(ShopinBitSettingsCompanion(displayName: Value(name)));
69+
70+
Future<void> _update(ShopinBitSettingsCompanion changes) async {
71+
await getSettings(); // ensure row exists
72+
await (update(
73+
shopinBitSettings,
74+
)..where((t) => t.id.equals(0))).write(changes);
75+
}
76+
}
77+
78+
@DriftDatabase(
79+
tables: [CakepayOrders, ShopinBitSettings],
80+
daos: [ShopinBitSettingsDao],
81+
)
3282
final class SharedDatabase extends _$SharedDatabase {
3383
SharedDatabase._([QueryExecutor? executor])
3484
: super(executor ?? _openConnection());
3585

3686
@override
37-
int get schemaVersion => 1;
87+
int get schemaVersion => 2;
88+
89+
@override
90+
MigrationStrategy get migration => MigrationStrategy(
91+
onUpgrade: (m, from, to) async {
92+
if (from == 1 && to == 2) {
93+
await m.createTable(shopinBitSettings);
94+
}
95+
},
96+
);
3897

3998
static QueryExecutor _openConnection() {
4099
return driftDatabase(

0 commit comments

Comments
 (0)