Fix add attachments

This commit is contained in:
J-Jamet
2021-08-10 19:31:08 +02:00
parent a904a51293
commit eefdeb0bb7
3 changed files with 41 additions and 20 deletions

View File

@@ -204,25 +204,23 @@ class EntryEditFragment: DatabaseFragment() {
when (entryAttachmentState?.downloadState) { when (entryAttachmentState?.downloadState) {
AttachmentState.START -> { AttachmentState.START -> {
putAttachment(entryAttachmentState) putAttachment(entryAttachmentState)
getAttachmentViewPosition(entryAttachmentState) { getAttachmentViewPosition(entryAttachmentState) { attachment, position ->
mEntryEditViewModel.binaryPreviewLoaded(entryAttachmentState, it) mEntryEditViewModel.binaryPreviewLoaded(attachment, position)
} }
} }
AttachmentState.IN_PROGRESS -> { AttachmentState.IN_PROGRESS -> {
putAttachment(entryAttachmentState) putAttachment(entryAttachmentState)
} }
AttachmentState.COMPLETE -> { AttachmentState.COMPLETE -> {
putAttachment(entryAttachmentState) { putAttachment(entryAttachmentState) { entryAttachment ->
getAttachmentViewPosition(entryAttachmentState) { getAttachmentViewPosition(entryAttachment) { attachment, position ->
mEntryEditViewModel.binaryPreviewLoaded(entryAttachmentState, it) mEntryEditViewModel.binaryPreviewLoaded(attachment, position)
} }
} }
mEntryEditViewModel.onAttachmentAction(null)
} }
AttachmentState.CANCELED, AttachmentState.CANCELED,
AttachmentState.ERROR -> { AttachmentState.ERROR -> {
removeAttachment(entryAttachmentState) removeAttachment(entryAttachmentState)
mEntryEditViewModel.onAttachmentAction(null)
} }
else -> {} else -> {}
} }
@@ -297,15 +295,15 @@ class EntryEditFragment: DatabaseFragment() {
} }
private fun putAttachment(attachment: EntryAttachmentState, private fun putAttachment(attachment: EntryAttachmentState,
onPreviewLoaded: (() -> Unit)? = null) { onPreviewLoaded: ((attachment: EntryAttachmentState) -> Unit)? = null) {
// When only one attachment is allowed // When only one attachment is allowed
if (mAllowMultipleAttachments) { if (!mAllowMultipleAttachments) {
clearAttachments() clearAttachments()
} }
attachmentsContainerView.visibility = View.VISIBLE attachmentsContainerView.visibility = View.VISIBLE
attachmentsAdapter?.putItem(attachment) attachmentsAdapter?.putItem(attachment)
attachmentsAdapter?.onBinaryPreviewLoaded = { attachmentsAdapter?.onBinaryPreviewLoaded = {
onPreviewLoaded?.invoke() onPreviewLoaded?.invoke(attachment)
} }
} }
@@ -317,10 +315,12 @@ class EntryEditFragment: DatabaseFragment() {
attachmentsAdapter?.clear() attachmentsAdapter?.clear()
} }
private fun getAttachmentViewPosition(attachment: EntryAttachmentState, position: (Float) -> Unit) { private fun getAttachmentViewPosition(attachment: EntryAttachmentState,
position: (attachment: EntryAttachmentState, Float) -> Unit) {
attachmentsListView.postDelayed({ attachmentsListView.postDelayed({
attachmentsAdapter?.indexOf(attachment)?.let { index -> attachmentsAdapter?.indexOf(attachment)?.let { index ->
position.invoke(attachmentsContainerView.y position.invoke(attachment,
attachmentsContainerView.y
+ attachmentsListView.y + attachmentsListView.y
+ (attachmentsListView.getChildAt(index)?.y + (attachmentsListView.getChildAt(index)?.y
?: 0F) ?: 0F)

View File

@@ -44,6 +44,7 @@ class AttachmentFileNotificationService: LockNotificationService() {
private var mDatabaseTaskProvider: DatabaseTaskProvider? = null private var mDatabaseTaskProvider: DatabaseTaskProvider? = null
private var mDatabase: Database? = null private var mDatabase: Database? = null
private val mPendingCommands: MutableList<Intent?> = mutableListOf()
override val notificationId: Int = 10000 override val notificationId: Int = 10000
private val attachmentNotificationList = CopyOnWriteArrayList<AttachmentNotification>() private val attachmentNotificationList = CopyOnWriteArrayList<AttachmentNotification>()
@@ -91,6 +92,13 @@ class AttachmentFileNotificationService: LockNotificationService() {
mDatabaseTaskProvider?.registerProgressTask() mDatabaseTaskProvider?.registerProgressTask()
mDatabaseTaskProvider?.onDatabaseRetrieved = { database -> mDatabaseTaskProvider?.onDatabaseRetrieved = { database ->
this.mDatabase = 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 { override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
super.onStartCommand(intent, flags, startId) 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) { val downloadFileUri: Uri? = if (intent?.hasExtra(FILE_URI_KEY) == true) {
intent.getParcelableExtra(FILE_URI_KEY) intent.getParcelableExtra(FILE_URI_KEY)
} else null } else null
@@ -112,16 +132,16 @@ class AttachmentFileNotificationService: LockNotificationService() {
when(intent?.action) { when(intent?.action) {
ACTION_ATTACHMENT_FILE_START_UPLOAD -> { ACTION_ATTACHMENT_FILE_START_UPLOAD -> {
actionStartUploadOrDownload(downloadFileUri, actionStartUploadOrDownload(downloadFileUri,
intent, intent,
StreamDirection.UPLOAD) StreamDirection.UPLOAD)
} }
ACTION_ATTACHMENT_FILE_STOP_UPLOAD -> { ACTION_ATTACHMENT_FILE_STOP_UPLOAD -> {
actionStopUpload() actionStopUpload()
} }
ACTION_ATTACHMENT_FILE_START_DOWNLOAD -> { ACTION_ATTACHMENT_FILE_START_DOWNLOAD -> {
actionStartUploadOrDownload(downloadFileUri, actionStartUploadOrDownload(downloadFileUri,
intent, intent,
StreamDirection.DOWNLOAD) StreamDirection.DOWNLOAD)
} }
ACTION_ATTACHMENT_REMOVE -> { ACTION_ATTACHMENT_REMOVE -> {
intent.getParcelableExtra<Attachment>(ATTACHMENT_KEY)?.let { entryAttachment -> intent.getParcelableExtra<Attachment>(ATTACHMENT_KEY)?.let { entryAttachment ->
@@ -142,8 +162,6 @@ class AttachmentFileNotificationService: LockNotificationService() {
} }
} }
} }
return START_REDELIVER_INTENT
} }
@Synchronized @Synchronized

View File

@@ -322,8 +322,11 @@ class EntryEditViewModel: NodeEditViewModel() {
} }
fun onAttachmentAction(entryAttachmentState: EntryAttachmentState?) { 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) mTempAttachments.add(entryAttachmentState)
} }
_onAttachmentAction.value = entryAttachmentState _onAttachmentAction.value = entryAttachmentState