improve detekt score

Signed-off-by: Andy Scherzinger <info@andy-scherzinger.de>
This commit is contained in:
Andy Scherzinger 2022-04-28 21:02:19 +02:00
parent 941d35e03f
commit 48c7ccb4b0
No known key found for this signature in database
GPG Key ID: 6CADC7E3523C308B
7 changed files with 76 additions and 65 deletions

View File

@ -70,7 +70,7 @@ class SharedItemsActivity : AppCompatActivity() {
adapter.authHeader = it.authHeader adapter.authHeader = it.authHeader
binding.imageRecycler.adapter = adapter binding.imageRecycler.adapter = adapter
val layoutManager = GridLayoutManager(this, 4) val layoutManager = GridLayoutManager(this, SPAN_COUNT)
binding.imageRecycler.layoutManager = layoutManager binding.imageRecycler.layoutManager = layoutManager
} else { } else {
val adapter = SharedItemsListAdapter() val adapter = SharedItemsListAdapter()
@ -164,5 +164,6 @@ class SharedItemsActivity : AppCompatActivity() {
const val TAB_LOCATION = "location" const val TAB_LOCATION = "location"
const val TAB_DECKCARD = "deckcard" const val TAB_DECKCARD = "deckcard"
const val TAB_OTHER = "other" const val TAB_OTHER = "other"
const val SPAN_COUNT: Int = 4
} }
} }

View File

@ -80,9 +80,11 @@ class SharedItemsAdapter : RecyclerView.Adapter<SharedItemsAdapter.ViewHolder>()
currentItem.path, currentItem.path,
currentItem.link, currentItem.link,
currentItem.mimeType, currentItem.mimeType,
holder.binding.progressBar, FileViewerUtils.ProgressUi(
null, holder.binding.progressBar,
it as SimpleDraweeView null,
it as SimpleDraweeView
)
) )
} }
} }

View File

@ -81,9 +81,11 @@ class SharedItemsListAdapter : RecyclerView.Adapter<SharedItemsListAdapter.ViewH
currentItem.path, currentItem.path,
currentItem.link, currentItem.link,
currentItem.mimeType, currentItem.mimeType,
holder.binding.progressBar, FileViewerUtils.ProgressUi(
null, holder.binding.progressBar,
holder.binding.fileImage null,
holder.binding.fileImage
)
) )
} }
} }

View File

@ -181,9 +181,12 @@ public abstract class MagicPreviewMessageViewHolder extends MessageHolders.Incom
} }
if (message.activeUser != null && message.activeUser.getUsername() != null && message.activeUser.getBaseUrl() != null) { if (message.activeUser != null && message.activeUser.getUsername() != null && message.activeUser.getBaseUrl() != null) {
clickView.setOnClickListener(v -> { clickView.setOnClickListener(v ->
fileViewerUtils.openFile(message, progressBar, getMessageText(), image); fileViewerUtils.openFile(
}); message,
new FileViewerUtils.ProgressUi(progressBar, getMessageText(), image)
)
);
clickView.setOnLongClickListener(l -> { clickView.setOnLongClickListener(l -> {
onMessageViewLongClick(message); onMessageViewLongClick(message);
@ -197,9 +200,7 @@ public abstract class MagicPreviewMessageViewHolder extends MessageHolders.Incom
Objects.requireNonNull(message.getSelectedIndividualHashMap().get(MagicPreviewMessageViewHolder.KEY_NAME)), Objects.requireNonNull(message.getSelectedIndividualHashMap().get(MagicPreviewMessageViewHolder.KEY_NAME)),
Objects.requireNonNull(message.getSelectedIndividualHashMap().get(MagicPreviewMessageViewHolder.KEY_ID)), Objects.requireNonNull(message.getSelectedIndividualHashMap().get(MagicPreviewMessageViewHolder.KEY_ID)),
Objects.requireNonNull(message.getSelectedIndividualHashMap().get(MagicPreviewMessageViewHolder.KEY_MIMETYPE)), Objects.requireNonNull(message.getSelectedIndividualHashMap().get(MagicPreviewMessageViewHolder.KEY_MIMETYPE)),
progressBar, new FileViewerUtils.ProgressUi(progressBar, getMessageText(), image));
getMessageText(),
image);
} else if (message.getMessageType() == ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE) { } else if (message.getMessageType() == ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE) {
getMessageText().setText("GIPHY"); getMessageText().setText("GIPHY");

View File

@ -15,10 +15,6 @@ import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class) @AutoInjector(NextcloudTalkApplication::class)
class SharedItemsRepository { class SharedItemsRepository {
companion object {
private val TAG = SharedItemsRepository::class.simpleName
}
var parameters: Parameters? = null var parameters: Parameters? = null
@Inject @Inject
@ -38,7 +34,9 @@ class SharedItemsRepository {
return ncApi.getSharedItems( return ncApi.getSharedItems(
credentials, credentials,
ApiUtils.getUrlForChatSharedItems(1, parameters!!.baseUrl, parameters!!.roomToken), ApiUtils.getUrlForChatSharedItems(1, parameters!!.baseUrl, parameters!!.roomToken),
type, lastKnownMessageId, 28 type,
lastKnownMessageId,
BATCH_SIZE
) )
} }
@ -61,4 +59,8 @@ class SharedItemsRepository {
val userEntity: UserEntity, val userEntity: UserEntity,
val roomToken: String val roomToken: String
) )
companion object {
const val BATCH_SIZE: Int = 28
}
} }

View File

@ -1,3 +1,23 @@
/*
* Nextcloud Talk application
*
* @author Marcel Hibbe
* Copyright (C) 2022 Marcel Hibbe <dev@mhibbe.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils package com.nextcloud.talk.utils
import android.annotation.SuppressLint import android.annotation.SuppressLint
@ -35,9 +55,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fun openFile( fun openFile(
message: ChatMessage, message: ChatMessage,
progressBar: ProgressBar?, progressUi: ProgressUi
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
) { ) {
val fileName = message.getSelectedIndividualHashMap()[MagicPreviewMessageViewHolder.KEY_NAME]!! val fileName = message.getSelectedIndividualHashMap()[MagicPreviewMessageViewHolder.KEY_NAME]!!
val mimetype = message.getSelectedIndividualHashMap()[MagicPreviewMessageViewHolder.KEY_MIMETYPE]!! val mimetype = message.getSelectedIndividualHashMap()[MagicPreviewMessageViewHolder.KEY_MIMETYPE]!!
@ -59,9 +77,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path, path,
link, link,
mimetype, mimetype,
progressBar, progressUi
messageText,
previewImage
) )
} }
@ -72,9 +88,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path: String, path: String,
link: String, link: String,
mimetype: String, mimetype: String,
progressBar: ProgressBar?, progressUi: ProgressUi
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
) { ) {
if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileName)) { if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileName)) {
openOrDownloadFile( openOrDownloadFile(
@ -83,9 +97,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path, path,
fileSize, fileSize,
mimetype, mimetype,
progressBar, progressUi
messageText,
previewImage
) )
} else { } else {
openFileInFilesApp(link, fileId) openFileInFilesApp(link, fileId)
@ -106,9 +118,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path: String, path: String,
fileSize: Int, fileSize: Int,
mimetype: String, mimetype: String,
progressBar: ProgressBar?, progressUi: ProgressUi
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
) { ) {
val file = File(context.cacheDir, fileName) val file = File(context.cacheDir, fileName)
if (file.exists()) { if (file.exists()) {
@ -120,9 +130,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path, path,
fileSize, fileSize,
mimetype, mimetype,
progressBar, progressUi
messageText,
previewImage
) )
} }
} }
@ -148,11 +156,12 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
} }
} }
@Suppress("Detekt.TooGenericExceptionCaught")
private fun openFileByExternalApp(fileName: String, mimetype: String) { private fun openFileByExternalApp(fileName: String, mimetype: String) {
val path = context.cacheDir.absolutePath + "/" + fileName val path = context.cacheDir.absolutePath + "/" + fileName
val file = File(path) val file = File(path)
val intent: Intent val intent: Intent
if (Build.VERSION.SDK_INT < 24) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent = Intent(Intent.ACTION_VIEW) intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.fromFile(file), mimetype) intent.setDataAndType(Uri.fromFile(file), mimetype)
intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
@ -259,14 +268,10 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path: String, path: String,
fileSize: Int, fileSize: Int,
mimetype: String, mimetype: String,
progressBar: ProgressBar?, progressUi: ProgressUi
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
) { ) {
// check if download worker is already running // check if download worker is already running
val workers = WorkManager.getInstance(context).getWorkInfosByTag( val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId!!)
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) {
@ -296,16 +301,14 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
.addTag(fileId) .addTag(fileId)
.build() .build()
WorkManager.getInstance().enqueue(downloadWorker) WorkManager.getInstance().enqueue(downloadWorker)
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, fileName,
mimetype, mimetype,
workInfo!!, workInfo!!,
progressBar, progressUi
messageText,
previewImage
) )
} }
} }
@ -314,15 +317,13 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fileName: String, fileName: String,
mimetype: String, mimetype: String,
workInfo: WorkInfo, workInfo: WorkInfo,
progressBar: ProgressBar?, progressUi: ProgressUi
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
) { ) {
when (workInfo.state) { when (workInfo.state) {
WorkInfo.State.RUNNING -> { WorkInfo.State.RUNNING -> {
val progress = workInfo.progress.getInt(DownloadFileToCacheWorker.PROGRESS, -1) val progress = workInfo.progress.getInt(DownloadFileToCacheWorker.PROGRESS, -1)
if (progress > -1) { if (progress > -1) {
messageText?.text = String.format( progressUi.messageText?.text = String.format(
context.resources.getString(R.string.filename_progress), context.resources.getString(R.string.filename_progress),
fileName, fileName,
progress progress
@ -330,7 +331,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
} }
} }
WorkInfo.State.SUCCEEDED -> { WorkInfo.State.SUCCEEDED -> {
if (previewImage.isShown) { if (progressUi.previewImage.isShown) {
openFileByMimetype(fileName, mimetype) openFileByMimetype(fileName, mimetype)
} else { } else {
Log.d( Log.d(
@ -339,12 +340,12 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
" was downloaded but it's not opened because view is not shown on screen" " was downloaded but it's not opened because view is not shown on screen"
) )
} }
messageText?.text = fileName progressUi.messageText?.text = fileName
progressBar?.visibility = View.GONE progressUi.progressBar?.visibility = View.GONE
} }
WorkInfo.State.FAILED -> { WorkInfo.State.FAILED -> {
messageText?.text = fileName progressUi.messageText?.text = fileName
progressBar?.visibility = View.GONE progressUi.progressBar?.visibility = View.GONE
} }
else -> { else -> {
} }
@ -355,9 +356,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fileName: String, fileName: String,
fileId: String, fileId: String,
mimeType: String, mimeType: String,
progressBar: ProgressBar, progressUi: ProgressUi
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
) { ) {
val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId) val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId)
@ -366,7 +365,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
if (workInfo.state == WorkInfo.State.RUNNING || if (workInfo.state == WorkInfo.State.RUNNING ||
workInfo.state == WorkInfo.State.ENQUEUED workInfo.state == WorkInfo.State.ENQUEUED
) { ) {
progressBar.visibility = View.VISIBLE progressUi.progressBar?.visibility = View.VISIBLE
WorkManager WorkManager
.getInstance(context) .getInstance(context)
.getWorkInfoByIdLiveData(workInfo.id) .getWorkInfoByIdLiveData(workInfo.id)
@ -375,9 +374,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fileName, fileName,
mimeType, mimeType,
info!!, info!!,
progressBar, progressUi
messageText,
previewImage
) )
} }
} }
@ -389,9 +386,14 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
} }
} }
data class ProgressUi(
val progressBar: ProgressBar?,
val messageText: EmojiTextView?,
val previewImage: SimpleDraweeView
)
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"
} }
} }

View File

@ -92,7 +92,7 @@ class SharedItemsViewModel(private val repository: SharedItemsRepository, privat
override fun onComplete() { override fun onComplete() {
val sortedMutableItems = items.toSortedMap().values.toList().reversed().toMutableList() val sortedMutableItems = items.toSortedMap().values.toList().reversed().toMutableList()
val moreItemsExisting = items.count() == 28 val moreItemsExisting = items.count() == BATCH_SIZE
if (initModel) { if (initModel) {
this@SharedItemsViewModel._sharedItems.value = this@SharedItemsViewModel._sharedItems.value =
@ -143,5 +143,6 @@ class SharedItemsViewModel(private val repository: SharedItemsRepository, privat
companion object { companion object {
private val TAG = SharedItemsViewModel::class.simpleName private val TAG = SharedItemsViewModel::class.simpleName
const val BATCH_SIZE: Int = 28
} }
} }