I don't usually use Maybe, but I believe that if you are just wanting remove the if, you can try something like this:
getAttachment()
.map { attachment ->
// This only happens if Maybe returns a value
val mail = MailMessage()
mail.setAttachment(attachment)
return@map mail
}
.toSingle(MailMessage())
.map { mail ->
mail.setFrom("...")
// ...
}
.subscribe().addTo(disposables)
If you don't want to create MailMessage() in two places, then you can try something like this:
Single.just(MailMessage().setFrom("..."))
.flatMap { mail ->
getAttachment()
.map { attachment ->
mail.apply {
this.attachment = attachment
}
}.toSingle(mail)
}
The first approach is a little easier to read (in my opinion), but both should work
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…