1 /*
2 * Copyright (C) 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.intentresolver.contentpreview.payloadtoggle.domain.model
18
19 /** A window of data loaded from a cursor. */
20 data class LoadedWindow<K, V>(
21 /** The index position of the item that should be displayed initially. */
22 val startIndex: Int,
23 /** First cursor page index loaded within this window. */
24 val firstLoadedPageNum: Int,
25 /** Last cursor page index loaded within this window. */
26 val lastLoadedPageNum: Int,
27 /** Keys of cursor data within this window, grouped by loaded page. */
28 val pages: List<Set<K>>,
29 /** Merged set of all cursor data within this window. */
30 val merged: Map<K, V>,
31 /** Is there more data to the left of this window? */
32 val hasMoreLeft: Boolean,
33 /** Is there more data to the right of this window? */
34 val hasMoreRight: Boolean,
35 )
36
37 /** Number of loaded pages stored within this [LoadedWindow]. */
38 val LoadedWindow<*, *>.numLoadedPages: Int
39 get() = (lastLoadedPageNum - firstLoadedPageNum) + 1
40
41 /** Inserts [newPage] to the right, and removes the leftmost page from the window. */
shiftWindowRightnull42 fun <K, V> LoadedWindow<K, V>.shiftWindowRight(
43 newPage: Map<K, V>,
44 hasMore: Boolean,
45 ): LoadedWindow<K, V> =
46 LoadedWindow(
47 startIndex = startIndex - newPage.size,
48 firstLoadedPageNum = firstLoadedPageNum + 1,
49 lastLoadedPageNum = lastLoadedPageNum + 1,
50 pages = pages.drop(1) + listOf(newPage.keys),
51 merged =
52 buildMap {
53 putAll(merged)
54 pages.first().forEach(::remove)
55 putAll(newPage)
56 },
57 hasMoreLeft = true,
58 hasMoreRight = hasMore,
59 )
60
61 /** Inserts [newPage] to the right, increasing the size of the window to accommodate it. */
expandWindowRightnull62 fun <K, V> LoadedWindow<K, V>.expandWindowRight(
63 newPage: Map<K, V>,
64 hasMore: Boolean,
65 ): LoadedWindow<K, V> =
66 LoadedWindow(
67 startIndex = startIndex,
68 firstLoadedPageNum = firstLoadedPageNum,
69 lastLoadedPageNum = lastLoadedPageNum + 1,
70 pages = pages + listOf(newPage.keys),
71 merged = merged + newPage,
72 hasMoreLeft = hasMoreLeft,
73 hasMoreRight = hasMore,
74 )
75
76 /** Inserts [newPage] to the left, and removes the rightmost page from the window. */
77 fun <K, V> LoadedWindow<K, V>.shiftWindowLeft(
78 newPage: Map<K, V>,
79 hasMore: Boolean,
80 ): LoadedWindow<K, V> =
81 LoadedWindow(
82 startIndex = startIndex + newPage.size,
83 firstLoadedPageNum = firstLoadedPageNum - 1,
84 lastLoadedPageNum = lastLoadedPageNum - 1,
85 pages = listOf(newPage.keys) + pages.dropLast(1),
86 merged =
87 buildMap {
88 putAll(newPage)
89 putAll(merged - pages.last())
90 },
91 hasMoreLeft = hasMore,
92 hasMoreRight = true,
93 )
94
95 /** Inserts [newPage] to the left, increasing the size olf the window to accommodate it. */
expandWindowLeftnull96 fun <K, V> LoadedWindow<K, V>.expandWindowLeft(
97 newPage: Map<K, V>,
98 hasMore: Boolean,
99 ): LoadedWindow<K, V> =
100 LoadedWindow(
101 startIndex = startIndex + newPage.size,
102 firstLoadedPageNum = firstLoadedPageNum - 1,
103 lastLoadedPageNum = lastLoadedPageNum,
104 pages = listOf(newPage.keys) + pages,
105 merged = newPage + merged,
106 hasMoreLeft = hasMore,
107 hasMoreRight = hasMoreRight,
108 )
109