Skip to content

Commit

Permalink
added profiles backend
Browse files Browse the repository at this point in the history
changed position of sign up page, now login if playing online only.
  • Loading branch information
vishnuagbly committed Mar 29, 2021
1 parent 155d2eb commit bf399bc
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 69 deletions.
32 changes: 0 additions & 32 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:flutter_phoenix/flutter_phoenix.dart';
import 'package:othello/providers/google_sign_in.dart';
import 'package:othello/screens/signup_screen.dart';
import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:othello/utils/app_module.dart';
import 'package:othello/utils/globals.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:provider/provider.dart';

import 'screens/main_menu.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
Expand Down Expand Up @@ -51,31 +47,3 @@ class MyApp extends StatelessWidget {
);
}
}

class MainPage extends StatefulWidget {
@override
_MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
var user = FirebaseAuth.instance.currentUser;

@override
void initState() {
FirebaseAuth.instance.authStateChanges().listen((user) {
this.user = user;
setState(() {});
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
Navigator.popUntil(context, ModalRoute.withName('/'));
});
});
super.initState();
}

@override
Widget build(BuildContext context) {
Globals.mediaQueryData = MediaQuery.of(context);
if (user == null) return SignUpScreen();
return MainMenu();
}
}
31 changes: 30 additions & 1 deletion lib/objects/profile.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:othello/objects/savable.dart';
import 'package:othello/screens/enter_name.dart';
import 'package:othello/utils/networks.dart';

class Profile extends Savable {
Profile(String name, this.id): this._name = name;
Profile(String name, this.id) : this._name = name;

static Profile? __globalProfile;

Profile.fromUser(User user)
: assert(user.displayName != null),
this._name = user.displayName!,
this.id = user.uid,
this._photoURL = user.photoURL;

Profile.fromMap(Map<String, dynamic> map)
: assert(map['name'] == null),
Expand All @@ -18,6 +30,23 @@ class Profile extends Savable {

String get name => _name;

///Call inside FirebaseAuth user changes listen method
static Future<void> setProfile(BuildContext context, User? user) async {
if (user != null) {
if (user.displayName == null) {
String? name =
await Navigator.pushNamed<String>(context, EnterName.routeName);
if (name == null) throw "Name is null";
await user.updateProfile(displayName: name);
}
__globalProfile = Profile.fromUser(user);
} else
__globalProfile = null;
if (__globalProfile != null) Networks.createProfile(__globalProfile!);
}

static Profile? get global => __globalProfile;

@override
Map<String, dynamic> toMap() => {
'name': _name,
Expand Down
18 changes: 0 additions & 18 deletions lib/providers/google_sign_in.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,14 @@ import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:google_sign_in/google_sign_in.dart';
import 'package:othello/objects/profile.dart';
import 'package:othello/utils/networks.dart';

class GoogleSignInProvider with ChangeNotifier {
final googleSignIn = GoogleSignIn();
final auth = FirebaseAuth.instance;
late Profile? __profile;
late bool _isSigningIn;

Profile? get profile => profile;

set _profile(Profile profile) {
this.__profile = profile;
notifyListeners();
}

GoogleSignInProvider() {
_isSigningIn = false;
}
Expand All @@ -28,17 +21,6 @@ class GoogleSignInProvider with ChangeNotifier {
notifyListeners();
}

Future<Profile?> setProfile() async {
final user = auth.currentUser;
if (this.profile == null || user == null) return Future.value(null);
final profile = await Networks.getProfile(user.uid);
if (profile != null) {
_profile = profile;
return this.profile;
}

}

Future login() async {
_isSigningIn = true;

Expand Down
42 changes: 42 additions & 0 deletions lib/screens/enter_name.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:flutter/material.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:othello/utils/globals.dart';

class EnterName extends StatelessWidget {
static const routeName = "/enter_name";
final _formKey = GlobalKey<FormState>();

@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.black,
body: Padding(
padding: const EdgeInsets.all(15),
child: Center(
child: Form(
key: _formKey,
child: TextFormField(
textAlign: TextAlign.center,
style: GoogleFonts.montserrat(fontSize: Globals.primaryFontSize),
decoration: Globals.textFieldDecoration.copyWith(
hintText: 'Enter Name',
prefixIcon: Icon(
Icons.account_box_rounded,
color: Colors.deepOrange,
size: Globals.maxScreenWidth * 0.06,
),
),
validator: (text) {
if (text == null || text.isEmpty) return "Please enter name";
},
onFieldSubmitted: (text) {
if (!_formKey.currentState!.validate()) return;
Navigator.pop(context, text);
},
),
),
),
),
);
}
}
27 changes: 25 additions & 2 deletions lib/screens/main_menu.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:othello/components/side_drawer.dart';
import 'package:othello/objects/profile.dart';
import 'package:othello/screens/game_room.dart';
import 'package:othello/screens/signup_screen.dart';

import 'online_rooms.dart';

class MainMenu extends StatelessWidget {
class MainMenu extends StatefulWidget {
@override
_MainMenuState createState() => _MainMenuState();
}

class _MainMenuState extends State<MainMenu> {
var user = FirebaseAuth.instance.currentUser;

@override
void initState() {
FirebaseAuth.instance.authStateChanges().listen((user) async {
this.user = user;
Navigator.popUntil(context, ModalRoute.withName('/'));
await Profile.setProfile(context, user);
});
super.initState();
}

@override
Widget build(BuildContext context) {
return Scaffold(
Expand All @@ -28,7 +48,10 @@ class MainMenu extends StatelessWidget {
),
ElevatedButton(
onPressed: () {
Navigator.pushNamed(context, OnlineRooms.routeName);
if (user == null)
Navigator.pushNamed(context, SignUpScreen.routeName);
else
Navigator.pushNamed(context, OnlineRooms.routeName);
},
child: Text('Online'),
),
Expand Down
8 changes: 1 addition & 7 deletions lib/screens/phone_input_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,12 @@ class _PhoneInputScreenState extends State<PhoneInputScreen> {
key: Key('phoneTextField'),
style: GoogleFonts.montserrat(fontSize: Globals.primaryFontSize),
textAlign: TextAlign.center,
decoration: InputDecoration(
decoration: Globals.textFieldDecoration.copyWith(
hintText: 'Enter phone number',
prefixIcon: Icon(
FontAwesomeIcons.phoneAlt,
color: Colors.green,
),
border: OutlineInputBorder(
borderRadius: Globals.borderRadius,
borderSide: BorderSide.none,
),
fillColor: Colors.white24,
filled: true,
),
controller: phoneNumberController,
keyboardType: TextInputType.number,
Expand Down
6 changes: 4 additions & 2 deletions lib/utils/app_module.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:flutter_modular/flutter_modular.dart';
import 'package:othello/main.dart';
import 'package:othello/screens/enter_name.dart';
import 'package:othello/screens/game_room.dart';
import 'package:othello/screens/main_menu.dart';
import 'package:othello/screens/online_rooms.dart';
import 'package:othello/screens/phone_input_screen.dart';
import 'package:othello/screens/signup_screen.dart';
Expand All @@ -13,7 +14,7 @@ class AppModule extends Module {
// Provide all the routes for your module
@override
final List<ModularRoute> routes = [
ChildRoute('/', child: (_, __) => MainPage()),
ChildRoute('/', child: (_, __) => MainMenu()),
ChildRoute(SignUpScreen.routeName, child: (_, __) => SignUpScreen()),
ChildRoute(PhoneInputScreen.routeName,
child: (_, __) => PhoneInputScreen()),
Expand All @@ -22,5 +23,6 @@ class AppModule extends Module {
ChildRoute(GameRoom.offlinePvPRouteName,
child: (_, __) => GameRoom.offlinePvP()),
ChildRoute(OnlineRooms.routeName, child: (_, __) => OnlineRooms()),
ChildRoute(EnterName.routeName, child: (_, __) => EnterName()),
];
}
25 changes: 18 additions & 7 deletions lib/utils/globals.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,30 @@ import 'package:uuid/uuid.dart';

class Globals {
static Uuid uuid = Uuid();

// UI Related finals
static MediaQueryData? _mediaQueryData;
static final TextStyle primaryTextStyle = GoogleFonts.montserrat(
fontSize: Globals.primaryFontSize,
);
static final double primaryFontSize = Globals.maxScreenWidth * 0.035;
static final BorderRadius borderRadius =
BorderRadius.circular((maxScreenWidth * 0.03));
static final textFieldDecoration = InputDecoration(
border: OutlineInputBorder(
borderRadius: Globals.borderRadius,
borderSide: BorderSide.none,
),
fillColor: Colors.white24,
filled: true,
);
// UI Related finals

static set mediaQueryData(MediaQueryData data) => _mediaQueryData = data;

static double get screenWidth => _mediaQueryData?.size.width ?? 500;

static double get maxScreenWidth => max(screenWidth, 600);

static double get screenHeight => _mediaQueryData?.size.height ?? 500;

static final TextStyle primaryTextStyle = GoogleFonts.montserrat(
fontSize: Globals.primaryFontSize,
);
static final double primaryFontSize = Globals.maxScreenWidth * 0.035;
static final BorderRadius borderRadius =
BorderRadius.circular((maxScreenWidth * 0.03));
}

0 comments on commit bf399bc

Please sign in to comment.