1 /*
2  * Copyright 2024 The Android Open Source Project
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.android.photopicker.core.components
18 
19 import com.android.photopicker.data.model.Group.Album
20 import com.android.photopicker.data.model.Media
21 
22 /**
23  * A wrapper around items in the [MediaGrid] to allow the grid to distinguish between separators and
24  * individual [Media].
25  */
26 sealed class MediaGridItem {
27 
28     /**
29      * Represents a single [Media] (photo or video) in the [MediaGrid].
30      *
31      * @property media The media that this item represents.
32      */
33     data class MediaItem(val media: Media) : MediaGridItem()
34 
35     /**
36      * Represents an [Album] in the [MediaGrid].
37      *
38      * @property album The album that this item represents.
39      */
40     data class AlbumItem(val album: Album) : MediaGridItem()
41 
42     /**
43      * Represents a separator in the [MediaGrid]. (Such as a month separator.)
44      *
45      * @property label A label that can be used to represent this separator in the UI.
46      */
47     data class SeparatorItem(val label: String) : MediaGridItem()
48 
49 
50     /**
51      * Handles operations that requires customized output based on the type of [MediaGridItem].
52      */
53     companion object {
54         /**
55          * Assembles a key for a [MediaGridItem]. This key must be always be stable and unique in
56          * the grid.
57          *
58          * @return a Unique, stable key that represents one item in the grid.
59          */
keyFactorynull60         fun keyFactory(item: MediaGridItem?, index: Int): String {
61             return when (item) {
62                 is MediaItem -> "${item.media.pickerId}"
63                 is SeparatorItem -> "${item.label}_$index"
64                 is AlbumItem -> "${item.album.pickerId}" // check if this should be id or pickerId
65                 null -> "$index"
66             }
67         }
68 
69         /**
70          * Default builder for generating a contentType signature for a grid item.
71          *
72          * ContentType is used to signify re-use of containers to increase the efficiency of the
73          * Grid loading. Each subtype of MediaGridItem should return a distinct value to ensure
74          * optimal re-use.
75          *
76          * @return The contentType signature of the provided item.
77          */
defaultBuildContentTypenull78         fun defaultBuildContentType(item: MediaGridItem?): Int {
79             return when (item) {
80                 is MediaItem -> 1
81                 is SeparatorItem -> 2
82                 is AlbumItem -> 3
83                 null -> 0
84             }
85         }
86     }
87 }
88