Skip to content

Commit

Permalink
PAINTROID-763: RedoStack not cleared for new project - extract back l…
Browse files Browse the repository at this point in the history
…ogic to top app bar
  • Loading branch information
Lenkomotive committed Oct 5, 2024
1 parent ae20a51 commit 70bf01a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 28 deletions.
36 changes: 36 additions & 0 deletions lib/ui/pages/workspace_page/components/top_bar/top_app_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:paintroid/core/commands/command_manager/command_manager.dart';
import 'package:paintroid/core/commands/command_manager/command_manager_provider.dart';
import 'package:paintroid/core/enums/tool_types.dart';
import 'package:paintroid/core/providers/object/io_handler.dart';
import 'package:paintroid/core/providers/state/app_bar_provider.dart';
import 'package:paintroid/core/providers/state/canvas_state_provider.dart';
import 'package:paintroid/core/providers/state/paint_provider.dart';
import 'package:paintroid/core/providers/state/toolbox_state_provider.dart';
import 'package:paintroid/core/providers/state/workspace_state_notifier.dart';
import 'package:paintroid/core/tools/line_tool/line_tool.dart';
import 'package:paintroid/core/tools/tool.dart';
import 'package:paintroid/core/utils/widget_identifier.dart';
import 'package:paintroid/ui/pages/workspace_page/components/top_bar/overflow_menu.dart';
import 'package:paintroid/ui/shared/action_button.dart';
import 'package:paintroid/ui/shared/dialogs/discard_changes_dialog.dart';
import 'package:paintroid/ui/utils/top_bar_action_data.dart';

class TopAppBar extends ConsumerWidget implements PreferredSizeWidget {
Expand All @@ -21,6 +25,31 @@ class TopAppBar extends ConsumerWidget implements PreferredSizeWidget {
@override
Size get preferredSize => const Size.fromHeight(kToolbarHeight);

Future<void Function()?> _onBack(BuildContext context, WidgetRef ref) async {
final isFullscreen = ref.watch(
workspaceStateProvider.select((state) => state.isFullscreen),
);
final ioHandler = ref.watch(IOHandler.provider);
final workspaceStateNotifier = ref.watch(workspaceStateProvider.notifier);

if (isFullscreen) {
workspaceStateNotifier.toggleFullscreen(false);
return null;
}
if (!workspaceStateNotifier.hasSavedLastWork) {
final shouldDiscard = await showDiscardChangesDialog(context);
if (shouldDiscard != null && !shouldDiscard && context.mounted) {
bool savedImage = await ioHandler.saveImage(context);
if (!savedImage) {
return null;
}
}
}
if (!context.mounted) return null;
Navigator.pop(context);
return null;
}

void Function()? _onUndo(
Tool currentTool,
CommandManager commandManager,
Expand Down Expand Up @@ -100,6 +129,13 @@ class TopAppBar extends ConsumerWidget implements PreferredSizeWidget {
ref.watch(appBarProvider);

return AppBar(
leading: IconButton(
key: const ValueKey(WidgetIdentifier.backButton),
icon: const Icon(Icons.arrow_back),
onPressed: () async {
await _onBack(context, ref);
},
),
title: Text(title),
centerTitle: false,
actions: [
Expand Down
29 changes: 1 addition & 28 deletions lib/ui/pages/workspace_page/workspace_page.dart
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:toast/toast.dart';

import 'package:paintroid/core/providers/object/io_handler.dart';
import 'package:paintroid/core/providers/state/workspace_state_notifier.dart';
import 'package:paintroid/ui/pages/workspace_page/components/bottom_bar/bottom_nav_bar.dart';
import 'package:paintroid/ui/pages/workspace_page/components/bottom_bar/tool_options/tool_options.dart';
import 'package:paintroid/ui/pages/workspace_page/components/drawing_surface/drawing_canvas.dart';
import 'package:paintroid/ui/pages/workspace_page/components/drawing_surface/exit_fullscreen_button.dart';
import 'package:paintroid/ui/pages/workspace_page/components/top_bar/top_app_bar.dart';
import 'package:paintroid/ui/shared/dialogs/discard_changes_dialog.dart';
import 'package:toast/toast.dart';

class WorkspacePage extends ConsumerStatefulWidget {
const WorkspacePage({super.key});
Expand Down Expand Up @@ -39,30 +35,7 @@ class _WorkspaceScreenState extends ConsumerState<WorkspacePage> {
workspaceStateProvider.select((state) => state.isFullscreen),
(_, isFullscreen) => _toggleStatusBar(isFullscreen),
);
final ioHandler = ref.watch(IOHandler.provider);
final workspaceStateNotifier = ref.watch(workspaceStateProvider.notifier);
return PopScope(
canPop: false,
onPopInvoked: (didPop) async {
if (didPop) {
return;
}
if (isFullscreen) {
workspaceStateNotifier.toggleFullscreen(false);
return;
}
if (!workspaceStateNotifier.hasSavedLastWork) {
final shouldDiscard = await showDiscardChangesDialog(context);
if (shouldDiscard != null && !shouldDiscard && context.mounted) {
bool savedImage = await ioHandler.saveImage(context);
if (!savedImage) {
return;
}
}
}
if (!context.mounted) return;
Navigator.pop(context);
},
child: Scaffold(
appBar: isFullscreen ? null : const TopAppBar(title: 'Pocket Paint'),
backgroundColor: Colors.grey.shade400,
Expand Down

0 comments on commit 70bf01a

Please sign in to comment.