Skip to content

Commit

Permalink
makes changes to user state provider and adds forgot-password page
Browse files Browse the repository at this point in the history
probably breaking changes for existing pages

anon users now will also be linked with new registered account if they make one
  • Loading branch information
K-Zawis committed Jul 3, 2022
1 parent 3b570f5 commit ae5fc19
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 153 deletions.
38 changes: 16 additions & 22 deletions lib/constants.dart
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,7 @@ final _availableDesktopPages = <String, WidgetBuilder>{
// Profile
final navigatorKey = GlobalKey<NavigatorState>();

enum Page {
screenQuestions,
screenAnalytics,
manageUsers,
language,
topic,
question,
level
}
enum Page { screenQuestions, screenAnalytics, manageUsers, language, topic, question, level }

final Map<Page, Widget> fragments = {
Page.screenQuestions: const MyQuestionsPage(),
Expand Down Expand Up @@ -165,18 +157,20 @@ final titleProvider = Provider<String>((ref) {
title = ref.watch(selectedPageNameProvider.state).state;
return title;
});
final firebaseAuthProvider =
Provider<FirebaseAuth>((_) => FirebaseAuth.instance);
final authRepositoryProvider =
Provider<AuthService>((_) => AuthService(_.read));
final userStateProvider = StateNotifierProvider<UserStateNotifier, dynamic>(
(_) => UserStateNotifier(_.read)..appInit());
final loginProvider =
ChangeNotifierProvider<LoginProvider>((_ref) => LoginProvider());
final languageProvider =
ChangeNotifierProvider<Languages>((ref) => Languages());
final qualificationProvider =
ChangeNotifierProvider<Qualifications>((ref) => Qualifications());

// * Auth Providers
final firebaseAuthProvider = Provider<FirebaseAuth>((_) => FirebaseAuth.instance);
final userStateProvider = StateNotifierProvider<UserStateNotifier, MyUserData?>(
(_) => UserStateNotifier(_.read)..appInit(),
);

// * App Providers
final languageProvider = ChangeNotifierProvider<Languages>((ref) => Languages());

// * Unsorted
final authRepositoryProvider = Provider<AuthService>((_) => AuthService(_.read));
final loginProvider = ChangeNotifierProvider<LoginProvider>((_ref) => LoginProvider());
final qualificationProvider = ChangeNotifierProvider<Qualifications>((ref) => Qualifications());
final topicProvider = ChangeNotifierProvider<Topics>((ref) {
var lan = ref.watch(languageProvider).getLanguage();
var level = ref.watch(qualificationProvider).getLevel();
Expand All @@ -201,7 +195,7 @@ final assessmentProvider = ChangeNotifierProvider<AssessmentProvider>((ref) {
final answerProvider = ChangeNotifierProvider.family((ref, bool custom) {
dynamic uid;
if (!custom) {
uid = ref.watch(userStateProvider).authData;
uid = ref.watch(userStateProvider)?.authData;
} else {
uid = ref.watch(usersProvider).currentUser;
}
Expand Down
108 changes: 73 additions & 35 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter_form_builder/flutter_form_builder.dart';
Expand All @@ -9,7 +10,9 @@ import 'package:responsive_framework/responsive_framework.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:vrouter/vrouter.dart';

import '../constants.dart';
import '../constants.dart' as constants;
import 'providers/auth_providers/user_state_notifier.dart';
import 'providers/language_provider.dart';

Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
Expand Down Expand Up @@ -53,8 +56,7 @@ class MyApp extends ConsumerWidget {
color: ThemeData.dark().scaffoldBackgroundColor,
titleSpacing: 32,
),
inputDecorationTheme:
const InputDecorationTheme(border: OutlineInputBorder()),
inputDecorationTheme: const InputDecorationTheme(border: OutlineInputBorder()),
),
localizationsDelegates: const [
FormBuilderLocalizations.delegate,
Expand Down Expand Up @@ -83,8 +85,7 @@ class MyApp extends ConsumerWidget {
VWidget.builder(
path: '/auth/:state',
name: 'auth',
builder: (context, params) => LogInPage(
state: params.pathParameters['state'] ?? 'register'),
builder: (context, params) => LogInPage(state: params.pathParameters['state'] ?? 'register'),
aliases: const ['/auth/register', '/auth/login'],
),
VWidget(
Expand Down Expand Up @@ -115,9 +116,15 @@ class MyHomePage extends ConsumerStatefulWidget {
class _MyHomePageState extends ConsumerState<MyHomePage> {
@override
Widget build(BuildContext context) {
MyUserData? user = ref.watch(constants.userStateProvider);
// TODO -- create loading splashcreen
if (user == null) return const Center(child: SizedBox(height: 50, width: 50, child: CircularProgressIndicator()));

Languages languageProvider = ref.watch(constants.languageProvider);

return Title(
color: Colors.black,
title: ref.watch(titleProvider),
title: ref.watch(constants.titleProvider),
child: Scaffold(
appBar: AppBar(
elevation: 0,
Expand All @@ -126,30 +133,59 @@ class _MyHomePageState extends ConsumerState<MyHomePage> {
children: [
const Text('Learn Languages'),
const Spacer(),
TextButton(
onPressed: () {
context.vRouter
.toNamed('auth', pathParameters: {'state': 'register'});
},
child: const Text(
'Sign up',
style: TextStyle(color: Colors.white),
Visibility(
visible: !user.authData.isAnonymous,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
onPressed: () {},
iconSize: 35,
splashRadius: 23,
padding: EdgeInsets.zero,
icon: const Icon(
Icons.account_circle_outlined,
),
)
],
),
),
const SizedBox(
width: 8,
),
TextButton(
onPressed: () async {
context.vRouter
.toNamed('auth', pathParameters: {'state': 'login'});
},
style: ElevatedButton.styleFrom(
side: const BorderSide(color: Colors.white),
),
child: const Text(
'Log in',
style: TextStyle(color: Colors.white),
Visibility(
visible: user.authData.isAnonymous,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () {
context.vRouter.toNamed(
'auth',
pathParameters: {'state': 'register'},
);
},
child: const Text(
'Sign up',
style: TextStyle(color: Colors.white),
),
),
const SizedBox(
width: 8,
),
TextButton(
onPressed: () async {
context.vRouter.toNamed(
'auth',
pathParameters: {'state': 'login'},
);
},
style: ElevatedButton.styleFrom(
side: const BorderSide(color: Colors.white),
),
child: const Text(
'Log in',
style: TextStyle(color: Colors.white),
),
),
],
),
),
],
Expand Down Expand Up @@ -189,7 +225,11 @@ class _MyHomePageState extends ConsumerState<MyHomePage> {
Flexible(
child: FormBuilderDropdown(
name: 'language',
items: const [],
initialValue: languageProvider.currentLanguage?.id,
items: languageProvider.getDropdownItems(context),
onChanged: (String? id) {
if (id != null) languageProvider.setCurrentLanguage(languageProvider.items[id]);
},
),
),
const SizedBox(
Expand Down Expand Up @@ -238,24 +278,22 @@ class _MyHomePageState extends ConsumerState<MyHomePage> {
mainAxisSize: MainAxisSize.min,
children: [
TextButton(
onPressed: () =>
launchUrl(Uri.parse('https://github.com/K-Zawis')),
onPressed: () => launchUrl(Uri.parse('https://github.com/K-Zawis')),
child: const Text(
'Github',
style: TextStyle(fontSize: 12),
),
),
TextButton(
onPressed: () => launchUrl(Uri.parse(
'https://www.linkedin.com/in/katarzyna-zawistowska-843302196')),
onPressed: () =>
launchUrl(Uri.parse('https://www.linkedin.com/in/katarzyna-zawistowska-843302196')),
child: const Text(
'LinkedIn',
style: TextStyle(fontSize: 12),
),
),
TextButton(
onPressed: () => launchUrl(Uri.parse(
'https://www.buymeacoffee.com/zawistowskQ')),
onPressed: () => launchUrl(Uri.parse('https://www.buymeacoffee.com/zawistowskQ')),
child: const Text(
'Buy Me a Coffee',
style: TextStyle(fontSize: 12),
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/assignment_mode_desktop_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class _DesktopAssignmentModeState extends ConsumerState<DesktopAssignmentMode> w
builder: (context, ref, child) {
var user = ref.watch(userStateProvider);
if (user != null) {
if (!user?.authData?.isAnonymous) {
if (!(user?.authData?.isAnonymous ?? false)) {
return IconButton(
onPressed: () {
setState(() {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/assignment_mode_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ class _AssignmentModeState extends ConsumerState<AssignmentMode> with TickerProv
builder: (context, ref, child) {
var user = ref.watch(userStateProvider);
if (user != null) {
if (!user?.authData.isAnonymous) {
if (!user.authData.isAnonymous) {
return IconButton(
onPressed: () {
setState(() {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class _HomePageState extends ConsumerState<HomePage> {
builder: (context, ref, child) {
var user = ref.watch(userStateProvider);
if (user != null) {
if (!user?.authData?.isAnonymous) {
if (!user.authData.isAnonymous) {
return IconButton(
onPressed: () {
setState(() {
Expand Down
2 changes: 1 addition & 1 deletion lib/pages/home_page_desktop.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class _DesktopHomePageState extends ConsumerState<DesktopHomePage> {
builder: (context, ref, child) {
var user = ref.watch(userStateProvider);
if (user != null) {
if (!user?.authData?.isAnonymous) {
if (!user.authData.isAnonymous) {
return IconButton(
onPressed: () {
setState(() {
Expand Down
Loading

0 comments on commit ae5fc19

Please sign in to comment.