Skip to content
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: 4947 - added local "last access" timestamp for products #4969

Merged
merged 3 commits into from
Jan 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions packages/smooth_app/lib/database/dao_product_last_access.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import 'dart:async';
import 'package:smooth_app/database/abstract_sql_dao.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:sqflite/sqflite.dart';

/// Table that stores the local last access timestamp for a product.
class DaoProductLastAccess extends AbstractSqlDao {
DaoProductLastAccess(super.localDatabase);

static const String TABLE = 'product_last_access';
static const String COLUMN_BARCODE = 'barcode';
static const String COLUMN_LAST_ACCESS = 'last_access';
Comment on lines +7 to +12
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name "LastAccess" could be confusing, we should clarify somewhere that its the last server access, not last local access to the product


static FutureOr<void> onUpgrade(
final Database db,
final int oldVersion,
final int newVersion,
) async {
if (oldVersion < 5) {
await db.execute('create table $TABLE('
// cf. https://www.sqlite.org/lang_conflict.html
'$COLUMN_BARCODE TEXT PRIMARY KEY on conflict replace'
',$COLUMN_LAST_ACCESS INT NOT NULL'
')');
}
}

Future<void> put(final String barcode) async =>
localDatabase.database.rawInsert(
'insert into $TABLE($COLUMN_BARCODE, $COLUMN_LAST_ACCESS) '
'values(?, ?)',
<Object>[
barcode,
LocalDatabase.nowInMillis(),
],
);

/// Delete all items from the database
Future<int> deleteAll() async => localDatabase.database.delete(TABLE);
}
4 changes: 3 additions & 1 deletion packages/smooth_app/lib/database/local_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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_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';
Expand Down Expand Up @@ -62,7 +63,7 @@ class LocalDatabase extends ChangeNotifier {
final String databasePath = join(databasesRootPath, 'smoothie.db');
final Database database = await openDatabase(
databasePath,
version: 4,
version: 5,
singleInstance: true,
onUpgrade: _onUpgrade,
);
Expand Down Expand Up @@ -105,5 +106,6 @@ class LocalDatabase extends ChangeNotifier {
) async {
await DaoProduct.onUpgrade(db, oldVersion, newVersion);
await DaoWorkBarcode.onUpgrade(db, oldVersion, newVersion);
await DaoProductLastAccess.onUpgrade(db, oldVersion, newVersion);
}
}
4 changes: 4 additions & 0 deletions packages/smooth_app/lib/pages/offline_data_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:provider/provider.dart';
import 'package:smooth_app/background/background_task_full_refresh.dart';
import 'package:smooth_app/background/background_task_offline.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_product_last_access.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/generic_lib/design_constants.dart';
import 'package:smooth_app/generic_lib/duration_constants.dart';
Expand Down Expand Up @@ -34,6 +35,8 @@ class _OfflineDataPageState extends State<OfflineDataPage> {
final double backgroundHeight = MediaQuery.of(context).size.height * .20;
final LocalDatabase localDatabase = context.watch<LocalDatabase>();
final DaoProduct daoProduct = DaoProduct(localDatabase);
final DaoProductLastAccess daoProductLastAccess =
DaoProductLastAccess(localDatabase);
final AppLocalizations appLocalizations = AppLocalizations.of(context);
return SmoothScaffold(
appBar: SmoothAppBar(
Expand Down Expand Up @@ -82,6 +85,7 @@ class _OfflineDataPageState extends State<OfflineDataPage> {
trailing: const Icon(Icons.delete),
onTap: () async {
final int totalProductsDeleted = await daoProduct.deleteAll();
await daoProductLastAccess.deleteAll();
if (mounted) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:smooth_app/data_models/fetched_product.dart';
import 'package:smooth_app/database/dao_product.dart';
import 'package:smooth_app/database/dao_product_last_access.dart';
import 'package:smooth_app/database/local_database.dart';
import 'package:smooth_app/query/barcode_product_query.dart';

Expand All @@ -25,6 +26,7 @@ class ProductModel with ChangeNotifier {
/// In the constructor we retrieve async'ly the product from the local db.
ProductModel(this.barcode, this.localDatabase) {
localDatabase.upToDate.showInterest(barcode);
DaoProductLastAccess(localDatabase).put(barcode);
_asyncLoad();
}

Expand Down
5 changes: 4 additions & 1 deletion packages/smooth_app/lib/pages/product/new_product_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:smooth_app/data_models/preferences/user_preferences.dart';
import 'package:smooth_app/data_models/product_list.dart';
import 'package:smooth_app/data_models/product_preferences.dart';
import 'package:smooth_app/data_models/up_to_date_mixin.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/local_database.dart';
import 'package:smooth_app/generic_lib/buttons/smooth_large_button_with_icon.dart';
Expand Down Expand Up @@ -74,7 +75,9 @@ class _ProductPageState extends State<ProductPage>
@override
void initState() {
super.initState();
initUpToDate(widget.product, context.read<LocalDatabase>());
final LocalDatabase localDatabase = context.read<LocalDatabase>();
initUpToDate(widget.product, localDatabase);
DaoProductLastAccess(localDatabase).put(barcode);
questionsLayout = getUserQuestionsLayout(context.read<UserPreferences>());
WidgetsBinding.instance.addPostFrameCallback((_) {
_updateLocalDatabaseWithProductHistory(context);
Expand Down