Skip to content

Commit 792461f

Browse files
committed
Added safe check
1 parent 6c78651 commit 792461f

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

JetPDFVue/src/main/java/com/pratikk/jetpdfvue/state/VueFilePicker.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,22 @@ class VueFilePicker {
6464
},
6565
restore = {
6666
VueFilePicker().apply {
67-
importFile = (it[0] as String?)?.let { path ->
68-
File(path)
67+
importFile = try {
68+
(it[0] as String?)?.let { path ->
69+
File(path)
70+
}
71+
} catch (_: Exception) {
72+
null
73+
}
74+
if(importFile != null) {
75+
vueFilePickerState = it[1] as VueFilePickerState
76+
} else {
77+
vueFilePickerState = VueFilePickerState.VueFilePickerIdeal
6978
}
70-
vueFilePickerState = it[1] as VueFilePickerState
7179
}
7280
}
7381
)
74-
val UriSaver:Saver<Uri,*> = listSaver(
82+
val UriSaver: Saver<Uri, *> = listSaver(
7583
save = { listOf(VueFilePickerState.VueFilePickerImported(it)) },
7684
restore = {
7785
it[0].uri
@@ -93,15 +101,18 @@ class VueFilePicker {
93101
lazyMessage = { "File Sources cannot be empty" })
94102
val intents = ArrayList<Intent>()
95103
val filterImportState = vueImportSources.toMutableList().let {
96-
if (it.contains(VueImportSources.BASE64) && it.contains(VueImportSources.PDF) && it.contains(VueImportSources.GALLERY)) {
104+
if (it.contains(VueImportSources.BASE64) && it.contains(VueImportSources.PDF) && it.contains(
105+
VueImportSources.GALLERY
106+
)
107+
) {
97108
it.remove(VueImportSources.PDF)
98109
it.remove(VueImportSources.BASE64)
99110
intents.add(base64PdfAndGalleryIntent())
100-
}else if(it.contains(VueImportSources.BASE64) && it.contains(VueImportSources.PDF)){
111+
} else if (it.contains(VueImportSources.BASE64) && it.contains(VueImportSources.PDF)) {
101112
it.remove(VueImportSources.PDF)
102113
it.remove(VueImportSources.BASE64)
103114
intents.add(base64AndPdfIntent())
104-
}else if(it.contains(VueImportSources.PDF) && it.contains(VueImportSources.GALLERY)) {
115+
} else if (it.contains(VueImportSources.PDF) && it.contains(VueImportSources.GALLERY)) {
105116
it.remove(VueImportSources.PDF)
106117
intents.add(pdfAndGalleryIntent())
107118
}
@@ -128,12 +139,15 @@ class VueFilePicker {
128139
fun getLauncher(
129140
interceptResult: suspend (File) -> Unit = {},
130141
onResult: (File) -> Unit = {},
131-
onError:(Exception) -> Unit = {}
142+
onError: (Exception) -> Unit = {}
132143
): ManagedActivityResultLauncher<Intent, ActivityResult> {
133144
val context = LocalContext.current
134145
LaunchedEffect(key1 = vueFilePickerState, block = {
135146
if (vueFilePickerState is VueFilePickerState.VueFilePickerImported && importJob == null) {
136-
importJob = launch(context = coroutineContext + Dispatchers.IO, start = CoroutineStart.LAZY) {
147+
importJob = launch(
148+
context = coroutineContext + Dispatchers.IO,
149+
start = CoroutineStart.LAZY
150+
) {
137151
with((vueFilePickerState as VueFilePickerState.VueFilePickerImported)) {
138152
//Create a temp file using result uri
139153
val file = try {
@@ -264,7 +278,7 @@ class VueFilePicker {
264278
intent.type = "*/*"
265279
intent.putExtra(
266280
Intent.EXTRA_MIME_TYPES,
267-
listOf("application/pdf", "text/plain","image/*").toTypedArray()
281+
listOf("application/pdf", "text/plain", "image/*").toTypedArray()
268282
)
269283
intent.addCategory(Intent.CATEGORY_OPENABLE)
270284
return intent
@@ -280,6 +294,7 @@ class VueFilePicker {
280294
intent.addCategory(Intent.CATEGORY_OPENABLE)
281295
return intent
282296
}
297+
283298
private fun pdfAndGalleryIntent(): Intent {
284299
val intent = Intent(Intent.ACTION_GET_CONTENT)
285300
intent.type = "*/*"

0 commit comments

Comments
 (0)