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: #682 - add a "Clear all" menu item in the product history page #683

Merged
merged 1 commit into from
Nov 25, 2021
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
14 changes: 14 additions & 0 deletions packages/smooth_app/lib/database/dao_product_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class DaoProductList extends AbstractDao {
return result;
}

/// Removes a product list (it won't exist anymore) and unlinks all its items
Future<void> delete(final ProductList productList) async {
await localDatabase.database.transaction(
(final Transaction transaction) async {
Expand All @@ -372,6 +373,19 @@ class DaoProductList extends AbstractDao {
);
}

/// Clears a product list: unlinks all its items
Future<void> clear(final ProductList productList) async =>
localDatabase.database.transaction(
(final Transaction transaction) async {
final int? id = await _getId(productList, transaction);
Copy link
Member

Choose a reason for hiding this comment

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

I'am cusious, what happens when _getId retuns null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@M123-dev You can find the answer in DaoProductExtra._getExtraKey. If I sum up, some lists will always be there and are always meaningful and unique (e.g. history, scan history). They don't need an id: there's only one scan history, we can give it the id 'scanHistory'. Other lists are user-defined: you can create them and delete them. They need a specific id, like list/$id.

await DaoProductExtra(localDatabase).clearList(
productList,
id,
transaction,
);
},
);

Future<Map<int, Map<String, String>>> _getExtras({final int? listId}) async {
final Map<int, Map<String, String>> result = <int, Map<String, String>>{};
final List<Map<String, dynamic>> queryResult =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,34 @@ class ProductListDialogHelper {
) ??
false;

Future<bool> openClear(
final BuildContext context,
final DaoProductList daoProductList,
final ProductList productList,
) async =>
await showDialog<bool>(
context: context,
builder: (BuildContext context) => SmoothAlertDialog(
close: false,
body: const Text(
'Do you really want to clear this list?'), // TODO(monsieurtanuki): translate
actions: <SmoothSimpleButton>[
SmoothSimpleButton(
text: AppLocalizations.of(context)!.no,
onPressed: () => Navigator.pop(context, false),
),
SmoothSimpleButton(
text: AppLocalizations.of(context)!.yes,
onPressed: () async {
await daoProductList.clear(productList);
Navigator.pop(context, true);
},
),
],
),
) ??
false;

Future<ProductList?> openNew(
final BuildContext context,
final DaoProductList daoProductList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class _ProductListPageState extends State<ProductListPage> {
deletable = true;
renamable = true;
reorderable = true;
dismissible = true;
dismissible = productList.barcodes.isNotEmpty;
break;
case ProductList.LIST_TYPE_HTTP_SEARCH_KEYWORDS:
case ProductList.LIST_TYPE_HTTP_SEARCH_CATEGORY:
Expand All @@ -82,7 +82,7 @@ class _ProductListPageState extends State<ProductListPage> {
case ProductList.LIST_TYPE_SCAN_HISTORY:
case ProductList.LIST_TYPE_SCAN_SESSION:
case ProductList.LIST_TYPE_HISTORY:
dismissible = true;
dismissible = productList.barcodes.isNotEmpty;
break;
default:
throw Exception('unknown list type ${productList.listType}');
Expand Down Expand Up @@ -116,7 +116,7 @@ class _ProductListPageState extends State<ProductListPage> {
),
],
),
actions: (!renamable) && (!deletable)
actions: (!renamable) && (!deletable) && (!dismissible)
? null
: <Widget>[
PopupMenuButton<String>(
Expand All @@ -128,11 +128,18 @@ class _ProductListPageState extends State<ProductListPage> {
child: Text(appLocalizations.rename),
enabled: true,
),
PopupMenuItem<String>(
value: 'change',
child: Text(appLocalizations.change_icon),
enabled: true,
),
if (renamable)
PopupMenuItem<String>(
value: 'change',
child: Text(appLocalizations.change_icon),
enabled: true,
),
if (dismissible)
const PopupMenuItem<String>(
value: 'clear',
child: Text('Clear'), // TODO(monsieurtanuki): translate
enabled: true,
),
if (deletable)
PopupMenuItem<String>(
value: 'delete',
Expand All @@ -159,6 +166,12 @@ class _ProductListPageState extends State<ProductListPage> {
localDatabase.notifyListeners();
}
break;
case 'clear':
if (await ProductListDialogHelper.instance
.openClear(context, daoProductList, productList)) {
localDatabase.notifyListeners();
}
break;
case 'change':
final bool changed = await ProductListDialogHelper
.instance
Expand Down