diff --git a/lib/src/authentication/authentication_repository.dart b/lib/src/authentication/authentication_repository.dart index f49f4b6..9fecdbe 100644 --- a/lib/src/authentication/authentication_repository.dart +++ b/lib/src/authentication/authentication_repository.dart @@ -176,6 +176,20 @@ class AuthenticationRepository extends GetxController { return city; } + Future getCoordsOfCity(String uid) async { + String coords = ''; + try { + final snapshot = + await FirebaseFirestore.instance.collection('users').doc(uid).get(); + final Map? data = snapshot.data(); + coords = data?['coordsOfCity'] ?? ''; + } catch (e) { + print('Error getting coords of city: $e'); + } + print('Error getting coords of city: $coords'); + return coords; + } + Future getAddress(String uid) async { String address = ''; try { diff --git a/lib/src/screens/city.dart b/lib/src/screens/city.dart index 28cd129..c3fcd35 100644 --- a/lib/src/screens/city.dart +++ b/lib/src/screens/city.dart @@ -17,13 +17,20 @@ final CollectionReference countriesCollection = class City { String? cityName; + String? coordsOfCity; String? stateOfCity; String? countryOfCity; - City({this.cityName, this.stateOfCity, this.countryOfCity}); + City({ + this.cityName, + this.coordsOfCity, + this.stateOfCity, + this.countryOfCity, + }); + @override String toString() { - return "${cityName ?? ""}, ${stateOfCity ?? ""}, ${countryOfCity ?? ""}"; + return "${cityName ?? ""}, ${coordsOfCity ?? ""}, ${stateOfCity ?? ""}, ${countryOfCity ?? ""}"; } } @@ -35,27 +42,27 @@ Future> getCountries() async { for (DocumentSnapshot country in countries.docs) { String countryName = country.id; Map data = country.data() as Map; - Map> states = {}; + Map> states = {}; for (var entry in data.entries) { String key = entry.key; - List value = List.from(entry.value); - states[key] = value; + Map cityData = Map.from(entry.value); + states[key] = cityData; } for (var state in states.entries) { - var citysState = state.value - .map((cityName) => City( - cityName: cityName, - stateOfCity: state.key, - countryOfCity: countryName)) - .toList(); + var citysState = state.value.entries.map((city) => City( + cityName: city.key, + coordsOfCity: city.value, + stateOfCity: state.key, + countryOfCity: countryName, + )); citys.addAll(citysState); } } } catch (e) { - print('xd $e'); + print('xd seqe $e'); } return citys; @@ -98,13 +105,18 @@ class _CityScreenState extends State { } } - Future updateCity(String cityName) async { + Future updateCity(String cityName, String coordsCity) async { try { await FirebaseFirestore.instance .collection('users') .doc(uid) .update({'city': cityName}); + await FirebaseFirestore.instance + .collection('users') + .doc(uid) + .update({'coordsOfCity': coordsCity}); + print('Ciudad actualizada correctamente'); } catch (e) { try { @@ -112,6 +124,11 @@ class _CityScreenState extends State { .collection('users') .doc(uid) .set({'city': cityName}); + + await FirebaseFirestore.instance + .collection('users') + .doc(uid) + .set({'coordsOfCity': coordsCity}); } catch (e) { print('Error al agregar la ciudad: $e'); } @@ -175,7 +192,8 @@ class _CityScreenState extends State { ), ), onTap: () { - updateCity(citys[index].cityName ?? ""); + updateCity(citys[index].cityName ?? "", + citys[index].coordsOfCity ?? ""); Navigator.pop(context, citys[index].cityName ?? ""); }, ); diff --git a/lib/src/screens/service.dart b/lib/src/screens/service.dart index 90c81c3..115feef 100644 --- a/lib/src/screens/service.dart +++ b/lib/src/screens/service.dart @@ -13,7 +13,6 @@ import 'package:prosappco/src/authentication/authentication_repository.dart'; import 'package:prosappco/src/components/drawer_menu.dart'; import 'package:prosappco/src/models/event_model.dart'; import 'package:prosappco/src/models/user_model.dart'; -import 'package:prosappco/src/screens/local_notification.dart'; import 'package:prosappco/src/screens/professional.dart'; import 'package:intl/intl.dart'; import 'package:prosappco/src/screens/profile.dart'; @@ -186,6 +185,10 @@ class _ServiceScreenState extends State { late String lat; late String long; + + double latUser = 0.0; + double lngUser = 0.0; + var coordinates; Future getLocationName(double latitude, double longitude) async { @@ -239,19 +242,24 @@ class _ServiceScreenState extends State { controller: _ubicationController, readOnly: true, onTap: () async { - final dynamic datos = await Navigator.push( + final List datos = await Navigator.push( context, CupertinoPageRoute( builder: (BuildContext context) { - return UbicacionScreen(); + return const UbicacionScreen(); }, ), ); - print(datos); - if (datos != null) { + + if (datos.length == 3) { + final formattedAddress = datos[0]; + final lat = datos[1]; + final lng = datos[2]; + setState(() { - _ubicationController.text = datos; - print(_ubicationController.text); + _ubicationController.text = formattedAddress; + latUser = lat; + lngUser = lng; }); } }, @@ -291,6 +299,8 @@ class _ServiceScreenState extends State { if (ubicacion == 'sitio') { professionalAddress = profesional.realAddress; _ubicationController.text = profesional.realAddress; + latUser = profesional.latitude; + lngUser = profesional.longitude; } }); } @@ -419,8 +429,8 @@ class _ServiceScreenState extends State { professionalId, ubicacion, professionalAddress, - 0.0, - 0.0, + latUser, + lngUser, 'pendiente', professionalTarifa == 0 ? 0 : professionalTarifa, false, diff --git a/lib/src/screens/ubicacion.dart b/lib/src/screens/ubicacion.dart index bb0e895..f36b9db 100644 --- a/lib/src/screens/ubicacion.dart +++ b/lib/src/screens/ubicacion.dart @@ -1,9 +1,10 @@ -import 'dart:convert'; - import 'package:flutter/material.dart'; +import 'dart:convert'; import 'package:prosappco/src/components/network_utility.dart'; import 'package:prosappco/src/components/pop_appbar.dart'; +import '../authentication/authentication_repository.dart'; + class UbicacionScreen extends StatefulWidget { const UbicacionScreen({super.key}); @@ -14,17 +15,34 @@ class UbicacionScreen extends StatefulWidget { class _UbicacionScreenState extends State { List _placesList = []; String selectedPlace = ''; + String _coordsOfCity = '0.0,0.0'; + + @override + void initState() { + super.initState(); + + final uid = AuthenticationRepository.instance.getCurrentUserUid(); + + if (_coordsOfCity == '0.0,0.0') { + AuthenticationRepository.instance + .getCoordsOfCity(uid.toString()) + .then((String s) => setState(() { + _coordsOfCity = s; + })); + } + } void placeAutoComplete(String query) async { Uri uri = Uri.https("admin.prosapp.co", "/autocomplete", { "input": query, + "location": _coordsOfCity, }); String? response = await NetworkUtility.fetchUrl(uri); if (response != null) { setState(() { - _placesList = jsonDecode(response.toString())['predictions']; + _placesList = jsonDecode(response.toString())['results']; }); } } @@ -38,7 +56,7 @@ class _UbicacionScreenState extends State { }, label: 'Tu ubicación'), body: Center( - child: Container( + child: SizedBox( width: 300, child: Column( children: [ @@ -60,10 +78,13 @@ class _UbicacionScreenState extends State { itemBuilder: (context, index) { return ListTile( onTap: () { - Navigator.pop( - context, _placesList[index]['description']); + Navigator.pop(context, [ + _placesList[index]['formatted_address'], + _placesList[index]['geometry']['location']['lat'], + _placesList[index]['geometry']['location']['lng'] + ]); }, - title: Text(_placesList[index]['description']), + title: Text(_placesList[index]['formatted_address']), ); }, ),