Skip to content

Commit

Permalink
Merge pull request #53 from AlankritNayak/sign-in-page-connected
Browse files Browse the repository at this point in the history
Sign in page connected to the database, model classes created, user ID will now to saved to the API
  • Loading branch information
paricleu committed Mar 30, 2020
2 parents 90ff88f + df5b884 commit 8fa57a0
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 146 deletions.
79 changes: 47 additions & 32 deletions flutter-app/lib/bloc/auth_bloc.dart
@@ -1,39 +1,54 @@
import 'dart:async';
import 'package:flatten/bloc/bloc_provider.dart';
import 'package:flutter/foundation.dart';
/// this is the user modal, and we can use this to access current user data
/// once they are logged in
class User implements BlocBase{
User({@required this.uid, @required this.preName, @required this.name, @required this.zip});
final String uid;
final String preName;
final String name;
final String zip;
import 'package:flatten/bloc/user_bloc.dart';
import 'package:flatten/models/api_response.dart';
import 'package:flatten/services/qr_code_service.dart';
import 'package:flatten/models/qr_code_add.dart';
import 'package:flatten/models/qr_code.dart';

void dispose(){

}

Map<String, String> get userData => {'uid' : uid, 'preName': preName, 'name': name, 'zip': zip};
}


///this class can be modified to do authenticaion, once we have a database ready, we just have to
///this class can be modified to do authenticaion, once we have a database ready, we just have to
///add the sign-in logic in the singIn() method and enter the user details, we can also add a method here to
///check whether the user already exists in our database or not
class Auth implements BlocBase {
StreamController _controller = StreamController<User>();
Stream get userStream => _controller.stream;
class Auth_Bloc implements BlocBase {

StreamController _usercontroller = StreamController<User_Bloc>();
StreamController _isAuthenticating = StreamController<AuthState>();

Stream get userStream => _usercontroller.stream;
Stream get isAuthenticationStream => _isAuthenticating.stream;

void _setUser(User user) => _controller.add(user);
void _setUser(User_Bloc user) => _usercontroller.add(user);
void _setIsAuthenicating(AuthState authState) => _isAuthenticating.add(authState);

void dispose(){
_controller.close();
}

void signIn(String uid, String preName, String name, String zip){
User user = User(name: name, uid: uid, preName: preName, zip: zip);
_setUser(user);
}

}
void dispose() {
_usercontroller.close();
_isAuthenticating.close();
}

void signIn(String preName, String name, String zip) async{
final qr_code_for_add = QR_Code_For_Add(
name: name,
useCase: 1,
active: true,
additionalInformation: "",
gpsPosition: zip,
status: 1);
QR_Code_Service service = QR_Code_Service();
_setIsAuthenicating(AuthState(inProgress: true, errorOccured: false));
final APIResponse<QrCode> qrCodeData = await service.upLoadQR(qr_code_for_add);
final QrCode qrCode = qrCodeData.data;
if(!qrCodeData.error){
User_Bloc user = User_Bloc(name: qrCode.name, uid: qrCode.id, preName: preName, zip: zip);
_setIsAuthenicating(AuthState(inProgress : false, errorOccured: false));
_setUser(user);
}else{
_setIsAuthenicating(AuthState(inProgress:false, errorOccured: true));
}
}
}

class AuthState{
AuthState({this.inProgress, this.errorOccured});
bool inProgress;
bool errorOccured;
}
21 changes: 21 additions & 0 deletions flutter-app/lib/bloc/user_bloc.dart
@@ -0,0 +1,21 @@

import 'package:flatten/bloc/bloc_provider.dart';
import 'package:flutter/foundation.dart';

/// this is the user modal, and we can use this to access current user data
/// once they are logged in

class User_Bloc implements BlocBase{
User_Bloc({@required this.uid, @required this.preName, @required this.name, @required this.zip});
final String uid;
final String preName;
final String name;
final String zip;

void dispose(){

}

Map<String, String> get userData => {'uid' : uid, 'preName': preName, 'name': name, 'zip': zip};
}
4 changes: 2 additions & 2 deletions flutter-app/lib/main.dart
Expand Up @@ -16,8 +16,8 @@ void main() {
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider<Auth>(
bloc: Auth(),
return BlocProvider<Auth_Bloc>(
bloc: Auth_Bloc(),
child: BlocProvider<HS_Info_Bloc>(
bloc: HS_Info_Bloc(),
child: MaterialApp(
Expand Down
7 changes: 7 additions & 0 deletions flutter-app/lib/models/api_response.dart
@@ -0,0 +1,7 @@
class APIResponse<T> {
T data;
bool error;
String errorMessage;

APIResponse({this.data, this.errorMessage, this.error=false});
}
38 changes: 21 additions & 17 deletions flutter-app/lib/models/qr_code.dart
@@ -1,21 +1,25 @@
class QrCode {
final String id;
final String createDateTime;
final String name;
final int useCase;
final String additionalInformation;
final bool active;
final int status;
String id;
String createDateTime;
String name;
int useCase;
String additionalInformation;
bool active;
int status;

QrCode(this.id, this.createDateTime, this.name, this.useCase,
this.additionalInformation, this.active, this.status);
QrCode({this.id, this.createDateTime, this.name, this.useCase,
this.additionalInformation, this.active, this.status});

QrCode.fromJson(Map<String, dynamic> json)
: id = json["id"],
createDateTime = json["createDateTime"],
name = json["name"],
useCase = json["useCase"],
additionalInformation = json["additionalInformation"],
active = json["active"],
status = json["status"];
factory QrCode.fromJson(Map<String, dynamic> json){
return QrCode(
id: json["id"],
createDateTime: json["createDateTime"],
name: json["name"],
useCase :json["useCase"],
additionalInformation : json["additionalInformation"],
active: json["active"],
status: json["status"]
);

}
}
30 changes: 30 additions & 0 deletions flutter-app/lib/models/qr_code_add.dart
@@ -0,0 +1,30 @@
import 'package:flutter/foundation.dart';

class QR_Code_For_Add {
String name;
int useCase;
String gpsPosition;
String additionalInformation;
bool active;
int status;

QR_Code_For_Add({
@required this.name,
@required this.useCase,
@required this.gpsPosition,
@required this.additionalInformation,
@required this.active,
@required this.status,
});

Map<String, dynamic> toJson() {
return {
"name": name,
"useCase": useCase,
"gpsPosition": gpsPosition,
"additionalInformation": additionalInformation,
"active": active,
"status": status
};
}
}
28 changes: 28 additions & 0 deletions flutter-app/lib/services/qr_code_service.dart
@@ -0,0 +1,28 @@
import 'dart:convert';
import 'package:flatten/bloc/user_bloc.dart';
import 'package:flatten/models/api_response.dart';
import 'package:flatten/models/qr_code.dart';
import 'package:flatten/models/qr_code_add.dart';
import 'package:http/http.dart' as http;
import 'dart:async';

class QR_Code_Service {
static const API = 'https://api.flatten-app.de';
static const headers = {'Content-Type': 'application/json'};

Future<APIResponse<QrCode>> upLoadQR(QR_Code_For_Add item) {
print('upLoad called');
return http
.post(API + '/flatten/qr-codes',
headers: headers, body: json.encode(item.toJson())).timeout(Duration(seconds: 5))
.then((data) {
print('statusCode: ' + '${data.statusCode}');
if (data.statusCode == 201) {
final jsonData = json.decode(data.body);
return APIResponse<QrCode>(data: QrCode.fromJson(jsonData));
}
return APIResponse<QrCode>(error: true, errorMessage: 'An error occured');
}).catchError((_) =>
APIResponse<QrCode>(error: true, errorMessage: 'An error occured'));
}
}
3 changes: 2 additions & 1 deletion flutter-app/lib/ui/handshake/handshake_screen.dart
@@ -1,3 +1,4 @@
import 'package:flatten/bloc/user_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flatten/ui/handshake/widgets/camera_view.dart';
import 'package:line_awesome_icons/line_awesome_icons.dart';
Expand All @@ -20,7 +21,7 @@ class HandshakeScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
final user = BlocProvider.of<User>(context); //this gives the current logged in user
final user = BlocProvider.of<User_Bloc>(context); //this gives the current logged in user
return SingleChildScrollView(
child: Container(
height: MediaQuery.of(context).size.height/1.2,
Expand Down
7 changes: 4 additions & 3 deletions flutter-app/lib/ui/handshake/widgets/qr_view.dart
@@ -1,4 +1,5 @@
import 'package:flatten/bloc/bloc_provider.dart';
import 'package:flatten/bloc/user_bloc.dart';
import 'package:flutter/material.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:flatten/bloc/auth_bloc.dart';
Expand All @@ -11,7 +12,7 @@ class QrCodeView extends StatelessWidget {

@override
Widget build(BuildContext context) {
final user = BlocProvider.of<User>(context); //this gives the current logged in user
final user = BlocProvider.of<User_Bloc>(context); //this gives the current logged in user
TextStyle titleStyle = TextStyle(
fontWeight: FontWeight.bold,
fontSize: 24,
Expand All @@ -25,7 +26,7 @@ class QrCodeView extends StatelessWidget {
//earlier this was showing
// AppLocalizations.of(context).pvhtitel,
//this is changed temporarily to check user login:
"Prename: ${user.userData['preName']}\nName: ${user.userData['name']}\nZIP: ${user.userData['zip']}",
"uid: ${user.userData['uid']}\nName: ${user.userData['name']}\nZIP: ${user.userData['zip']}",
/*Later this can be changed to a unique is for every user, which is stored in the User class
and can be accessed though user.userData['uid']
*/
Expand All @@ -34,7 +35,7 @@ class QrCodeView extends StatelessWidget {
Padding(
padding: const EdgeInsets.only(left: 48, right: 48),
child: QrImage(
data: "nkldsfkldsfdknflsnkldfs",
data: user.userData['uid'],
foregroundColor: Color(0xFF08A388),
),
),
Expand Down
5 changes: 3 additions & 2 deletions flutter-app/lib/ui/home_page.dart
@@ -1,5 +1,6 @@
import 'package:flatten/bloc/auth_bloc.dart';
import 'package:flatten/bloc/bloc_provider.dart';
import 'package:flatten/bloc/user_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flatten/ui/create/create_qr_screen.dart';
import 'encounters/encounter_list_screen.dart';
Expand All @@ -12,7 +13,7 @@ import 'package:flatten/ui/handshake/handshake_decider.dart';

class HomePage extends StatefulWidget {
HomePage({@required this.user});
User user;
User_Bloc user;
@override
_HomePageState createState() => _HomePageState();
}
Expand Down Expand Up @@ -56,7 +57,7 @@ class _HomePageState extends State<HomePage> {
break;
}

return BlocProvider<User>(
return BlocProvider<User_Bloc>(
bloc: widget.user,
child: Scaffold(
appBar: AppBar(
Expand Down
5 changes: 3 additions & 2 deletions flutter-app/lib/ui/landing_page.dart
@@ -1,5 +1,6 @@
import 'package:flatten/bloc/bloc_provider.dart';
import 'package:flatten/bloc/hs_info_block.dart';
import 'package:flatten/bloc/user_bloc.dart';
import 'home_page.dart';
import 'package:flutter/material.dart';
import 'package:flatten/bloc/auth_bloc.dart';
Expand All @@ -9,15 +10,15 @@ import 'package:flatten/ui/sign_in_page.dart';
class LandingPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final Auth auth = BlocProvider.of<Auth>(context);
final Auth_Bloc auth = BlocProvider.of<Auth_Bloc>(context);
return StreamBuilder(
stream: auth.userStream,
initialData: null,
builder: (context, snapshot){
// this can be used to show a progress indicator when we authenticate from database,
// we can un-comment this.
// if(snapshot.connectionState == ConnectionState.active){
User user = snapshot.data;
User_Bloc user = snapshot.data;
if(user == null){
return SignInPage();
}
Expand Down

0 comments on commit 8fa57a0

Please sign in to comment.