improve detekt score

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-04-28 21:29:54 +02:00
parent eaab887765
commit 5c662427f8
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
3 changed files with 27 additions and 41 deletions

View File

@ -74,9 +74,7 @@ class SharedItemsAdapter : RecyclerView.Adapter<SharedItemsAdapter.ViewHolder>()
val fileViewerUtils = FileViewerUtils(it.context, currentItem.userEntity) val fileViewerUtils = FileViewerUtils(it.context, currentItem.userEntity)
fileViewerUtils.openFile( fileViewerUtils.openFile(
currentItem.id, FileViewerUtils.FileInfo(currentItem.id, currentItem.name, currentItem.fileSize),
currentItem.name,
currentItem.fileSize,
currentItem.path, currentItem.path,
currentItem.link, currentItem.link,
currentItem.mimeType, currentItem.mimeType,

View File

@ -75,17 +75,11 @@ class SharedItemsListAdapter : RecyclerView.Adapter<SharedItemsListAdapter.ViewH
val fileViewerUtils = FileViewerUtils(it.context, currentItem.userEntity) val fileViewerUtils = FileViewerUtils(it.context, currentItem.userEntity)
fileViewerUtils.openFile( fileViewerUtils.openFile(
currentItem.id, FileViewerUtils.FileInfo(currentItem.id, currentItem.name, currentItem.fileSize),
currentItem.name,
currentItem.fileSize,
currentItem.path, currentItem.path,
currentItem.link, currentItem.link,
currentItem.mimeType, currentItem.mimeType,
FileViewerUtils.ProgressUi( FileViewerUtils.ProgressUi(holder.binding.progressBar, null, holder.binding.fileImage)
holder.binding.progressBar,
null,
holder.binding.fileImage
)
) )
} }
} }

View File

@ -71,9 +71,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
val fileSize = Integer.valueOf(size) val fileSize = Integer.valueOf(size)
openFile( openFile(
fileId, FileInfo(fileId, fileName, fileSize),
fileName,
fileSize,
path, path,
link, link,
mimetype, mimetype,
@ -82,25 +80,21 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
} }
fun openFile( fun openFile(
fileId: String, fileInfo: FileInfo,
fileName: String,
fileSize: Int,
path: String, path: String,
link: String, link: String,
mimetype: String, mimetype: String,
progressUi: ProgressUi progressUi: ProgressUi
) { ) {
if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileName)) { if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileInfo.fileName)) {
openOrDownloadFile( openOrDownloadFile(
fileName, fileInfo,
fileId,
path, path,
fileSize,
mimetype, mimetype,
progressUi progressUi
) )
} else { } else {
openFileInFilesApp(link, fileId) openFileInFilesApp(link, fileInfo.fileId)
} }
} }
@ -113,22 +107,18 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
} }
private fun openOrDownloadFile( private fun openOrDownloadFile(
fileName: String, fileInfo: FileInfo,
fileId: String,
path: String, path: String,
fileSize: Int,
mimetype: String, mimetype: String,
progressUi: ProgressUi progressUi: ProgressUi
) { ) {
val file = File(context.cacheDir, fileName) val file = File(context.cacheDir, fileInfo.fileName)
if (file.exists()) { if (file.exists()) {
openFileByMimetype(fileName!!, mimetype!!) openFileByMimetype(fileInfo.fileName!!, mimetype!!)
} else { } else {
downloadFileToCache( downloadFileToCache(
fileName, fileInfo,
fileId,
path, path,
fileSize,
mimetype, mimetype,
progressUi progressUi
) )
@ -263,26 +253,24 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
@SuppressLint("LongLogTag") @SuppressLint("LongLogTag")
private fun downloadFileToCache( private fun downloadFileToCache(
fileName: String, fileInfo: FileInfo,
fileId: String,
path: String, path: String,
fileSize: Int,
mimetype: String, mimetype: String,
progressUi: ProgressUi progressUi: ProgressUi
) { ) {
// check if download worker is already running // check if download worker is already running
val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId!!) val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileInfo.fileId!!)
try { try {
for (workInfo in workers.get()) { for (workInfo in workers.get()) {
if (workInfo.state == WorkInfo.State.RUNNING || workInfo.state == WorkInfo.State.ENQUEUED) { if (workInfo.state == WorkInfo.State.RUNNING || workInfo.state == WorkInfo.State.ENQUEUED) {
Log.d(TAG, "Download worker for $fileId is already running or scheduled") Log.d(TAG, "Download worker for $fileInfo.fileId is already running or scheduled")
return return
} }
} }
} catch (e: ExecutionException) { } catch (e: ExecutionException) {
Log.e(TAG, "Error when checking if worker already exsists", e) Log.e(TAG, "Error when checking if worker already exists", e)
} catch (e: InterruptedException) { } catch (e: InterruptedException) {
Log.e(TAG, "Error when checking if worker already exsists", e) Log.e(TAG, "Error when checking if worker already exists", e)
} }
val downloadWorker: OneTimeWorkRequest val downloadWorker: OneTimeWorkRequest
val data: Data = Data.Builder() val data: Data = Data.Builder()
@ -292,20 +280,20 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
DownloadFileToCacheWorker.KEY_ATTACHMENT_FOLDER, DownloadFileToCacheWorker.KEY_ATTACHMENT_FOLDER,
CapabilitiesUtil.getAttachmentFolder(userEntity) CapabilitiesUtil.getAttachmentFolder(userEntity)
) )
.putString(DownloadFileToCacheWorker.KEY_FILE_NAME, fileName) .putString(DownloadFileToCacheWorker.KEY_FILE_NAME, fileInfo.fileName)
.putString(DownloadFileToCacheWorker.KEY_FILE_PATH, path) .putString(DownloadFileToCacheWorker.KEY_FILE_PATH, path)
.putInt(DownloadFileToCacheWorker.KEY_FILE_SIZE, fileSize) .putInt(DownloadFileToCacheWorker.KEY_FILE_SIZE, fileInfo.fileSize)
.build() .build()
downloadWorker = OneTimeWorkRequest.Builder(DownloadFileToCacheWorker::class.java) downloadWorker = OneTimeWorkRequest.Builder(DownloadFileToCacheWorker::class.java)
.setInputData(data) .setInputData(data)
.addTag(fileId) .addTag(fileInfo.fileId)
.build() .build()
WorkManager.getInstance().enqueue(downloadWorker) WorkManager.getInstance().enqueue(downloadWorker)
progressUi.progressBar?.visibility = View.VISIBLE progressUi.progressBar?.visibility = View.VISIBLE
WorkManager.getInstance(context).getWorkInfoByIdLiveData(downloadWorker.id) WorkManager.getInstance(context).getWorkInfoByIdLiveData(downloadWorker.id)
.observeForever { workInfo: WorkInfo? -> .observeForever { workInfo: WorkInfo? ->
updateViewsByProgress( updateViewsByProgress(
fileName, fileInfo.fileName,
mimetype, mimetype,
workInfo!!, workInfo!!,
progressUi progressUi
@ -392,6 +380,12 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
val previewImage: SimpleDraweeView val previewImage: SimpleDraweeView
) )
data class FileInfo(
val fileId: String,
val fileName: String,
val fileSize: Int
)
companion object { companion object {
private val TAG = FileViewerUtils::class.simpleName private val TAG = FileViewerUtils::class.simpleName
const val KEY_ID = "id" const val KEY_ID = "id"