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

Enhancement 15 #18

Merged
merged 24 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
1129f78
Update Dart SDK version (2.17.1 -> 2.17.3) & kassakuitti version
areee Jun 11, 2022
972f468
Fix an error with parsing an S-kaupat HTML file
areee Jun 11, 2022
546cf9a
Update dependencies (incl. dev dependencies)
areee Jun 11, 2022
215bd90
Update transitive dependencies
areee Jun 11, 2022
c282c7a
Changes to arg commands
areee Jun 11, 2022
0a7fb50
Some refactoring
areee Jun 12, 2022
1ffddee
Pull Hive initialization to the root level & add count command for Hi…
areee Jun 12, 2022
352fb79
Add methods for Hive CLI & add isNullOrEmpty String extension
areee Jun 12, 2022
1f3b72b
Update add, readAll & searchBy methods in Hive CLI
areee Jun 12, 2022
69fee16
Make needed Hive methods awaitable / asynchronous
areee Jun 18, 2022
aa5f4be
Make ean_handler awaitable / asynchronous & close the box in the main…
areee Jun 18, 2022
0bc705c
Update Hive read methods to show the key value (currently an order nu…
areee Jun 18, 2022
5313dcd
Fix delete method to delete based on given order number
areee Jun 18, 2022
bee34b4
Fix update method of hive_handling
areee Jun 18, 2022
fb99953
Refactor printing methods
areee Jun 18, 2022
49595c3
Fix div parsing in load_html_s_kaupat
areee Jun 18, 2022
096a4d3
Removed '_countProducts' from '_updateProduct' method
areee Jun 19, 2022
b7cd5c9
Some refactoring into update & delete methods
areee Jun 19, 2022
cecbd69
Fix a bug with digit-beginning receipt product
areee Jun 23, 2022
f0e6f66
Refactor update & delete methods
areee Jun 24, 2022
147b560
Add installation page & update readme
areee Jun 24, 2022
c7d7691
Add developing page & update readme
areee Jun 24, 2022
f1da9fd
Update readme
areee Jun 24, 2022
b0ab637
Update readme
areee Jun 24, 2022
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
1 change: 1 addition & 0 deletions bin/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
const kHiveBoxName = 'hiveProducts';
78 changes: 30 additions & 48 deletions bin/dart_kassakuitti_cli.dart
Original file line number Diff line number Diff line change
@@ -1,72 +1,54 @@
import 'dart:io';

import 'package:args/args.dart';
import 'read_ean_products.dart';
import 'ean_products_2_csv.dart';
import 'specific/s_kaupat/ean_handler.dart';
import 'specific/s_kaupat/read_receipt_products.dart';
import 'specific/s_kaupat/receipt_products_2_csv.dart';
import 'package:hive/hive.dart';

import 'constants.dart';
import 'hive_handling.dart';
import 'models/hive_product.dart';
import 'run_main_program.dart';
import 'utils/arg_selector_helper.dart';
import 'utils/parse_kassakuitti_arguments.dart';
import 'utils/printing_helper.dart';
import 'utils/shop_selector_helper.dart';

void main(List<String> arguments) async {
exitCode = 0; // presume success
var hiveProducts = await _initializeHiveProducts();

var parser = getParser();
var argResults = parser.parse(arguments);

await handleArgCommands(argResults, parser);
await _handleArgCommands(argResults, parser, hiveProducts);

hiveProducts.close();
}

/// Handles the commands in the arguments.
Future<void> handleArgCommands(ArgResults argResults, ArgParser parser) async {
Future<Box<HiveProduct>> _handleArgCommands(ArgResults argResults,
ArgParser parser, Box<HiveProduct> hiveProducts) async {
// Run command
if (argResults.command?.name == ArgSelector.run.value) {
await runMainProgram(argResults, hiveProducts);
}
// Help command
if (argResults.command?.name == ArgSelector.help.value) {
print('Help:\n${parser.usage}');
else if (argResults.command?.name == ArgSelector.help.value) {
printHelpers(parser);
}
// Run command
else if (argResults.command?.name == ArgSelector.run.value) {
print('\nRunning...\n');

var selectedTextFile = argResults[ArgSelector.textFile.value] as String?;
var selectedHtmlFile = argResults[ArgSelector.htmlFile.value] as String;
var selectedStore = argResults[ArgSelector.foodOnlineStore.value] as String;
var csvFilesPath = argResults[ArgSelector.csvPath.value] as String;

printSelectedValues(
selectedTextFile, selectedHtmlFile, selectedStore, csvFilesPath);

try {
if (ShopSelector.sKaupat.value == selectedStore) {
var receiptProducts =
await readReceiptProducts(selectedTextFile!, csvFilesPath);
var eanProducts = await readEANProducts(
selectedHtmlFile, ShopSelector.sKaupat, csvFilesPath);

await eanHandler(receiptProducts, eanProducts.toList());

receiptProducts2CSV(receiptProducts, csvFilesPath);
eanProducts2CSV(eanProducts, csvFilesPath, ShopSelector.sKaupat.name);
} else if (ShopSelector.kRuoka.value == selectedStore) {
var eanProducts = await readEANProducts(
selectedHtmlFile, ShopSelector.kRuoka, csvFilesPath);

eanProducts2CSV(eanProducts, csvFilesPath, ShopSelector.kRuoka.name);
} else {
print('Unknown store: $selectedStore');
exitCode = 1;
}
} on Exception catch (e) {
print('Error: $e');
exitCode = 1;
}

print('\nDone!');
// Hive (storage) command
else if (argResults.command?.name == ArgSelector.hive.value) {
await hiveHandling(hiveProducts);
}
// Empty command (or other commands, e.g. 'moro' / 'hello')
else {
await printBasicInfo(parser);
}

return hiveProducts;
}

/// Initializes the Hive products.
Future<Box<HiveProduct>> _initializeHiveProducts() async {
Hive.init(Directory.current.path);
Hive.registerAdapter(HiveProductAdapter());
return await Hive.openBox<HiveProduct>(kHiveBoxName);
}
196 changes: 196 additions & 0 deletions bin/hive_handling.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
import 'dart:io';

import 'package:hive/hive.dart';

import 'models/hive_product.dart';
import 'utils/extensions/string_extension.dart';

/// Hive handling (CRUD for storage handling).
Future<Box<HiveProduct>> hiveHandling(Box<HiveProduct> hiveProducts) async {
while (true) {
print('''

*** Hive handling ***
1. Create
2. Read all
3. Search by keyword
4. Update
5. Delete
6. Count
Empty command or something else to exit.
''');

var input = stdin.readLineSync();

switch (input) {
case '1':
await _addProduct(hiveProducts);
break;
case '2':
_readAllProducts(hiveProducts);
break;
case '3':
_searchByKeyword(hiveProducts);
break;
case '4':
await _updateProduct(hiveProducts);
break;
case '5':
await _deleteProduct(hiveProducts);
break;
case '6':
_countProducts(hiveProducts);
break;
default:
print('Exiting...');
return hiveProducts;
}
}
}

/// Adds a product to the storage.
Future<void> _addProduct(Box<HiveProduct> hiveProducts) async {
print('Enter the receipt name of the product:');
var name = stdin.readLineSync();

print('Enter the EAN name of the product:');
var eanName = stdin.readLineSync();

if (name.isNotNullOrEmpty() && eanName.isNotNullOrEmpty()) {
print('You entered: $name, $eanName');
print('Do you want to add this product? (y/n)');
var input = stdin.readLineSync();

if (input == 'y') {
await hiveProducts
.add(HiveProduct(receiptName: name!, eanName: eanName!));
print('Product added!');
_countProducts(hiveProducts);
} else {
print('Product not added!');
}
}
}

/// Reads all products from the storage.
void _readAllProducts(Box<HiveProduct> hiveProducts) {
_countProducts(hiveProducts);
print('All products:');
_printProducts(products: hiveProducts);
}

/// Prints Hive products.
void _printProducts(
{required Box<HiveProduct> products, Iterable<dynamic>? filteredProducts}) {
if (filteredProducts == null) {
for (var product in products.keys) {
_printSingleProduct(products: products, product: product);
}
} else {
for (var product in filteredProducts) {
_printSingleProduct(products: products, product: product);
}
}
}

/// Prints single Hive product.
void _printSingleProduct(
{required Box<HiveProduct> products, required dynamic product}) {
print('\t#$product: ${products.get(product)}');
}

/// Search by keyword in the storage.
void _searchByKeyword(Box<HiveProduct> hiveProducts) {
print('Enter a keyword:');
var keyword = stdin.readLineSync();

if (keyword.isNotNullOrEmpty()) {
print('You entered: $keyword');

var filteredProducts = hiveProducts.keys.where((key) => hiveProducts
.get(key)!
.receiptName
.toLowerCase()
.contains(keyword!.toLowerCase()));

var amount = filteredProducts.length;

print('Found $amount ${amount == 1 ? 'product' : 'products'}:');

_printProducts(products: hiveProducts, filteredProducts: filteredProducts);
}
}

/// Updates a product in the storage.
Future<void> _updateProduct(Box<HiveProduct> hiveProducts) async {
print('Enter the order number of the product:');

var orderNumber = stdin.readLineSync();

if (orderNumber.isNotNullOrEmpty()) {
var orderNumberAsInt = int.tryParse(orderNumber!);

var product = hiveProducts.get(orderNumberAsInt!);

if (product != null) {
print('Product: $product');

print('Enter the new receipt name of the product:');
var name = stdin.readLineSync();

print('Enter the new EAN name of the product:');
var eanName = stdin.readLineSync();

if (name.isNotNullOrEmpty() && eanName.isNotNullOrEmpty()) {
print('You entered: $name, $eanName');
print('Do you want to update this product? (y/n)');
var input = stdin.readLineSync();

if (input == 'y') {
await hiveProducts.put(orderNumberAsInt,
HiveProduct(receiptName: name!, eanName: eanName!));
print('Product updated!');
_countProducts(hiveProducts);
} else {
print('Product not updated!');
}
}
} else {
print('Product not found!');
}
}
}

/// Delete a product from the storage.
Future<void> _deleteProduct(Box<HiveProduct> hiveProducts) async {
print('Enter the order number of the product:');

var orderNumber = stdin.readLineSync();

if (orderNumber.isNotNullOrEmpty()) {
var orderNumberAsInt = int.tryParse(orderNumber!);

var product = hiveProducts.get(orderNumberAsInt!);
if (product != null) {
print('Product: $product');

print('Do you want to delete this product? (y/n)');
var input = stdin.readLineSync();

if (input == 'y') {
await hiveProducts.delete(orderNumberAsInt);
print('Product deleted!');
_countProducts(hiveProducts);
} else {
print('Product not deleted!');
}
} else {
print('Product not found!');
}
}
}

/// Counts the products in the storage.
void _countProducts(Box<HiveProduct> hiveProducts) {
print('Amount of products: ${hiveProducts.length}');
}
2 changes: 1 addition & 1 deletion bin/models/hive_product.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class HiveProduct extends HiveObject {

@override
String toString() {
return 'ReceiptName: $receiptName, eanName: $eanName';
return 'receiptName: $receiptName, eanName: $eanName';
}
}
56 changes: 56 additions & 0 deletions bin/run_main_program.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'dart:io';

import 'package:args/args.dart';
import 'package:hive/hive.dart';

import 'ean_products_2_csv.dart';
import 'models/hive_product.dart';
import 'read_ean_products.dart';
import 'specific/s_kaupat/ean_handler.dart';
import 'specific/s_kaupat/read_receipt_products.dart';
import 'specific/s_kaupat/receipt_products_2_csv.dart';
import 'utils/arg_selector_helper.dart';
import 'utils/printing_helper.dart';
import 'utils/shop_selector_helper.dart';

Future<Box<HiveProduct>> runMainProgram(
ArgResults argResults, Box<HiveProduct> hiveProducts) async {
print('\nRunning...\n');

var selectedTextFile = argResults[ArgSelector.textFile.value] as String?;
var selectedHtmlFile = argResults[ArgSelector.htmlFile.value] as String;
var selectedStore = argResults[ArgSelector.foodOnlineStore.value] as String;
var csvFilesPath = argResults[ArgSelector.csvPath.value] as String;

printSelectedValues(
selectedTextFile, selectedHtmlFile, selectedStore, csvFilesPath);

try {
if (ShopSelector.sKaupat.value == selectedStore) {
var receiptProducts =
await readReceiptProducts(selectedTextFile!, csvFilesPath);
var eanProducts = await readEANProducts(
selectedHtmlFile, ShopSelector.sKaupat, csvFilesPath);

await eanHandler(receiptProducts, eanProducts.toList(), hiveProducts);

receiptProducts2CSV(receiptProducts, csvFilesPath);
eanProducts2CSV(eanProducts, csvFilesPath, ShopSelector.sKaupat.name);
} else if (ShopSelector.kRuoka.value == selectedStore) {
var eanProducts = await readEANProducts(
selectedHtmlFile, ShopSelector.kRuoka, csvFilesPath);

eanProducts2CSV(eanProducts, csvFilesPath, ShopSelector.kRuoka.name);
} else {
print('Unknown store: $selectedStore');
exitCode = 1;
}
} on Exception catch (e) {
print('Error: $e');
exitCode = 1;
}

print('\nDone!');

return hiveProducts;
}
Loading