Overview
TheDeviceLocationScreen provides functionality for users to retrieve their device’s current location. It displays both human-readable address information and precise GPS coordinates, with features to speak the location aloud using text-to-speech and copy location data to the clipboard.
File Location: com.demodogo.ev_sum_2.ui.location.DeviceLocationScreen.kt:46
Function Signature
Parameters
Callback function invoked when the user clicks the “Volver” (Back) button. Typically navigates back to the previous screen.
State Management
Location Data State
Stores the human-readable address obtained from reverse geocoding. Null if not yet retrieved or unavailable.
Stores the latitude coordinate of the device’s location.
Stores the longitude coordinate of the device’s location.
UI Feedback State
Indicates whether a location fetch operation is currently in progress.
Stores error messages to display to the user (e.g., permission denied, location unavailable).
Stores informational messages to display to the user (e.g., “Ubicación actualizada”, “Copiado al portapapeles”).
Services & Controllers
LocationService
Handles location operations:getLocationWithAddress()- Retrieves GPS coordinates and performs reverse geocoding to get addresshasPermission()- Checks if location permissions are granted
TextToSpeechController
Provides text-to-speech functionality:speak(text: String)- Speaks the provided location information alouddestroy()- Cleans up TTS resources
User Interactions
Retrieving Location
The location fetch process handles permissions and errors gracefully:DeviceLocationScreen.kt:88-105
- Check if location permissions are granted
- If not granted, request permissions
- If granted, fetch location with address
- Display results or error message
Permission Handling
The screen requests location permissions when needed:DeviceLocationScreen.kt:107-119
Text-to-Speech Location
The screen can speak the location information:DeviceLocationScreen.kt:72-77
Copy to Clipboard
Users can copy location data in a formatted string:DeviceLocationScreen.kt:79-86
DeviceLocationScreen.kt:66-70
UI Components
The DeviceLocationScreen uses Material 3 components:- Surface - Root container with background color
- Column - Main vertical layout with spacing
- Card - Display containers for location data, errors, and info messages
- Button - Primary action to obtain location
- OutlinedButton - Secondary actions (speak, copy, back)
- LinearProgressIndicator - Loading state indicator
- Icon - Visual indicators (MyLocation, VolumeUp, ContentCopy)
- Row - Horizontal layout for action buttons
Permissions
The screen requests the following Android permissions:Manifest.permission.ACCESS_FINE_LOCATION- For precise GPS coordinatesManifest.permission.ACCESS_COARSE_LOCATION- For approximate location
The screen accepts either FINE or COARSE location permission. If the user grants only COARSE, the location will be less precise but still functional.
Location Data Display
The location information is displayed in a card:DeviceLocationScreen.kt:128-142
- Dirección - Full address string or ”—” if unavailable
- Coordenadas - Latitude and longitude with 6 decimal places or ”—” if unavailable
Error and Info Messages
Feedback messages are displayed in styled cards:DeviceLocationScreen.kt:146-158
Example Usage
Action Buttons
The screen provides three action buttons:- Obtener ubicación - Fetches current location (disabled while loading)
- Hablar - Speaks location using TTS
- Copiar - Copies formatted location to clipboard
- Volver - Returns to previous screen
DeviceLocationScreen.kt:160-177
Best Practices
- Resource Cleanup - TTS controller is destroyed in
DisposableEffectto prevent memory leaks - Graceful Degradation - Shows coordinates even if address lookup fails
- User Feedback - Clear loading states and error messages
- Accessibility - Text-to-speech support for reading location aloud
- Permission Handling - Gracefully requests and handles permission denials
- State Persistence - Location data preserved with
rememberSaveableacross configuration changes