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
binding.imageRecycler.adapter = adapter
val layoutManager = GridLayoutManager(this, 4)
val layoutManager = GridLayoutManager(this, SPAN_COUNT)
binding.imageRecycler.layoutManager = layoutManager
} else {
val adapter = SharedItemsListAdapter()
@ -164,5 +164,6 @@ class SharedItemsActivity : AppCompatActivity() {
const val TAB_LOCATION = "location"
const val TAB_DECKCARD = "deckcard"
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.link,
currentItem.mimeType,
holder.binding.progressBar,
null,
it as SimpleDraweeView
FileViewerUtils.ProgressUi(
holder.binding.progressBar,
null,
it as SimpleDraweeView
)
)
}
}

View file

@ -81,9 +81,11 @@ class SharedItemsListAdapter : RecyclerView.Adapter<SharedItemsListAdapter.ViewH
currentItem.path,
currentItem.link,
currentItem.mimeType,
holder.binding.progressBar,
null,
holder.binding.fileImage
FileViewerUtils.ProgressUi(
holder.binding.progressBar,
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) {
clickView.setOnClickListener(v -> {
fileViewerUtils.openFile(message, progressBar, getMessageText(), image);
});
clickView.setOnClickListener(v ->
fileViewerUtils.openFile(
message,
new FileViewerUtils.ProgressUi(progressBar, getMessageText(), image)
)
);
clickView.setOnLongClickListener(l -> {
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_ID)),
Objects.requireNonNull(message.getSelectedIndividualHashMap().get(MagicPreviewMessageViewHolder.KEY_MIMETYPE)),
progressBar,
getMessageText(),
image);
new FileViewerUtils.ProgressUi(progressBar, getMessageText(), image));
} else if (message.getMessageType() == ChatMessage.MessageType.SINGLE_LINK_GIPHY_MESSAGE) {
getMessageText().setText("GIPHY");

View file

@ -15,10 +15,6 @@ import javax.inject.Inject
@AutoInjector(NextcloudTalkApplication::class)
class SharedItemsRepository {
companion object {
private val TAG = SharedItemsRepository::class.simpleName
}
var parameters: Parameters? = null
@Inject
@ -38,7 +34,9 @@ class SharedItemsRepository {
return ncApi.getSharedItems(
credentials,
ApiUtils.getUrlForChatSharedItems(1, parameters!!.baseUrl, parameters!!.roomToken),
type, lastKnownMessageId, 28
type,
lastKnownMessageId,
BATCH_SIZE
)
}
@ -61,4 +59,8 @@ class SharedItemsRepository {
val userEntity: UserEntity,
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
import android.annotation.SuppressLint
@ -35,9 +55,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fun openFile(
message: ChatMessage,
progressBar: ProgressBar?,
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
progressUi: ProgressUi
) {
val fileName = message.getSelectedIndividualHashMap()[MagicPreviewMessageViewHolder.KEY_NAME]!!
val mimetype = message.getSelectedIndividualHashMap()[MagicPreviewMessageViewHolder.KEY_MIMETYPE]!!
@ -59,9 +77,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path,
link,
mimetype,
progressBar,
messageText,
previewImage
progressUi
)
}
@ -72,9 +88,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path: String,
link: String,
mimetype: String,
progressBar: ProgressBar?,
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
progressUi: ProgressUi
) {
if (isSupportedForInternalViewer(mimetype) || canBeHandledByExternalApp(mimetype, fileName)) {
openOrDownloadFile(
@ -83,9 +97,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path,
fileSize,
mimetype,
progressBar,
messageText,
previewImage
progressUi
)
} else {
openFileInFilesApp(link, fileId)
@ -106,9 +118,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path: String,
fileSize: Int,
mimetype: String,
progressBar: ProgressBar?,
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
progressUi: ProgressUi
) {
val file = File(context.cacheDir, fileName)
if (file.exists()) {
@ -120,9 +130,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path,
fileSize,
mimetype,
progressBar,
messageText,
previewImage
progressUi
)
}
}
@ -148,11 +156,12 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
}
}
@Suppress("Detekt.TooGenericExceptionCaught")
private fun openFileByExternalApp(fileName: String, mimetype: String) {
val path = context.cacheDir.absolutePath + "/" + fileName
val file = File(path)
val intent: Intent
if (Build.VERSION.SDK_INT < 24) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(Uri.fromFile(file), mimetype)
intent.flags = Intent.FLAG_ACTIVITY_NO_HISTORY
@ -259,14 +268,10 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
path: String,
fileSize: Int,
mimetype: String,
progressBar: ProgressBar?,
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
progressUi: ProgressUi
) {
// check if download worker is already running
val workers = WorkManager.getInstance(context).getWorkInfosByTag(
fileId!!
)
val workers = WorkManager.getInstance(context).getWorkInfosByTag(fileId!!)
try {
for (workInfo in workers.get()) {
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)
.build()
WorkManager.getInstance().enqueue(downloadWorker)
progressBar?.visibility = View.VISIBLE
progressUi.progressBar?.visibility = View.VISIBLE
WorkManager.getInstance(context).getWorkInfoByIdLiveData(downloadWorker.id)
.observeForever { workInfo: WorkInfo? ->
updateViewsByProgress(
fileName,
mimetype,
workInfo!!,
progressBar,
messageText,
previewImage
progressUi
)
}
}
@ -314,15 +317,13 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fileName: String,
mimetype: String,
workInfo: WorkInfo,
progressBar: ProgressBar?,
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
progressUi: ProgressUi
) {
when (workInfo.state) {
WorkInfo.State.RUNNING -> {
val progress = workInfo.progress.getInt(DownloadFileToCacheWorker.PROGRESS, -1)
if (progress > -1) {
messageText?.text = String.format(
progressUi.messageText?.text = String.format(
context.resources.getString(R.string.filename_progress),
fileName,
progress
@ -330,7 +331,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
}
}
WorkInfo.State.SUCCEEDED -> {
if (previewImage.isShown) {
if (progressUi.previewImage.isShown) {
openFileByMimetype(fileName, mimetype)
} else {
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"
)
}
messageText?.text = fileName
progressBar?.visibility = View.GONE
progressUi.messageText?.text = fileName
progressUi.progressBar?.visibility = View.GONE
}
WorkInfo.State.FAILED -> {
messageText?.text = fileName
progressBar?.visibility = View.GONE
progressUi.messageText?.text = fileName
progressUi.progressBar?.visibility = View.GONE
}
else -> {
}
@ -355,9 +356,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fileName: String,
fileId: String,
mimeType: String,
progressBar: ProgressBar,
messageText: EmojiTextView?,
previewImage: SimpleDraweeView
progressUi: ProgressUi
) {
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 ||
workInfo.state == WorkInfo.State.ENQUEUED
) {
progressBar.visibility = View.VISIBLE
progressUi.progressBar?.visibility = View.VISIBLE
WorkManager
.getInstance(context)
.getWorkInfoByIdLiveData(workInfo.id)
@ -375,9 +374,7 @@ class FileViewerUtils(private val context: Context, private val userEntity: User
fileName,
mimeType,
info!!,
progressBar,
messageText,
previewImage
progressUi
)
}
}
@ -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 {
private val TAG = FileViewerUtils::class.simpleName
const val KEY_ID = "id"
}
}

View file

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