Skip to content

Commit

Permalink
Merge pull request #549 from jorgeblacio/release_0_21_13
Browse files Browse the repository at this point in the history
Various bug fixes.
  • Loading branch information
danieltigse authored Sep 26, 2019
2 parents 09d7410 + 3b2646d commit 139b1d7
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ abstract class BackgroundWorkManager<in I: Any, O: Any> {
var listener: ((O) -> Unit)? = null
set (newListener) {
if (newListener != null) {
state.keys.forEach({ key ->
state.keys.forEach { key ->
val workState = state[key]
if (workState is WorkState.Done) {
state.remove(key)
newListener(workState.result)
}
})
}
}
field = newListener
}
Expand All @@ -35,6 +35,4 @@ abstract class BackgroundWorkManager<in I: Any, O: Any> {
runner.workInBackground(worker)
}
}


}
5 changes: 5 additions & 0 deletions src/main/kotlin/com/criptext/mail/db/dao/AccountDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,9 @@ interface AccountDao {
where recipientId=:recipientId AND domain=:domain""")
fun updateRefreshToken(recipientId: String, domain: String, token: String)

@Query("""UPDATE account
SET signature=:signature
where id=:id""")
fun updateSignature(id: Long, signature: String)

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,15 @@ class NewMailActionService : IntentService("New Mail Action Service") {
val manager = this.applicationContext
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val db = AppDatabase.getAppDatabase(this)
if(activeAccount.userEmail != data.recipientId.plus("@${data.domain}"))
activeAccount = ActiveAccount.loadFromDB(db.accountDao().getAccount(data.recipientId, data.domain)!!)!!
if(activeAccount.userEmail != data.recipientId.plus("@${data.domain}")) {
val account = db.accountDao().getAccount(data.recipientId, data.domain)
if(account != null){
activeAccount = ActiveAccount.loadFromDB(account)!!
} else {
deletePush(storage, manager)
return
}
}
val requestHandler = PushAPIRequestHandler(NotificationError(this), manager,
activeAccount, HttpClient.Default(),
storage)
Expand All @@ -49,17 +56,21 @@ class NewMailActionService : IntentService("New Mail Action Service") {
EmailDetailLocalDB.Default(db, this.filesDir), db.emailDao(), db.pendingEventDao(), db.accountDao())
}
DELETE -> {
val notCount = storage.getInt(KeyValueStorage.StringKey.NewMailNotificationCount, 0)
if((notCount - 1) <= 0) {
manager.cancel(CriptextNotification.INBOX_ID)
}
storage.putInt(KeyValueStorage.StringKey.NewMailNotificationCount,
if(notCount <= 0) 0 else notCount - 1)
deletePush(storage, manager)
}
else -> throw IllegalArgumentException("Unsupported action: " + data.action)
}
}

private fun deletePush(storage: KeyValueStorage, manager: NotificationManager){
val notCount = storage.getInt(KeyValueStorage.StringKey.NewMailNotificationCount, 0)
if((notCount - 1) <= 0) {
manager.cancel(CriptextNotification.INBOX_ID)
}
storage.putInt(KeyValueStorage.StringKey.NewMailNotificationCount,
if(notCount <= 0) 0 else notCount - 1)
}

private fun getIntentData(intent: Intent?, activeRecipientId: String): IntentData {
val action = intent!!.action ?: ""
val notificationId = intent.getIntExtra("notificationId", 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class SaveEmailWorker(

private fun getEmailPreview(emailId: Long): EmailThread {
val email = db.loadFullEmail(emailId, account)!!
val label = db.getLabelById(currentLabel.id, account.id)!!
val label = db.getLabelById(currentLabel.id, account.id) ?: Label.defaultItems.inbox
return db.getEmailThreadFromEmail(email.email, label.text, Label.defaultItems.rejectedLabelsByFolder(label.text).map { it.id }, account.userEmail, account)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ class MailboxSceneController(private val scene: MailboxScene,
scene.showExtraAccountsBadge(false)
scene.hideMultipleAccountsMenu()
threadListController.clear()
model.threads.clear()
dataSource.submitRequest(MailboxRequest.SetActiveAccount(account))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class SendMailWorker(private val signalClient: SignalClient,
return when (finalResult) {
is Result.Success -> {
db.increaseContactScore(listOf(emailId))
val label = db.getLabelById(currentLabel.id, activeAccount.id)!!
val label = db.getLabelById(currentLabel.id, activeAccount.id) ?: Label.defaultItems.inbox
val thread = db.getEmailThreadFromEmail(currentEmail!!, label.text, Label.defaultItems.rejectedLabelsByFolder(label.text).map { it.id }, activeAccount.userEmail, activeAccount)
MailboxResult.SendMail.Success(EmailPreview.fromEmailThread(thread), currentLabel, isSecure)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ class SettingsDataSource(
activeAccount = activeAccount,
publishFn = { res -> flushResults(res) }
)
is SettingsRequest.UpdateSignature -> UpdateSignatureWorker(
signature = params.signature,
accountDao = settingsLocalDB.accountDao,
activeAccount = activeAccount,
httpClient = httpClient,
publishFn = { res -> flushResults(res) }
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.criptext.mail.scenes.settings.data
sealed class SettingsRequest{
class ResetPassword: SettingsRequest()
class SyncBegin: SettingsRequest()
data class UpdateSignature(val signature: String): SettingsRequest()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.criptext.mail.scenes.settings.data

import com.criptext.mail.db.models.Label
import com.criptext.mail.utils.UIMessage

sealed class SettingsResult{
Expand All @@ -14,4 +13,9 @@ sealed class SettingsResult{
data class NoDevicesAvailable(val message: UIMessage): SyncBegin()
data class Failure(val message: UIMessage): SyncBegin()
}

sealed class UpdateSignature: SettingsResult() {
class Success: UpdateSignature()
data class Failure(val message: UIMessage): UpdateSignature()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class SignatureController(

private fun updateSignature(){
activeAccount.updateSignature(storage, scene.getSignature())
dataSource.submitRequest(SettingsRequest.UpdateSignature(scene.getSignature()))
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.criptext.mail.scenes.settings.workers

import com.criptext.mail.R
import com.criptext.mail.api.HttpClient
import com.criptext.mail.api.HttpErrorHandlingHelper
import com.criptext.mail.api.ServerErrorException
import com.criptext.mail.bgworker.BackgroundWorker
import com.criptext.mail.bgworker.ProgressReporter
import com.criptext.mail.db.KeyValueStorage
import com.criptext.mail.db.dao.AccountDao
import com.criptext.mail.db.models.ActiveAccount
import com.criptext.mail.scenes.settings.data.SettingsAPIClient
import com.criptext.mail.scenes.settings.data.SettingsResult
import com.criptext.mail.utils.EmailAddressUtils
import com.criptext.mail.utils.ServerCodes
import com.criptext.mail.utils.UIMessage
import com.github.kittinunf.result.Result
import com.github.kittinunf.result.mapError


class UpdateSignatureWorker(val httpClient: HttpClient,
val activeAccount: ActiveAccount,
private val signature: String,
private val accountDao: AccountDao,
override val publishFn: (SettingsResult) -> Unit)
: BackgroundWorker<SettingsResult.UpdateSignature> {

override val canBeParallelized = true

override fun catchException(ex: Exception): SettingsResult.UpdateSignature {
return SettingsResult.UpdateSignature.Failure(createErrorMessage(ex))
}

override fun work(reporter: ProgressReporter<SettingsResult.UpdateSignature>): SettingsResult.UpdateSignature? {
val result = Result.of {
accountDao.updateSignature(activeAccount.id, signature)
}

return when (result) {
is Result.Success -> SettingsResult.UpdateSignature.Success()
is Result.Failure -> catchException(result.error)
}

}

override fun cancel() {
TODO("not implemented") //To change body of created functions use CRFile | Settings | CRFile Templates.
}

private val createErrorMessage: (ex: Exception) -> UIMessage = { ex ->
UIMessage(R.string.local_error, arrayOf(ex.toString()))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ class SignUpSceneController(
}

private fun handleRegisterUserFailure(result: SignUpResult.RegisterUser.Failure) {
scene.initListeners(uiObserver)
scene.showError(result.message)
resetWidgetsFromModel()
when(result.exception)
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/com/criptext/mail/utils/AccountUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import com.criptext.mail.db.models.ActiveAccount
object AccountUtils {
fun setUserAsActiveAccount(user: Account, storage: KeyValueStorage): ActiveAccount{
val activeAccount = ActiveAccount(id = user.id, name = user.name, recipientId = user.recipientId,
deviceId = user.deviceId, jwt = user.jwt, signature = "", refreshToken = user.refreshToken,
deviceId = user.deviceId, jwt = user.jwt, signature = user.signature, refreshToken = user.refreshToken,
domain = user.domain)
storage.putString(KeyValueStorage.StringKey.ActiveAccount,
activeAccount.toJSON().toString())
Expand Down

0 comments on commit 139b1d7

Please sign in to comment.