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 @file:OptIn(ExperimentalStdlibApi::class)
17 
18 package androidx.lifecycle
19 
20 import androidx.annotation.MainThread
21 import androidx.lifecycle.viewmodel.internal.ViewModelImpl
22 import kotlinx.coroutines.CoroutineScope
23 
24 public actual abstract class ViewModel {
25 
26     private val impl: ViewModelImpl
27 
28     public actual constructor() {
29         impl = ViewModelImpl()
30     }
31 
32     public actual constructor(viewModelScope: CoroutineScope) {
33         impl = ViewModelImpl(viewModelScope)
34     }
35 
36     public actual constructor(vararg closeables: AutoCloseable) {
37         impl = ViewModelImpl(*closeables)
38     }
39 
40     public actual constructor(viewModelScope: CoroutineScope, vararg closeables: AutoCloseable) {
41         impl = ViewModelImpl(viewModelScope, *closeables)
42     }
43 
onClearednull44     protected actual open fun onCleared() {}
45 
46     @MainThread
clearnull47     internal actual fun clear() {
48         impl.clear()
49         onCleared()
50     }
51 
addCloseablenull52     public actual fun addCloseable(key: String, closeable: AutoCloseable) {
53         impl.addCloseable(key, closeable)
54     }
55 
addCloseablenull56     public actual open fun addCloseable(closeable: AutoCloseable) {
57         impl.addCloseable(closeable)
58     }
59 
getCloseablenull60     public actual fun <T : AutoCloseable> getCloseable(key: String): T? = impl.getCloseable(key)
61 }
62