diff --git a/lib/core/config/supabase_config.dart b/lib/core/config/supabase_config.dart index e69de29..333c1d9 100644 --- a/lib/core/config/supabase_config.dart +++ b/lib/core/config/supabase_config.dart @@ -0,0 +1,2 @@ +const String SUPABASE_URL = 'https://qckndtzudciejpnwqfzt.supabase.co'; +const String SUPABASE_ANON_KEY = 'sb_secret_2y3a_9qD5nRtZl-41CY-jw_LA-smvxC'; \ No newline at end of file diff --git a/lib/core/supabase_service.dart b/lib/core/supabase_service.dart new file mode 100644 index 0000000..4a3a905 --- /dev/null +++ b/lib/core/supabase_service.dart @@ -0,0 +1,119 @@ +import 'package:supabase_flutter/supabase_flutter.dart'; + +final supabaseClient = Supabase.instance.client; + +class RutasService { + final SupabaseClient _client = supabaseClient; + + // ── Obtener ruta por ID ── + Future?> obtenerRuta(String routeId) async { + try { + final response = await _client + .from('rutas') + .select('*') + .eq('id', routeId) + .single(); + return response; + } catch (e) { + print('Error obtener_ruta: $e'); + return null; + } + } + + // ── Obtener puntos de ruta ── + Future>> obtenerPuntosRuta(String routeId) async { + try { + final response = await _client + .from('puntos_ruta') + .select('*') + .eq('ruta_id', routeId) + .order('orden'); + return response; + } catch (e) { + print('Error obtener_puntos_ruta: $e'); + return []; + } + } + + // ── Obtener truck status ── + Future?> obtenerTruckStatus(String routeId) async { + try { + final response = await _client + .from('truck_status') + .select('*') + .eq('route_id', routeId) + .single(); + return response; + } catch (e) { + print('Error obtener_truck_status: $e'); + return null; + } + } + + // ── Obtener template de notificación ── + Future?> obtenerTemplate(String triggerEvent) async { + try { + final response = await _client + .from('notification_templates') + .select('*') + .eq('trigger_event', triggerEvent) + .single(); + return response; + } catch (e) { + print('Error obtener_template: $e'); + return null; + } + } + + // ── Obtener preferencias de usuario ── + Future?> obtenerPreferencias(String userId) async { + try { + final response = await _client + .from('notification_preferences') + .select('*') + .eq('user_id', userId) + .single(); + return response; + } catch (e) { + print('Error obtener_preferencias: $e'); + return null; + } + } + + // ── Obtener dirección de usuario ── + Future?> obtenerDireccion(int addressId) async { + try { + final response = await _client + .from('addresses') + .select('*') + .eq('id', addressId) + .single(); + return response; + } catch (e) { + print('Error obtener_direccion: $e'); + return null; + } + } + + // ── Guardar notificación ── + Future guardarNotificacion({ + required String tipo, + required String routeId, + required int addressId, + required String mensaje, + int? etaMinutos, + }) async { + try { + await _client.from('notificaciones').insert({ + 'tipo': tipo, + 'ruta_id': routeId, + 'address_id': addressId, + 'mensaje': mensaje, + 'eta_minutos': etaMinutos, + 'creada_en': DateTime.now().toIso8601String(), + }); + } catch (e) { + print('Error guardar_notificacion: $e'); + } + } +} diff --git a/lib/main.dart b/lib/main.dart index 244a702..28b2f80 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,7 +1,16 @@ import 'package:flutter/material.dart'; +import 'package:supabase_flutter/supabase_flutter.dart'; +import 'core/config/supabase_config.dart'; -void main() { - runApp(const MyApp()); +void main() async { // ← Cambia a async + WidgetsFlutterBinding.ensureInitialized(); // ← NUEVA + + await Supabase.initialize( // ← NUEVA (3 líneas) + url: SUPABASE_URL, + anonKey: SUPABASE_ANON_KEY, + ); + + runApp(const MyApp()); // ← Esta sí estaba } class MyApp extends StatelessWidget { diff --git a/pubspec.yaml b/pubspec.yaml index 356e262..76154c9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,6 +34,9 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 + supabase_flutter: ^2.5.0 + riverpod: ^2.6.1 + dio: ^5.3.1 dev_dependencies: flutter_test: