|
| 1 | +/* |
| 2 | + * Copyright 2024 Esri |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.arcgismaps.toolkit.popup.internal.element.media |
| 18 | + |
| 19 | +import androidx.compose.runtime.Composable |
| 20 | +import androidx.compose.runtime.Immutable |
| 21 | +import androidx.compose.runtime.saveable.Saver |
| 22 | +import androidx.compose.runtime.saveable.rememberSaveable |
| 23 | +import com.arcgismaps.mapping.popup.MediaPopupElement |
| 24 | +import com.arcgismaps.mapping.popup.Popup |
| 25 | +import com.arcgismaps.mapping.popup.PopupMedia |
| 26 | +import com.arcgismaps.mapping.popup.PopupMediaType |
| 27 | +import com.arcgismaps.toolkit.popup.internal.element.state.PopupElementState |
| 28 | + |
| 29 | +/** |
| 30 | + * Represents the state of an [MediaPopupElement] |
| 31 | + */ |
| 32 | +@Immutable |
| 33 | +internal class MediaElementState( |
| 34 | + val description: String, |
| 35 | + val title: String, |
| 36 | + val media: List<PopupMediaState>, |
| 37 | + override val id : Int = createId(), |
| 38 | +) : PopupElementState() { |
| 39 | + |
| 40 | + constructor(mediaPopupElement: MediaPopupElement): this( |
| 41 | + description = mediaPopupElement.description, |
| 42 | + title = mediaPopupElement.title, |
| 43 | + media = mediaPopupElement.media |
| 44 | + .filter { it.type is PopupMediaType.Image } |
| 45 | + .map { PopupMediaState(it) } |
| 46 | + ) |
| 47 | + |
| 48 | + companion object { |
| 49 | + fun Saver( |
| 50 | + element: MediaPopupElement |
| 51 | + ): Saver<MediaElementState, Any> = Saver( |
| 52 | + save = { null }, |
| 53 | + restore = { |
| 54 | + MediaElementState( |
| 55 | + element |
| 56 | + ) |
| 57 | + } |
| 58 | + ) |
| 59 | + |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +@Composable |
| 64 | +internal fun rememberMediaElementState( |
| 65 | + element: MediaPopupElement, |
| 66 | + popup: Popup |
| 67 | +): MediaElementState { |
| 68 | + return rememberSaveable( |
| 69 | + inputs = arrayOf(popup, element), |
| 70 | + saver = MediaElementState.Saver(element) |
| 71 | + ) { |
| 72 | + MediaElementState( |
| 73 | + element |
| 74 | + ) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +/** |
| 79 | + * Represents the state of a [PopupMedia value]. |
| 80 | + */ |
| 81 | +internal class PopupMediaState( |
| 82 | + val title: String, |
| 83 | + val caption: String, |
| 84 | + @Suppress("unused") val refreshInterval: Long, |
| 85 | + @Suppress("unused") val linkUrl: String, |
| 86 | + val sourceUrl: String, |
| 87 | + val type: PopupMediaType |
| 88 | +) { |
| 89 | + constructor(media: PopupMedia) : this( |
| 90 | + title = media.title, |
| 91 | + caption = media.caption, |
| 92 | + refreshInterval = media.imageRefreshInterval, |
| 93 | + linkUrl = media.value?.linkUrl ?: "", |
| 94 | + sourceUrl = media.value?.sourceUrl ?: "", |
| 95 | + type = media.type |
| 96 | + ) |
| 97 | +} |
0 commit comments