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_FINE_LOCATION" />
<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
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide

View File

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

View File

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

View File

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

View File

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

View File

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