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 #53 from AlankritNayak/sign-in-page-connected
Sign in page connected to the database, model classes created, user ID will now to saved to the API
- Loading branch information
Showing
12 changed files
with
265 additions
and
146 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
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,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}; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class APIResponse<T> { | ||
T data; | ||
bool error; | ||
String errorMessage; | ||
|
||
APIResponse({this.data, this.errorMessage, this.error=false}); | ||
} |
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,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"] | ||
); | ||
|
||
} | ||
} |
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,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 | ||
}; | ||
} | ||
} |
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,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')); | ||
} | ||
} |
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
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
Oops, something went wrong.