The Daily Image feature displays NASA’s Astronomy Picture of the Day (APOD), showcasing stunning space imagery with detailed explanations. Users can view today’s image or select any specific date to explore past astronomical wonders.
data class NasaResponse( @SerializedName("title") val title: String, @SerializedName("url") val url: String, @SerializedName("date") val date: String, @SerializedName("explanation") val explanation: String, @SerializedName("media_type") val mediaType: String?, @SerializedName("hdurl") val hdUrl: String?, @SerializedName("service_version") val serviceVersion: String?, @SerializedName("copyright") val copyright: String?)fun NasaResponse.toNasaModel(): NasaModel { return NasaModel( title = this.title, url = this.url, date = this.date, explanation = this.explanation )}
@HiltViewModelclass DailyImageViewModel @Inject constructor( private val nasaRepository: NasaRepository, private val firebaseAuth: FirebaseAuth, private val firebaseDatabase: FirebaseDatabase) : ViewModel() { private val _dailyImage = MutableStateFlow<NasaModel?>(null) val dailyImage: StateFlow<NasaModel?> = _dailyImage private val _errorMessage = MutableStateFlow<String?>(null) val errorMessage: StateFlow<String?> = _errorMessage private val _isLoading = MutableStateFlow(false) val isLoading: StateFlow<Boolean> = _isLoading private val _isFavorite = MutableStateFlow<Boolean>(false) val isFavorite: StateFlow<Boolean> = _isFavorite /** * Load the daily image * @param date Optional parameter to load image from a specific date */ fun loadDailyImage(date: String? = null) { viewModelScope.launch { _isLoading.value = true try { val result = nasaRepository.getImageOfTheDay(date = date) _dailyImage.value = result _errorMessage.value = null } catch (e: Exception) { _errorMessage.value = "Sin conexión a internet. Conéctate a una red Wi-Fi o habilita datos móviles para ver las imágenes" _dailyImage.value = null } finally { _isLoading.value = false } } }}
val dailyImage by dailyImageViewModel.dailyImage.collectAsState()val isLoading by dailyImageViewModel.isLoading.collectAsState()val errorMessage by dailyImageViewModel.errorMessage.collectAsState()