-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathExternalFilesViewModel.kt
More file actions
44 lines (38 loc) · 1.45 KB
/
ExternalFilesViewModel.kt
File metadata and controls
44 lines (38 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package com.hicham.activityresult.files
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import com.hicham.activityresult.BaseViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import dev.hichamboushaba.suspendactivityresult.ActivityResultManager
import dev.hichamboushaba.suspendactivityresult.getContent
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
@HiltViewModel
class ExternalFilesViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
private val activityResultManager: ActivityResultManager
) : BaseViewModel() {
companion object {
private const val IMAGE_URI_KEY = "image"
private const val IS_WAITING_FOR_FILE_KEY = "is_waiting_for_key"
}
private val _image = MutableStateFlow<Uri?>(savedStateHandle.get(IMAGE_URI_KEY))
val image = _image.asStateFlow()
init {
if (savedStateHandle.get<Boolean>(IS_WAITING_FOR_FILE_KEY) == true) {
pickFile()
}
}
fun pickFile() {
viewModelScope.launch {
savedStateHandle.set(IS_WAITING_FOR_FILE_KEY, true)
val uri = activityResultManager.getContent("image/*")
savedStateHandle.set(IMAGE_URI_KEY, uri)
savedStateHandle.set(IS_WAITING_FOR_FILE_KEY, false)
_image.value = uri
}
}
}