diff --git a/packages/dart/sshnp_flutter/lib/src/controllers/terminal_session_controller.dart b/packages/dart/sshnp_flutter/lib/src/controllers/terminal_session_controller.dart index 5d21869f9..aa9307dda 100644 --- a/packages/dart/sshnp_flutter/lib/src/controllers/terminal_session_controller.dart +++ b/packages/dart/sshnp_flutter/lib/src/controllers/terminal_session_controller.dart @@ -185,6 +185,11 @@ class TerminalSessionFamilyController extends FamilyNotifier { } Future onPressed() async { + ref.read(navigationRailController.notifier).setRoute(AppRoute.terminal); if (mounted) { - showProgress('Starting Shell Session...'); - } - - /// Issue a new session id - final sessionId = ref.watch(terminalSessionController.notifier).createSession(); - - /// Create the session controller for the new session id - final sessionController = ref.watch(terminalSessionFamilyController(sessionId).notifier); - - try { - AtClient atClient = AtClientManager.getInstance().atClient; - - final profilePrivateKey = - await ProfilePrivateKeyManagerRepository.readProfilePrivateKeyManager(widget.params.profileName ?? ''); - final privateKeyManager = - await PrivateKeyManagerRepository.readPrivateKeyManager(profilePrivateKey.privateKeyNickname); - - final keyPair = privateKeyManager.toAtSshKeyPair(); - - final sshnp = Sshnp.dartPure( - params: SshnpParams.merge( - widget.params, - SshnpPartialParams( - verbose: kDebugMode, - idleTimeout: 30, - ), - ), - atClient: atClient, - identityKeyPair: keyPair, - ); - - final result = await sshnp.run(); - if (result is SshnpError) { - throw result; - } - - if (result is SshnpCommand) { - if (sshnp.canRunShell) { - if (mounted) { - context.pop(); - showProgress('running shell session...'); - } - log('running shell session...'); - - SshnpRemoteProcess shell = await sshnp.runShell(); - if (mounted) { - context.pop(); - showProgress('starting terminal session...'); - } - log('starting terminal session'); - sessionController.startSession( - shell, - terminalTitle: '${widget.params.sshnpdAtSign}-${widget.params.device}', - ); - } - - sessionController.issueDisplayName(widget.params.profileName!); - - ref.read(navigationRailController.notifier).setRoute(AppRoute.terminal); - if (mounted) { - context.pushReplacementNamed(AppRoute.terminal.name); - } - } - } catch (e) { - sessionController.dispose(); - if (mounted) { - log('error: ${e.toString()}'); - context.pop(); - CustomSnackBar.error(content: e.toString()); - } + context.pushReplacementNamed(AppRoute.terminal.name, extra: {'params': widget.params, 'runShell': true}); } } diff --git a/packages/dart/sshnp_flutter/lib/src/presentation/widgets/terminal_screen_widgets/terminal_screen_desktop_view.dart b/packages/dart/sshnp_flutter/lib/src/presentation/widgets/terminal_screen_widgets/terminal_screen_desktop_view.dart index 826695e97..76c2313ed 100644 --- a/packages/dart/sshnp_flutter/lib/src/presentation/widgets/terminal_screen_widgets/terminal_screen_desktop_view.dart +++ b/packages/dart/sshnp_flutter/lib/src/presentation/widgets/terminal_screen_widgets/terminal_screen_desktop_view.dart @@ -1,12 +1,22 @@ +import 'dart:developer'; + +import 'package:at_onboarding_flutter/at_onboarding_flutter.dart'; +import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/flutter_svg.dart'; +import 'package:go_router/go_router.dart'; +import 'package:noports_core/sshnp.dart'; import 'package:sshnp_flutter/src/controllers/terminal_session_controller.dart'; import 'package:sshnp_flutter/src/presentation/widgets/navigation/app_navigation_rail.dart'; import 'package:sshnp_flutter/src/utility/sizes.dart'; import 'package:xterm/xterm.dart'; +import '../../../repository/private_key_manager_repository.dart'; +import '../../../repository/profile_private_key_manager_repository.dart'; +import '../utility/custom_snack_bar.dart'; + // * Once the onboarding process is completed you will be taken to this screen class TerminalScreenDesktopView extends ConsumerStatefulWidget { const TerminalScreenDesktopView({Key? key}) : super(key: key); @@ -18,6 +28,79 @@ class TerminalScreenDesktopView extends ConsumerStatefulWidget { class _TerminalScreenDesktopViewState extends ConsumerState with TickerProviderStateMixin { final terminalController = TerminalController(); + @override + void initState() { + super.initState(); + + WidgetsBinding.instance.addPostFrameCallback( + (_) async { + final Map shellInfo = (GoRouterState.of(context).extra ?? {'runShell': false}) as Map; + if (shellInfo['runShell']) { + /// Issue a new session id + final sessionId = ref.watch(terminalSessionController.notifier).createSession(); + + /// Create the session controller for the new session id + final sessionController = ref.watch(terminalSessionFamilyController(sessionId).notifier); + + sessionController.issueDisplayName(shellInfo['params'].profileName!); + + sessionController.write('Starting Shell Session...'); + log('Starting Shell Session...'); + + try { + AtClient atClient = AtClientManager.getInstance().atClient; + + final profilePrivateKey = await ProfilePrivateKeyManagerRepository.readProfilePrivateKeyManager( + shellInfo['params'].profileName ?? ''); + final privateKeyManager = + await PrivateKeyManagerRepository.readPrivateKeyManager(profilePrivateKey.privateKeyNickname); + + final keyPair = privateKeyManager.toAtSshKeyPair(); + + final sshnp = Sshnp.dartPure( + params: SshnpParams.merge( + shellInfo['params'], + SshnpPartialParams( + verbose: kDebugMode, + idleTimeout: 30, + ), + ), + atClient: atClient, + identityKeyPair: keyPair, + ); + + final result = await sshnp.run(); + if (result is SshnpError) { + throw result; + } + + if (result is SshnpCommand) { + if (sshnp.canRunShell) { + sessionController.write('running shell session...'); + log('running shell session...'); + + SshnpRemoteProcess shell = await sshnp.runShell(); + sessionController.write('starting terminal session...'); + log('starting terminal session'); + sessionController.startSession( + shell, + terminalTitle: '${shellInfo['sshnpdAtSign']}-${shellInfo['params'].device}', + ); + } + } + } catch (e) { + sessionController.dispose(); + if (mounted) { + log('error: ${e.toString()}'); + + CustomSnackBar.error(content: e.toString()); + } + } + } + }, + ); + } + @override void dispose() { terminalController.dispose();