Skip to content
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
62 changes: 40 additions & 22 deletions lib/ui/views/ocr/ocr_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ class _Body extends StackedHookView<OcrViewModel> {
image: viewModel.image!,
onImageProcessed: viewModel.handleImageProcessed,
onImageError: viewModel.handleImageError,
onTextSelected: (text) {
controller.text = controller.text + text;
viewModel.handleTextSelected();
onTextBlockSelected: (textBlock) {
if (controller.text.isEmpty) {
controller.text = textBlock.text;
} else {
controller.text =
"${controller.text}\n${textBlock.text}";
}
viewModel.appendToHistory(
controller.text, textBlock);
},
locked: false,
singleSelection: false,
Expand Down Expand Up @@ -144,30 +150,42 @@ class _SelectedText extends ViewModelWidget<OcrViewModel> {
),
maxLength: 1000,
inputFormatters: [LengthLimitingTextInputFormatter(1000)],
onChanged: viewModel.appendToHistory,
),
),
),
),
AnimatedCrossFade(
crossFadeState: controller.text.isEmpty
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
duration: const Duration(milliseconds: 200),
firstChild: SizedBox.shrink(),
secondChild: Container(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).padding.bottom,
),
width: double.infinity,
color: Colors.deepPurple,
child: TextButton.icon(
icon: const Icon(Icons.text_snippet, color: Colors.white),
label: const Text(
'Analyze',
style: TextStyle(color: Colors.white),
Container(
padding: EdgeInsets.only(
bottom: MediaQuery.of(context).padding.bottom,
),
width: double.infinity,
color: Colors.deepPurple,
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: [
IconButton(
onPressed: viewModel.canUndo
? () => controller.text = viewModel.undo()
: null,
icon: const Icon(Icons.undo, color: Colors.white),
splashColor: Colors.transparent,
),
onPressed: () => viewModel.analyzeText(controller.text),
),
IconButton(
onPressed: viewModel.canRedo
? () => controller.text = viewModel.redo()
: null,
icon: const Icon(Icons.redo, color: Colors.white),
splashColor: Colors.transparent,
),
TextButton(
child: const Text(
'Analyze',
style: TextStyle(color: Colors.white),
),
onPressed: () => viewModel.analyzeText(controller.text),
),
],
),
),
],
Expand Down
50 changes: 46 additions & 4 deletions lib/ui/views/ocr/ocr_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:image_picker/image_picker.dart';
import 'package:sagase/app/app.locator.dart';
import 'package:sagase/datamodels/recognized_text_block.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';

Expand All @@ -15,6 +16,11 @@ class OcrViewModel extends BaseViewModel {
XFile? _image;
XFile? get image => _image;

late List<(String, RecognizedTextBlock?)> _history;
late int _historyIndex;
bool get canUndo => _historyIndex >= 0;
bool get canRedo => _historyIndex < _history.length - 1;

OcrViewModel(bool cameraStart) {
if (cameraStart) {
openCamera();
Expand All @@ -32,6 +38,8 @@ class OcrViewModel extends BaseViewModel {
}

Future<void> _processImage(ImageSource imageSource) async {
_history = [];
_historyIndex = -1;
_image = null;
_state = OcrState.waiting;
rebuildUi();
Expand Down Expand Up @@ -69,15 +77,49 @@ class OcrViewModel extends BaseViewModel {
rebuildUi();
}

void handleTextSelected() {
rebuildUi();
}

void analyzeText(String text) {
if (text.isEmpty) return;

_navigationService.back(result: text);
}

void appendToHistory(String text, [RecognizedTextBlock? block]) {
_historyIndex++;

if (_historyIndex != _history.length) {
_history = _history.sublist(0, _historyIndex);
}

_history.add((text, block));

rebuildUi();
}

String undo() {
if (!canUndo) return '';

_history[_historyIndex].$2?.selected = false;

_historyIndex--;
rebuildUi();

if (_historyIndex == -1) {
return '';
} else {
return _history[_historyIndex].$1;
}
}

String redo() {
if (!canRedo) return '';

_historyIndex++;
_history[_historyIndex].$2?.selected = true;

rebuildUi();

return _history[_historyIndex].$1;
}
}

enum OcrState {
Expand Down
9 changes: 6 additions & 3 deletions lib/ui/widgets/ocr_image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ class OcrImage extends StatefulWidget {
final XFile image;
final void Function(int)? onImageProcessed;
final void Function() onImageError;
final void Function(String) onTextSelected;
final void Function(String)? onTextSelected;
final void Function(RecognizedTextBlock)? onTextBlockSelected;
final bool locked;
final bool singleSelection;

Expand All @@ -27,7 +28,8 @@ class OcrImage extends StatefulWidget {
this.onImageProcessed,
required this.onImageError,
required this.locked,
required this.onTextSelected,
this.onTextSelected,
this.onTextBlockSelected,
required this.singleSelection,
});

Expand Down Expand Up @@ -108,7 +110,8 @@ class _OcrImageState extends State<OcrImage> {
void handleSelect(RecognizedTextBlock textBlock) {
setState(() {
if (!textBlock.selected) {
widget.onTextSelected(textBlock.text);
widget.onTextSelected?.call(textBlock.text);
widget.onTextBlockSelected?.call(textBlock);
}

if (widget.singleSelection) {
Expand Down