Skip to content

Commit 63edcc0

Browse files
author
Jan Guegel
committed
fix detekt complexMethod by using smaller functions
1 parent e7eda60 commit 63edcc0

File tree

1 file changed

+55
-57
lines changed

1 file changed

+55
-57
lines changed

app/src/main/kotlin/org/fossify/filemanager/activities/DecompressActivity.kt

Lines changed: 55 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -214,78 +214,76 @@ class DecompressActivity : SimpleActivity() {
214214
}
215215

216216
private fun fillAllListItems(uri: Uri, callback: () -> Unit) = ensureBackgroundThread {
217-
val inputStream = try {
218-
contentResolver.openInputStream(uri)
217+
val zipStream = openZipInputStream(uri) ?: return@ensureBackgroundThread
218+
processZipEntries(zipStream)
219+
runOnUiThread { binding.progressIndicator.hide() }
220+
callback()
221+
}
222+
223+
private fun openZipInputStream(uri: Uri): ZipInputStream? {
224+
return try {
225+
val inputStream = contentResolver.openInputStream(uri)
226+
ZipInputStream(BufferedInputStream(inputStream)).apply {
227+
password?.let { setPassword(it.toCharArray()) }
228+
}
219229
} catch (e: Exception) {
220230
showErrorToast(e)
221-
return@ensureBackgroundThread
231+
null
222232
}
233+
}
223234

224-
val zipInputStream = ZipInputStream(BufferedInputStream(inputStream))
225-
if (password != null) {
226-
zipInputStream.setPassword(password?.toCharArray())
227-
}
235+
private fun processZipEntries(zipInputStream: ZipInputStream) {
228236
var zipEntry: LocalFileHeader?
229237
while (true) {
230238
try {
231-
zipEntry = zipInputStream.nextEntry
232-
} catch (passwordException: ZipException) {
233-
if (passwordException.type == Type.WRONG_PASSWORD) {
234-
if (password != null) {
235-
toast(getString(R.string.invalid_password))
236-
passwordDialog?.clearPassword()
237-
} else {
238-
runOnUiThread {
239-
askForPassword()
240-
}
241-
}
242-
return@ensureBackgroundThread
243-
} else {
244-
break
245-
}
246-
} catch (ignored: Exception) {
247-
break
248-
}
249-
250-
if (zipEntry == null) {
239+
zipEntry = zipInputStream.nextEntry ?: break
240+
} catch (e: ZipException) {
241+
handleZipException(e)
251242
break
252243
}
244+
handleZipEntry(zipEntry)
245+
}
246+
}
253247

254-
// Show progress bar only after password dialog is dismissed.
255-
runOnUiThread {
256-
if (binding.progressIndicator.isGone()) {
257-
binding.progressIndicator.show()
258-
}
259-
}
260-
261-
if (passwordDialog != null) {
262-
passwordDialog?.dismiss(notify = false)
263-
passwordDialog = null
264-
}
265-
266-
val lastModified = zipEntry.lastModifiedTime
267-
val filename = zipEntry.fileName.removeSuffix("/")
268-
if (allFiles.none { it.mPath == filename }) {
269-
allFiles.add(
270-
ListItem(
271-
mPath = filename,
272-
mName = filename.getFilenameFromPath(),
273-
mIsDirectory = zipEntry.isDirectory,
274-
mChildren = 0,
275-
mSize = 0L,
276-
mModified = lastModified,
277-
isSectionTitle = false,
278-
isGridTypeDivider = false
279-
)
280-
)
248+
private fun handleZipException(e: ZipException) {
249+
if (e.type == Type.WRONG_PASSWORD) {
250+
if (password != null) {
251+
toast(getString(R.string.invalid_password))
252+
passwordDialog?.clearPassword()
253+
} else {
254+
runOnUiThread { askForPassword() }
281255
}
256+
} else {
257+
showErrorToast(e)
282258
}
259+
}
283260

261+
private fun handleZipEntry(zipEntry: LocalFileHeader) {
284262
runOnUiThread {
285-
binding.progressIndicator.hide()
263+
if (binding.progressIndicator.isGone()) {
264+
binding.progressIndicator.show()
265+
}
266+
}
267+
passwordDialog?.dismiss(notify = false)
268+
passwordDialog = null
269+
270+
val filename = zipEntry.fileName.removeSuffix("/")
271+
val lastModified = zipEntry.lastModifiedTime
272+
273+
if (allFiles.none { it.mPath == filename }) {
274+
allFiles.add(
275+
ListItem(
276+
mPath = filename,
277+
mName = filename.getFilenameFromPath(),
278+
mIsDirectory = zipEntry.isDirectory,
279+
mChildren = 0,
280+
mSize = 0L,
281+
mModified = lastModified,
282+
isSectionTitle = false,
283+
isGridTypeDivider = false
284+
)
285+
)
286286
}
287-
288-
callback()
289287
}
290288

291289
private fun askForPassword() {

0 commit comments

Comments
 (0)