Skip to content

Commit

Permalink
feat: Tutorial after sign up
Browse files Browse the repository at this point in the history
  • Loading branch information
TomBursch committed Nov 23, 2024
1 parent cde6739 commit d64b8ae
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
18 changes: 16 additions & 2 deletions kitchenowl/lib/cubits/auth_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -140,19 +140,28 @@ class AuthCubit extends Cubit<AuthState> {
}
}

void onboard({
Future<void> onboard({
required String username,
required String name,
required String password,
Function()? wrongCredentialsCallback,
Function()? correctCredentialsCallback,
}) async {
emit(const Loading());
if (await ApiService.getInstance().isOnboarding()) {
final token =
await ApiService.getInstance().onboarding(username, name, password);
if (token != null && ApiService.getInstance().isAuthenticated()) {
await SecureStorage.getInstance().write(key: 'TOKEN', value: token);
await this.stream.any((s) => s is Authenticated);
if (correctCredentialsCallback != null) {
correctCredentialsCallback();
}
} else {
updateState();
await updateState();
if (wrongCredentialsCallback != null) {
wrongCredentialsCallback();
}
}
}
}
Expand All @@ -163,6 +172,7 @@ class AuthCubit extends Cubit<AuthState> {
required String password,
required String email,
Function(String?)? wrongCredentialsCallback,
Function()? correctCredentialsCallback,
}) async {
emit(const Loading());
final (token, msg) = await ApiService.getInstance().signup(
Expand All @@ -173,6 +183,10 @@ class AuthCubit extends Cubit<AuthState> {
);
if (token != null && ApiService.getInstance().isAuthenticated()) {
await SecureStorage.getInstance().write(key: 'TOKEN', value: token);
await this.stream.any((s) => s is Authenticated);
if (correctCredentialsCallback != null) {
correctCredentialsCallback();
}
} else {
await updateState();
if (ApiService.getInstance().connectionStatus == Connection.connected &&
Expand Down
8 changes: 5 additions & 3 deletions kitchenowl/lib/pages/onboarding_page.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:go_router/go_router.dart';
import 'package:kitchenowl/cubits/auth_cubit.dart';
import 'package:kitchenowl/kitchenowl.dart';
import 'package:kitchenowl/widgets/create_user_form_fields.dart';
Expand Down Expand Up @@ -44,7 +45,7 @@ class _OnboardingPageState extends State<OnboardingPage> {
),
Padding(
padding: const EdgeInsets.only(top: 16, bottom: 8),
child: ElevatedButton(
child: LoadingElevatedButton(
onPressed: () => _submit(context),
child: Text(AppLocalizations.of(context)!.start),
),
Expand All @@ -67,12 +68,13 @@ class _OnboardingPageState extends State<OnboardingPage> {
);
}

void _submit(BuildContext context) {
Future<void> _submit(BuildContext context) async {
if (_formKey.currentState!.validate()) {
BlocProvider.of<AuthCubit>(context).onboard(
await BlocProvider.of<AuthCubit>(context).onboard(
username: usernameController.text,
name: nameController.text,
password: passwordController.text,
correctCredentialsCallback: () => context.push("/tutorial"),
);
}
}
Expand Down
2 changes: 2 additions & 0 deletions kitchenowl/lib/pages/signup_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ class _SignupPageState extends State<SignupPage> {
Future.delayed(
Duration(milliseconds: 1), () => router.go("/register"));
},
correctCredentialsCallback: () => Future.delayed(
Duration(milliseconds: 1), () => router.push("/tutorial")),
);
}
}
Expand Down
27 changes: 27 additions & 0 deletions kitchenowl/lib/pages/tutorial_page.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import 'package:flutter/material.dart';
import 'package:kitchenowl/kitchenowl.dart';

class TutorialPage extends StatelessWidget {
const TutorialPage({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Align(
alignment: Alignment.topCenter,
child: ConstrainedBox(
constraints: const BoxConstraints.expand(width: 600),
child: Padding(
padding: const EdgeInsets.all(16),
child: Text(
"Tutorial",
style: Theme.of(context).textTheme.headlineLarge,
),
),
),
),
),
);
}
}
10 changes: 10 additions & 0 deletions kitchenowl/lib/router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import 'package:kitchenowl/pages/settings_user_page.dart';
import 'package:kitchenowl/pages/setup_page.dart';
import 'package:kitchenowl/pages/signup_page.dart';
import 'package:kitchenowl/pages/splash_page.dart';
import 'package:kitchenowl/pages/tutorial_page.dart';
import 'package:kitchenowl/pages/unreachable_page.dart';
import 'package:kitchenowl/pages/household_page.dart';
import 'package:kitchenowl/pages/unsupported_page.dart';
Expand Down Expand Up @@ -184,6 +185,15 @@ final router = GoRouter(
return (authState is! Unreachable) ? "/" : null;
},
),
GoRoute(
path: '/tutorial',
pageBuilder: (context, state) => SharedAxisTransitionPage(
key: state.pageKey,
name: state.name,
transitionType: SharedAxisTransitionType.scaled,
child: const TutorialPage(),
),
),
GoRoute(
path: "/household",
pageBuilder: (context, state) => SharedAxisTransitionPage(
Expand Down

0 comments on commit d64b8ae

Please sign in to comment.