-
-
Notifications
You must be signed in to change notification settings - Fork 396
Expand file tree
/
Copy pathCompressAsDialog.kt
More file actions
98 lines (87 loc) · 4.6 KB
/
CompressAsDialog.kt
File metadata and controls
98 lines (87 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package com.simplemobiletools.filemanager.pro.dialogs
import android.view.View
import android.widget.ArrayAdapter
import androidx.appcompat.app.AlertDialog
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.filemanager.pro.R
import com.simplemobiletools.filemanager.pro.databinding.DialogCompressAsBinding
import com.simplemobiletools.filemanager.pro.extensions.config
import com.simplemobiletools.filemanager.pro.helpers.CompressionFormat
class CompressAsDialog(
val activity: BaseSimpleActivity,
val path: String,
val callback: (destination: String, compressionFormat: CompressionFormat, password: String?) -> Unit
) {
private val binding = DialogCompressAsBinding.inflate(activity.layoutInflater)
init {
val filename = path.getFilenameFromPath()
val indexOfDot = if (filename.contains('.') && !activity.getIsPathDirectory(path)) filename.lastIndexOf(".") else filename.length
val baseFilename = filename.substring(0, indexOfDot)
var realPath = path.getParentPath()
binding.apply {
filenameValue.setText(baseFilename)
folder.setText(activity.humanizePath(realPath))
folder.setOnClickListener {
FilePickerDialog(activity, realPath, false, activity.config.shouldShowHidden(), true, true, showFavoritesButton = true) {
folder.setText(activity.humanizePath(it))
realPath = it
}
}
compressionFormatValue.apply {
setOnClickListener {
activity.hideKeyboard(filenameValue)
}
val adapter = ArrayAdapter(
activity,
android.R.layout.simple_dropdown_item_1line,
CompressionFormat.entries.take(CompressionFormat.entries.size - 1).map { it.extension })
setAdapter(adapter)
setText(adapter.getItem(0), false)
setOnItemClickListener { _, _, i, _ ->
val compressionFormat = CompressionFormat.entries[i]
filenameHint.hint = String.format(activity.getString(R.string.filename_without_extension), compressionFormat.extension)
passwordProtect.beVisibleIf(compressionFormat.canCreateEncryptedArchive)
enterPasswordHint.beVisibleIf(compressionFormat.canCreateEncryptedArchive && passwordProtect.isChecked)
}
}
passwordProtect.setOnCheckedChangeListener { _, _ ->
enterPasswordHint.beVisibleIf(passwordProtect.isChecked)
}
}
activity.getAlertDialogBuilder()
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.apply {
activity.setupDialogStuff(binding.root, this, R.string.compress_as) { alertDialog ->
alertDialog.showKeyboard(binding.filenameValue)
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(View.OnClickListener {
val name = binding.filenameValue.value
var password: String? = null
if (binding.passwordProtect.isChecked) {
password = binding.password.value
if (password.isEmpty()) {
activity.toast(R.string.empty_password_new)
return@OnClickListener
}
}
when {
name.isEmpty() -> activity.toast(R.string.empty_name)
name.isAValidFilename() -> {
val newPath = "$realPath/$name${getSelectedCompressionFormat().extension}"
if (activity.getDoesFilePathExist(newPath)) {
activity.toast(R.string.name_taken)
return@OnClickListener
}
alertDialog.dismiss()
callback(newPath, getSelectedCompressionFormat(), password)
}
else -> activity.toast(R.string.invalid_name)
}
})
}
}
}
private fun getSelectedCompressionFormat() = CompressionFormat.fromExtension(binding.compressionFormatValue.text.toString())
}