-
-
Notifications
You must be signed in to change notification settings - Fork 271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add offline mode data manager #2322
Changes from 31 commits
f8e9002
4526ad9
96afeec
1786b66
748ebc2
b7c1a4f
16838e2
70e668f
996cb33
c38c954
240b12f
22c5c91
b7edb5a
57d35a4
999a754
ac635a2
8a66caf
be0b44e
9b2dfc0
d604001
d203118
96eb626
cbe9e3c
f414347
bc47207
bf3945c
f72d4eb
327e167
c498ea5
fe811a2
3a96937
1323f99
02e5de5
f435840
e182bd6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,6 +73,18 @@ class DaoProduct extends AbstractSqlDao | |
return result; | ||
} | ||
|
||
// Returns the no of rows deleted/effected from the table | ||
Future<int> clearAll() async { | ||
AshAman999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
final int rowsDeleted = await localDatabase.database.delete(_TABLE_PRODUCT); | ||
return rowsDeleted; | ||
} | ||
|
||
Future<int> getLength() async { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A getter would be better here |
||
return Sqflite.firstIntValue(await localDatabase.database | ||
.rawQuery('SELECT COUNT(*) FROM $_TABLE_PRODUCT')) ?? | ||
0; | ||
} | ||
|
||
Future<void> put(final Product product) async => putAll(<Product>[product]); | ||
|
||
/// Replaces products in database | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,6 +200,10 @@ class DaoProductList extends AbstractDao { | |
_put(_getKey(productList), newList); | ||
} | ||
|
||
void updateTimeStamp(final ProductList productList) { | ||
// TODO(ashaman999): update the timestamp for all the entries with the current timestamp | ||
} | ||
|
||
void clear(final ProductList productList) { | ||
final _BarcodeList newList = _BarcodeList.now(<String>[]); | ||
_put(_getKey(productList), newList); | ||
|
@@ -292,6 +296,53 @@ class DaoProductList extends AbstractDao { | |
return result; | ||
} | ||
|
||
/// Returns the number of items cleared from the box | ||
Future<int> clearAll() async { | ||
final int clearedItems = await _getBox().clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should use a single line instruction here |
||
return clearedItems; | ||
} | ||
|
||
/// Get the keys for every item in the box | ||
Future<List<String>> getKeys() async { | ||
final List<String> keys = <String>[]; | ||
for (final dynamic key in _getBox().keys) { | ||
keys.add(key.toString()); | ||
} | ||
return keys; | ||
} | ||
|
||
Future<void> deleteWithKey(final String key) async { | ||
await _getBox().delete(key); | ||
} | ||
|
||
Future<void> updateTimeStampForAKey(String key) async { | ||
final _BarcodeList? list = _getBox().get(key); | ||
if (list == null) { | ||
return; | ||
} | ||
final _BarcodeList newList = _BarcodeList.now(list.barcodes); | ||
_put(key, newList); | ||
} | ||
|
||
/// Get the list of the keys to delete | ||
Future<List<String>> typesToDelete() async { | ||
final List<String> keys = await getKeys(); | ||
// ignore: list_remove_unrelated_type | ||
keys.remove(!keys.contains(ProductListType.HISTORY.key) || | ||
keys.contains(ProductListType.HTTP_SEARCH_KEYWORDS.key) || | ||
keys.contains(ProductListType.SCAN_SESSION.key) || | ||
keys.contains(ProductListType.HTTP_SEARCH_CATEGORY.key)); | ||
return keys; | ||
} | ||
|
||
/// Delete just enteries containing HTTP_SEARCH_KEYWORDS.key | ||
teolemon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Future<List<String>> getKeysToDelete() async { | ||
final List<String> keys = await getKeys(); | ||
keys.removeWhere((String key) => | ||
!key.contains(ProductListType.HTTP_SEARCH_KEYWORDS.key)); | ||
return keys; | ||
} | ||
|
||
/// Returns a write-safe copy of [_BarcodeList] barcodes. | ||
/// | ||
/// cf. https://github.com/openfoodfacts/smooth-app/issues/1786 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,4 +43,7 @@ class DaoStringList extends AbstractDao { | |
} | ||
return false; | ||
} | ||
|
||
// Returns the number of items removed from the box | ||
Future<int> removeAll() async => _getBox().clear(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Async is useless here |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,15 +33,7 @@ class LocalDatabase extends ChangeNotifier { | |
|
||
static Future<LocalDatabase> getLocalDatabase() async { | ||
// sql from there | ||
final 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 { | ||
databasesRootPath = await getDatabasesPath(); | ||
} | ||
final String databasePath = join(databasesRootPath, 'smoothie.db'); | ||
final String databasePath = await _getDatabasePath(); | ||
final Database database = await openDatabase( | ||
databasePath, | ||
version: 2, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you make this a constant |
||
|
@@ -81,6 +73,18 @@ class LocalDatabase extends ChangeNotifier { | |
return localDatabase; | ||
} | ||
|
||
static Future<String> _getDatabasePath() async { | ||
final 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 { | ||
databasesRootPath = await getDatabasesPath(); | ||
} | ||
return join(databasesRootPath, 'smoothie.db'); | ||
AshAman999 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
static int nowInMillis() => DateTime.now().millisecondsSinceEpoch; | ||
|
||
/// we don't use onCreate and onUpgrade, we use only onUpgrade instead. | ||
|
@@ -93,4 +97,12 @@ class LocalDatabase extends ChangeNotifier { | |
await DaoUnzippedProduct.onUpgrade(db, oldVersion, newVersion); | ||
await DaoProduct.onUpgrade(db, oldVersion, newVersion); | ||
} | ||
|
||
//Returns the approximate size of the database in MB | ||
Future<double> getSizeinMb() async { | ||
final String path = await _getDatabasePath(); | ||
final File file = File(path); | ||
final double size = file.lengthSync() / 1024 / 1024; | ||
return double.parse(size.toStringAsFixed(1)); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.