Skip to content

Commit

Permalink
add google maps reference
Browse files Browse the repository at this point in the history
  • Loading branch information
itMatos committed Sep 2, 2024
1 parent 40cb4c9 commit 206e3b7
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 15 deletions.
18 changes: 16 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,30 @@ import 'package:zenith_monitor/modules/signup/screen/sign_up_screen.dart';
import 'package:zenith_monitor/utils/ui/animations/zenith_progress_indicator.dart';
import 'package:zenith_monitor/modules/login/screen/login_screen.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:permission_handler/permission_handler.dart';

void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await dotenv.load(fileName: ".env");
await requestBluetoothPermissions();
runApp(const ZenithMonitor());
}

Future<void> requestBluetoothPermissions() async {
final statusScan = await Permission.bluetoothScan.request();
final statusConnect = await Permission.bluetoothConnect.request();
final statusLocation = await Permission.locationWhenInUse.request();

if (!statusScan.isGranted ||
!statusConnect.isGranted ||
!statusLocation.isGranted) {
openAppSettings();
}
}

class ZenithMonitor extends StatelessWidget {
const ZenithMonitor({Key? key}) : super(key: key);

Expand Down Expand Up @@ -82,8 +96,8 @@ class Application extends StatelessWidget {
backgroundColor: Colors.black.withOpacity(0)),
primaryColor: Colors.black,
),
initialRoute: currentUser != null ? '/home' : '/login',
// initialRoute: '/login',
//initialRoute: currentUser != null ? '/home' : '/login',
initialRoute: '/login',
routes: {
'/login': (context) => const LoginScreen(),
'/signup': (context) => const SignUpScreen(),
Expand Down
72 changes: 59 additions & 13 deletions lib/modules/bluetooth/ChatPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_bluetooth_serial/flutter_bluetooth_serial.dart';
import 'package:url_launcher/url_launcher.dart';

class ChatPage extends StatefulWidget {
final BluetoothDevice server;
Expand All @@ -17,8 +18,10 @@ class ChatPage extends StatefulWidget {
class Message {
int whom;
String text;
double? latitude;
double? longitude;

Message(this.whom, this.text);
Message(this.whom, this.text, {this.latitude, this.longitude});
}

class ChatPageState extends State<ChatPage> {
Expand All @@ -28,8 +31,7 @@ class ChatPageState extends State<ChatPage> {
List<Message> messages = List<Message>.empty(growable: true);
String _messageBuffer = '';

final TextEditingController textEditingController =
new TextEditingController();
final TextEditingController textEditingController = TextEditingController();
final ScrollController listScrollController = ScrollController();

bool isConnecting = true;
Expand Down Expand Up @@ -99,11 +101,27 @@ class ChatPageState extends State<ChatPage> {
color:
_message.whom == clientID ? Colors.blueAccent : Colors.grey,
borderRadius: BorderRadius.circular(7.0)),
child: Text(
(text) {
return text == '/shrug' ? \\_(ツ)_/¯' : text;
}(_message.text.trim()),
style: const TextStyle(color: Colors.white)),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
(text) {
return text == '/shrug' ? \\_(ツ)_/¯' : text;
}(_message.text.trim()),
style: const TextStyle(color: Colors.white),
),
if (_message.longitude != null &&
_message.latitude != null) ...[
const SizedBox(height: 8.0),
ElevatedButton(
onPressed: () {
openGoogleMaps(_message.latitude!, _message.longitude!);
},
child: const Text('Abrir endereço no Google Maps'),
),
],
],
),
),
],
);
Expand All @@ -113,10 +131,10 @@ class ChatPageState extends State<ChatPage> {
return Scaffold(
appBar: AppBar(
title: (isConnecting
? Text('Connecting chat to ' + serverName + '...')
? Text('Connecting chat to $serverName...')
: isConnected
? Text('Live chat with ' + serverName)
: Text('Chat log with ' + serverName))),
? Text('Live chat with $serverName')
: Text('Chat log with $serverName'))),
body: SafeArea(
child: Column(
children: <Widget>[
Expand Down Expand Up @@ -162,6 +180,17 @@ class ChatPageState extends State<ChatPage> {
);
}

Future<void> openGoogleMaps(double latitude, double longitude) async {
final Uri googleMapsUrl = Uri.parse(
'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude');

if (await canLaunchUrl(googleMapsUrl)) {
await launchUrl(googleMapsUrl);
} else {
throw 'Could not open the map.';
}
}

void _onDataReceived(Uint8List data) {
// Allocate buffer for parsed data
int backspacesCounter = 0;
Expand All @@ -187,8 +216,23 @@ class ChatPageState extends State<ChatPage> {
}
}

// Create message if there is new line character
// Cria uma string a partir do buffer
String dataString = String.fromCharCodes(buffer);

// Divide a string em partes usando ponto e vírgula como delimitador
List<String> parts = dataString.split(';');
String message = dataString; // Mensagem original
double? longitude;
double? latitude;

if (parts.length >= 3) {
// Extrai longitude e latitude (segundo e terceiro valores)
longitude = double.tryParse(parts[1].trim());
latitude = double.tryParse(parts[2].trim());
}

// Create message if there is new line character
// String dataString = String.fromCharCodes(buffer);
int index = buffer.indexOf(13);
if (~index != 0) {
setState(() {
Expand All @@ -199,6 +243,8 @@ class ChatPageState extends State<ChatPage> {
? _messageBuffer.substring(
0, _messageBuffer.length - backspacesCounter)
: _messageBuffer + dataString.substring(0, index),
longitude: longitude,
latitude: latitude,
),
);
_messageBuffer = dataString.substring(index);
Expand All @@ -217,7 +263,7 @@ class ChatPageState extends State<ChatPage> {

if (text.isNotEmpty) {
try {
connection!.output.add(Uint8List.fromList(utf8.encode(text + "\r\n")));
connection!.output.add(Uint8List.fromList(utf8.encode("$text\r\n")));
await connection!.output.allSent;

setState(() {
Expand Down
18 changes: 18 additions & 0 deletions lib/modules/bluetooth/MainPage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import './BackgroundCollectingTask.dart';
import './ChatPage.dart';
import './DiscoveryPage.dart';
import './SelectBondedDevicePage.dart';
import 'package:url_launcher/url_launcher.dart';

// import './helpers/LineChart.dart';

Expand Down Expand Up @@ -114,6 +115,12 @@ class MainPageState extends State<MainPage> {
});
},
),
ListTile(
title: const Text("Teste google maps"),
onTap: () async {
await openGoogleMaps(48.8584, 2.2945);
},
),
ListTile(
title: const Text('Bluetooth status'),
subtitle: Text(_bluetoothState.toString()),
Expand Down Expand Up @@ -323,6 +330,17 @@ class MainPageState extends State<MainPage> {
);
}

Future<void> openGoogleMaps(double latitude, double longitude) async {
final Uri googleMapsUrl = Uri.parse(
'https://www.google.com/maps/search/?api=1&query=$latitude,$longitude');

if (await canLaunchUrl(googleMapsUrl)) {
await launchUrl(googleMapsUrl);
} else {
throw 'Could not open the map.';
}
}

Future<void> _startBackgroundTask(
BuildContext context,
BluetoothDevice server,
Expand Down
104 changes: 104 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,46 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.3.0"
permission_handler:
dependency: "direct main"
description:
name: permission_handler
sha256: bc56bfe9d3f44c3c612d8d393bd9b174eb796d706759f9b495ac254e4294baa5
url: "https://pub.dev"
source: hosted
version: "10.4.5"
permission_handler_android:
dependency: transitive
description:
name: permission_handler_android
sha256: "59c6322171c29df93a22d150ad95f3aa19ed86542eaec409ab2691b8f35f9a47"
url: "https://pub.dev"
source: hosted
version: "10.3.6"
permission_handler_apple:
dependency: transitive
description:
name: permission_handler_apple
sha256: "99e220bce3f8877c78e4ace901082fb29fa1b4ebde529ad0932d8d664b34f3f5"
url: "https://pub.dev"
source: hosted
version: "9.1.4"
permission_handler_platform_interface:
dependency: transitive
description:
name: permission_handler_platform_interface
sha256: "6760eb5ef34589224771010805bea6054ad28453906936f843a8cc4d3a55c4a4"
url: "https://pub.dev"
source: hosted
version: "3.12.0"
permission_handler_windows:
dependency: transitive
description:
name: permission_handler_windows
sha256: cc074aace208760f1eee6aa4fae766b45d947df85bc831cde77009cdb4720098
url: "https://pub.dev"
source: hosted
version: "0.1.3"
petitparser:
dependency: transitive
description:
Expand Down Expand Up @@ -941,6 +981,70 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.2"
url_launcher:
dependency: "direct main"
description:
name: url_launcher
sha256: "21b704ce5fa560ea9f3b525b43601c678728ba46725bab9b01187b4831377ed3"
url: "https://pub.dev"
source: hosted
version: "6.3.0"
url_launcher_android:
dependency: transitive
description:
name: url_launcher_android
sha256: f0c73347dfcfa5b3db8bc06e1502668265d39c08f310c29bff4e28eea9699f79
url: "https://pub.dev"
source: hosted
version: "6.3.9"
url_launcher_ios:
dependency: transitive
description:
name: url_launcher_ios
sha256: e43b677296fadce447e987a2f519dcf5f6d1e527dc35d01ffab4fff5b8a7063e
url: "https://pub.dev"
source: hosted
version: "6.3.1"
url_launcher_linux:
dependency: transitive
description:
name: url_launcher_linux
sha256: e2b9622b4007f97f504cd64c0128309dfb978ae66adbe944125ed9e1750f06af
url: "https://pub.dev"
source: hosted
version: "3.2.0"
url_launcher_macos:
dependency: transitive
description:
name: url_launcher_macos
sha256: "9a1a42d5d2d95400c795b2914c36fdcb525870c752569438e4ebb09a2b5d90de"
url: "https://pub.dev"
source: hosted
version: "3.2.0"
url_launcher_platform_interface:
dependency: transitive
description:
name: url_launcher_platform_interface
sha256: "552f8a1e663569be95a8190206a38187b531910283c3e982193e4f2733f01029"
url: "https://pub.dev"
source: hosted
version: "2.3.2"
url_launcher_web:
dependency: transitive
description:
name: url_launcher_web
sha256: "772638d3b34c779ede05ba3d38af34657a05ac55b06279ea6edd409e323dca8e"
url: "https://pub.dev"
source: hosted
version: "2.3.3"
url_launcher_windows:
dependency: transitive
description:
name: url_launcher_windows
sha256: "49c10f879746271804767cb45551ec5592cdab00ee105c06dddde1a98f73b185"
url: "https://pub.dev"
source: hosted
version: "3.1.2"
usb_serial:
dependency: "direct main"
description:
Expand Down
2 changes: 2 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ dependencies:
flutter_blue_plus: ^1.32.12
flutter_bluetooth_serial: ^0.4.0
scoped_model: ^2.0.0
url_launcher: ^6.1.10
permission_handler: ^10.2.0

dependency_overrides:
firebase_core_platform_interface: 5.1.0
Expand Down

0 comments on commit 206e3b7

Please sign in to comment.