Skip to content

Commit

Permalink
feat: landscape domain page
Browse files Browse the repository at this point in the history
  • Loading branch information
matthew-hagemann committed Jan 23, 2025
1 parent d78296e commit 063a85e
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:ubuntu_bootstrap/l10n.dart';
import 'package:ubuntu_logger/ubuntu_logger.dart';
import 'package:ubuntu_provision/ubuntu_provision.dart';
import 'package:ubuntu_widgets/ubuntu_widgets.dart';
import 'package:ubuntu_wizard/ubuntu_wizard.dart';
import 'package:yaru/yaru.dart';

import 'autoinstall_landscape_model.dart';

final _log = Logger();

class LandscapeDomainPage extends ConsumerWidget with ProvisioningPage {
const LandscapeDomainPage({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final l10n = UbuntuBootstrapLocalizations.of(context);
final landscapeModel = ref.watch(landscapeDataModelProvider);
final hasActiveConnection = ref.watch(networkModelProvider).hasActiveConnection;

return HorizontalPage(
windowTitle: l10n.landscapePageTitle,
title: l10n.landscapeDomainHeader,
bottomBar: WizardBar(
leading: const BackWizardButton(),
trailing: [_NextButton()],
),
children: [
if (!hasActiveConnection) ...[
YaruInfoBox(
yaruInfoType: YaruInfoType.danger,
title: Text('Connect to the internet to continue'),
child: Text(
'Internet is needed to fetch the autoinstall file from Landscape',
),
),
const SizedBox(height: kWizardSpacing),
],
Text(l10n.landscapeDomainInstructions),
const SizedBox(height: kWizardSpacing),
TextFormField(
enabled: hasActiveConnection,
initialValue: landscapeModel.domainUrl,
onChanged: ref.read(landscapeDataModelProvider.notifier).setUrl,
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: (_) => landscapeModel.error != null ? 'Invalid domain,please check or contact your IT support' : null,
),
],
);
}
}

class _NextButton extends ConsumerWidget {
const _NextButton();

@override
Widget build(BuildContext context, WidgetRef ref) {
final landscapeModel = ref.watch(landscapeDataModelProvider);
final theme = Theme.of(context);
// final l10n = UbuntuBootstrapLocalizations.of(context);

return ElevatedButton(
style: theme.elevatedButtonTheme.style?.copyWith(
minimumSize: WidgetStateProperty.all(kPushButtonSize),
),
onPressed: landscapeModel.error == null &&
(landscapeModel.domainUrl != '')
? () async {
if (await ref
.read(landscapeDataModelProvider.notifier)
.attach()) {
if (context.mounted) await Wizard.of(context).next();
}
}
: null,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
if (landscapeModel.isLoading) ...[
SizedBox.square(
dimension: 16.0,
child: YaruCircularProgressIndicator(
color: Theme.of(context).colorScheme.onPrimary,
strokeWidth: 2,
),
),
const SizedBox(width: 8),
],
Text('Next'),
],
),
);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ class LandscapeData with _$LandscapeData {
@Default(AuthenticationStatus.authenticationPending)
AuthenticationStatus authenticationStatus,
@Default('') String autoinstall,
@Default('') String domainUrl,
@Default(false) bool isLoading,
Object? error,
}) = _LandscapeData;

LandscapeData._();
Expand All @@ -45,6 +48,11 @@ class LandscapeDataModel extends _$LandscapeDataModel {
return data;
}

void setUrl(String? url) {
if (url == null) return;
state = state.copyWith(domainUrl: url, error: null);
}

Future<void> _handleAuthenticationStatus(
AuthenticationStatus status,
String? autoinstall,
Expand All @@ -60,12 +68,21 @@ class LandscapeDataModel extends _$LandscapeDataModel {
}
}

Future<void> attach() async {
final response = await getService<LandscapeService>().attach();
Future<bool> attach() async {

state = state.copyWith(isLoading: true);
try {
final response = await getService<LandscapeService>().attach(state.domainUrl);
if (response.status == AttachStatus.attachSuccess &&
response.userCode != null) {
state = state.copyWith(userCode: response.userCode!);
}}
on Exception catch (e) {
_log.debug('Caught error during attach: $e');
state = state.copyWith(error: e, isLoading: false);
return false;
}
return true;
}

Future<void> watch() async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ mixin _$LandscapeData {
AuthenticationStatus get authenticationStatus =>
throw _privateConstructorUsedError;
String get autoinstall => throw _privateConstructorUsedError;
String get domainUrl => throw _privateConstructorUsedError;
bool get isLoading => throw _privateConstructorUsedError;
Object? get error => throw _privateConstructorUsedError;

/// Create a copy of LandscapeData
/// with the given fields replaced by the non-null parameter values.
Expand All @@ -37,7 +40,10 @@ abstract class $LandscapeDataCopyWith<$Res> {
$Res call(
{String userCode,
AuthenticationStatus authenticationStatus,
String autoinstall});
String autoinstall,
String domainUrl,
bool isLoading,
Object? error});
}

/// @nodoc
Expand All @@ -58,6 +64,9 @@ class _$LandscapeDataCopyWithImpl<$Res, $Val extends LandscapeData>
Object? userCode = null,
Object? authenticationStatus = null,
Object? autoinstall = null,
Object? domainUrl = null,
Object? isLoading = null,
Object? error = freezed,
}) {
return _then(_value.copyWith(
userCode: null == userCode
Expand All @@ -72,6 +81,15 @@ class _$LandscapeDataCopyWithImpl<$Res, $Val extends LandscapeData>
? _value.autoinstall
: autoinstall // ignore: cast_nullable_to_non_nullable
as String,
domainUrl: null == domainUrl
? _value.domainUrl
: domainUrl // ignore: cast_nullable_to_non_nullable
as String,
isLoading: null == isLoading
? _value.isLoading
: isLoading // ignore: cast_nullable_to_non_nullable
as bool,
error: freezed == error ? _value.error : error,
) as $Val);
}
}
Expand All @@ -87,7 +105,10 @@ abstract class _$$LandscapeDataImplCopyWith<$Res>
$Res call(
{String userCode,
AuthenticationStatus authenticationStatus,
String autoinstall});
String autoinstall,
String domainUrl,
bool isLoading,
Object? error});
}

/// @nodoc
Expand All @@ -106,6 +127,9 @@ class __$$LandscapeDataImplCopyWithImpl<$Res>
Object? userCode = null,
Object? authenticationStatus = null,
Object? autoinstall = null,
Object? domainUrl = null,
Object? isLoading = null,
Object? error = freezed,
}) {
return _then(_$LandscapeDataImpl(
userCode: null == userCode
Expand All @@ -120,6 +144,15 @@ class __$$LandscapeDataImplCopyWithImpl<$Res>
? _value.autoinstall
: autoinstall // ignore: cast_nullable_to_non_nullable
as String,
domainUrl: null == domainUrl
? _value.domainUrl
: domainUrl // ignore: cast_nullable_to_non_nullable
as String,
isLoading: null == isLoading
? _value.isLoading
: isLoading // ignore: cast_nullable_to_non_nullable
as bool,
error: freezed == error ? _value.error : error,
));
}
}
Expand All @@ -130,7 +163,10 @@ class _$LandscapeDataImpl extends _LandscapeData {
_$LandscapeDataImpl(
{this.userCode = '',
this.authenticationStatus = AuthenticationStatus.authenticationPending,
this.autoinstall = ''})
this.autoinstall = '',
this.domainUrl = '',
this.isLoading = false,
this.error})
: super._();

@override
Expand All @@ -142,10 +178,18 @@ class _$LandscapeDataImpl extends _LandscapeData {
@override
@JsonKey()
final String autoinstall;
@override
@JsonKey()
final String domainUrl;
@override
@JsonKey()
final bool isLoading;
@override
final Object? error;

@override
String toString() {
return 'LandscapeData(userCode: $userCode, authenticationStatus: $authenticationStatus, autoinstall: $autoinstall)';
return 'LandscapeData(userCode: $userCode, authenticationStatus: $authenticationStatus, autoinstall: $autoinstall, domainUrl: $domainUrl, isLoading: $isLoading, error: $error)';
}

@override
Expand All @@ -158,12 +202,23 @@ class _$LandscapeDataImpl extends _LandscapeData {
(identical(other.authenticationStatus, authenticationStatus) ||
other.authenticationStatus == authenticationStatus) &&
(identical(other.autoinstall, autoinstall) ||
other.autoinstall == autoinstall));
other.autoinstall == autoinstall) &&
(identical(other.domainUrl, domainUrl) ||
other.domainUrl == domainUrl) &&
(identical(other.isLoading, isLoading) ||
other.isLoading == isLoading) &&
const DeepCollectionEquality().equals(other.error, error));
}

@override
int get hashCode =>
Object.hash(runtimeType, userCode, authenticationStatus, autoinstall);
int get hashCode => Object.hash(
runtimeType,
userCode,
authenticationStatus,
autoinstall,
domainUrl,
isLoading,
const DeepCollectionEquality().hash(error));

/// Create a copy of LandscapeData
/// with the given fields replaced by the non-null parameter values.
Expand All @@ -178,7 +233,10 @@ abstract class _LandscapeData extends LandscapeData {
factory _LandscapeData(
{final String userCode,
final AuthenticationStatus authenticationStatus,
final String autoinstall}) = _$LandscapeDataImpl;
final String autoinstall,
final String domainUrl,
final bool isLoading,
final Object? error}) = _$LandscapeDataImpl;
_LandscapeData._() : super._();

@override
Expand All @@ -187,6 +245,12 @@ abstract class _LandscapeData extends LandscapeData {
AuthenticationStatus get authenticationStatus;
@override
String get autoinstall;
@override
String get domainUrl;
@override
bool get isLoading;
@override
Object? get error;

/// Create a copy of LandscapeData
/// with the given fields replaced by the non-null parameter values.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 063a85e

Please sign in to comment.