-
Notifications
You must be signed in to change notification settings - Fork 0
/
onboarding.dart
132 lines (112 loc) · 4.25 KB
/
onboarding.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../bloc/app_bloc.dart';
import '../data/settings.dart';
import '../strings_context.dart';
import 'screens/onboarding_accept_document_screen.dart';
import 'screens/onboarding_finished_screen.dart';
import 'screens/onboarding_select_signing_certificate_screen.dart';
/// Helper for Onboarding flow.
///
/// Reads [Settings].
///
/// Onboarding flow:
/// 1. Accept Privacy Policy - [OnboardingAcceptDocumentScreen]
/// 2. Accept Terms of Service - [OnboardingAcceptDocumentScreen]
/// 3. Select Signing Certificate - [OnboardingSelectSigningCertificateScreen]
/// 4. Success - [OnboardingFinishedScreen]
abstract class Onboarding {
static final ValueNotifier<bool?> _onboardingRequired = ValueNotifier(null);
/// Indicates whether starting Onboarding is required.
static ValueListenable<bool?> get onboardingRequired => _onboardingRequired;
/// Refresh [onboardingRequired] value.
static void refreshOnboardingRequired(BuildContext context) {
final settings = context.read<Settings>();
bool flag;
if (settings.acceptedPrivacyPolicyVersion.value == null) {
flag = true;
} else if (settings.acceptedTermsOfServiceVersion.value == null) {
flag = true;
} else {
flag = false;
}
_onboardingRequired.value = flag;
}
/// Starts 1st Onboarding screen.
static Future<dynamic> startOnboarding(BuildContext context) {
final settings = context.read<Settings>();
final strings = context.strings;
final screen = OnboardingAcceptDocumentScreen(
title: strings.privacyPolicyTitle,
url: Uri.parse(strings.privacyPolicyUrl),
step: 1,
versionSetter: (version) async {
settings.acceptedPrivacyPolicyVersion.value = version;
},
onAccepted: _handlePrivacyPolicyAccepted,
);
return _navigateToScreen(context, screen);
}
static Future<dynamic> _navigateToScreen(
BuildContext context,
Widget screen, [
bool replace = false,
]) {
final route = MaterialPageRoute(
settings: RouteSettings(
name: screen.runtimeType.toString(),
),
builder: (_) => screen,
);
final navigator = Navigator.of(context);
return replace ? navigator.pushReplacement(route) : navigator.push(route);
}
static void _handlePrivacyPolicyAccepted(BuildContext context) {
final settings = context.read<Settings>();
final strings = context.strings;
final screen = OnboardingAcceptDocumentScreen(
title: strings.termsOfServiceTitle,
url: Uri.parse(strings.termsOfServiceUrl),
step: 2,
versionSetter: (version) async {
settings.acceptedTermsOfServiceVersion.value = version;
},
onAccepted: _handleTermsOfServiceAccepted,
);
_navigateToScreen(context, screen, true);
}
static void _handleTermsOfServiceAccepted(BuildContext context) {
final settings = context.read<Settings>();
final hasSigningCertificate = settings.signingCertificate.value != null;
final Widget screen;
if (!hasSigningCertificate) {
screen = const OnboardingSelectSigningCertificateScreen(
onCertificateSelected: _handleCertificateSelectionCompleted,
onSkipRequested: _handleCertificateSelectionCompleted,
);
} else {
screen = const OnboardingFinishedScreen(
onStartRequested: _handleStartRequested,
);
}
_navigateToScreen(context, screen, true);
}
static void _handleCertificateSelectionCompleted(BuildContext context) {
const screen = OnboardingFinishedScreen(
onStartRequested: _handleStartRequested,
);
_navigateToScreen(context, screen, true);
}
static void _handleStartRequested(BuildContext context) {
// TODO Make sure this is always in sync by listening to both values; use TransformValueListenable
refreshOnboardingRequired(context);
// Notify parent
context.read<AppBloc>().add(const RequestOpenFileEvent());
// Navigate to root
Navigator.of(context).popUntil((route) {
// Remove until MainScreen
return (route.settings.name == '/');
});
}
}