Skip to content

Commit

Permalink
Display balance and bug fixes #44
Browse files Browse the repository at this point in the history
Added display balance functionality to buy/sell page and stocks page.
Fixed a few authentication bugs.
  • Loading branch information
johnjjsung committed Dec 8, 2020
1 parent 73f905a commit 92442d9
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 107 deletions.
23 changes: 23 additions & 0 deletions lib/StockPage.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'firebase_auth.dart';
import 'stock_list.dart';
import 'package:provider/provider.dart';
import 'stock_ticker.dart';
Expand Down Expand Up @@ -57,6 +59,9 @@ class HomePage extends StatelessWidget {

@override
Widget build(BuildContext context) {
var balance = 0.0;
final DatabaseReference dbRef = FirebaseDatabase.instance.reference().child("users/$currUID");

return Scaffold(
body: Stack(children: <Widget>[
Container(
Expand All @@ -67,6 +72,24 @@ class HomePage extends StatelessWidget {
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
FutureBuilder(
future: dbRef.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
Map<dynamic, dynamic> values = snapshot.data.value;
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Name: " + values["name"]),
Text("Balance: \$" + values["balance"]),
],
),
);
}
return CircularProgressIndicator();
}
),
Text("Stocks",
style: TextStyle(
color: Colors.white,
Expand Down
23 changes: 23 additions & 0 deletions lib/buySell.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import 'package:firebase_database/firebase_database.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import 'NavPage.dart';
import 'firebase_auth.dart';

void main() => runApp(BuySell());
final amountController = TextEditingController();
Expand Down Expand Up @@ -33,12 +35,33 @@ void initState() {
class _BuySell extends State<MyStatefulWidget> {
@override
Widget build(BuildContext context) {
var balance = 0.0;
final DatabaseReference dbRef = FirebaseDatabase.instance.reference().child("users/$currUID");

return Scaffold(
appBar: AppBar(
title: Text('Number Field increment decrement'),
),
body: Column(children: <Widget>[
Column(children: <Widget>[
FutureBuilder(
future: dbRef.once(),
builder: (context, AsyncSnapshot<DataSnapshot> snapshot) {
if (snapshot.hasData) {
Map<dynamic, dynamic> values = snapshot.data.value;
return Card(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text("Name: " + values["name"]),
Text("Balance: \$" + values["balance"]),
],
),
);
}
return CircularProgressIndicator();
}
),
SizedBox(height: 50),
TextFormField(
controller: amountController,
Expand Down
130 changes: 23 additions & 107 deletions lib/firebase_auth.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,135 +74,49 @@ void signOutGoogle() async {
print("User Sign Out");
}

final ref = firebaseDB.reference().child("users");

Future<void> signUpWithEmail(String formName, String formEmail,
String formPassword, BuildContext context) async {
try {
_auth.createUserWithEmailAndPassword(
email: formEmail, password: formPassword);
} catch (e) {
// Display alert if signup fails for some reason.
showDialog(
context: context,
builder: (context) {
return AlertDialog(
title: Text("Error"),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
Text(e.code),
Text(e.message),
],
),
),
actions: <Widget>[
FlatButton(
child: Text('Ok'),
onPressed: () {
Navigator.of(context).pop();
},
),
],
);
});
}
final FirebaseUser currentUser = await _auth.currentUser();
UserUpdateInfo userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = formName;

currentUser.reload();
print(currentUser.displayName);
final AuthResult authResult = await _auth.createUserWithEmailAndPassword(email: formEmail, password: formPassword);
final FirebaseUser user = authResult.user;

//name = formName;
name = currentUser.displayName;
email = currentUser.email;
currUID = currentUser.uid;
name = formName;
//name = currentUser.displayName;
//UserUpdateInfo userUpdateInfo = UserUpdateInfo();
//userUpdateInfo.displayName = formName;
email = user.email;
currUID = user.uid;
imageUrl = "";

assert(await user.getIdToken() != null);

// create firebase db reference to access entries
final ref = firebaseDB.reference().child("users");

// check if user exists; if not create new entry in db
ref.orderByChild("uid").equalTo(currentUser.uid);
ref.orderByChild("uid").equalTo(user.uid);
DataSnapshot snapshot =
await ref.orderByChild("uid").equalTo(currentUser.uid).once();
await ref.orderByChild("uid").equalTo(user.uid).once();
if (snapshot.value == null) {
print("adding new user");
ref.push().set({
ref.child(currUID).set({
// add to database
"name": name,
"uid": currUID,
"email": email,
"photo": imageUrl,
"photo": imageUrl
});
}
}

Future<void> signUpWithEmail2(String formName, String formEmail,
String formPassword, BuildContext context) async {
print(formName);
print(formEmail);
print(formPassword);
print('123');

int succeed = 0;
FirebaseUser currentUser;

_auth
.createUserWithEmailAndPassword(email: formEmail, password: formPassword)
.then((credential) {
currentUser = credential.user;
UserUpdateInfo userUpdateInfo = UserUpdateInfo();
userUpdateInfo.displayName = formName;

currentUser.reload();
//print(currentUser.displayName);

name = formName;
//name = currentUser.displayName;
email = currentUser.email;
currUID = currentUser.uid;
imageUrl = "";

succeed = 1;
}).catchError((error) {
print(error);
});
Future<void> signInWithEmail(String formEmail, String formPassword) async {
final AuthResult authResult = await _auth.signInWithEmailAndPassword(email: formEmail, password: formPassword);
final FirebaseUser user = authResult.user;

// Didn't put this in then() because await keyword doesn't work in then().
if (succeed == 1) {
// create firebase db reference to access entries

// check if user exists; if not create new entry in db
ref.orderByChild("email").equalTo(currentUser.uid);
DataSnapshot snapshot =
await ref.orderByChild("email").equalTo(currentUser.uid).once();
if (snapshot.value == null) {
print("adding new user");
ref.push().set({
// add to database
"name": name,
"uid": currUID,
"email": email,
"photo": imageUrl
});
}
}
}
assert(await user.getIdToken() != null);

email = user.email;
currUID = user.uid;

Future<void> signInWithEmail(String formEmail, String formPassword) async {
_auth
.signInWithEmailAndPassword(email: formEmail, password: formPassword)
.then((credential) {
final FirebaseUser currentUser = credential.user;
email = currentUser.email;
currUID = currentUser.uid;
return;
}).catchError((error) {
print(error);
return;
});
return;
}

Expand Down Expand Up @@ -259,6 +173,7 @@ TextEditingController nameChange = TextEditingController();
TextEditingController bioChange = TextEditingController();

Future<String> update() async {
final ref = firebaseDB.reference().child("users");
final FirebaseUser currentUser = await _auth.currentUser();

ref.orderByChild("uid").equalTo(currentUser.uid);
Expand All @@ -272,6 +187,7 @@ Future<String> update() async {
}

Future<String> updateProfile() async {
final ref = firebaseDB.reference().child("users");
final FirebaseUser currentUser = await _auth.currentUser();
ref.orderByChild("uid").equalTo(currentUser.uid);
DataSnapshot snapshot =
Expand Down

0 comments on commit 92442d9

Please sign in to comment.