-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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'; | ||
| } | ||
| } | ||
19 changes: 19 additions & 0 deletions
19
lib/features/barcode_scan/service/barcode_cache_service.dart
|
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.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> save(String barcode, String data) async { | ||
| final cacheKey = _getKey(barcode); | ||
| await _cacheManager.save(cacheKey, data); | ||
| } | ||
|
|
||
| Future<String?> load(String barcode) async { | ||
| final cacheKey = _getKey(barcode); | ||
| return await _cacheManager.load(cacheKey); | ||
| } | ||
|
|
||
| String _getKey(String barcode) { | ||
| return barcode; | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
lib/features/barcode_scan/service/barcode_scan_service.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
| ), | ||
| ], | ||
| ), | ||
| ], | ||
| ), | ||
| ); | ||
| } | ||
| } |
121 changes: 121 additions & 0 deletions
121
lib/features/barcode_scan/viewmodel/scan_viewmodel.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| 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 { | ||
| try { | ||
| final barcode = await _performBarcodeScan(context); | ||
| if (barcode != null && context.mounted) { | ||
| await _handleScannerBarcode(context, barcode.value); | ||
| } | ||
| } catch (e) { | ||
| _handleError(e); | ||
| } finally { | ||
| if (context.mounted) { | ||
| await _checkDebugModeCachedData(context); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Future<Barcode?> _performBarcodeScan(BuildContext context) async { | ||
| return await _scanService.scanBarcode(context); | ||
| } | ||
|
|
||
| Future<void> _handleScannerBarcode( | ||
| BuildContext context, String barcodeValue) async { | ||
| _updateScannedBarcode(barcodeValue); | ||
| if (context.mounted) { | ||
| await _checkCachedData(context, barcodeValue); | ||
| } | ||
| } | ||
|
|
||
| Future<void> _checkDebugModeCachedData(BuildContext context) async { | ||
| if (kDebugMode) { | ||
| _setDebugBarcodeTemp(); | ||
| } | ||
|
|
||
| await _checkCachedData(context, scannedBarcode ?? ''); | ||
| } | ||
|
|
||
| void _setDebugBarcodeTemp() { | ||
| _updateScannedBarcode('1234567890123'); | ||
| } | ||
|
|
||
| Future<void> _checkCachedData(BuildContext context, String? barcode) async { | ||
| final cachedData = await _cacheService.load(barcode ?? ''); | ||
| if (!context.mounted) return; | ||
| if (cachedData != null) { | ||
| _navigateToIntakeGuide(context, cachedData); | ||
| } else { | ||
| _navigateToNutrientScan(context); | ||
| } | ||
| } | ||
|
|
||
| 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)), | ||
| ); | ||
| } | ||
| } |
25 changes: 25 additions & 0 deletions
25
lib/features/nutrient_intake_guide/viewmodel/analysis_result_viewmodel.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.save(cacheKey, result.answer); | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| Future<AnalysisResult?> loadFromCache(String barcode) async { | ||||||||||||
| final cacheKey = _getCacheKey(barcode); | ||||||||||||
| final cachedAnswer = await _cacheManager.load(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; | ||||||||||||
| } | ||||||||||||
| } | ||||||||||||
2 changes: 1 addition & 1 deletion
2
lib/features/nutrient_intake_guide/viewmodel/guide_viewmodel.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...s/nutrient_scanner/view/scanner_view.dart → ...eatures/nutrient_scan/view/scan_view.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
수정했습니다!