Skip to content

Commit 53c4185

Browse files
committed
Added a fix for adding files in the composer which had a couple of issues.
1 parent 66e6c51 commit 53c4185

File tree

27 files changed

+133
-184
lines changed

27 files changed

+133
-184
lines changed

src/androidTest/kotlin/com/criptext/mail/scenes/composer/data/UploadAttachmentWorkerTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ class UploadAttachmentWorkerTest {
6969
private fun newWorker(filepath: String): UploadAttachmentWorker =
7070
UploadAttachmentWorker(filepath = filepath, activeAccount = activeAccount,
7171
httpClient = httpClient, publishFn = {}, fileKey = null,
72-
accountDao = db.accountDao(), filesSize = 0L, storage = storage)
72+
accountDao = db.accountDao(), filesSize = 0L, storage = storage,
73+
uuid = "_UUID_")
7374

7475
@Test
7576
fun should_upload_file_without_errors() {

src/androidTest/kotlin/com/criptext/mail/scenes/emaildetail/data/DownloadAttachmentWorkerTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class DownloadAttachmentWorkerTest {
9595
private fun newWorker(filepath: String): UploadAttachmentWorker =
9696
UploadAttachmentWorker(filepath = filepath, activeAccount = activeAccount,
9797
httpClient = httpClient, publishFn = {}, fileKey = null, accountDao = db.accountDao(),
98-
storage = storage, filesSize = 0L)
98+
storage = storage, filesSize = 0L, uuid = "_UUID_")
9999

100100
private fun newDownloadWorker(filetoken: String): DownloadAttachmentWorker =
101101
DownloadAttachmentWorker(fileToken = filetoken, emailId = 0,

src/androidTest/kotlin/com/criptext/mail/scenes/mailbox/data/SendEmailWorkerTest.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ import org.junit.Test
3131
import org.junit.rules.TemporaryFolder
3232
import org.junit.runner.RunWith
3333
import org.whispersystems.libsignal.SignalProtocolAddress
34+
import java.util.*
35+
import kotlin.collections.ArrayList
3436

3537

3638
/**
@@ -215,6 +217,7 @@ class SendEmailWorkerTest {
215217
val file = folder.newFile(String.format("file_%s.png", i))
216218
attachmentList.add(ComposerAttachment(
217219
id=0,
220+
uuid = UUID.randomUUID().toString(),
218221
filepath=file.name,
219222
uploadProgress=100,
220223
filetoken="__FILE_TOKEN__",

src/androidTest/kotlin/com/criptext/mail/scenes/settings/data/LogoutWorkerTest.kt

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import androidx.test.runner.AndroidJUnit4
55
import com.criptext.mail.androidtest.TestActivity
66
import com.criptext.mail.androidtest.TestDatabase
77
import com.criptext.mail.api.HttpClient
8+
import com.criptext.mail.db.EventLocalDB
89
import com.criptext.mail.db.KeyValueStorage
910
import com.criptext.mail.db.MailboxLocalDB
1011
import com.criptext.mail.db.SettingsLocalDB
@@ -13,9 +14,10 @@ import com.criptext.mail.db.models.ActiveAccount
1314
import com.criptext.mail.db.models.Contact
1415
import com.criptext.mail.db.models.Label
1516
import com.criptext.mail.mocks.MockEmailData
16-
import com.criptext.mail.scenes.settings.workers.LogoutWorker
1717
import com.criptext.mail.utils.MockedResponse
1818
import com.criptext.mail.utils.enqueueResponses
19+
import com.criptext.mail.utils.generaldatasource.data.GeneralResult
20+
import com.criptext.mail.utils.generaldatasource.workers.LogoutWorker
1921
import io.mockk.mockk
2022
import okhttp3.mockwebserver.MockWebServer
2123
import org.amshove.kluent.shouldBe
@@ -33,7 +35,7 @@ class LogoutWorkerTest{
3335

3436
private lateinit var db: TestDatabase
3537
private lateinit var mailboxLocalDB: MailboxLocalDB
36-
private lateinit var settingsLocalDB: SettingsLocalDB
38+
private lateinit var eventLocalDB: EventLocalDB
3739
private lateinit var storage: KeyValueStorage
3840
private val activeAccount = ActiveAccount(name = "Tester", recipientId = "tester",
3941
deviceId = 1, jwt = "__JWTOKEN__", signature = "", refreshToken = "", id = 1,
@@ -59,7 +61,7 @@ class LogoutWorkerTest{
5961
signature = "", refreshToken = "__REFRESH__", isActive = true, domain = "criptext.com", isLoggedIn = true,
6062
backupPassword = null, autoBackupFrequency = 0, hasCloudBackup = false, wifiOnly = true, lastTimeBackup = null))
6163
mailboxLocalDB = MailboxLocalDB.Default(db, mActivityRule.activity.filesDir)
62-
settingsLocalDB = SettingsLocalDB.Default(db)
64+
eventLocalDB = EventLocalDB(db, mActivityRule.activity.filesDir, mActivityRule.activity.cacheDir)
6365

6466
MockEmailData.insertEmailsNeededForTests(db, listOf(Label.defaultItems.inbox),
6567
mActivityRule.activity.filesDir, activeAccount.recipientId, accountId = activeAccount.id,
@@ -81,7 +83,7 @@ class LogoutWorkerTest{
8183

8284
val worker = newWorker()
8385

84-
worker.work(mockk()) as SettingsResult.Logout.Success
86+
worker.work(mockk()) as GeneralResult.Logout.Success
8587

8688
mailboxLocalDB.getThreadsIdsFromLabel(
8789
labelName = Label.defaultItems.inbox.text,
@@ -93,10 +95,12 @@ class LogoutWorkerTest{
9395
private fun newWorker(): LogoutWorker =
9496

9597
LogoutWorker(
96-
db = settingsLocalDB,
98+
db = eventLocalDB,
9799
httpClient = httpClient,
98100
activeAccount = activeAccount,
99101
storage = storage,
102+
accountDao = db.accountDao(),
103+
shouldDeleteAllData = false,
100104
publishFn = {})
101105

102106
@After

src/main/kotlin/com/criptext/mail/db/dao/SignUpDao.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ interface SignUpDao {
2929
registrationId=:registrationId,
3030
domain=:domain,
3131
isActive=:isActive,
32-
isLoggedIn=:isLoggedIn
32+
isLoggedIn=:isLoggedIn,
33+
hasCloudBackup=:hasCloudBackup,
34+
wifiOnly=:wifiOnly,
35+
autoBackupFrequency=:backupFrequency
3336
WHERE recipientId=:recipientId
3437
""")
3538
fun updateAccount(recipientId: String, name: String, jwt: String, refreshJwt: String,
3639
deviceId: Int, identityKey: String, registrationId: Int, domain: String,
37-
isActive: Int, isLoggedIn: Int)
40+
isActive: Int, isLoggedIn: Int, hasCloudBackup: Boolean, wifiOnly: Boolean, backupFrequency: Int)
3841

3942
@Insert
4043
fun insertPreKeys(preKeys : List<CRPreKey>)
@@ -68,7 +71,8 @@ interface SignUpDao {
6871
updateAccount(recipientId = account.recipientId, name = account.name, deviceId = account.deviceId,
6972
domain = account.domain, isLoggedIn = if(account.isLoggedIn) 1 else 0, isActive = if(account.isActive) 1 else 0,
7073
identityKey = account.identityKeyPairB64, jwt = account.jwt, refreshJwt = account.refreshToken,
71-
registrationId = account.registrationId)
74+
registrationId = account.registrationId, backupFrequency = account.autoBackupFrequency, hasCloudBackup = account.hasCloudBackup,
75+
wifiOnly = account.wifiOnly)
7276

7377
val savedAccount = accountDao.getLoggedInAccount()!!
7478
preKeyList.forEach { it.accountId = savedAccount.id }

src/main/kotlin/com/criptext/mail/scenes/composer/ComposerController.kt

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package com.criptext.mail.scenes.composer
22

33

44
import android.Manifest
5-
import android.content.DialogInterface
65
import android.content.pm.PackageManager
76
import android.view.View
87
import com.criptext.mail.BaseActivity
@@ -19,20 +18,17 @@ import com.criptext.mail.scenes.ActivityMessage
1918
import com.criptext.mail.scenes.SceneController
2019
import com.criptext.mail.scenes.composer.data.*
2120
import com.criptext.mail.scenes.composer.ui.ComposerUIObserver
22-
import com.criptext.mail.scenes.mailbox.data.EmailThread
2321
import com.criptext.mail.scenes.params.EmailDetailParams
2422
import com.criptext.mail.scenes.params.LinkingParams
2523
import com.criptext.mail.scenes.params.MailboxParams
2624
import com.criptext.mail.scenes.params.SignInParams
27-
import com.criptext.mail.scenes.settings.data.SettingsRequest
2825
import com.criptext.mail.utils.*
2926
import com.criptext.mail.utils.file.FileUtils
3027
import com.criptext.mail.utils.generaldatasource.data.GeneralDataSource
3128
import com.criptext.mail.utils.generaldatasource.data.GeneralRequest
3229
import com.criptext.mail.utils.generaldatasource.data.GeneralResult
3330
import com.criptext.mail.utils.ui.data.DialogResult
3431
import com.criptext.mail.utils.ui.data.DialogType
35-
import com.criptext.mail.websocket.WebSocketSingleton
3632
import java.io.File
3733
import java.util.*
3834

@@ -244,7 +240,7 @@ class ComposerController(private val storage: KeyValueStorage,
244240
EmailAddressUtils.extractEmailAddressDomain(email) in
245241
checkedData.filter { it.isCriptextDomain }
246242
.map { it.name } }
247-
contacts.forEachIndexed { index, contact ->
243+
contacts.forEachIndexed { _, contact ->
248244
if(contact.email in isCriptext)
249245
contact.isCriptextDomain = true
250246
}
@@ -272,7 +268,7 @@ class ComposerController(private val storage: KeyValueStorage,
272268
when (result) {
273269
is GeneralResult.GetRemoteFile.Success -> {
274270
scene.dismissPreparingFileDialog()
275-
model.attachments.addAll(result.remoteFiles.map { ComposerAttachment(it.first, it.second, model.fileKey!!) })
271+
model.attachments.addAll(result.remoteFiles.map { ComposerAttachment(UUID.randomUUID().toString(), it.first, it.second, model.fileKey!!) })
276272
scene.notifyAttachmentSetChanged()
277273
handleNextUpload()
278274
}
@@ -297,15 +293,15 @@ class ComposerController(private val storage: KeyValueStorage,
297293
private fun onUploadFile(result: ComposerResult.UploadFile){
298294
when (result) {
299295
is ComposerResult.UploadFile.Register -> {
300-
val composerAttachment = getAttachmentByPath(result.filepath) ?: return
296+
val composerAttachment = getAttachmentByUUID(result.uuid) ?: return
301297
composerAttachment.filetoken = result.filetoken
302298
}
303299
is ComposerResult.UploadFile.Progress -> {
304-
val composerAttachment = getAttachmentByPath(result.filepath) ?: return
300+
val composerAttachment = getAttachmentByUUID(result.uuid) ?: return
305301
composerAttachment.uploadProgress = result.percentage
306302
}
307303
is ComposerResult.UploadFile.Success -> {
308-
val composerAttachment = getAttachmentByPath(result.filepath)
304+
val composerAttachment = getAttachmentByUUID(result.uuid)
309305
composerAttachment?.uploadProgress = 100
310306
model.isUploadingAttachments = false
311307
model.filesSize = result.filesSize
@@ -339,8 +335,8 @@ class ComposerController(private val storage: KeyValueStorage,
339335
scene.notifyAttachmentSetChanged()
340336
}
341337

342-
private fun getAttachmentByPath(filepath: String): ComposerAttachment? {
343-
return model.attachments.firstOrNull{it.filepath == filepath}
338+
private fun getAttachmentByUUID(uuid: String): ComposerAttachment? {
339+
return model.attachments.firstOrNull{it.uuid == uuid}
344340
}
345341

346342
private fun removeAttachmentByPath(filepath: String) {
@@ -469,13 +465,14 @@ class ComposerController(private val storage: KeyValueStorage,
469465

470466
private fun isReadyForSending() = (model.to.isNotEmpty() || model.cc.isNotEmpty() || model.bcc.isNotEmpty())
471467

472-
private fun uploadSelectedFile(filepath: String, fileKey: String){
468+
private fun uploadSelectedFile(filepath: String, fileKey: String, uuid: String){
473469
model.isUploadingAttachments = true
474470
scene.dismissPreparingFileDialog()
475471
dataSource.submitRequest(ComposerRequest.UploadAttachment(
476472
filepath = filepath,
477473
fileKey = fileKey,
478-
filesSize = model.filesSize
474+
filesSize = model.filesSize,
475+
uuid = uuid
479476
))
480477
}
481478

@@ -540,7 +537,7 @@ class ComposerController(private val storage: KeyValueStorage,
540537
val localAttachments = filesMetadata
541538
.filter(isNewAttachment)
542539
.filter {it.second != -1L}
543-
.map{ComposerAttachment(it.first, it.second, model.fileKey!!)}
540+
.map{ComposerAttachment(UUID.randomUUID().toString(), it.first, it.second, model.fileKey!!)}
544541
val remoteAttachments = filesMetadata
545542
.filter(isNewAttachment)
546543
.filter{ it.second == -1L }
@@ -563,13 +560,13 @@ class ComposerController(private val storage: KeyValueStorage,
563560
return
564561
}
565562
val attachmentToUpload = model.attachments.firstOrNull { it.uploadProgress == -1 } ?: return
566-
val composerAttachment = getAttachmentByPath(attachmentToUpload.filepath)
563+
val composerAttachment = getAttachmentByUUID(attachmentToUpload.uuid)
567564
if(composerAttachment == null){
568565
scene.showMaxFilesExceedsDialog()
569566
return
570567
}else {
571568
composerAttachment.uploadProgress = 0
572-
uploadSelectedFile(attachmentToUpload.filepath, composerAttachment.fileKey)
569+
uploadSelectedFile(attachmentToUpload.filepath, composerAttachment.fileKey, composerAttachment.uuid)
573570
}
574571
}
575572

src/main/kotlin/com/criptext/mail/scenes/composer/data/ComposerAttachment.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@ import com.criptext.mail.db.AttachmentTypes
44
import com.criptext.mail.utils.file.FileUtils
55
import org.json.JSONArray
66
import org.json.JSONObject
7+
import java.util.*
78

8-
data class ComposerAttachment(val id: Long, val filepath: String, var uploadProgress: Int,
9+
data class ComposerAttachment(val id: Long, val uuid: String, val filepath: String, var uploadProgress: Int,
910
var filetoken: String, val type: AttachmentTypes, var size: Long,
1011
val fileKey: String, val cid: String?) {
11-
constructor(filepath: String, size: Long, fileKey: String): this (0, filepath, -1, filetoken = "",
12+
constructor(uuid: String, filepath: String, size: Long, fileKey: String): this (0, uuid ,filepath, -1, filetoken = "",
1213
type = FileUtils.getAttachmentTypeFromPath(filepath), size = size, fileKey = fileKey, cid = null)
1314

1415
companion object{
1516
fun fromJSON(jsonString: String): ComposerAttachment{
1617
val json = JSONObject(jsonString)
1718
return ComposerAttachment(
1819
filepath = json.getString("filepath"),
20+
uuid = json.getString("uuid"),
1921
size = json.getLong("size"),
2022
fileKey = json.getString("fileKey")
2123
)
@@ -35,6 +37,7 @@ data class ComposerAttachment(val id: Long, val filepath: String, var uploadProg
3537
attachments.forEach {
3638
val json = JSONObject()
3739
json.put("id", it.id)
40+
json.put("uuid", it.uuid)
3841
json.put("filepath", it.filepath)
3942
json.put("uploadProgress", it.uploadProgress)
4043
json.put("filetoken", it.filetoken)

src/main/kotlin/com/criptext/mail/scenes/composer/data/ComposerDataSource.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class ComposerDataSource(
5454
filepath = params.filepath,
5555
httpClient = httpClient, activeAccount = activeAccount,
5656
publishFn = { res -> flushResults(res) }, fileKey = params.fileKey,
57-
accountDao = composerLocalDB.accountDao, storage = storage)
57+
accountDao = composerLocalDB.accountDao, storage = storage, uuid = params.uuid)
5858
is ComposerRequest.LoadInitialData -> LoadInitialDataWorker(
5959
httpClient = HttpClient.Default(),
6060
db = composerLocalDB,

src/main/kotlin/com/criptext/mail/scenes/composer/data/ComposerRequest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ sealed class ComposerRequest {
1515
val onlySave: Boolean, val attachments: List<ComposerAttachment>,
1616
val fileKey: String?, val originalId: Long?, val senderAccount: ActiveAccount? = null,
1717
val currentLabel: Label): ComposerRequest()
18-
class UploadAttachment(val filepath: String, val fileKey: String?, val filesSize: Long): ComposerRequest()
18+
class UploadAttachment(val filepath: String, val fileKey: String?, val filesSize: Long, val uuid: String): ComposerRequest()
1919
class LoadInitialData(val composerType: ComposerType, val emailId: Long): ComposerRequest()
2020
data class CheckDomain(val emails: List<String>) : ComposerRequest()
2121
}

src/main/kotlin/com/criptext/mail/scenes/composer/data/ComposerResult.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.criptext.mail.db.models.Account
55
import com.criptext.mail.db.models.Contact
66
import com.criptext.mail.email_preview.EmailPreview
77
import com.criptext.mail.utils.UIMessage
8+
import java.util.*
89

910
/**
1011
* Created by gabriel on 2/26/18.
@@ -38,9 +39,9 @@ sealed class ComposerResult {
3839
}
3940

4041
sealed class UploadFile : ComposerResult() {
41-
data class Success(val filepath: String, val filesSize: Long): UploadFile()
42-
data class Register(val filepath: String, val filetoken: String): UploadFile()
43-
data class Progress(val filepath: String, val percentage: Int): UploadFile()
42+
data class Success(val filepath: String, val filesSize: Long, val uuid: String): UploadFile()
43+
data class Register(val filepath: String, val filetoken: String, val uuid: String): UploadFile()
44+
data class Progress(val filepath: String, val percentage: Int, val uuid: String): UploadFile()
4445
data class MaxFilesExceeds(val filepath: String): UploadFile()
4546
data class PayloadTooLarge(val filepath: String, val headers: ResultHeaders): UploadFile()
4647
data class Failure(val filepath: String, val message: UIMessage): UploadFile()

0 commit comments

Comments
 (0)