Introducción a la Integración Nativa en Android

1

Integra APIs Nativas en Android

2

Creación proyecto base

Google Maps SDK

3

Cómo crear una API key para Google Maps en Google Cloud

4

Creación de marcadores en Google Maps con Jetpack Compose

5

Cómo dibujar rutas en mapas usando polilíneas en Jetpack Compose

6

Cómo enfocar automáticamente mapas usando Camera Update Factory

Quiz: Google Maps SDK

Servicios de Localización

7

Uso de Flows en Kotlin para Controlar Intervalos de Tiempo y Emisión de Datos

8

Cómo simular ubicación en emuladores y dispositivos reales

9

Creación de Modelos y Cálculos de Localización con Clean Architecture

10

Implementación de Localización en Android Usando Flows

11

Inyección de dependencia para seguimiento de localización en Android

12

Uso de StateFlows para rastrear ubicación en aplicaciones Android

13

Location Tracker

14

Implementación de Location Tracker con Inyección de Dependencias

Quiz: Servicios de Localización

Integración Maps con Localización

15

Integración de mapas dinámicos con CameraPositionState en Android

16

Creación y uso de polilíneas en mapas con datos reales

17

Creación de una pantalla de mapa con Intents y estados en Jetpack Compose

18

Creación de un ViewModel para Seguimiento de Localización en Android

Quiz: Integración Maps con Localización

Manejo de permisos

19

Gestión de permisos en Android para localización, cámara y notificaciones

20

Cómo implementar diálogos para solicitar permisos en Android

21

Manejo de permisos de localización y notificación en Android

22

Cómo gestionar permisos en Android con Jetpack Compose

Quiz: Manejo de permisos

Integración cámara

23

Integración de cámara en Android con Photo Handler y manejo de permisos

24

Convierte Bitmaps a ByteArrays en Android con Kotlin

25

Creación de intents y estados UI para cámara en Android con Kotlin

26

Implementación de funciones clave en ViewModel para cámara Android

27

Integrar CámaraX en Jetpack Compose para Android

28

Captura y previsualización de fotos en Android con Jetpack Compose

29

Cómo Mostrar Fotos en Marcadores de Ubicación en Mapas con Jetpack Compose

Quiz: Integración cámara

Servicios en Android

30

Implementación de servicios en Android: normal services y foreground services

31

Implementar Foreground Services en Android para Persistencia en Segundo Plano

Quiz: Servicios en Android

Transmisiones en Android (Broadcast)

32

Implementación de BroadcastReceiver en Android para Escuchar Eventos del Sistema

33

Pruebas finales y cierre

You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
14 Hrs
51 Min
43 Seg

Creación de intents y estados UI para cámara en Android con Kotlin

25/33
Resources

Working effectively with the logic of a camera screen in Android requires clearly defining intents and states of the user interface (UI). In this content, we will learn how to create and manipulate these essential elements using Kotlin and its ViewModel, facilitating actions such as taking photos, requesting permissions and previewing.

What are intents and why are they important in our camera app?

Intents are specific actions that tell our application what task to execute, in this case related to the device's camera. To organize them, we use a sealed interface in Kotlin. The main intents to consider are:

  • PhotoTaken: notifies us when a photo has been taken and contains its information in a ByteArray. The use of the Equals and HashCode functions generated by Kotlin allows us to identify if two photos are exactly the same image.
  • SubmitCameraPermissionInfo: indicates if thepermission to access the camera was accepted by the user and if the app should explain why (rationale). This is pointed through boolean values:
  • AcceptedCameraPermission: acceptance of the permission by the user.
  • shouldShowCameraRationally: need to show a clear rationale to the user before asking for permission.
  • AdditionalDataObjects:
  • PhotoSaved: indicates that the user accepted and saved the photo.
  • CancelPhoto: describes the action of canceling the preview or capture of the photo.

How to create and manage the state of the user interface?

Managing the state of our UI involves clearly defining what condition the interface is in at any given time. We define CameraUIState using a DataClass, which includes:

  • isInPreviewMode: state that defines whether the screen currently shows a preview after taking a photo.
  • LastSavedPhoto: reference to the file representing the last saved photo.
  • ShowCameraRationals: boolean that indicates if we should show permission rationals to the user.

We initialize these values with useful default conditions:

  • isInPreviewMode: 0 (camera is ready to take a picture).
  • ShowCameraRationals: false (by default we do not show rationals).

How to implement the ViewModel using Hilt and why do it this way?

The ViewModel is an essential component in Android that maintains and manages the interface logic. We use Hilt to facilitate dependency injection, thus benefiting from better memory management and code structure. In our example, the ViewModel named CameraViewModel has the following key features:

  • It is annotated with @HiltViewModel and extends from the ViewModel itself.
  • Two essential components are injected via constructor:
  • PhotoHandler: handles photo fetching and saving.
  • LocationTracker: provides the location associated to each captured photo.

Inside the ViewModel as well:

  • We define a mutable state exposed via a MutableStateFlow and asStateFlow for the UI.
  • We expose the PreviewPhoto preview photo in the UI via a StateFlow, using the stateIn operator, which holds the value for five seconds after unsubscribing.

How to manage intents in our ViewModel?

To manage actions derived from the defined intents, we use a function that handles these events using the when structure:

fun onIntentReceived(intent: CameraIntent)  = when(intent)  {    is CameraIntent.SubmitCameraPermissionInfo ->  {         uiState.value  =  uiState.value.copy(            showCameraRationales  = intent.shouldShowCameraRationally,            permissionGranted  = intent.acceptedCameraPermission        )    }}    // Here we add more actions according to the available intents}

This structure allows you to easily react to events, appropriately modifying the state of the interface accordingly.

Do you have any specific questions about these components or how to use them in your Android projects? Leave us a comment and keep learning!

Contributions 0

Questions 0

Sort by:

Want to see more contributions, questions and answers from the community?