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
48 changes: 17 additions & 31 deletions lib/cubit/canvas_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,12 +150,11 @@ class CanvasCubit extends Cubit<CanvasState> {

if (savedPath != null) {
_updateState(backgroundImagePath: savedPath);
emit(
state.copyWith(message: 'Background image uploaded successfully!'));
CustomSnackbar.showSuccess('Background image uploaded successfully!');
}
} catch (e) {
log('Error uploading background image: $e');
emit(state.copyWith(message: 'Error uploading image: $e'));
CustomSnackbar.showError('Error uploading image: $e');
}
}

Expand All @@ -174,12 +173,11 @@ class CanvasCubit extends Cubit<CanvasState> {

if (savedPath != null) {
_updateState(backgroundImagePath: savedPath);
emit(
state.copyWith(message: 'Background photo captured successfully!'));
CustomSnackbar.showSuccess('Background photo captured successfully!');
}
} catch (e) {
log('Error taking photo for background: $e');
emit(state.copyWith(message: 'Error taking photo: $e'));
CustomSnackbar.showError('Error taking photo: $e');
}
}

Expand All @@ -191,7 +189,7 @@ class CanvasCubit extends Cubit<CanvasState> {
}

_updateState(clearBackgroundImage: true);
emit(state.copyWith(message: 'Background image removed'));
CustomSnackbar.showInfo('Background image removed');
}

// Helper method to save image to app directory
Expand Down Expand Up @@ -344,6 +342,7 @@ class CanvasCubit extends Cubit<CanvasState> {
clearBackgroundImage: true,
),
);
CustomSnackbar.showInfo('Canvas cleared');
}

void _updateState({
Expand Down Expand Up @@ -430,14 +429,12 @@ class CanvasCubit extends Cubit<CanvasState> {
// Set the current page name and emit success message
emit(state.copyWith(
currentPageName: pageName,
message: 'Page "$pageName" saved successfully!',
));
CustomSnackbar.showSuccess('Page "$pageName" saved successfully!');
} catch (e, stackTrace) {
log('❌ Error saving page: $e');
log('Stack trace: $stackTrace');
emit(state.copyWith(
message: 'Error saving page: $e',
));
CustomSnackbar.showError('Error saving page: $e');
}
}

Expand All @@ -463,7 +460,7 @@ class CanvasCubit extends Cubit<CanvasState> {

if (pageDataString == null) {
log('❌ Page not found: $pageName');
emit(state.copyWith(message: 'Page "$pageName" not found'));
CustomSnackbar.showError('Page "$pageName" not found');
return;
}

Expand Down Expand Up @@ -523,17 +520,15 @@ class CanvasCubit extends Cubit<CanvasState> {
backgroundColor: Color(pageData['backgroundColor']),
backgroundImagePath: validImagePath,
selectedTextItemIndex: null,
message: 'Page "$pageName" loaded successfully!',
history: [],
future: [],
currentPageName: pageName,
));
CustomSnackbar.showSuccess('Page "$pageName" loaded successfully!');
} catch (e, stackTrace) {
log('❌ Error loading page: $e');
log('Stack trace: $stackTrace');
emit(state.copyWith(
message: 'Error loading page: $e',
));
CustomSnackbar.showError('Error loading page: $e');
}
}

Expand All @@ -554,8 +549,8 @@ class CanvasCubit extends Cubit<CanvasState> {
isDrawingMode: false, // Exit drawing mode when creating new page
clearCurrentPageName: true,
clearBackgroundImage: true,
message: 'New page created',
));
CustomSnackbar.showInfo('New page created');
}

// Get list of saved pages
Expand Down Expand Up @@ -621,19 +616,13 @@ class CanvasCubit extends Cubit<CanvasState> {
if (state.currentPageName == pageName) {
emit(state.copyWith(
clearCurrentPageName: true,
message: 'Page "$pageName" deleted successfully!',
));
} else {
emit(state.copyWith(
message: 'Page "$pageName" deleted successfully!',
));
}
CustomSnackbar.showSuccess('Page "$pageName" deleted successfully!');
} catch (e, stackTrace) {
log('❌ Error deleting page: $e');
log('Stack trace: $stackTrace');
emit(state.copyWith(
message: 'Error deleting page: $e',
));
CustomSnackbar.showError('Error deleting page: $e');
}
}

Expand Down Expand Up @@ -673,11 +662,6 @@ class CanvasCubit extends Cubit<CanvasState> {
}
}

// Clear message (useful for dismissing notifications)
void clearMessage() {
emit(state.copyWith(message: null));
}

// Debug method to clear all saved data (now also cleans up image files)
Future<void> clearAllSavedData() async {
try {
Expand Down Expand Up @@ -717,12 +701,13 @@ class CanvasCubit extends Cubit<CanvasState> {
log('🧹 Cleared all saved data: $keysToRemove');

emit(state.copyWith(
message: 'All saved data cleared!',
clearCurrentPageName: true,
clearBackgroundImage: true,
));
CustomSnackbar.showSuccess('All saved data cleared!');
} catch (e) {
log('❌ Error clearing saved data: $e');
CustomSnackbar.showError('Error clearing saved data: $e');
}
}

Expand Down Expand Up @@ -900,6 +885,7 @@ class CanvasCubit extends Cubit<CanvasState> {
history: newHistory,
future: [], // Clear future as we've made a new action
));
CustomSnackbar.showInfo('Drawings cleared');
}

// Undo the last drawing stroke
Expand Down
7 changes: 0 additions & 7 deletions lib/cubit/canvas_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class CanvasState {
final String? backgroundImagePath;
final int? selectedTextItemIndex;
final bool isTrayShown;
final String? message;
final String? currentPageName;
final bool isDrawingMode;
final Color currentDrawColor;
Expand All @@ -28,7 +27,6 @@ class CanvasState {
this.backgroundImagePath,
this.selectedTextItemIndex,
this.isTrayShown = false,
this.message,
this.currentPageName,
this.isDrawingMode = false,
this.currentDrawColor = ColorConstants.dialogTextBlack,
Expand All @@ -46,7 +44,6 @@ class CanvasState {
backgroundImagePath: null,
selectedTextItemIndex: null,
isTrayShown: false,
message: null,
currentPageName: null,
isDrawingMode: false,
currentDrawColor: ColorConstants.dialogTextBlack,
Expand All @@ -70,7 +67,6 @@ class CanvasState {
Color? currentDrawColor,
double? currentStrokeWidth,
BrushType? currentBrushType,
String? message,
String? currentPageName,
bool clearCurrentPageName = false,
}) {
Expand All @@ -87,7 +83,6 @@ class CanvasState {
? null
: (selectedTextItemIndex ?? this.selectedTextItemIndex),
isTrayShown: deselect ? false : (isTrayShown ?? this.isTrayShown),
message: message ?? this.message,
currentPageName: clearCurrentPageName
? null
: (currentPageName ?? this.currentPageName),
Expand All @@ -112,7 +107,6 @@ class CanvasState {
history == other.history &&
future == other.future &&
isTrayShown == other.isTrayShown &&
message == other.message &&
currentPageName == other.currentPageName &&
currentDrawColor == other.currentDrawColor &&
currentStrokeWidth == other.currentStrokeWidth &&
Expand All @@ -132,6 +126,5 @@ class CanvasState {
currentDrawColor.hashCode ^
currentStrokeWidth.hashCode ^
currentBrushType.hashCode ^
message.hashCode ^
currentPageName.hashCode;
}
16 changes: 2 additions & 14 deletions lib/ui/screens/canvas_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,10 @@ class CanvasScreen extends StatelessWidget {
if (cubit.state.textItems.isNotEmpty ||
cubit.state.backgroundImagePath != null) {
cubit.clearCanvas();
CustomSnackbar.showInfo('Canvas cleared');
} else if (cubit.state.drawPaths.isNotEmpty) {
cubit.clearDrawings();
CustomSnackbar.showInfo('Drawings cleared');
} else {
// Show info when canvas is already empty
CustomSnackbar.showInfo('Canvas is already empty');
}
},
Expand All @@ -99,7 +98,6 @@ class CanvasScreen extends StatelessWidget {
final cubit = context.read<CanvasCubit>();
if (cubit.state.history.isNotEmpty) {
cubit.undo();
CustomSnackbar.showInfo('Action undone');
} else {
CustomSnackbar.showInfo('Nothing to undo');
}
Expand All @@ -113,7 +111,6 @@ class CanvasScreen extends StatelessWidget {
final cubit = context.read<CanvasCubit>();
if (cubit.state.future.isNotEmpty) {
cubit.redo();
CustomSnackbar.showInfo('Action redone');
} else {
CustomSnackbar.showInfo('Nothing to redo');
}
Expand All @@ -133,7 +130,6 @@ class CanvasScreen extends StatelessWidget {
switch (value) {
case 'new_page':
cubit.createNewPage();
CustomSnackbar.showInfo('New page created');
break;
case 'load_pages':
Navigator.push(
Expand Down Expand Up @@ -220,15 +216,7 @@ class CanvasScreen extends StatelessWidget {
),
],
),
body: BlocConsumer<CanvasCubit, CanvasState>(
listener: (context, state) {
// Show snackbar when there's a message from save/load operations
if (state.message != null) {
CustomSnackbar.showInfo(state.message!);
// Clear the message after showing
context.read<CanvasCubit>().clearMessage();
}
},
body: BlocBuilder<CanvasCubit, CanvasState>(
builder: (context, state) {
return GestureDetector(
onTap: () {
Expand Down
6 changes: 0 additions & 6 deletions lib/ui/screens/save_page_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:texterra/utils/custom_snackbar.dart';

import '../../cubit/canvas_cubit.dart';
import '../../constants/color_constants.dart';
Expand Down Expand Up @@ -99,17 +98,12 @@ class _SavePageDialogState extends State<SavePageDialog> {

if (mounted) {
Navigator.of(context).pop();

// Show success message
CustomSnackbar.showSuccess('Page "$pageName" saved successfully!');
}
} catch (e) {
if (mounted) {
setState(() {
_isSaving = false;
});

CustomSnackbar.showError('Error saving page: $e');
}
}
}
Expand Down
14 changes: 3 additions & 11 deletions lib/ui/screens/saved_pages.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class _SavedPagesScreenState extends State<SavedPagesScreen> {
} catch (e) {
// Handle error
if (mounted) {
CustomSnackbar.showError('Error deleting page');
CustomSnackbar.showError('Error loading pages');
}
} finally {
if (mounted) {
Expand Down Expand Up @@ -295,9 +295,7 @@ class _SavedPagesScreenState extends State<SavedPagesScreen> {
Navigator.of(context).pop();
}
} catch (e) {
if (mounted) {
CustomSnackbar.showError('Error deleting page');
}
// Error is already handled by cubit
}
}

Expand Down Expand Up @@ -358,14 +356,8 @@ class _SavedPagesScreenState extends State<SavedPagesScreen> {
try {
await context.read<CanvasCubit>().deletePage(pageName);
await _loadSavedPages(); // Refresh the list

if (mounted) {
CustomSnackbar.showSuccess('Page "$pageName" deleted successfully');
}
} catch (e) {
if (mounted) {
CustomSnackbar.showError('Error deleting page');
}
// Error is already handled by cubit
}
}
}
Loading