From 8bec726206be125bbdce0271926c929e561b46f8 Mon Sep 17 00:00:00 2001 From: Supan Adit Pratama Date: Thu, 31 Oct 2019 22:51:22 +0700 Subject: [PATCH] Connecting with Geo Smart System --- README.md | 3 +- android/app/src/main/AndroidManifest.xml | 3 +- lib/config.dart | 3 + lib/main.dart | 91 +++++++--- lib/model/position.dart | 72 ++++++++ lib/model/response.dart | 15 ++ lib/model/unique_id.dart | 15 ++ pubspec.lock | 207 ++++++++++++++++++++++- pubspec.yaml | 4 + 9 files changed, 383 insertions(+), 30 deletions(-) create mode 100644 lib/config.dart create mode 100644 lib/model/position.dart create mode 100644 lib/model/response.dart create mode 100644 lib/model/unique_id.dart diff --git a/README.md b/README.md index 5c1f214..0ad742e 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ This is the Mobile Application for [Geo Smart System](https://github.com/supanad - Flutter ## Todo -- Integration With Geo Smart System +- Integration With Geo Smart System ( OK ) +- Documentation ## License Copyright 2019 Supan Adit Pratama diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 3e0fb48..c2c7103 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -12,7 +12,8 @@ + android:label="Geo Smart App" + android:usesCleartextTraffic="true"> diff --git a/lib/config.dart b/lib/config.dart new file mode 100644 index 0000000..9abac73 --- /dev/null +++ b/lib/config.dart @@ -0,0 +1,3 @@ +class Config { + static const String api = "http://192.168.1.6:8080"; +} diff --git a/lib/main.dart b/lib/main.dart index 35ce04b..708745c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,7 @@ import 'dart:async'; import 'package:background_location/background_location.dart'; import 'package:flutter/material.dart'; +import 'package:geo_app/model/position.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; void main() => runApp(MyApp()); @@ -55,12 +56,23 @@ class _MyHomePageState extends State { double accuracy = 0.0; double bearing = 0.0; double speed = 0.0; + String message = "Connecting to Server"; + bool _finish = false; + Position _position; @override void initState() { super.initState(); - BackgroundLocation.startLocationService(); + message = "Connecting to Server"; + Position.getPosition().then((position) { + setState(() { + _position = position; + _finish = this._position.isValid(); + }); + }, onError: (error) { + message = "Something wrong"; + }); } static final CameraPosition _kGooglePlex = CameraPosition( @@ -71,33 +83,60 @@ class _MyHomePageState extends State { @override Widget build(BuildContext context) { return new Scaffold( - body: GoogleMap( - mapType: MapType.normal, - initialCameraPosition: _kGooglePlex, - myLocationEnabled: true, - myLocationButtonEnabled: true, - onMapCreated: (GoogleMapController controller) { - _controller.complete(controller); - BackgroundLocation.getLocationUpdates((location) async { - setState(() { - this.latitude = location.latitude; - this.longitude = location.longitude; - this.accuracy = location.accuracy; - this.altitude = location.altitude; - this.bearing = location.bearing; - this.speed = location.speed; - }); - final GoogleMapController controller = await _controller.future; - controller.animateCamera( - CameraUpdate.newCameraPosition( - new CameraPosition( - target: LatLng(this.latitude, this.longitude), - zoom: 19.151926040649414, + body: Stack( + children: [ + GoogleMap( + mapType: MapType.normal, + initialCameraPosition: _kGooglePlex, + myLocationEnabled: true, + myLocationButtonEnabled: true, + onMapCreated: (GoogleMapController controller) { + _controller.complete(controller); + BackgroundLocation.getLocationUpdates((location) async { + setState(() { + this.latitude = location.latitude; + this.longitude = location.longitude; + this.accuracy = location.accuracy; + this.altitude = location.altitude; + this.bearing = location.bearing; + this.speed = location.speed; + + if (this._position != null) { + if (this._position.isValid()) { + this._position.lat = this.latitude.toString(); + this._position.lng = this.longitude.toString(); + this._position.sendPosition(); + } + } + }); + // Make Camera Follow the Marker + // final GoogleMapController controller = await _controller.future; + // controller.animateCamera( + // CameraUpdate.newCameraPosition( + // new CameraPosition( + // target: LatLng(this.latitude, this.longitude), + // zoom: 19.151926040649414, + // ), + // ), + // ); + }); + }, + ), + Visibility( + visible: !_finish, + child: Container( + decoration: BoxDecoration( + color: Colors.black26.withOpacity(0.8), + ), + child: Center( + child: Text( + message, + style: TextStyle(color: Colors.white), ), ), - ); - }); - }, + ), + ) + ], ), ); } diff --git a/lib/model/position.dart b/lib/model/position.dart new file mode 100644 index 0000000..38cb212 --- /dev/null +++ b/lib/model/position.dart @@ -0,0 +1,72 @@ +import 'dart:convert'; + +import 'package:geo_app/config.dart'; +import 'package:geo_app/model/response.dart'; +import 'package:geo_app/model/unique_id.dart'; +import 'package:http/http.dart' as http; +import 'package:shared_preferences/shared_preferences.dart'; + +class Position { + String id; + String type; + String lat; + String lng; + + Position({this.id, this.type, this.lat, this.lng}); + + Position.fromJson(Map json) { + id = json['id']; + type = json['type']; + lat = json['lat']; + lng = json['lng']; + } + + Map toJson() { + final Map data = new Map(); + data['id'] = this.id; + data['type'] = this.type; + data['lat'] = this.lat; + data['lng'] = this.lng; + return data; + } + + static Future getPosition() async { + SharedPreferences sp = await SharedPreferences.getInstance(); + String keyName = "unique_id"; + String spId = (sp.getString(keyName) ?? null); + Position position = new Position( + id: spId, + type: "user", + lat: "0.0", + lng: "0.0", + ); + if (spId == null) { + final response = await http.get(Config.api + "/unique/id"); + if (response.statusCode == 200) { + position.id = UniqueId.fromJson(json.decode(response.body)).id; + } else { + throw Exception("Something Error"); + } + sp.setString(keyName, position.id); + } + return position; + } + + bool isValid() { + return id != null; + } + + Future sendPosition() async { + Response result; + final response = await http.post( + Config.api + "/set/point", + body: json.encode(this.toJson()), + ); + if (response.statusCode == 200) { + result = Response.fromJson(json.decode(response.body)); + } else { + throw Exception("Something Error"); + } + return result; + } +} diff --git a/lib/model/response.dart b/lib/model/response.dart new file mode 100644 index 0000000..73fccfc --- /dev/null +++ b/lib/model/response.dart @@ -0,0 +1,15 @@ +class Response { + String status; + + Response({this.status}); + + Response.fromJson(Map json) { + status = json['status']; + } + + Map toJson() { + final Map data = new Map(); + data['status'] = this.status; + return data; + } +} diff --git a/lib/model/unique_id.dart b/lib/model/unique_id.dart new file mode 100644 index 0000000..0837b58 --- /dev/null +++ b/lib/model/unique_id.dart @@ -0,0 +1,15 @@ +class UniqueId { + String id; + + UniqueId({this.id}); + + UniqueId.fromJson(Map json) { + id = json['id']; + } + + Map toJson() { + final Map data = new Map(); + data['id'] = this.id; + return data; + } +} diff --git a/pubspec.lock b/pubspec.lock index c85308d..c9eb4bd 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,6 +1,27 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: + adhara_socket_io: + dependency: "direct main" + description: + name: adhara_socket_io + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.1" + analyzer: + dependency: transitive + description: + name: analyzer + url: "https://pub.dartlang.org" + source: hosted + version: "0.38.5" + args: + dependency: transitive + description: + name: args + url: "https://pub.dartlang.org" + source: hosted + version: "1.5.2" async: dependency: transitive description: @@ -22,6 +43,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.5" + build: + dependency: transitive + description: + name: build + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.1" + build_config: + dependency: transitive + description: + name: build_config + url: "https://pub.dartlang.org" + source: hosted + version: "0.4.1+1" charcode: dependency: transitive description: @@ -29,6 +64,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.2" + checked_yaml: + dependency: transitive + description: + name: checked_yaml + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.2" collection: dependency: transitive description: @@ -36,6 +78,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.14.11" + convert: + dependency: transitive + description: + name: convert + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + crypto: + dependency: transitive + description: + name: crypto + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.3" + csslib: + dependency: transitive + description: + name: csslib + url: "https://pub.dartlang.org" + source: hosted + version: "0.16.1" cupertino_icons: dependency: "direct main" description: @@ -43,6 +106,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + dart_style: + dependency: transitive + description: + name: dart_style + url: "https://pub.dartlang.org" + source: hosted + version: "1.3.3" flutter: dependency: "direct main" description: flutter @@ -53,6 +123,20 @@ packages: description: flutter source: sdk version: "0.0.0" + front_end: + dependency: transitive + description: + name: front_end + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.27" + glob: + dependency: transitive + description: + name: glob + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0" google_maps_flutter: dependency: "direct main" description: @@ -60,6 +144,62 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.5.21+8" + html: + dependency: transitive + description: + name: html + url: "https://pub.dartlang.org" + source: hosted + version: "0.14.0+3" + http: + dependency: "direct main" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.12.0+2" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.3" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.1+1" + json_annotation: + dependency: transitive + description: + name: json_annotation + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.0" + json_serializable: + dependency: "direct main" + description: + name: json_serializable + url: "https://pub.dartlang.org" + source: hosted + version: "3.2.3" + kernel: + dependency: transitive + description: + name: kernel + url: "https://pub.dartlang.org" + source: hosted + version: "0.3.27" + logging: + dependency: transitive + description: + name: logging + url: "https://pub.dartlang.org" + source: hosted + version: "0.11.3+2" matcher: dependency: transitive description: @@ -74,6 +214,27 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.1.7" + node_interop: + dependency: transitive + description: + name: node_interop + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.3" + node_io: + dependency: transitive + description: + name: node_io + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1+2" + package_config: + dependency: transitive + description: + name: package_config + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0" path: dependency: transitive description: @@ -95,6 +256,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.3.0" + pub_semver: + dependency: transitive + description: + name: pub_semver + url: "https://pub.dartlang.org" + source: hosted + version: "1.4.2" + pubspec_parse: + dependency: transitive + description: + name: pubspec_parse + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.5" quiver: dependency: transitive description: @@ -102,11 +277,25 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.5" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.4+3" sky_engine: dependency: transitive description: flutter source: sdk version: "0.0.99" + source_gen: + dependency: transitive + description: + name: source_gen + url: "https://pub.dartlang.org" + source: hosted + version: "0.9.4+6" source_span: dependency: transitive description: @@ -163,6 +352,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.8" + watcher: + dependency: transitive + description: + name: watcher + url: "https://pub.dartlang.org" + source: hosted + version: "0.9.7+12" + yaml: + dependency: transitive + description: + name: yaml + url: "https://pub.dartlang.org" + source: hosted + version: "2.2.0" sdks: - dart: ">=2.2.2 <3.0.0" - flutter: ">=1.5.0 <2.0.0" + dart: ">=2.3.0 <3.0.0" + flutter: ">=1.6.7 <2.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 2915df6..4209ee7 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -25,6 +25,10 @@ dependencies: cupertino_icons: ^0.1.2 google_maps_flutter: ^0.5.21+8 background_location: ^0.0.7 + adhara_socket_io: ^0.4.1 + http: ^0.12.0+2 + json_serializable: ^3.2.3 + shared_preferences: ^0.5.4+3 dev_dependencies: flutter_test: