-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/cache layer #3
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
Changes from 2 commits
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 |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class Barcode { | ||
| final String value; | ||
|
|
||
| Barcode(String value) : value = value.trim(); | ||
|
|
||
| bool isValid() { | ||
| return value.isNotEmpty; | ||
| } | ||
|
|
||
| String formattedValue() { | ||
| return 'Barcode: $value'; | ||
| } | ||
| } | ||
|
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.
|
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,19 @@ | ||||||
| import 'package:nutrient_scanner/util/cache_manager.dart'; | ||||||
|
|
||||||
| class BarcodeCacheService { | ||||||
| final CacheManager _cacheManager = CacheManager(); | ||||||
|
|
||||||
| Future<void> saveToCache(String barcode, String data) 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.
Suggested change
Collaborator
Author
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. 수정했습니다! |
||||||
| final cacheKey = _getCacheKey(barcode); | ||||||
| await _cacheManager.saveToCache(cacheKey, data); | ||||||
| } | ||||||
|
|
||||||
| Future<String?> loadFromCache(String barcode) 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.
Suggested change
Collaborator
Author
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. 수정했습니다! |
||||||
| final cacheKey = _getCacheKey(barcode); | ||||||
| return await _cacheManager.loadFromCache(cacheKey); | ||||||
| } | ||||||
|
|
||||||
| String _getCacheKey(String barcode) { | ||||||
|
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.
Suggested change
Collaborator
Author
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. 수정했습니다! |
||||||
| return barcode; | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:nutrient_scanner/features/barcode_scan/model/barcode_model.dart'; | ||
| import 'package:simple_barcode_scanner/simple_barcode_scanner.dart'; | ||
|
|
||
| class BarcodeScanService { | ||
| Future<Barcode?> scanBarcode(BuildContext context) async { | ||
| try { | ||
| final String? result = await SimpleBarcodeScanner.scanBarcode( | ||
| context, | ||
| isShowFlashIcon: true, | ||
| delayMillis: 500, | ||
| cameraFace: CameraFace.back, | ||
| scanFormat: ScanFormat.ONLY_BARCODE, | ||
| ); | ||
|
|
||
| if (result != null && result.isNotEmpty) { | ||
| final barcode = Barcode(result); | ||
| return barcode.isValid() ? barcode : null; | ||
| } | ||
| return null; | ||
| } catch (e) { | ||
| debugPrint('Error during barcode scanning: $e'); | ||
| return null; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| part of '../viewmodel/scan_viewmodel.dart'; | ||
|
|
||
| class _BarcodeScanView extends StatelessWidget { | ||
| final Function() onScanButtonPressed; | ||
| const _BarcodeScanView({ | ||
| required this.onScanButtonPressed, | ||
| }); | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| return Center( | ||
| child: Column( | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| children: <Widget>[ | ||
| Row( | ||
| mainAxisAlignment: MainAxisAlignment.center, | ||
| children: <Widget>[ | ||
| ElevatedButton( | ||
| onPressed: onScanButtonPressed, | ||
| child: Text('바코드 스캔', | ||
| style: Theme.of(context).textTheme.headlineMedium), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,102 @@ | ||||||||||||||||||||||||||
| import 'package:flutter/foundation.dart'; | ||||||||||||||||||||||||||
| import 'package:flutter/material.dart'; | ||||||||||||||||||||||||||
| import 'package:nutrient_scanner/features/barcode_scan/model/barcode_model.dart'; | ||||||||||||||||||||||||||
| import 'package:nutrient_scanner/features/barcode_scan/service/barcode_cache_service.dart'; | ||||||||||||||||||||||||||
| import 'package:nutrient_scanner/features/nutrient_intake_guide/viewmodel/guide_viewmodel.dart'; | ||||||||||||||||||||||||||
| import 'package:nutrient_scanner/util/error_util.dart'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| import '../../nutrient_scan/model/recognized_text_model.dart'; | ||||||||||||||||||||||||||
| import '../../nutrient_scan/viewmodel/scan_viewmodel.dart'; | ||||||||||||||||||||||||||
| import '../service/barcode_scan_service.dart'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| part '../view/scan_view.dart'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class BarcodeScanViewModel extends StatefulWidget { | ||||||||||||||||||||||||||
| const BarcodeScanViewModel({super.key}); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||
| State<BarcodeScanViewModel> createState() => _BarcodeScanState(); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| class _BarcodeScanState extends State<BarcodeScanViewModel> { | ||||||||||||||||||||||||||
| final BarcodeScanService _scanService = BarcodeScanService(); | ||||||||||||||||||||||||||
| final BarcodeCacheService _cacheService = BarcodeCacheService(); | ||||||||||||||||||||||||||
| String? scannedBarcode; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| @override | ||||||||||||||||||||||||||
| Widget build(BuildContext context) { | ||||||||||||||||||||||||||
| return Scaffold( | ||||||||||||||||||||||||||
| appBar: AppBar( | ||||||||||||||||||||||||||
| title: const Text('Barcode Label Scan'), | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| body: _BarcodeScanView( | ||||||||||||||||||||||||||
| onScanButtonPressed: () => startBarcodeScan(context), | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Future<void> startBarcodeScan(BuildContext context) async { | ||||||||||||||||||||||||||
| final barcode = await _scanService.scanBarcode(context); | ||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| if (barcode != null) { | ||||||||||||||||||||||||||
| _updateScannedBarcode(barcode.value); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||
| _handleError(e); | ||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||
| ///디버그 모드일 때에는 촬영이 불가능하여 임의의 바코드 값을 설정한다. | ||||||||||||||||||||||||||
| if (kDebugMode) { | ||||||||||||||||||||||||||
| _updateScannedBarcode('1234567890123'); | ||||||||||||||||||||||||||
| if (context.mounted) { | ||||||||||||||||||||||||||
| await _checkCachedData(context, scannedBarcode); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+38
to
+51
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. 현재
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. startBarcodeScan() : Refactoring
Collaborator
Author
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. 메소드를 더 잘게 나누어 수정했습니다! |
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Future<void> _checkCachedData(BuildContext context, String? barcode) async { | ||||||||||||||||||||||||||
| final cachedData = await _cacheService.loadFromCache(barcode ?? ''); | ||||||||||||||||||||||||||
| if (context.mounted) { | ||||||||||||||||||||||||||
| if (cachedData != null) { | ||||||||||||||||||||||||||
| _navigateToIntakeGuide(context, cachedData); | ||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||
| _navigateToNutrientScan(context); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
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.
Collaborator
Author
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. 수정했습니다!
Comment on lines
+66
to
+68
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.
Suggested change
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _updateScannedBarcode(String barcode) { | ||||||||||||||||||||||||||
| setState(() { | ||||||||||||||||||||||||||
| scannedBarcode = barcode; | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _navigateToIntakeGuide(BuildContext context, String cachedData) { | ||||||||||||||||||||||||||
| Navigator.push( | ||||||||||||||||||||||||||
| context, | ||||||||||||||||||||||||||
| MaterialPageRoute( | ||||||||||||||||||||||||||
| builder: (context) => NutrientIntakeGuideViewModel( | ||||||||||||||||||||||||||
| recognizedText: NutrientRecognizedText(cachedData), | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _navigateToNutrientScan(BuildContext context) { | ||||||||||||||||||||||||||
| Navigator.push( | ||||||||||||||||||||||||||
| context, | ||||||||||||||||||||||||||
| MaterialPageRoute( | ||||||||||||||||||||||||||
| builder: (context) => NutrientLabelScanViewModel( | ||||||||||||||||||||||||||
| barcode: Barcode(scannedBarcode ?? ''), | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| ), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| void _handleError(Object error) { | ||||||||||||||||||||||||||
| final errorMessage = ErrorUtil.formatErrorMessage(error); | ||||||||||||||||||||||||||
| ScaffoldMessenger.of(context).showSnackBar( | ||||||||||||||||||||||||||
| SnackBar(content: Text(errorMessage)), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,25 @@ | ||||||||||||
| import '../../../util/cache_manager.dart'; | ||||||||||||
| import '../model/analysis_result.dart'; | ||||||||||||
|
|
||||||||||||
| class AnalysisResultViewModel { | ||||||||||||
| final CacheManager _cacheManager = CacheManager(); | ||||||||||||
|
|
||||||||||||
| Future<void> saveToCache(String barcode, AnalysisResult result) async { | ||||||||||||
| final cacheKey = _getCacheKey(barcode); | ||||||||||||
| await _cacheManager.saveToCache(cacheKey, result.answer); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Future<AnalysisResult?> loadFromCache(String barcode) async { | ||||||||||||
| final cacheKey = _getCacheKey(barcode); | ||||||||||||
| final cachedAnswer = await _cacheManager.loadFromCache(cacheKey); | ||||||||||||
|
|
||||||||||||
| if (cachedAnswer != null) { | ||||||||||||
| return AnalysisResult(cachedAnswer); | ||||||||||||
| } | ||||||||||||
| return null; | ||||||||||||
|
Comment on lines
+16
to
+19
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.
Suggested change
|
||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| String _getCacheKey(String barcode) { | ||||||||||||
| return barcode; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
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.
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.
수정했습니다!