mirror of
https://github.com/Kunzisoft/KeePassDX.git
synced 2025-12-04 15:49:33 +01:00
Fix add attachments
This commit is contained in:
@@ -204,25 +204,23 @@ class EntryEditFragment: DatabaseFragment() {
|
||||
when (entryAttachmentState?.downloadState) {
|
||||
AttachmentState.START -> {
|
||||
putAttachment(entryAttachmentState)
|
||||
getAttachmentViewPosition(entryAttachmentState) {
|
||||
mEntryEditViewModel.binaryPreviewLoaded(entryAttachmentState, it)
|
||||
getAttachmentViewPosition(entryAttachmentState) { attachment, position ->
|
||||
mEntryEditViewModel.binaryPreviewLoaded(attachment, position)
|
||||
}
|
||||
}
|
||||
AttachmentState.IN_PROGRESS -> {
|
||||
putAttachment(entryAttachmentState)
|
||||
}
|
||||
AttachmentState.COMPLETE -> {
|
||||
putAttachment(entryAttachmentState) {
|
||||
getAttachmentViewPosition(entryAttachmentState) {
|
||||
mEntryEditViewModel.binaryPreviewLoaded(entryAttachmentState, it)
|
||||
putAttachment(entryAttachmentState) { entryAttachment ->
|
||||
getAttachmentViewPosition(entryAttachment) { attachment, position ->
|
||||
mEntryEditViewModel.binaryPreviewLoaded(attachment, position)
|
||||
}
|
||||
}
|
||||
mEntryEditViewModel.onAttachmentAction(null)
|
||||
}
|
||||
AttachmentState.CANCELED,
|
||||
AttachmentState.ERROR -> {
|
||||
removeAttachment(entryAttachmentState)
|
||||
mEntryEditViewModel.onAttachmentAction(null)
|
||||
}
|
||||
else -> {}
|
||||
}
|
||||
@@ -297,15 +295,15 @@ class EntryEditFragment: DatabaseFragment() {
|
||||
}
|
||||
|
||||
private fun putAttachment(attachment: EntryAttachmentState,
|
||||
onPreviewLoaded: (() -> Unit)? = null) {
|
||||
onPreviewLoaded: ((attachment: EntryAttachmentState) -> Unit)? = null) {
|
||||
// When only one attachment is allowed
|
||||
if (mAllowMultipleAttachments) {
|
||||
if (!mAllowMultipleAttachments) {
|
||||
clearAttachments()
|
||||
}
|
||||
attachmentsContainerView.visibility = View.VISIBLE
|
||||
attachmentsAdapter?.putItem(attachment)
|
||||
attachmentsAdapter?.onBinaryPreviewLoaded = {
|
||||
onPreviewLoaded?.invoke()
|
||||
onPreviewLoaded?.invoke(attachment)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -317,10 +315,12 @@ class EntryEditFragment: DatabaseFragment() {
|
||||
attachmentsAdapter?.clear()
|
||||
}
|
||||
|
||||
private fun getAttachmentViewPosition(attachment: EntryAttachmentState, position: (Float) -> Unit) {
|
||||
private fun getAttachmentViewPosition(attachment: EntryAttachmentState,
|
||||
position: (attachment: EntryAttachmentState, Float) -> Unit) {
|
||||
attachmentsListView.postDelayed({
|
||||
attachmentsAdapter?.indexOf(attachment)?.let { index ->
|
||||
position.invoke(attachmentsContainerView.y
|
||||
position.invoke(attachment,
|
||||
attachmentsContainerView.y
|
||||
+ attachmentsListView.y
|
||||
+ (attachmentsListView.getChildAt(index)?.y
|
||||
?: 0F)
|
||||
|
||||
@@ -44,6 +44,7 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
|
||||
private var mDatabaseTaskProvider: DatabaseTaskProvider? = null
|
||||
private var mDatabase: Database? = null
|
||||
private val mPendingCommands: MutableList<Intent?> = mutableListOf()
|
||||
|
||||
override val notificationId: Int = 10000
|
||||
private val attachmentNotificationList = CopyOnWriteArrayList<AttachmentNotification>()
|
||||
@@ -91,6 +92,13 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
mDatabaseTaskProvider?.registerProgressTask()
|
||||
mDatabaseTaskProvider?.onDatabaseRetrieved = { database ->
|
||||
this.mDatabase = database
|
||||
// Execute each command in wait state
|
||||
val commandIterator = this.mPendingCommands.iterator()
|
||||
while (commandIterator.hasNext()) {
|
||||
val command = commandIterator.next()
|
||||
actionRequested(command)
|
||||
commandIterator.remove()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -105,6 +113,18 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
|
||||
super.onStartCommand(intent, flags, startId)
|
||||
|
||||
// Wait for database to execute action request
|
||||
if (mDatabase != null) {
|
||||
actionRequested(intent)
|
||||
} else {
|
||||
mPendingCommands.add(intent)
|
||||
}
|
||||
|
||||
return START_REDELIVER_INTENT
|
||||
}
|
||||
|
||||
private fun actionRequested(intent: Intent?) {
|
||||
|
||||
val downloadFileUri: Uri? = if (intent?.hasExtra(FILE_URI_KEY) == true) {
|
||||
intent.getParcelableExtra(FILE_URI_KEY)
|
||||
} else null
|
||||
@@ -112,16 +132,16 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
when(intent?.action) {
|
||||
ACTION_ATTACHMENT_FILE_START_UPLOAD -> {
|
||||
actionStartUploadOrDownload(downloadFileUri,
|
||||
intent,
|
||||
StreamDirection.UPLOAD)
|
||||
intent,
|
||||
StreamDirection.UPLOAD)
|
||||
}
|
||||
ACTION_ATTACHMENT_FILE_STOP_UPLOAD -> {
|
||||
actionStopUpload()
|
||||
}
|
||||
ACTION_ATTACHMENT_FILE_START_DOWNLOAD -> {
|
||||
actionStartUploadOrDownload(downloadFileUri,
|
||||
intent,
|
||||
StreamDirection.DOWNLOAD)
|
||||
intent,
|
||||
StreamDirection.DOWNLOAD)
|
||||
}
|
||||
ACTION_ATTACHMENT_REMOVE -> {
|
||||
intent.getParcelableExtra<Attachment>(ATTACHMENT_KEY)?.let { entryAttachment ->
|
||||
@@ -142,8 +162,6 @@ class AttachmentFileNotificationService: LockNotificationService() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return START_REDELIVER_INTENT
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
|
||||
@@ -322,8 +322,11 @@ class EntryEditViewModel: NodeEditViewModel() {
|
||||
}
|
||||
|
||||
fun onAttachmentAction(entryAttachmentState: EntryAttachmentState?) {
|
||||
if (entryAttachmentState?.downloadState == AttachmentState.START) {
|
||||
// Add in temp list
|
||||
// Add in temp list
|
||||
if (mTempAttachments.contains(entryAttachmentState)) {
|
||||
mTempAttachments.remove(entryAttachmentState)
|
||||
}
|
||||
if (entryAttachmentState != null) {
|
||||
mTempAttachments.add(entryAttachmentState)
|
||||
}
|
||||
_onAttachmentAction.value = entryAttachmentState
|
||||
|
||||
Reference in New Issue
Block a user