1 /* 2 * Copyright 2021 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 androidx.compose.runtime.saveable 18 19 /** 20 * The [Saver] implementation which allows to represent your [Original] class as a list of 21 * [Saveable] values. 22 * 23 * What types can be saved is defined by [SaveableStateRegistry], by default everything which can be 24 * stored in the Bundle class can be saved. 25 * 26 * You can use it as a parameter for [rememberSaveable]. 27 * 28 * @sample androidx.compose.runtime.saveable.samples.ListSaverSample 29 */ listSavernull30fun <Original, Saveable> listSaver( 31 save: SaverScope.(value: Original) -> List<Saveable>, 32 restore: (list: List<Saveable>) -> Original? 33 ): Saver<Original, Any> = 34 @Suppress("UNCHECKED_CAST") 35 Saver( 36 save = { 37 val list = save(it) 38 for (index in list.indices) { 39 val item = list[index] 40 if (item != null) { 41 require(canBeSaved(item)) { "item at index $index can't be saved: $item" } 42 } 43 } 44 if (list.isNotEmpty()) ArrayList(list) else null 45 }, 46 restore = restore as (Any) -> Original? 47 ) 48