Skip to content

Commit

Permalink
Merge pull request #546 from jorgeblacio/fix_mostaza
Browse files Browse the repository at this point in the history
Fixed an issue with attching files when the max capacity per email exceeded.
  • Loading branch information
danieltigse authored Sep 19, 2019
2 parents f501d9f + 4179811 commit f223c59
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 18 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 88
versionName "0.21.11"
versionCode 89
versionName "0.21.12"
applicationId "com.criptext.mail"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,11 @@ class ComposerController(private val storage: KeyValueStorage,
is ComposerResult.UploadFile.Success -> {
val composerAttachment = getAttachmentByUUID(result.uuid)
composerAttachment?.uploadProgress = 100
model.isUploadingAttachments = false
model.filesSize = result.filesSize
handleNextUpload()
}
is ComposerResult.UploadFile.Failure -> {
removeAttachmentByPath(result.filepath)
removeAttachmentByUUID(result.uuid)
scene.showAttachmentErrorDialog(result.filepath)
handleNextUpload()
}
Expand All @@ -319,12 +318,13 @@ class ComposerController(private val storage: KeyValueStorage,
scene.showConfirmPasswordDialog(observer)
}
is ComposerResult.UploadFile.MaxFilesExceeds -> {
removeAttachmentByPath(result.filepath)
removeAttachmentByUUID(result.uuid, true)
model.filesExceedingMaxEmailSize.add(FileUtils.getName(result.filepath))
scene.showMaxFilesExceedsDialog()
handleNextUpload()
}
is ComposerResult.UploadFile.PayloadTooLarge -> {
removeAttachmentByPath(result.filepath)
removeAttachmentByUUID(result.uuid)
scene.showPayloadTooLargeDialog(result.filepath, result.headers.getLong("Max-Size"))
handleNextUpload()
}
Expand All @@ -339,8 +339,14 @@ class ComposerController(private val storage: KeyValueStorage,
return model.attachments.firstOrNull{it.uuid == uuid}
}

private fun removeAttachmentByPath(filepath: String) {
model.attachments.removeAll{it.filepath == filepath}
private fun removeAttachmentByUUID(uuid: String, removeRemaining: Boolean = false) {
if(!removeRemaining) {
model.attachments.removeAll { it.uuid == uuid }
} else {
val attachment = model.attachments.firstOrNull { it.uuid == uuid } ?: return
model.attachments.remove(attachment)
model.attachments.removeAll(model.attachments.filter { it.uploadProgress == -1 })
}
}

private fun onEmailSavesAsDraft(result: ComposerResult.SaveEmail) {
Expand Down Expand Up @@ -466,7 +472,6 @@ class ComposerController(private val storage: KeyValueStorage,
private fun isReadyForSending() = (model.to.isNotEmpty() || model.cc.isNotEmpty() || model.bcc.isNotEmpty())

private fun uploadSelectedFile(filepath: String, fileKey: String, uuid: String){
model.isUploadingAttachments = true
scene.dismissPreparingFileDialog()
dataSource.submitRequest(ComposerRequest.UploadAttachment(
filepath = filepath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ class ComposerModel(val type: ComposerType, val currentLabel: Label): SceneModel
val filesExceedingMaxEmailSize = mutableListOf<String>()
val filesExceedingMaxFileSize = mutableListOf<Pair<String, String>>()

var isUploadingAttachments = false
val isUploadingAttachments = when {
attachments.isEmpty() -> false
attachments.any { it.uploadProgress == -1 } -> true
attachments.any { it.uploadProgress in 0..99 } -> true
else -> false
}

var to = LinkedList<Contact>()
var cc = LinkedList<Contact>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ sealed class ComposerResult {
data class Success(val filepath: String, val filesSize: Long, val uuid: String): UploadFile()
data class Register(val filepath: String, val filetoken: String, val uuid: String): UploadFile()
data class Progress(val filepath: String, val percentage: Int, val uuid: String): UploadFile()
data class MaxFilesExceeds(val filepath: String): UploadFile()
data class PayloadTooLarge(val filepath: String, val headers: ResultHeaders): UploadFile()
data class Failure(val filepath: String, val message: UIMessage): UploadFile()
data class MaxFilesExceeds(val filepath: String, val uuid: String): UploadFile()
data class PayloadTooLarge(val filepath: String, val headers: ResultHeaders, val uuid: String): UploadFile()
data class Failure(val filepath: String, val message: UIMessage, val uuid: String): UploadFile()
data class Unauthorized(val message: UIMessage): UploadFile()
class Forbidden: UploadFile()
class EnterpriseSuspended(): UploadFile()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class UploadAttachmentWorker(private val filesSize: Long,
ex.errorCode == ServerCodes.Forbidden ->
ComposerResult.UploadFile.Forbidden()
ex.errorCode == ServerCodes.PayloadTooLarge ->
ComposerResult.UploadFile.PayloadTooLarge(filepath, ex.headers!!)
else -> ComposerResult.UploadFile.Failure(filepath, createErrorMessage(ex))
ComposerResult.UploadFile.PayloadTooLarge(filepath, ex.headers!!, uuid)
else -> ComposerResult.UploadFile.Failure(filepath, createErrorMessage(ex), uuid)
}
}
else ComposerResult.UploadFile.Failure(filepath, createErrorMessage(ex))
else ComposerResult.UploadFile.Failure(filepath, createErrorMessage(ex), uuid)


private fun MaxFilesExceeds(): ComposerResult.UploadFile =
ComposerResult.UploadFile.MaxFilesExceeds(filepath)
ComposerResult.UploadFile.MaxFilesExceeds(filepath, uuid)


private fun uploadFile(file: File, reporter: ProgressReporter<ComposerResult.UploadFile>): (String) -> Result<Unit, Exception> = { fileToken ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ComposerControllerDataSourceEventsTest: ComposerControllerTest() {
runAfterSelectingAnAttachment {
clearMocks(host)
simulateAddAttachmentEvent(ComposerResult.UploadFile.Failure(filepath = "/test.pdf",
message = UIMessage(R.string.network_error_exception)))
message = UIMessage(R.string.network_error_exception), uuid = model.attachments[0].uuid))

model.attachments.size `should be` 0
verify { scene.showAttachmentErrorDialog("/test.pdf") }
Expand Down

0 comments on commit f223c59

Please sign in to comment.