forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRemoveFilesDialogFragment.kt
More file actions
231 lines (193 loc) · 8.09 KB
/
RemoveFilesDialogFragment.kt
File metadata and controls
231 lines (193 loc) · 8.09 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2023 Alper Ozturk <[email protected]>
* SPDX-FileCopyrightText: 2018 Andy Scherzinger <[email protected]>
* SPDX-FileCopyrightText: 2018 Jessie Chatham Spencer <[email protected]>
* SPDX-FileCopyrightText: 2016-2022 Tobias Kaminsky <[email protected]>
* SPDX-FileCopyrightText: 2015 ownCloud Inc.
* SPDX-FileCopyrightText: 2015 David A. Velasco <[email protected]>
* SPDX-License-Identifier: GPL-2.0-only AND (AGPL-3.0-or-later OR GPL-2.0-only)
*/
package com.owncloud.android.ui.dialog
import android.app.Dialog
import android.os.Bundle
import android.view.ActionMode
import androidx.appcompat.app.AlertDialog
import com.google.android.material.button.MaterialButton
import com.nextcloud.client.di.Injectable
import com.nextcloud.utils.extensions.getTypedActivity
import com.owncloud.android.R
import com.owncloud.android.datamodel.FileDataStorageManager
import com.owncloud.android.datamodel.OCFile
import com.owncloud.android.ui.activity.FileActivity
import com.owncloud.android.ui.activity.FileDisplayActivity
import com.owncloud.android.ui.dialog.ConfirmationDialogFragment.ConfirmationDialogFragmentListener
import com.owncloud.android.ui.preview.PreviewImageActivity
import javax.inject.Inject
/**
* Dialog requiring confirmation before removing a collection of given OCFiles.
* Triggers the removal according to the user response.
*/
class RemoveFilesDialogFragment :
ConfirmationDialogFragment(),
ConfirmationDialogFragmentListener,
Injectable {
private var mTargetFiles: Collection<OCFile>? = null
private var actionMode: ActionMode? = null
@Inject
lateinit var fileDataStorageManager: FileDataStorageManager
private var positiveButton: MaterialButton? = null
override fun onStart() {
super.onStart()
val alertDialog = dialog as AlertDialog? ?: return
positiveButton = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE) as? MaterialButton
positiveButton?.let {
viewThemeUtils?.material?.colorMaterialButtonPrimaryTonal(it)
}
val negativeButton = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE) as? MaterialButton
negativeButton?.let {
viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(negativeButton)
}
val neutralButton = alertDialog.getButton(AlertDialog.BUTTON_NEUTRAL) as? MaterialButton
neutralButton?.let {
viewThemeUtils?.material?.colorMaterialButtonPrimaryBorderless(neutralButton)
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val dialog = super.onCreateDialog(savedInstanceState)
val arguments = arguments ?: return dialog
mTargetFiles = arguments.getParcelableArrayList(ARG_TARGET_FILES)
setOnConfirmationListener(this)
return dialog
}
/**
* Performs the removal of the target file, both locally and in the server and
* finishes the supplied ActionMode if one was given.
*/
override fun onConfirmation(callerTag: String?) {
removeFiles(false)
}
/**
* Performs the removal of the local copy of the target file
*/
override fun onCancel(callerTag: String?) {
removeFiles(true)
}
private fun removeFiles(onlyLocalCopy: Boolean) {
val (offlineFiles, files) = mTargetFiles?.partition { it.isOfflineOperation } ?: Pair(emptyList(), emptyList())
offlineFiles.forEach {
fileDataStorageManager.deleteOfflineOperation(it)
}
val fileActivity = getTypedActivity(FileActivity::class.java)
val fda = getTypedActivity(FileDisplayActivity::class.java)
val pia = getTypedActivity(PreviewImageActivity::class.java)
fileActivity?.connectivityService?.isNetworkAndServerAvailable { result ->
if (result) {
fileActivity.showLoadingDialog(fileActivity.getString(R.string.wait_a_moment))
fda?.deleteBatchTracker?.startBatchDelete(files.size)
if (files.isNotEmpty()) {
// Display the snackbar message only when a single file is deleted.
val inBackground = (files.size != 1)
fileActivity.fileOperationsHelper?.removeFiles(files, onlyLocalCopy, inBackground)
}
if (offlineFiles.isNotEmpty()) {
fda?.refreshCurrentDirectory()
pia?.initViewPager()
}
fileActivity.dismissLoadingDialog()
} else {
if (onlyLocalCopy) {
fileActivity.fileOperationsHelper?.removeFiles(files, true, true)
} else {
files.forEach { file ->
fileDataStorageManager.addRemoveFileOfflineOperation(file)
}
}
fda?.refreshCurrentDirectory()
pia?.initViewPager()
}
finishActionMode()
}
}
override fun onNeutral(callerTag: String?) = Unit
private fun setActionMode(actionMode: ActionMode?) {
this.actionMode = actionMode
}
/**
* This is used when finishing an actionMode,
* for example if we want to exit the selection mode
* after deleting the target files.
*/
private fun finishActionMode() {
actionMode?.finish()
}
companion object {
private const val SINGLE_SELECTION = 1
private const val ARG_TARGET_FILES = "TARGET_FILES"
@JvmStatic
fun newInstance(files: ArrayList<OCFile>, actionMode: ActionMode?): RemoveFilesDialogFragment =
newInstance(files).apply {
setActionMode(actionMode)
}
@JvmStatic
fun newInstance(files: ArrayList<OCFile>): RemoveFilesDialogFragment {
val messageStringId: Int
var containsFolder = false
var containsDown = false
for (file in files) {
containsFolder = containsFolder or file.isFolder
containsDown = containsDown or file.isDown
}
if (files.size == SINGLE_SELECTION) {
val file = files[0]
messageStringId =
if (file.isFolder) {
R.string.confirmation_remove_folder_alert
} else {
R.string.confirmation_remove_file_alert
}
} else {
messageStringId =
if (containsFolder) {
R.string.confirmation_remove_folders_alert
} else {
R.string.confirmation_remove_files_alert
}
}
val bundle = Bundle().apply {
putInt(ARG_MESSAGE_RESOURCE_ID, messageStringId)
if (files.size == SINGLE_SELECTION) {
putStringArray(
ARG_MESSAGE_ARGUMENTS,
arrayOf(
files[0].fileName
)
)
}
val positiveButtonTextId = if (files.any { it.isSharedWithMe }) {
R.string.common_leave_this_share
} else {
R.string.file_delete
}
putInt(ARG_POSITIVE_BTN_RES, positiveButtonTextId)
val isAnyFileOffline = files.any { it.isOfflineOperation }
if ((containsFolder || containsDown) && !isAnyFileOffline) {
putInt(ARG_NEGATIVE_BTN_RES, R.string.confirmation_remove_local)
}
putInt(ARG_NEUTRAL_BTN_RES, R.string.file_keep)
putParcelableArrayList(ARG_TARGET_FILES, files)
}
return RemoveFilesDialogFragment().apply {
arguments = bundle
}
}
@JvmStatic
fun newInstance(file: OCFile): RemoveFilesDialogFragment {
val list = ArrayList<OCFile>().apply {
add(file)
}
return newInstance(list)
}
}
}