Refactor files
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package de.mm20.launcher2.ui.icons
|
||||
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.rounded.*
|
||||
import androidx.compose.runtime.Composable
|
||||
|
||||
@@ -8,7 +8,6 @@ import android.view.ViewGroup
|
||||
import android.widget.FrameLayout
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModelProvider
|
||||
import de.mm20.launcher2.files.FilesViewModel
|
||||
import de.mm20.launcher2.permissions.PermissionsManager
|
||||
import de.mm20.launcher2.search.data.File
|
||||
|
||||
@@ -72,11 +72,10 @@ class FileDetailRepresentation : Representation, KoinComponent {
|
||||
val favAction = FavoriteToolbarAction(context, file)
|
||||
toolbar.addAction(favAction, ToolbarView.PLACEMENT_END)
|
||||
|
||||
val jFile = java.io.File(file.path)
|
||||
if (jFile.canWrite() && jFile.parentFile.canWrite()) {
|
||||
if (file.isDeletable) {
|
||||
val deleteAction = ToolbarAction(R.drawable.ic_delete, context.getString(R.string.menu_delete))
|
||||
deleteAction.clickAction = {
|
||||
delete(context, file, jFile)
|
||||
delete(context, file)
|
||||
}
|
||||
toolbar.addAction(deleteAction, ToolbarView.PLACEMENT_END)
|
||||
}
|
||||
@@ -93,21 +92,16 @@ class FileDetailRepresentation : Representation, KoinComponent {
|
||||
}
|
||||
}
|
||||
|
||||
private fun delete(context: Context, file: File, jFile: java.io.File) {
|
||||
private fun delete(context: Context, file: File) {
|
||||
MaterialDialog(context).show {
|
||||
message(text = context.getString(
|
||||
if (file.isDirectory) R.string.alert_delete_directory
|
||||
else R.string.alert_delete_file,
|
||||
file.path))
|
||||
positiveButton(android.R.string.yes) {
|
||||
Thread { jFile.deleteRecursively() }.start()
|
||||
context.contentResolver.delete(
|
||||
MediaStore.Files.getContentUri("external"),
|
||||
"${MediaStore.Files.FileColumns._ID} = ?",
|
||||
arrayOf(file.id.toString()))
|
||||
it.dismiss()
|
||||
val fileViewModel: FilesViewModel by (context as AppCompatActivity).viewModel()
|
||||
fileViewModel.removeFile(file)
|
||||
it.dismiss()
|
||||
fileViewModel.deleteFile(file)
|
||||
}
|
||||
negativeButton(android.R.string.no) {
|
||||
it.dismiss()
|
||||
|
||||
@@ -34,7 +34,7 @@ class FileListRepresentation : Representation, KoinComponent {
|
||||
scene.setEnterAction {
|
||||
with(rootView) {
|
||||
findViewById<TextView>(R.id.fileLabel).text = file.label
|
||||
findViewById<TextView>(R.id.fileInfo).text = getFileType(context, file)
|
||||
findViewById<TextView>(R.id.fileInfo).text = file.getFileType(context)
|
||||
findViewById<LauncherIconView>(R.id.icon).apply {
|
||||
badge = badgeProvider.getLiveBadge(file.badgeKey)
|
||||
shape = LauncherIconView.getDefaultShape(context)
|
||||
@@ -60,40 +60,4 @@ class FileListRepresentation : Representation, KoinComponent {
|
||||
}
|
||||
return scene
|
||||
}
|
||||
|
||||
fun getFileType(context: Context, file: File): String {
|
||||
if (file.isDirectory) return context.getString(R.string.file_type_directory)
|
||||
val mimeType = file.mimeType
|
||||
val resource = when (mimeType) {
|
||||
"application/zip", "application/x-gtar", "application/x-tar",
|
||||
"application/java-archive", "application/x-7z-compressed" -> R.string.file_type_archive
|
||||
"application/x-gzip", "application/x-bzip2" -> R.string.file_type_compressed
|
||||
"application/vnd.android.package-archive" -> R.string.file_type_android
|
||||
"text/x-asm", "text/x-c", "text/x-java-source", "text/x-script.phyton", "text/x-pascal",
|
||||
"text/x-script.perl", "text/javascript", "application/json" ->
|
||||
R.string.file_type_source_code
|
||||
"application/vnd.oasis.opendocument.text",
|
||||
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
"application/msword", "application/vnd.google-apps.document" -> R.string.file_type_document
|
||||
"application/vnd.oasis.opendocument.spreadsheet",
|
||||
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
||||
"application/vnd.ms-excel", "application/vnd.google-apps.spreadsheet" -> R.string.file_type_spreadsheet
|
||||
"application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
||||
"application/vnd.ms-powerpoint", "application/vnd.google-apps.presentation" -> R.string.file_type_presentation
|
||||
"text/plain" -> R.string.file_type_text
|
||||
"application/vnd.google-apps.drawing" -> R.string.file_type_drawing
|
||||
"application/vnd.google-apps.form" -> R.string.file_type_form
|
||||
else -> when {
|
||||
mimeType.startsWith("image/") -> R.string.file_type_image
|
||||
mimeType.startsWith("video/") -> R.string.file_type_video
|
||||
mimeType.startsWith("audio/") -> R.string.file_type_music
|
||||
else -> R.string.file_type_none
|
||||
}
|
||||
}
|
||||
if (resource == R.string.file_type_none && file.label.matches(Regex(".+\\..+"))) {
|
||||
val extension = file.label.substringAfterLast(".").uppercase()
|
||||
return context.getString(R.string.file_type_generic, extension)
|
||||
}
|
||||
return context.getString(resource)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user