Files
MrbWebLibre/app/lib/features/onboarding/presentation/onboarding.dart
T

215 lines
8.5 KiB
Dart

/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:smooth_page_indicator/smooth_page_indicator.dart';
import 'package:weblibre/core/providers/router.dart';
import 'package:weblibre/features/geckoview/features/browser/domain/services/browser_addon.dart';
import 'package:weblibre/features/onboarding/domain/entities/onboarding_mode.dart';
import 'package:weblibre/features/onboarding/domain/providers.dart';
import 'package:weblibre/features/onboarding/presentation/pages/abstract/i_form_page.dart';
import 'package:weblibre/features/onboarding/presentation/pages/ai_configuration.dart';
import 'package:weblibre/features/onboarding/presentation/pages/default_search.dart';
import 'package:weblibre/features/onboarding/presentation/pages/doh_settings.dart';
import 'package:weblibre/features/onboarding/presentation/pages/permissions.dart';
import 'package:weblibre/features/onboarding/presentation/pages/privacy_hardening.dart';
import 'package:weblibre/features/onboarding/presentation/pages/toolbar_layout.dart';
import 'package:weblibre/features/onboarding/presentation/pages/ublock_opt_in.dart';
import 'package:weblibre/features/onboarding/presentation/pages/welcome.dart';
import 'package:weblibre/features/user/domain/repositories/onboarding.dart';
class OnboardingScreen extends HookConsumerWidget {
final int currentRevision;
final int targetRevision;
const OnboardingScreen({
super.key,
required this.currentRevision,
required this.targetRevision,
});
@override
Widget build(BuildContext context, WidgetRef ref) {
final theme = Theme.of(context);
final disableAnimations = MediaQuery.disableAnimationsOf(context);
final pageController = usePageController();
final eulaAccepted = ref.watch(eulaAcceptedProvider);
final onboardingMode = ref.watch(onboardingModeProvider);
final isReturningUser = currentRevision == 2;
final showDetailedPages =
isReturningUser || onboardingMode == OnboardingMode.detailed;
final pages = useMemoized<List<Widget>>(() {
switch (currentRevision) {
case 1:
return [const AiConfigurationPage()];
default:
return [
WelcomePage(isReturningUser: isReturningUser),
const DefaultSearchPage(),
if (showDetailedPages) const DohSettingsPage(),
if (showDetailedPages) const ToolbarLayoutPage(),
const PrivacyHardeningPage(),
const AiConfigurationPage(),
if (showDetailedPages)
UBlockOptInPage(formKey: GlobalKey<FormState>()),
PermissionsPage(formKey: GlobalKey<FormState>()),
];
}
}, [currentRevision, targetRevision, onboardingMode]);
final lastPage = useRef(pageController.initialPage);
final currentPage = useState(pageController.initialPage);
return PopScope(
canPop: currentPage.value == 0,
onPopInvokedWithResult: (didPop, _) async {
if (didPop) return;
if (disableAnimations) {
pageController.jumpToPage(currentPage.value - 1);
} else {
await pageController.previousPage(
duration: const Duration(milliseconds: 250),
curve: Curves.easeInOut,
);
}
},
child: Scaffold(
body: SafeArea(
child: Column(
children: [
Expanded(
child: PageView(
controller: pageController,
physics: currentPage.value == 0 && !eulaAccepted
? const NeverScrollableScrollPhysics()
: null,
children: pages,
onPageChanged: (value) {
if (value > lastPage.value) {
if (pages[lastPage.value] case final IFormPage formPage) {
formPage.formKey.currentState?.save();
}
}
lastPage.value = value;
currentPage.value = value;
},
),
),
Row(
children: [
Expanded(
child: Visibility(
visible: currentPage.value > 0,
child: TextButton.icon(
onPressed: () async {
if (disableAnimations) {
pageController.jumpToPage(currentPage.value - 1);
} else {
await pageController.previousPage(
duration: const Duration(milliseconds: 250),
curve: Curves.easeInOut,
);
}
},
icon: const Icon(Icons.chevron_left),
label: const Text('Previous'),
),
),
),
Expanded(
child: Center(
child: SmoothPageIndicator(
controller: pageController,
count: pages.length,
effect: WormEffect(
activeDotColor: theme.colorScheme.primary,
dotHeight: 10.0,
dotWidth: 10.0,
),
),
),
),
if (currentPage.value < pages.length - 1)
Expanded(
child: TextButton.icon(
onPressed: currentPage.value == 0 && !eulaAccepted
? null
: () async {
if (disableAnimations) {
pageController.jumpToPage(
currentPage.value + 1,
);
} else {
await pageController.nextPage(
duration: const Duration(milliseconds: 250),
curve: Curves.easeInOut,
);
}
},
iconAlignment: IconAlignment.end,
icon: const Icon(Icons.chevron_right),
label: const Text('Next'),
),
)
else
Expanded(
child: TextButton.icon(
onPressed: () async {
if (pages[currentPage.value]
case final IFormPage formPage) {
formPage.formKey.currentState?.save();
}
if (onboardingMode == OnboardingMode.express) {
unawaited(
ref
.read(browserAddonServiceProvider.notifier)
.install('uBlock0@raymondhill.net'),
);
}
await ref
.read(onboardingRepositoryProvider.notifier)
.pushRevision(targetRevision);
ref.invalidate(routerProvider);
},
iconAlignment: IconAlignment.end,
icon: const Icon(Icons.done),
label: const Text('Done'),
),
),
],
),
],
),
),
),
);
}
}