1 /*
2  * Copyright 2019 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.mock
18 
19 class ContactModel(
20     var filter: String = "",
21     var contacts: List<Contact>,
22     var selected: Contact? = null
23 ) {
24     val filtered
<lambda>null25         get() = contacts.filter { it.name.contains(filter) }
26 
addnull27     fun add(contact: Contact, after: Contact? = null) {
28         val retList = mutableListOf<Contact>().apply { addAll(contacts) }
29         if (after == null) {
30             retList.add(contact)
31         } else {
32             retList.add(find(retList, after) + 1, contact)
33         }
34 
35         contacts = retList
36     }
37 
movenull38     fun move(contact: Contact, after: Contact?) {
39         val retList = mutableListOf<Contact>().apply { addAll(contacts) }
40         if (after == null) {
41             retList.removeAt(find(retList, contact))
42             retList.add(0, contact)
43         } else {
44             retList.removeAt(find(retList, contact))
45             retList.add(find(retList, after) + 1, contact)
46         }
47         contacts = retList
48     }
49 
findnull50     private fun find(list: List<Contact>, contact: Contact): Int {
51         val index = list.indexOf(contact)
52         if (index < 0) error("Contact $contact not found")
53         return index
54     }
55 }
56