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
2 changes: 1 addition & 1 deletion integration_test/pdf_provider_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class DummyOcrService implements OCRProvider {
@override
Future<OCRResult> process(String pdfPath) async {
return const OCRResult(
metadata: Metadata(title: 'test', author: 'test', pages: 0),
metadata: Metadata(title: 'test', pages: 0),
pages: [],
figures: [],
);
Expand Down
15 changes: 2 additions & 13 deletions lib/features/pdf_viewer/screens/pdf_viewer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class _PDFViewerState extends State<PDFViewer> {
if (layout.type != LayoutType.figure) return;
final pageNumber = _viewModel!.getPageNumberForFigure(layout);
if (pageNumber == null) return;
_pdfController.goToPage(pageNumber: pageNumber);
_pdfController.goToPage(pageNumber: pageNumber + 1);
}

void _showFigurePopover(BaseLayout reference, Offset tapPosition) {
Expand Down Expand Up @@ -167,18 +167,7 @@ class _PDFViewerState extends State<PDFViewer> {
listenable: _viewModel!,
builder: (context, child) {
return Scaffold(
appBar: AppBar(
title: Text(_viewModel!.pdfTitle),
actions: [
IconButton(
onPressed: () {
Navigator.of(context).pushNamed('/ai-settings');
},
icon: const Icon(Icons.smart_toy),
tooltip: 'AI 설정',
),
],
),
appBar: AppBar(title: Text(_viewModel!.pdfTitle)),
body: Row(
children: [
Expanded(
Expand Down
3 changes: 2 additions & 1 deletion lib/features/pdf_viewer/widgets/sidebar/figure_sidebar.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:snapfig/shared/services/pdf_core/pdf_core.dart';
import 'package:gpt_markdown/gpt_markdown.dart';

class FigureSidebar extends StatelessWidget {
final List<BaseLayout> figures;
Expand All @@ -18,7 +19,7 @@ class FigureSidebar extends StatelessWidget {
itemBuilder: (context, index) {
return ListTile(
leading: _buildThumbnail(figures[index], context: context),
title: Text(figures[index].figureId ?? ''),
title: GptMarkdown(figures[index].text ?? '', maxLines: 2),
trailing: const Icon(Icons.chevron_right),
onTap: () => onFigureSelected(figures[index]),
);
Expand Down
10 changes: 7 additions & 3 deletions lib/features/pdf_viewer/widgets/sidebar/pdf_sidebar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ class _PDFSideBarState extends State<PDFSideBar> {
children: [
SegmentedButton<_PDFSideBarTab>(
showSelectedIcon: true,
segments: const [
ButtonSegment(value: _PDFSideBarTab.page, label: Text('Outline')),
ButtonSegment(
segments: [
if (widget.viewModel.outlines.isNotEmpty)
const ButtonSegment(
value: _PDFSideBarTab.page,
label: Text('Outline'),
),
const ButtonSegment(
value: _PDFSideBarTab.figure,
label: Text('Figure'),
),
Expand Down
172 changes: 117 additions & 55 deletions lib/features/pdf_viewer/widgets/simple_draggable_ai_chat.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:snapfig/shared/services/ai_service/ai_service.dart';
import 'package:snapfig/shared/services/ai_service/models/ai_provider.dart';
import 'package:snapfig/shared/services/pdf_core/models/models.dart';
import 'package:snapfig/features/pdf_viewer/models/pdf_data_viewmodel.dart';
import 'package:gpt_markdown/gpt_markdown.dart';

class Message {
final String content;
Expand Down Expand Up @@ -49,9 +50,17 @@ class _SimpleDraggableAIChatState extends State<SimpleDraggableAIChat>
late AnimationController _animationController;
late Animation<double> _scaleAnimation;
bool _isDragging = false;
bool _isResizing = false;

static const double _chatWidth = 320.0;
static const double _chatHeight = 400.0;
// Size variables
double _chatWidth = 400.0;
double _chatHeight = 500.0;

// Size constraints
static const double _minWidth = 300.0;
static const double _maxWidth = 600.0;
static const double _minHeight = 400.0;
static const double _maxHeight = 800.0;

@override
void initState() {
Expand Down Expand Up @@ -203,36 +212,109 @@ class _SimpleDraggableAIChatState extends State<SimpleDraggableAIChat>
_position = _snapToEdges(_position, screenSize);
});
},
child: Container(
width: _chatWidth,
height: _chatHeight,
decoration: BoxDecoration(
color: theme.colorScheme.surface,
borderRadius: BorderRadius.circular(16),
border: Border.all(
color:
_isDragging
? theme.colorScheme.primary.withValues(alpha: 0.5)
: theme.colorScheme.outline.withValues(alpha: 0.2),
width: _isDragging ? 2 : 1,
child: Stack(
children: [
Container(
width: _chatWidth,
height: _chatHeight,
decoration: BoxDecoration(
color: theme.colorScheme.surface,
borderRadius: BorderRadius.circular(16),
border: Border.all(
color:
_isDragging || _isResizing
? theme.colorScheme.primary.withValues(alpha: 0.5)
: theme.colorScheme.outline.withValues(
alpha: 0.2,
),
width: _isDragging || _isResizing ? 2 : 1,
),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(
alpha: _isDragging || _isResizing ? 0.3 : 0.15,
),
blurRadius: _isDragging || _isResizing ? 20 : 10,
offset: Offset(0, _isDragging || _isResizing ? 8 : 4),
),
],
),
child: Column(
children: [
_buildDraggableHeader(context),
Expanded(child: _buildMessageList(context)),
_buildInputSection(context),
],
),
),
boxShadow: [
BoxShadow(
color: Colors.black.withValues(
alpha: _isDragging ? 0.3 : 0.15,
// Resize handle
Positioned(
right: 0,
bottom: 0,
child: GestureDetector(
onPanStart: (details) {
setState(() {
_isResizing = true;
});
},
onPanUpdate: (details) {
setState(() {
// Update size based on drag delta
_chatWidth = (_chatWidth + details.delta.dx).clamp(
_minWidth,
_maxWidth,
);
_chatHeight = (_chatHeight + details.delta.dy).clamp(
_minHeight,
_maxHeight,
);

// Ensure window stays within screen bounds after resizing
final screenSize = MediaQuery.of(context).size;
if (_position.dx + _chatWidth > screenSize.width) {
_position = Offset(
screenSize.width - _chatWidth,
_position.dy,
);
}
if (_position.dy + _chatHeight > screenSize.height) {
_position = Offset(
_position.dx,
screenSize.height - _chatHeight,
);
}
});
},
onPanEnd: (details) {
setState(() {
_isResizing = false;
});
},
child: Container(
width: 20,
height: 20,
decoration: BoxDecoration(
color:
_isResizing
? theme.colorScheme.primary
: theme.colorScheme.surfaceVariant,
borderRadius: const BorderRadius.only(
topLeft: Radius.circular(4),
bottomRight: Radius.circular(16),
),
),
child: Icon(
Icons.drag_handle,
size: 12,
color:
_isResizing
? theme.colorScheme.onPrimary
: theme.colorScheme.onSurfaceVariant,
),
),
blurRadius: _isDragging ? 20 : 10,
offset: Offset(0, _isDragging ? 8 : 4),
),
],
),
child: Column(
children: [
_buildDraggableHeader(context),
Expanded(child: _buildMessageList(context)),
_buildInputSection(context),
],
),
),
],
),
),
),
Expand All @@ -245,7 +327,7 @@ class _SimpleDraggableAIChatState extends State<SimpleDraggableAIChat>
final configurations = AIService.instance.configurations;

return Container(
padding: const EdgeInsets.all(16),
padding: const EdgeInsets.symmetric(horizontal: 12, vertical: 8),
decoration: BoxDecoration(
color:
_isDragging
Expand All @@ -270,17 +352,8 @@ class _SimpleDraggableAIChatState extends State<SimpleDraggableAIChat>
),
Row(
children: [
Icon(
Icons.smart_toy,
color:
_isDragging
? theme.colorScheme.onPrimaryContainer
: theme.colorScheme.primary,
size: 20,
),
const SizedBox(width: 8),
Text(
'AI Assistant',
'Assistant',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
color:
Expand Down Expand Up @@ -405,18 +478,18 @@ class _SimpleDraggableAIChatState extends State<SimpleDraggableAIChat>
Text(
'Thinking...',
style: theme.textTheme.bodyMedium?.copyWith(
color: theme.colorScheme.onSurfaceVariant,
color: theme.colorScheme.onSecondaryContainer,
),
),
],
)
: Text(
: GptMarkdown(
message.content,
style: theme.textTheme.bodyMedium?.copyWith(
color:
message.isUser
? theme.colorScheme.onPrimary
: theme.colorScheme.onSurface,
: theme.colorScheme.onSecondaryContainer,
),
),
),
Expand Down Expand Up @@ -559,18 +632,7 @@ class _SimpleDraggableAIChatState extends State<SimpleDraggableAIChat>
),
Row(
children: [
Icon(
Icons.smart_toy,
color: theme.colorScheme.primary,
size: 20,
),
const SizedBox(width: 8),
Text(
'AI Assistant',
style: theme.textTheme.titleMedium?.copyWith(
fontWeight: FontWeight.w600,
),
),
Text('Assistant', style: theme.textTheme.titleMedium),
const Spacer(),
IconButton(
onPressed: _closeWithAnimation,
Expand Down
10 changes: 10 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

// lib/main.dart

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart';
import 'package:snapfig/features/home/screens/home_widget.dart';
import 'package:snapfig/features/settings/ai_settings_screen.dart';
Expand All @@ -14,6 +16,14 @@ import 'core/theme/theme.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
if (kDebugMode) {
Logger.root.level = Level.ALL;
Logger.root.onRecord.listen((record) {
debugPrint(
'(${record.time}) ${record.level.name} - ${record.loggerName}: ${record.message}',
);
});
}
await dotenv.load(fileName: '.env');
final dir = await getApplicationCacheDirectory();
final pdfProvider = await PDFProviderImpl.load(
Expand Down
13 changes: 6 additions & 7 deletions lib/shared/services/ocr_core/models/bounding_box.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,12 @@ class BBox {
return {'x': x, 'y': y, 'width': width, 'height': height};
}

factory BBox.fromJson(Map<String, dynamic> json) {
return BBox(
x: (json['x'] as num).toDouble(),
y: (json['y'] as num).toDouble(),
width: (json['width'] as num).toDouble(),
height: (json['height'] as num).toDouble(),
);
factory BBox.fromJson(List json) {
final x1 = (json[0] as num).toDouble();
final y1 = (json[1] as num).toDouble();
final x2 = (json[2] as num).toDouble();
final y2 = (json[3] as num).toDouble();
return BBox(x: x1, y: y1, width: x2 - x1, height: y2 - y1);
}

@override
Expand Down
14 changes: 4 additions & 10 deletions lib/shared/services/ocr_core/models/metadata.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
class Metadata {
final String title;
final String author;
final int pages;

const Metadata({
required this.title,
required this.author,
required this.pages,
});
const Metadata({required this.title, required this.pages});

factory Metadata.fromJson(Map<String, dynamic> json) {
return Metadata(
title: json['title'] as String,
author: json['author'] as String,
pages: (json['pages'] as num).toInt(),
title: json['filename'] as String,
pages: (json['total_pages'] as num).toInt(),
);
}

Map<String, dynamic> toJson() {
return {'title': title, 'author': author, 'pages': pages};
return {'title': title, 'pages': pages};
}
}
Loading
Loading