Guard for System Error

This commit is contained in:
Supan Adit Pratama 2020-01-04 06:36:33 +07:00
parent 3ead80b465
commit fa6e4e43ab
8 changed files with 87 additions and 39 deletions

View File

@ -4,6 +4,8 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<!-- io.flutter.app.FlutterApplication is an android.app.Application that <!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method. calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide In most cases you can leave this as-is, but you if you want to provide

View File

@ -1,4 +1,4 @@
class Config { class Config {
static const String api = "http://192.168.1.7:8080"; static const String api = "http://192.168.1.7:8080";
static const bool dynamicHostSetting = false; static const bool dynamicHostSetting = true;
} }

View File

@ -1,4 +1,6 @@
class ResponseModel { class ResponseModel {
final String errorMessage = "error";
String status; String status;
ResponseModel({this.status}); ResponseModel({this.status});
@ -8,7 +10,11 @@ class ResponseModel {
} }
ResponseModel.fromNull() { ResponseModel.fromNull() {
status = "Error"; status = errorMessage;
}
isError() {
return (status == errorMessage);
} }
Map<String, dynamic> toJson() { Map<String, dynamic> toJson() {

View File

@ -2,6 +2,7 @@ import 'dart:async';
import 'package:background_location/background_location.dart'; import 'package:background_location/background_location.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geo_app/bloc/position_bloc.dart'; import 'package:geo_app/bloc/position_bloc.dart';
import 'package:geo_app/bloc/setting.dart'; import 'package:geo_app/bloc/setting.dart';
import 'package:geo_app/model/position.dart'; import 'package:geo_app/model/position.dart';
@ -58,14 +59,18 @@ class _MapState extends State<Map> {
this.host = settingModel.host; this.host = settingModel.host;
if (settingModel.isNullId()) { if (settingModel.isNullId()) {
Navigator.of(context).pushReplacement(new MaterialPageRoute( settingPage();
builder: (BuildContext context) => Setting(),
));
} }
if (this.host != null && this.host != "") { if (this.host != null && this.host != "") {
setState(() { setState(() {
_positionBloc = new PositionBloc(settingModel); _positionBloc = new PositionBloc(settingModel);
_positionBloc.subject.listen((position) {
if (position.isError()) {
BackgroundLocation.stopLocationService();
settingPage();
}
});
}); });
} }
}); });
@ -94,23 +99,21 @@ class _MapState extends State<Map> {
); );
} else { } else {
BackgroundLocation.stopLocationService(); BackgroundLocation.stopLocationService();
Navigator.of(context).pushReplacement(new MaterialPageRoute( settingPage();
builder: (BuildContext context) => Setting(),
));
} }
if (this._position != null) { if (this._position != null) {
if (this._position.isValid()) { if (this._position.isValid()) {
print("Valid Position");
if (_positionBloc != null) { if (_positionBloc != null) {
print("Send Position " +
this._position.lat.toString() +
", " +
this._position.lng.toString());
_positionBloc.sendPosition( _positionBloc.sendPosition(
this._position.lat.toString(), this._position.lat.toString(),
this._position.lng.toString(), this._position.lng.toString(),
); );
} else {
Fluttertoast.showToast(
msg: "Failed to start position service",
);
settingPage();
} }
} else { } else {
print("Invalid Position"); print("Invalid Position");
@ -173,10 +176,18 @@ class _MapState extends State<Map> {
isTracking = !isTracking; isTracking = !isTracking;
}); });
} else { } else {
print("Access Denied"); Fluttertoast.showToast(
msg: "Make sure you have turn on location services",
);
} }
} }
settingPage() {
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => Setting(),
));
}
@override @override
void dispose() { void dispose() {
print("Disposed"); print("Disposed");

View File

@ -1,5 +1,6 @@
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geo_app/bloc/setting.dart'; import 'package:geo_app/bloc/setting.dart';
import 'package:geo_app/bloc/unique_id_bloc.dart'; import 'package:geo_app/bloc/unique_id_bloc.dart';
import 'package:geo_app/config.dart'; import 'package:geo_app/config.dart';
@ -40,32 +41,38 @@ class _SettingState extends State<Setting> {
_hostController.text = this.host; _hostController.text = this.host;
if (settingModel.isNullId()) { if (!settingModel.isNullHost()) {
print("Requesting Unique ID");
_uniqueIDBloc = new UniqueIDBloc(settingModel); _uniqueIDBloc = new UniqueIDBloc(settingModel);
_uniqueIDBloc.getUniqueID(); _uniqueIDBloc.getUniqueID();
}
if (_uniqueIDBloc != null) { if (_uniqueIDBloc != null) {
this._uniqueIDBloc.subject.listen((uniqueId) { this._uniqueIDBloc.subject.listen((uniqueId) {
print("Your Unique ID " + uniqueId.id.toString()); print("Your Unique ID " + uniqueId.id.toString());
if (uniqueId.id != null && uniqueId.id != "") { if (uniqueId.id != null && uniqueId.id != "") {
if (!settingModel.isNullId()) { if (!settingModel.isNullId()) {
Navigator.of(context).pushReplacement(new MaterialPageRoute( mapPage();
builder: (BuildContext context) => Map(), } else {
)); this._settingBloc.setSetting(
new SettingModel(this._hostController.text, uniqueId.id),
);
mapPage();
}
} else { } else {
this._settingBloc.setSetting( Fluttertoast.showToast(msg: "Invalid host address");
new SettingModel(this._hostController.text, uniqueId.id),
);
} }
} });
}); }
} }
}); });
super.initState(); super.initState();
} }
mapPage() {
Navigator.of(context).pushReplacement(new MaterialPageRoute(
builder: (BuildContext context) => Map(),
));
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Scaffold( return Scaffold(
@ -92,13 +99,23 @@ class _SettingState extends State<Setting> {
border: OutlineInputBorder(), border: OutlineInputBorder(),
), ),
), ),
FlatButton( SizedBox(
onPressed: () { height: 10.0,
this._settingBloc.setSetting( ),
new SettingModel(this._hostController.text, null), Container(
); width: double.infinity,
}, child: FlatButton(
child: Text("Save"), color: Colors.blueAccent,
onPressed: () {
this._settingBloc.setSetting(
new SettingModel(this._hostController.text, null),
);
},
child: Text(
"Save",
style: TextStyle(color: Colors.white),
),
),
) )
], ],
), ),

View File

@ -23,8 +23,12 @@ class PositionProvider {
); );
return ResponseModel.fromJson(response.data); return ResponseModel.fromJson(response.data);
} on DioError catch (e) { } on DioError catch (e) {
print(e); print(e.response);
return ResponseModel.fromJson(e.response.data); if (e.response != null) {
return ResponseModel.fromJson(e.response.data);
} else {
return ResponseModel.fromNull();
}
} }
} }

View File

@ -149,6 +149,13 @@ packages:
description: flutter description: flutter
source: sdk source: sdk
version: "0.0.0" version: "0.0.0"
fluttertoast:
dependency: "direct main"
description:
name: fluttertoast
url: "https://pub.dartlang.org"
source: hosted
version: "3.1.3"
glob: glob:
dependency: transitive dependency: transitive
description: description:

View File

@ -31,6 +31,7 @@ dependencies:
shared_preferences: ^0.5.6 shared_preferences: ^0.5.6
rxdart: ^0.23.1 rxdart: ^0.23.1
dio: ^3.0.7 dio: ^3.0.7
fluttertoast: ^3.1.3
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: