1111import 'dart:async' ;
1212import 'dart:convert' ;
1313import 'dart:io' ;
14- import 'dart:typed_data' ;
1514
15+ import 'package:drift/drift.dart' ;
1616import 'package:flutter/material.dart' ;
1717import 'package:isar_community/isar.dart' ;
1818import 'package:stack_wallet_backup/stack_wallet_backup.dart' ;
@@ -21,6 +21,7 @@ import 'package:uuid/uuid.dart';
2121import 'package:wakelock_plus/wakelock_plus.dart' ;
2222
2323import '../../../../../app_config.dart' ;
24+ import '../../../../../db/drift/shared_db/shared_database.dart' ;
2425import '../../../../../db/hive/db.dart' ;
2526import '../../../../../db/isar/main_db.dart' ;
2627import '../../../../../models/exchange/change_now/exchange_transaction.dart' ;
@@ -34,7 +35,9 @@ import '../../../../../models/trade_wallet_lookup.dart';
3435import '../../../../../models/wallet_restore_state.dart' ;
3536import '../../../../../notifications/show_flush_bar.dart' ;
3637import '../../../../../services/address_book_service.dart' ;
38+ import '../../../../../services/cakepay/cakepay_service.dart' ;
3739import '../../../../../services/node_service.dart' ;
40+ import '../../../../../services/shopinbit/shopinbit_service.dart' ;
3841import '../../../../../services/trade_notes_service.dart' ;
3942import '../../../../../services/trade_sent_from_stack_service.dart' ;
4043import '../../../../../services/trade_service.dart' ;
@@ -236,6 +239,29 @@ abstract class SWB {
236239 Logging .instance.e ("" , error: e, stackTrace: s);
237240 }
238241
242+ Logging .instance.i ("SWB backing up cakepay orders" );
243+ final cakepayOrderIds = await CakePayService .instance.getOrderIds ();
244+ backupJson["cakepayOrderIds" ] = cakepayOrderIds;
245+
246+ Logging .instance.i ("SWB backing up shopin bit info" );
247+ final sharedDB = SharedDrift .get ();
248+ final shopinBitSettings = await sharedDB.shopinBitSettings.select ().get ();
249+ final shopinBitCustomerKey =
250+ await (ShopInBitService ()..ensureInitialized (_secureStore))
251+ .loadCustomerKey ();
252+ final shopinBitOrders = await sharedDB.shopInBitTickets.select ().get ();
253+
254+ backupJson["shopinBit" ] = {
255+ if (shopinBitCustomerKey != null )
256+ "shopinBitCustomerKey" : shopinBitCustomerKey,
257+ if (shopinBitSettings.isNotEmpty)
258+ "shopinBitSettings" : shopinBitSettings.first.toJson (),
259+ if (shopinBitOrders.isNotEmpty)
260+ "shopinBitOrders" : shopinBitOrders
261+ .map ((e) => e.toJson ())
262+ .toList (growable: false ),
263+ };
264+
239265 Logging .instance.d ("SWB backing up prefs" );
240266
241267 final Map <String , dynamic > prefs = {};
@@ -609,6 +635,9 @@ abstract class SWB {
609635
610636 uiState? .preferences = StackRestoringStatus .restoring;
611637
638+ Logging .instance.d ("SWB restoring cakepay order ids and shop in bit info" );
639+ await _restoreCakepayAndShopinBitInfo (validJSON, secureStorageInterface);
640+
612641 Logging .instance.d ("SWB restoring prefs" );
613642 await _restorePrefs (prefs);
614643
@@ -886,6 +915,12 @@ abstract class SWB {
886915 final Map <String , dynamic >? tradeNotes =
887916 revertToState.validJSON["tradeNotes" ] as Map <String , dynamic >? ;
888917
918+ // cakepay and shopinbit
919+ await _restoreCakepayAndShopinBitInfo (
920+ revertToState.validJSON,
921+ secureStorageInterface,
922+ );
923+
889924 // prefs
890925 await _restorePrefs (prefs);
891926
@@ -1085,6 +1120,65 @@ abstract class SWB {
10851120 Logging .instance.d ("Revert SWB complete" );
10861121 }
10871122
1123+ static Future <void > _restoreCakepayAndShopinBitInfo (
1124+ Map <String , dynamic > backupJson,
1125+ SecureStorageInterface _secureStore,
1126+ ) async {
1127+ final cakepayOrderIds = (backupJson["cakepayOrderIds" ] as List ? ?? [])
1128+ .cast <String >();
1129+ for (final orderId in cakepayOrderIds) {
1130+ await CakePayService .instance.addOrderId (orderId);
1131+ }
1132+
1133+ final sharedDB = SharedDrift .get ();
1134+ final json = backupJson["shopinBit" ] as Map ? ?? {};
1135+
1136+ if (json.isEmpty) return ;
1137+
1138+ final shopinBitCustomerKey = json["shopinBitCustomerKey" ] as String ? ;
1139+ if (shopinBitCustomerKey != null ) {
1140+ final currentKey =
1141+ await (ShopInBitService ()..ensureInitialized (_secureStore))
1142+ .loadCustomerKey ();
1143+
1144+ if (currentKey != null && currentKey != shopinBitCustomerKey) {
1145+ // TODO come back to this at some point
1146+ // for now
1147+ Logging .instance.w (
1148+ "SWB restore found mismatching shopinbit customer keys. "
1149+ "Ignoring the backup data in favor of the current data." ,
1150+ );
1151+ return ;
1152+ }
1153+ }
1154+
1155+ final shopinBitSettings = json["shopinBitSettings" ] as Map ? ;
1156+ if (shopinBitSettings != null ) {
1157+ final settings = ShopinBitSetting .fromJson (shopinBitSettings.cast ());
1158+
1159+ await sharedDB.transaction (() async {
1160+ await sharedDB
1161+ .into (sharedDB.shopinBitSettings)
1162+ .insertOnConflictUpdate (settings.toCompanion (true ));
1163+ });
1164+ }
1165+
1166+ final shopinBitOrders = json["shopinBitOrders" ] as List ? ;
1167+ if (shopinBitOrders != null ) {
1168+ final orders = shopinBitOrders
1169+ .map ((e) => ShopInBitTicket .fromJson ((e as Map ).cast ()))
1170+ .map ((e) => e.toCompanion (true ));
1171+
1172+ await sharedDB.transaction (() async {
1173+ for (final order in orders) {
1174+ await sharedDB
1175+ .into (sharedDB.shopInBitTickets)
1176+ .insertOnConflictUpdate (order);
1177+ }
1178+ });
1179+ }
1180+ }
1181+
10881182 static Future <void > _restorePrefs (Map <String , dynamic > prefs) async {
10891183 final _prefs = Prefs .instance;
10901184 await _prefs.init ();
0 commit comments