Skip to content

Commit

Permalink
Merge pull request #9 from coronatorid/fix-error-output
Browse files Browse the repository at this point in the history
Fix error output and little bit refactoring
  • Loading branch information
insomnius authored Jan 13, 2021
2 parents 68afb45 + ccaace1 commit 02bdb14
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 107 deletions.
22 changes: 8 additions & 14 deletions lib/src/controller/login_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,36 +18,32 @@ class LoginController implements LoginInterface {
LoginController(this._api);

Widget build(BuildContext context) {
return Provider(
return ChangeNotifierProvider(
create: (context) => LoginProvider(""),
child: LoginScreen(this),
);
}

void sendOtp(BuildContext context) async {
Scaffold.of(context).hideCurrentSnackBar();

this._sendOtpLock.synchronized(() async {
if (this._sendOtpClicked) {
return;
} else {
this._sendOtpClicked = true;
}

try {
LoginProvider loginProvider =
Provider.of<LoginProvider>(context, listen: false);
LoginProvider loginProvider =
Provider.of<LoginProvider>(context, listen: false);

try {
RegExp regExp = new RegExp(
r"^\+62\d{10,12}",
caseSensitive: false,
multiLine: false,
);

if (regExp.hasMatch(loginProvider.phoneNumber()) == false) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Nomor telfon tidak valid"),
));
loginProvider.setErrorString("Nomor telfon tidak valid");
return;
}

Expand All @@ -58,17 +54,15 @@ class LoginController implements LoginInterface {

print(otpSerializer.phoneNumber.toString());
print(otpSerializer.sentTime.toString());
loginProvider.setErrorString("");

Navigator.of(context).pushNamed('/otp', arguments: otpSerializer);
} on APIException catch (e, backtrace) {
Scaffold.of(context).hideCurrentSnackBar();
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(e.error.firstError().detail),
));

loginProvider.setErrorString(e.error.firstError().detail);
print("API ERROR: " + e.toString());
print("STACKTRACE: " + backtrace.toString());
} catch (e, backtrace) {
loginProvider.setErrorString("Telah terjadi error pad aplikasi");
print("ERROR: " + e.toString());
print("STACKTRACE: " + backtrace.toString());
} finally {
Expand Down
33 changes: 15 additions & 18 deletions lib/src/controller/otp_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class OTPController implements OTPInterface {

Widget build(BuildContext context) {
this._otpSerializer = ModalRoute.of(context).settings.arguments;
return Provider(
return ChangeNotifierProvider(
create: (context) => OTPProvider(),
child: OTPScreen(this),
);
Expand All @@ -36,25 +36,21 @@ class OTPController implements OTPInterface {
Scaffold.of(context).hideCurrentSnackBar();

this._submitOTPLock.synchronized(() async {
try {
if (this._submitOTPClicked) {
return;
} else {
this._submitOTPClicked = true;
}
if (this._submitOTPClicked) {
return;
} else {
this._submitOTPClicked = true;
}

OTPProvider otpProvider =
Provider.of<OTPProvider>(context, listen: false);
OTPProvider otpProvider =
Provider.of<OTPProvider>(context, listen: false);

try {
if (otpProvider.otpString().length < 4) {
Scaffold.of(context).showSnackBar(SnackBar(
content: Text("Kode OTP tidak valid"),
));
otpProvider.setErrorString("Kode OTP tidak valid");
return;
}

print("OTP STRING: " + otpProvider.otpString());

LoginSerializer loginSerializer = await this._api.auth().login(
context,
phoneNumber: this._otpSerializer.phoneNumber,
Expand All @@ -69,19 +65,20 @@ class OTPController implements OTPInterface {
Provider.of<AuthProvider>(context, listen: false);
await authProvider.authorize(loginSerializer);

otpProvider.setErrorString("");

Navigator.of(context).pushNamedAndRemoveUntil(
'/timeline', (Route<dynamic> route) => false);
} on APIException catch (e, backtrace) {
Scaffold.of(context).hideCurrentSnackBar();
Scaffold.of(context).showSnackBar(SnackBar(
content: Text(e.error.firstError().detail),
));
otpProvider.setErrorString(e.error.firstError().detail);

print("API ERROR: " + e.toString());
print("API STATUS CODE: " + e.statusCode.toString());
print("DETAIL: " + e.error.firstError().detail);
print("STACKTRACE: " + backtrace.toString());
} catch (e, backtrace) {
otpProvider.setErrorString("Telah terjadi kesalahan pada aplikasi");

print("ERROR: " + e.toString());
print("STACKTRACE: " + backtrace.toString());
} finally {
Expand Down
17 changes: 12 additions & 5 deletions lib/src/provider/login_provider.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
class LoginProvider {
import 'package:flutter/widgets.dart';

class LoginProvider with ChangeNotifier {
LoginProvider(this._phoneNumber);

String _phoneNumber;
String phoneNumber() => _phoneNumber;

String phoneNumber() {
return "+62" + this._phoneNumber;
}
String _errorString;
String errorString() => _errorString;

void setPhoneNumber(String phoneNumber) {
this._phoneNumber = phoneNumber;
this._phoneNumber = "+62" + phoneNumber;
}

void setErrorString(String errorString) {
this._errorString = errorString;
notifyListeners();
}
}
12 changes: 11 additions & 1 deletion lib/src/provider/otp_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
class OTPProvider {
import 'package:flutter/widgets.dart';

class OTPProvider with ChangeNotifier {
final List<String> _otp = List(4);

String _errorString;
String errorString() => _errorString;

void setErrorString(String errorString) {
this._errorString = errorString;
notifyListeners();
}

void setOTP(String digit, int index) {
this._otp[index] = digit;
}
Expand Down
19 changes: 19 additions & 0 deletions lib/src/screen/component/alert_text.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:flutter/material.dart';

class AlertText extends StatelessWidget {
final String text;

AlertText(this.text);

@override
Widget build(BuildContext context) {
return Text(
this.text,
style: TextStyle(
fontFamily: 'Hind',
fontSize: 14,
color: Colors.red,
),
);
}
}
56 changes: 21 additions & 35 deletions lib/src/screen/login_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:coronator/src/core/color.dart';
import 'package:coronator/src/interface/login_interface.dart';
import 'package:coronator/src/provider/login_provider.dart';
import 'package:coronator/src/screen/component/alert_text.dart';
import 'package:coronator/src/screen/component/button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -73,45 +75,29 @@ class LoginScreen extends StatelessWidget {
style: TextStyle(fontFamily: 'Hind', fontSize: 16),
),
SizedBox(
height: 75,
height: 10,
),
Consumer<LoginProvider>(
builder: (_, loginProvider, __) {
if (loginProvider.errorString() == null) {
return Container();
}

return AlertText(loginProvider.errorString());
},
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Stack(
children: [
Container(
padding: EdgeInsets.all(15),
child: Text(
"KIRIM OTP",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
),
),
width: 150,
decoration: BoxDecoration(
color: CustomColor.redTheme.withAlpha(255),
borderRadius:
BorderRadius.all(Radius.circular(50)),
),
),
Positioned.fill(
child: Material(
color: Colors.transparent,
borderRadius:
BorderRadius.all(Radius.circular(50)),
child: InkWell(
borderRadius:
BorderRadius.all(Radius.circular(50)),
splashColor: CustomColor.wewak,
onTap: () {
this.interface.sendOtp(context);
},
),
),
)
],
Button(
buttonText: "KIRIM OTP",
onTap: () {
this.interface.sendOtp(context);
},
width: 150,
),
],
),
Expand Down
58 changes: 24 additions & 34 deletions lib/src/screen/otp_screen.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'package:coronator/src/core/color.dart';
import 'package:coronator/src/interface/otp_interface.dart';
import 'package:coronator/src/provider/otp_provider.dart';
import 'package:coronator/src/screen/component/alert_text.dart';
import 'package:coronator/src/screen/component/button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -78,6 +80,23 @@ class OTPScreen extends StatelessWidget {
SizedBox(
height: 30,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Consumer<OTPProvider>(
builder: (_, otpProvider, __) {
if (otpProvider.errorString() == null) {
return Container();
}

return AlertText(otpProvider.errorString());
},
),
],
),
SizedBox(
height: 10,
),
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Expand All @@ -101,40 +120,11 @@ class OTPScreen extends StatelessWidget {
Row(
mainAxisAlignment: MainAxisAlignment.end,
children: [
Stack(
children: [
Container(
padding: EdgeInsets.all(15),
child: Text(
"SUBMIT",
textAlign: TextAlign.center,
style: TextStyle(
color: Colors.white,
),
),
width: 150,
decoration: BoxDecoration(
color: CustomColor.redTheme.withAlpha(255),
borderRadius:
BorderRadius.all(Radius.circular(50)),
),
),
Positioned.fill(
child: Material(
color: Colors.transparent,
borderRadius:
BorderRadius.all(Radius.circular(50)),
child: InkWell(
borderRadius:
BorderRadius.all(Radius.circular(50)),
splashColor: CustomColor.wewak,
onTap: () {
this.interface.submitOTP(context);
},
),
),
)
],
Button(
buttonText: "SUBMIT",
onTap: () {
this.interface.submitOTP(context);
},
),
],
),
Expand Down

0 comments on commit 02bdb14

Please sign in to comment.