-
-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathlocal_database.dart
More file actions
132 lines (113 loc) · 4.87 KB
/
local_database.dart
File metadata and controls
132 lines (113 loc) · 4.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:smooth_app/background/background_task_manager.dart';
import 'package:smooth_app/data_models/up_to_date_product_list_provider.dart';
import 'package:smooth_app/data_models/up_to_date_product_provider.dart';
import 'package:smooth_app/database/abstract_dao.dart';
import 'package:smooth_app/database/dao_autocomplete.dart';
import 'package:smooth_app/database/dao_folksonomy.dart';
import 'package:smooth_app/database/dao_hive_product.dart';
import 'package:smooth_app/database/dao_instant_string.dart';
import 'package:smooth_app/database/dao_int.dart';
import 'package:smooth_app/database/dao_osm_location.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_product_last_access.dart';
import 'package:smooth_app/database/dao_product_list.dart';
import 'package:smooth_app/database/dao_string.dart';
import 'package:smooth_app/database/dao_string_list.dart';
import 'package:smooth_app/database/dao_string_list_map.dart';
import 'package:smooth_app/database/dao_transient_folksonomy.dart';
import 'package:smooth_app/database/dao_transient_operation.dart';
import 'package:smooth_app/database/dao_work_barcode.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';
class LocalDatabase extends ChangeNotifier {
LocalDatabase._(final Database database) : _database = database {
_upToDateProductProvider = UpToDateProductProvider(this);
_upToDateProductListProvider = UpToDateProductListProvider(this);
}
final Database _database;
late final UpToDateProductProvider _upToDateProductProvider;
late final UpToDateProductListProvider _upToDateProductListProvider;
Database get database => _database;
UpToDateProductProvider get upToDate => _upToDateProductProvider;
UpToDateProductListProvider get upToDateProductList =>
_upToDateProductListProvider;
@override
void notifyListeners() {
BackgroundTaskManager.runAgain(this);
super.notifyListeners();
}
/// Returns all the pending background task ids.
///
/// Ugly solution to be able to mock hive data.
List<String> getAllTaskIds(final String key) =>
DaoStringList(this).getAll(key);
/// Ugly solution to be able to mock hive data.
int? daoIntGet(final String key) => DaoInt(this).get(key);
/// Ugly solution to be able to mock hive data.
Future<void> daoIntPut(final String key, final int? value) =>
DaoInt(this).put(key, value);
static Future<LocalDatabase> getLocalDatabase() async {
// sql from there
String? databasesRootPath;
if (defaultTargetPlatform == TargetPlatform.iOS) {
// as suggested in https://pub.dev/documentation/sqflite/latest/sqflite/getDatabasesPath.html
final Directory directory = await getLibraryDirectory();
databasesRootPath = directory.path;
} else if (Platform.isLinux || Platform.isWindows) {
sqfliteFfiInit();
databaseFactory = databaseFactoryFfi;
}
databasesRootPath ??= await getDatabasesPath();
final String databasePath = join(databasesRootPath, 'smoothie.db');
final Database database = await openDatabase(
databasePath,
version: 10,
singleInstance: true,
onUpgrade: _onUpgrade,
);
final LocalDatabase localDatabase = LocalDatabase._(database);
// only hive from there
await Hive.initFlutter();
final List<AbstractDao> daos = <AbstractDao>[
DaoHiveProduct(localDatabase),
DaoProductList(localDatabase),
DaoStringList(localDatabase),
DaoString(localDatabase),
DaoInstantString(localDatabase),
DaoInt(localDatabase),
DaoStringListMap(localDatabase),
DaoTransientOperation(localDatabase),
DaoTransientFolksonomy(localDatabase),
];
for (final AbstractDao dao in daos) {
dao.registerAdapter();
}
for (final AbstractDao dao in daos) {
await dao.init();
}
// Migrations here
// (no migration for the moment)
return localDatabase;
}
static int nowInMillis() => DateTime.now().millisecondsSinceEpoch;
/// we don't use onCreate and onUpgrade, we use only onUpgrade instead.
/// checked: from scratch, onUpgrade is called with oldVersion = 0.
static FutureOr<void> _onUpgrade(
final Database db,
final int oldVersion,
final int newVersion,
) async {
await DaoProduct.onUpgrade(db, oldVersion, newVersion);
await DaoWorkBarcode.onUpgrade(db, oldVersion, newVersion);
await DaoProductLastAccess.onUpgrade(db, oldVersion, newVersion);
await DaoOsmLocation.onUpgrade(db, oldVersion, newVersion);
await DaoFolksonomy.onUpgrade(db, oldVersion, newVersion);
await DaoNamespace.onUpgrade(db, oldVersion, newVersion);
await DaoAutocompleteCache.onUpgrade(db, oldVersion, newVersion);
}
}