-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from nhandrew/part21
Part21
- Loading branch information
Showing
18 changed files
with
575 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,65 @@ | ||
import 'package:farmers_market/src/blocs/auth_bloc.dart'; | ||
import 'package:farmers_market/src/routes.dart'; | ||
import 'package:farmers_market/src/screens/landing.dart'; | ||
import 'package:farmers_market/src/screens/login.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'dart:io'; | ||
|
||
class App extends StatelessWidget { | ||
import 'package:provider/provider.dart'; | ||
final authBloc = AuthBloc(); | ||
|
||
class App extends StatefulWidget { | ||
@override | ||
_AppState createState() => _AppState(); | ||
} | ||
|
||
class _AppState extends State<App> { | ||
@override | ||
Widget build(BuildContext context) { | ||
return PlatformApp(); | ||
return MultiProvider( | ||
providers: [ | ||
Provider(create: (context) => authBloc), | ||
FutureProvider(create: (context) => authBloc.isLoggedIn()) | ||
], | ||
child: PlatformApp()); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
authBloc.dispose(); | ||
super.dispose(); | ||
} | ||
} | ||
|
||
class PlatformApp extends StatelessWidget { | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
|
||
var isLoggedIn = Provider.of<bool>(context); | ||
|
||
if (Platform.isIOS) { | ||
return CupertinoApp( | ||
home: Login(), | ||
home: (isLoggedIn == null) ? loadingScreen(true) : (isLoggedIn == true ) ? Landing() : Login(), | ||
onGenerateRoute: Routes.cupertinoRoutes, | ||
theme: CupertinoThemeData( | ||
scaffoldBackgroundColor: Colors.white | ||
) | ||
); | ||
); | ||
} else { | ||
return MaterialApp( | ||
home: Login(), | ||
home: (isLoggedIn == null) ? loadingScreen(false) : (isLoggedIn == true ) ? Landing() : Login(), | ||
onGenerateRoute: Routes.materialRoutes, | ||
theme: ThemeData(scaffoldBackgroundColor: Colors.white) | ||
); | ||
} | ||
} | ||
|
||
Widget loadingScreen(bool isIOS){ | ||
return (isIOS) | ||
? CupertinoPageScaffold(child: Center(child: CupertinoActivityIndicator(),),) | ||
: Scaffold(body: Center(child: CircularProgressIndicator())); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
|
||
import 'dart:async'; | ||
|
||
import 'package:farmers_market/src/models/user.dart'; | ||
import 'package:farmers_market/src/services/firestore_service.dart'; | ||
import 'package:firebase_auth/firebase_auth.dart'; | ||
import 'package:rxdart/rxdart.dart'; | ||
import 'package:rxdart/subjects.dart'; | ||
|
||
final RegExp regExpEmail = RegExp( | ||
r'^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$'); | ||
|
||
class AuthBloc { | ||
final _email = BehaviorSubject<String>(); | ||
final _password = BehaviorSubject<String>(); | ||
final _user = BehaviorSubject<User>(); | ||
final FirebaseAuth _auth = FirebaseAuth.instance; | ||
final FirestoreService _firestoreService = FirestoreService(); | ||
|
||
//Get Data | ||
Stream<String> get email => _email.stream.transform(validateEmail); | ||
Stream<String> get password => _password.stream.transform(validatePassword); | ||
Stream<bool> get isValid => CombineLatestStream.combine2(email, password, (email,password)=> true); | ||
Stream<User> get user => _user.stream; | ||
|
||
//Set Data | ||
Function(String) get changeEmail => _email.sink.add; | ||
Function(String) get changePassword => _password.sink.add; | ||
|
||
dispose(){ | ||
_email.close(); | ||
_password.close(); | ||
_user.close(); | ||
} | ||
|
||
//Transformers | ||
final validateEmail = StreamTransformer<String,String>.fromHandlers(handleData: (email, sink){ | ||
if (regExpEmail.hasMatch(email.trim())){ | ||
sink.add(email.trim()); | ||
}else { | ||
sink.addError('Must Be Valid Email Address'); | ||
} | ||
}); | ||
|
||
final validatePassword = StreamTransformer<String,String>.fromHandlers(handleData: (password, sink){ | ||
if (password.length >= 8){ | ||
sink.add(password.trim()); | ||
}else { | ||
sink.addError('8 Character Minimum'); | ||
} | ||
}); | ||
|
||
//Functions | ||
signupEmail() async{ | ||
try{ | ||
AuthResult authResult = await _auth.createUserWithEmailAndPassword(email: _email.value.trim(), password: _password.value.trim()); | ||
var user = User(userId: authResult.user.uid, email: _email.value.trim()); | ||
await _firestoreService.addUser(user); | ||
_user.sink.add(user); | ||
} catch (error){ | ||
print(error); | ||
} | ||
} | ||
|
||
loginEmail() async{ | ||
try{ | ||
AuthResult authResult = await _auth.signInWithEmailAndPassword(email: _email.value.trim(), password: _password.value.trim()); | ||
var user = await _firestoreService.fetchUser(authResult.user.uid); | ||
_user.sink.add(user); | ||
} catch (error){ | ||
print(error); | ||
} | ||
} | ||
|
||
Future<bool> isLoggedIn() async { | ||
var firebaseUser = await _auth.currentUser(); | ||
if (firebaseUser == null) return false; | ||
|
||
var user = await _firestoreService.fetchUser(firebaseUser.uid); | ||
if (user == null) return false; | ||
|
||
_user.sink.add(user); | ||
return true; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class User { | ||
final String userId; | ||
final String email; | ||
|
||
User({this.email, this.userId}); | ||
|
||
Map<String,dynamic> toMap(){ | ||
return { | ||
'userId': userId, | ||
'email': email | ||
}; | ||
} | ||
|
||
User.fromFirestore(Map<String,dynamic> firestore) | ||
: userId = firestore['userId'], | ||
email = firestore['email']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,38 @@ | ||
import 'package:farmers_market/src/screens/landing.dart'; | ||
import 'package:farmers_market/src/screens/login.dart'; | ||
import 'package:farmers_market/src/screens/signup.dart'; | ||
import 'package:farmers_market/src/screens/vendor.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
|
||
abstract class Routes { | ||
|
||
static MaterialPageRoute materialRoutes(RouteSettings settings){ | ||
switch(settings.name){ | ||
case "/": | ||
return MaterialPageRoute(builder: (context) => Landing()); | ||
static MaterialPageRoute materialRoutes(RouteSettings settings) { | ||
switch (settings.name) { | ||
case "/landing": | ||
return MaterialPageRoute(builder: (context) => Landing()); | ||
case "/signup": | ||
return MaterialPageRoute(builder: (context) => Signup()); | ||
return MaterialPageRoute(builder: (context) => Signup()); | ||
case "/login": | ||
return MaterialPageRoute(builder: (context) => Login()); | ||
return MaterialPageRoute(builder: (context) => Login()); | ||
case "/vendor": | ||
return MaterialPageRoute(builder: (context) => Vendor()); | ||
default: | ||
return MaterialPageRoute(builder: (context) => Login()); | ||
return MaterialPageRoute(builder: (context) => Login()); | ||
} | ||
} | ||
|
||
static CupertinoPageRoute cupertinoRoutes(RouteSettings settings){ | ||
switch(settings.name){ | ||
case "/": | ||
return CupertinoPageRoute(builder: (context) => Landing()); | ||
static CupertinoPageRoute cupertinoRoutes(RouteSettings settings) { | ||
switch (settings.name) { | ||
case "/landing": | ||
return CupertinoPageRoute(builder: (context) => Landing()); | ||
case "/signup": | ||
return CupertinoPageRoute(builder: (context) => Signup()); | ||
return CupertinoPageRoute(builder: (context) => Signup()); | ||
case "/login": | ||
return CupertinoPageRoute(builder: (context) => Login()); | ||
return CupertinoPageRoute(builder: (context) => Login()); | ||
case "/vendor": | ||
return CupertinoPageRoute(builder: (context) => Vendor()); | ||
default: | ||
return CupertinoPageRoute(builder: (context) => Login()); | ||
return CupertinoPageRoute(builder: (context) => Login()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,30 @@ | ||
import 'package:farmers_market/src/widgets/button.dart'; | ||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'dart:io'; | ||
|
||
class Landing extends StatelessWidget{ | ||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold(); | ||
if (Platform.isIOS){ | ||
return CupertinoPageScaffold( | ||
child: pageBody(context), | ||
); | ||
} else { | ||
return Scaffold(body:pageBody(context)); | ||
} | ||
} | ||
|
||
Widget pageBody(BuildContext context){ | ||
return Column( | ||
mainAxisAlignment: MainAxisAlignment.center, | ||
children: <Widget>[ | ||
AppButton( | ||
buttonText: 'Vendor Page', | ||
buttonType: ButtonType.Straw, | ||
onPressed: () => Navigator.pushNamed(context, '/vendor'), | ||
) | ||
], | ||
); | ||
} | ||
} |
Oops, something went wrong.