• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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
18 
19 import android.app.Application
20 import androidx.annotation.MainThread
21 import androidx.lifecycle.ViewModel
22 import androidx.lifecycle.ViewModelProvider
23 import androidx.lifecycle.ViewModelProvider.AndroidViewModelFactory.Companion.APPLICATION_KEY
24 import androidx.lifecycle.viewModelScope
25 import androidx.lifecycle.viewmodel.CreationExtras
26 import com.android.intentresolver.ChooserRequestParameters
27 import com.android.intentresolver.R
28 import kotlinx.coroutines.CoroutineDispatcher
29 import kotlinx.coroutines.Dispatchers
30 import kotlinx.coroutines.plus
31 
32 /** A trivial view model to keep a [PreviewDataProvider] instance over a configuration change */
33 class PreviewViewModel(
34     private val application: Application,
35     private val dispatcher: CoroutineDispatcher = Dispatchers.IO,
36 ) : BasePreviewViewModel() {
37     private var previewDataProvider: PreviewDataProvider? = null
38     private var imageLoader: ImagePreviewImageLoader? = null
39 
40     @MainThread
createOrReuseProvidernull41     override fun createOrReuseProvider(
42         chooserRequest: ChooserRequestParameters
43     ): PreviewDataProvider =
44         previewDataProvider
45             ?: PreviewDataProvider(
46                     viewModelScope + dispatcher,
47                     chooserRequest.targetIntent,
48                     application.contentResolver
49                 )
50                 .also { previewDataProvider = it }
51 
52     @MainThread
createOrReuseImageLoadernull53     override fun createOrReuseImageLoader(): ImageLoader =
54         imageLoader
55             ?: ImagePreviewImageLoader(
56                     viewModelScope + dispatcher,
57                     thumbnailSize =
58                         application.resources.getDimensionPixelSize(
59                             R.dimen.chooser_preview_image_max_dimen
60                         ),
61                     application.contentResolver,
62                     cacheSize = 16
63                 )
64                 .also { imageLoader = it }
65 
66     companion object {
67         val Factory: ViewModelProvider.Factory =
68             object : ViewModelProvider.Factory {
69                 @Suppress("UNCHECKED_CAST")
createnull70                 override fun <T : ViewModel> create(
71                     modelClass: Class<T>,
72                     extras: CreationExtras
73                 ): T = PreviewViewModel(checkNotNull(extras[APPLICATION_KEY])) as T
74             }
75     }
76 }
77