added changes to login model and start of receipt

This commit is contained in:
Finn 2019-05-10 12:43:45 +01:00
parent 3af4997c01
commit 075a57278f
No known key found for this signature in database
GPG key ID: 7455B4B17685B598
10 changed files with 122 additions and 79 deletions

View file

@ -23,4 +23,9 @@ flutter build apk -t lib/main_dev.dart
## Links and Things ## Links and Things
https://github.com/putraxor/flutter-login-ui https://github.com/putraxor/flutter-login-ui
https://github.com/pbirdsall/medium_splash_tokenauth https://github.com/pbirdsall/medium_splash_tokenauth
## How to debug code
// debug
import 'package:flutter/foundation.dart';
debugPrint('$foo');

View file

@ -7,6 +7,7 @@ import 'package:local_spend/common/functions/save_current_login.dart';
import 'package:local_spend/common/functions/show_dialog_single_button.dart'; import 'package:local_spend/common/functions/show_dialog_single_button.dart';
import 'package:local_spend/model/json/login_model.dart'; import 'package:local_spend/model/json/login_model.dart';
import 'package:local_spend/config.dart'; import 'package:local_spend/config.dart';
// debug
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
Future<LoginModel> requestLoginAPI( Future<LoginModel> requestLoginAPI(
@ -32,14 +33,14 @@ Future<LoginModel> requestLoginAPI(
final responseJson = json.decode(response.body); final responseJson = json.decode(response.body);
var user = new LoginModel.fromJson(responseJson); var user = new LoginModel.fromJson(responseJson);
saveCurrentLogin(responseJson); saveCurrentLogin(responseJson, body["email"]);
Navigator.of(context).pushReplacementNamed('/HomePage'); Navigator.of(context).pushReplacementNamed('/HomePage');
return LoginModel.fromJson(responseJson); return LoginModel.fromJson(responseJson);
} else { } else {
final responseJson = json.decode(response.body); final responseJson = json.decode(response.body);
saveCurrentLogin(responseJson); saveCurrentLogin(responseJson, body["email"]);
showDialogSingleButton( showDialogSingleButton(
context, context,
"Unable to Login", "Unable to Login",

View file

@ -0,0 +1,47 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:local_spend/common/functions/save_current_login.dart';
import 'package:local_spend/common/functions/show_dialog_single_button.dart';
import 'package:local_spend/model/json/login_model.dart';
import 'package:local_spend/config.dart';
// debug
import 'package:flutter/foundation.dart';
Future<LoginModel> submitReceiptAPI(
BuildContext context, String amount, String time) async {
//var apiUrl = ConfigWrapper.of(context).apiKey;
final url = "https://dev.peartrade.org/api/login";
Map<String, String> body = {
'transaction_value': amount,
'purchase_time': time,
};
debugPrint('$body');
final response = await http.post(
url,
body: json.encode(body),
);
debugPrint(response.body);
if (response.statusCode == 200) {
final responseJson = json.decode(response.body);
return LoginModel.fromJson(responseJson);
} else {
final responseJson = json.decode(response.body);
showDialogSingleButton(
context,
"Unable to Submit Receipt",
"You may have supplied an invalid 'Email' / 'Password' combination. Please try again or email an administrator.",
"OK");
return null;
}
}

View file

@ -1,7 +1,7 @@
import 'package:local_spend/model/json/login_model.dart'; import 'package:local_spend/model/json/login_model.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
saveCurrentLogin(Map responseJson) async { saveCurrentLogin(Map responseJson, loginEmail) async {
SharedPreferences preferences = await SharedPreferences.getInstance(); SharedPreferences preferences = await SharedPreferences.getInstance();
var user; var user;
@ -13,12 +13,9 @@ saveCurrentLogin(Map responseJson) async {
var token = (responseJson != null && responseJson.isNotEmpty) var token = (responseJson != null && responseJson.isNotEmpty)
? LoginModel.fromJson(responseJson).token ? LoginModel.fromJson(responseJson).token
: ""; : "";
var email = (responseJson != null && responseJson.isNotEmpty) var email = (loginEmail != null)
? LoginModel.fromJson(responseJson).email ? loginEmail
: ""; : "";
var pk = (responseJson != null && responseJson.isNotEmpty)
? LoginModel.fromJson(responseJson).userId
: 0;
await preferences.setString( await preferences.setString(
'LastUser', (user != null && user.length > 0) ? user : ""); 'LastUser', (user != null && user.length > 0) ? user : "");
@ -26,5 +23,4 @@ saveCurrentLogin(Map responseJson) async {
'LastToken', (token != null && token.length > 0) ? token : ""); 'LastToken', (token != null && token.length > 0) ? token : "");
await preferences.setString( await preferences.setString(
'LastEmail', (email != null && email.length > 0) ? email : ""); 'LastEmail', (email != null && email.length > 0) ? email : "");
await preferences.setInt('LastUserId', (pk != null && pk > 0) ? pk : 0);
} }

View file

@ -6,5 +6,4 @@ saveLogout() async {
await preferences.setString('LastUser', ""); await preferences.setString('LastUser', "");
await preferences.setString('LastToken', ""); await preferences.setString('LastToken', "");
await preferences.setString('LastEmail', ""); await preferences.setString('LastEmail', "");
await preferences.setInt('LastUserId', 0);
} }

View file

@ -1,6 +1,9 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:local_spend/common/apifunctions/request_logout_api.dart'; import 'package:local_spend/common/apifunctions/request_logout_api.dart';
import 'package:local_spend/common/functions/get_token.dart';
// debug
import 'package:flutter/foundation.dart';
class BasicDrawer extends StatefulWidget { class BasicDrawer extends StatefulWidget {
@override @override
@ -8,6 +11,9 @@ class BasicDrawer extends StatefulWidget {
} }
class _BasicDrawerState extends State<BasicDrawer> { class _BasicDrawerState extends State<BasicDrawer> {
var token;
// TODO: add getter with getToken to check logged in
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Drawer( return Drawer(
@ -21,9 +27,10 @@ class _BasicDrawerState extends State<BasicDrawer> {
style: TextStyle(color: Colors.black, fontSize: 20.0), style: TextStyle(color: Colors.black, fontSize: 20.0),
), ),
onTap: () { onTap: () {
requestLogoutAPI(context); debugPrint('$token');
Navigator.of(context).pushNamed('/ReceiptPage'); Navigator.of(context).pushNamed('/ReceiptPage');
}, },
// enabled: token != null && token.isNotEmpty,
), ),
ListTile( ListTile(
title: Text( title: Text(

View file

@ -1,21 +1,15 @@
class LoginModel { class LoginModel {
final String userName; final String userName;
final String token; final String token;
final String email;
final int userId;
LoginModel(this.userName, this.token, this.email, this.userId); LoginModel(this.userName, this.token);
LoginModel.fromJson(Map<String, dynamic> json) LoginModel.fromJson(Map<String, dynamic> json)
: userName = json['name'], : userName = json['display_name'],
token = json['token'], token = json['session_key'];
email = json['email'],
userId = json['pk'];
Map<String, dynamic> toJson() => { Map<String, dynamic> toJson() => {
'name': userName, 'name': userName,
'token': token, 'token': token,
'email': email,
'pk': userId,
}; };
} }

View file

@ -2,12 +2,13 @@ import 'dart:async';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:local_spend/common/apifunctions/request_login_api.dart'; import 'package:local_spend/common/apifunctions/submit_receipt_api.dart';
import 'package:local_spend/common/functions/show_dialog_single_button.dart'; import 'package:local_spend/common/functions/show_dialog_single_button.dart';
import 'package:local_spend/common/platform/platform_scaffold.dart'; import 'package:local_spend/common/platform/platform_scaffold.dart';
import 'package:local_spend/common/widgets/basic_drawer.dart'; import 'package:local_spend/common/widgets/basic_drawer.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart'; import 'package:url_launcher/url_launcher.dart';
import 'package:datetime_picker_formfield/datetime_picker_formfield.dart';
const URL = "https://flutter.io/"; const URL = "https://flutter.io/";
@ -19,9 +20,12 @@ class ReceiptPage extends StatefulWidget {
} }
class ReceiptPageState extends State<ReceiptPage> { class ReceiptPageState extends State<ReceiptPage> {
final TextEditingController _emailController = TextEditingController(); final TextEditingController _timeController = TextEditingController();
final TextEditingController _passwordController = TextEditingController(); final TextEditingController _amountController = TextEditingController();
String _welcomeString = ""; final TextEditingController _essentialController = TextEditingController();
final TextEditingController _recurringController = TextEditingController();
final TextEditingController _typeController = TextEditingController();
final TextEditingController _orgController = TextEditingController();
Future launchURL(String url) async { Future launchURL(String url) async {
if (await canLaunch(url)) { if (await canLaunch(url)) {
@ -38,7 +42,7 @@ class ReceiptPageState extends State<ReceiptPage> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
_saveCurrentRoute("/LoginPage"); _saveCurrentRoute("/ReceiptPageState");
} }
_saveCurrentRoute(String lastRoute) async { _saveCurrentRoute(String lastRoute) async {
@ -62,7 +66,7 @@ class ReceiptPageState extends State<ReceiptPage> {
drawer: BasicDrawer(), drawer: BasicDrawer(),
appBar: AppBar( appBar: AppBar(
title: Text( title: Text(
"LOGIN", "Submit Receipt",
style: TextStyle( style: TextStyle(
fontSize: 30.0, fontSize: 30.0,
color: Colors.black, color: Colors.black,
@ -83,37 +87,12 @@ class ReceiptPageState extends State<ReceiptPage> {
child: Padding( child: Padding(
padding: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 15.0), padding: EdgeInsets.fromLTRB(0.0, 40.0, 0.0, 15.0),
child: Text( child: Text(
"Local Loop", "Required Fields in bold",
style: TextStyle(fontSize: 40.0, color: Colors.black), style: TextStyle(fontSize: 20.0, color: Colors.black),
), ),
)), )),
Padding(
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 78.0),
child: RichText(
text: TextSpan(
children: [
TextSpan(
text:
'This is the logon page.',
style: new TextStyle(
fontSize: 20.0,
color: Colors.black,
),
),
TextSpan(
text:
' It is currently in development.',
style: new TextStyle(
fontSize: 20.0,
color: Colors.black,
),
),
],
),
),
),
Text( Text(
"Email", "Time of Transaction",
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.black, color: Colors.black,
@ -123,7 +102,7 @@ class ReceiptPageState extends State<ReceiptPage> {
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
child: TextField( child: TextField(
controller: _emailController, controller: _timeController,
decoration: InputDecoration( decoration: InputDecoration(
hintText: "Use your login email", hintText: "Use your login email",
), ),
@ -137,7 +116,7 @@ class ReceiptPageState extends State<ReceiptPage> {
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 35.0, 0.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 35.0, 0.0, 0.0),
child: Text( child: Text(
"Password", "Amount",
style: TextStyle( style: TextStyle(
fontSize: 18.0, fontSize: 18.0,
color: Colors.black, color: Colors.black,
@ -148,7 +127,7 @@ class ReceiptPageState extends State<ReceiptPage> {
Padding( Padding(
padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0), padding: EdgeInsets.fromLTRB(0.0, 5.0, 0.0, 0.0),
child: TextField( child: TextField(
controller: _passwordController, controller: _amountController,
decoration: InputDecoration( decoration: InputDecoration(
hintText: 'Your password, keep it secret, keep it safe.', hintText: 'Your password, keep it secret, keep it safe.',
), ),
@ -167,8 +146,8 @@ class ReceiptPageState extends State<ReceiptPage> {
child: RaisedButton( child: RaisedButton(
onPressed: () { onPressed: () {
SystemChannels.textInput.invokeMethod('TextInput.hide'); SystemChannels.textInput.invokeMethod('TextInput.hide');
requestLoginAPI(context, _emailController.text, submitReceiptAPI(context, _amountController.text,
_passwordController.text); _timeController.text);
}, },
child: Text("LOGIN", child: Text("LOGIN",
style: style:

View file

@ -7,7 +7,7 @@ packages:
name: analyzer name: analyzer
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.36.2" version: "0.36.3"
args: args:
dependency: transitive dependency: transitive
description: description:
@ -21,7 +21,7 @@ packages:
name: async name: async
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.8" version: "2.1.0"
boolean_selector: boolean_selector:
dependency: transitive dependency: transitive
description: description:
@ -42,14 +42,14 @@ packages:
name: build_config name: build_config
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.3.2" version: "0.4.0"
build_daemon: build_daemon:
dependency: transitive dependency: transitive
description: description:
name: build_daemon name: build_daemon
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.5.0" version: "0.6.0"
build_resolvers: build_resolvers:
dependency: transitive dependency: transitive
description: description:
@ -63,28 +63,28 @@ packages:
name: build_runner name: build_runner
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.3.3" version: "1.4.0"
build_runner_core: build_runner_core:
dependency: transitive dependency: transitive
description: description:
name: build_runner_core name: build_runner_core
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.0.3" version: "3.0.5"
built_collection: built_collection:
dependency: transitive dependency: transitive
description: description:
name: built_collection name: built_collection
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "4.2.0" version: "4.2.1"
built_value: built_value:
dependency: transitive dependency: transitive
description: description:
name: built_value name: built_value
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "6.4.0" version: "6.5.0"
charcode: charcode:
dependency: transitive dependency: transitive
description: description:
@ -141,6 +141,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.2.7" version: "1.2.7"
datetime_picker_formfield:
dependency: "direct main"
description:
name: datetime_picker_formfield
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.8"
fixnum: fixnum:
dependency: transitive dependency: transitive
description: description:
@ -164,7 +171,7 @@ packages:
name: front_end name: front_end
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.1.17" version: "0.1.18"
glob: glob:
dependency: transitive dependency: transitive
description: description:
@ -207,6 +214,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "3.1.3" version: "3.1.3"
intl:
dependency: transitive
description:
name: intl
url: "https://pub.dartlang.org"
source: hosted
version: "0.15.8"
io: io:
dependency: transitive dependency: transitive
description: description:
@ -234,14 +248,14 @@ packages:
name: json_serializable name: json_serializable
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.2.1" version: "2.2.2"
kernel: kernel:
dependency: transitive dependency: transitive
description: description:
name: kernel name: kernel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.3.17" version: "0.3.18"
logging: logging:
dependency: transitive dependency: transitive
description: description:
@ -255,7 +269,7 @@ packages:
name: matcher name: matcher
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.3+1" version: "0.12.5"
meta: meta:
dependency: transitive dependency: transitive
description: description:
@ -297,7 +311,7 @@ packages:
name: pedantic name: pedantic
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.4.0" version: "1.5.0"
pool: pool:
dependency: transitive dependency: transitive
description: description:
@ -325,7 +339,7 @@ packages:
name: quiver name: quiver
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.1" version: "2.0.2"
shared_preferences: shared_preferences:
dependency: "direct main" dependency: "direct main"
description: description:
@ -365,7 +379,7 @@ packages:
name: source_span name: source_span
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.5.4" version: "1.5.5"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
@ -379,14 +393,14 @@ packages:
name: stream_channel name: stream_channel
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.6.8" version: "2.0.0"
stream_transform: stream_transform:
dependency: transitive dependency: transitive
description: description:
name: stream_transform name: stream_transform
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.0.16+1" version: "0.0.19"
string_scanner: string_scanner:
dependency: transitive dependency: transitive
description: description:
@ -407,7 +421,7 @@ packages:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.2" version: "0.2.4"
timing: timing:
dependency: transitive dependency: transitive
description: description:
@ -458,5 +472,5 @@ packages:
source: hosted source: hosted
version: "2.1.15" version: "2.1.15"
sdks: sdks:
dart: ">=2.1.1 <3.0.0" dart: ">=2.3.0-dev.0.1 <3.0.0"
flutter: ">=0.1.4 <2.0.0" flutter: ">=0.1.4 <2.0.0"

View file

@ -19,6 +19,7 @@ dependencies:
url_launcher: ^3.0.3 url_launcher: ^3.0.3
json_annotation : ^2.2.0 json_annotation : ^2.2.0
http: ^0.12.0+2 http: ^0.12.0+2
datetime_picker_formfield: ^0.1.8
# The following adds the Cupertino Icons font to your application. # The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons. # Use with the CupertinoIcons class for iOS style icons.