Skip to content

Commit

Permalink
refactor: Factorize Draft related ApiRepository functions
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Jan 30, 2025
1 parent fe1e85c commit 5e55fdc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 26 deletions.
33 changes: 9 additions & 24 deletions app/src/main/java/com/infomaniak/mail/data/api/ApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ import com.infomaniak.mail.data.models.thread.ThreadResult
import com.infomaniak.mail.ui.newMessage.AiViewModel.Shortcut
import com.infomaniak.mail.utils.Utils
import io.realm.kotlin.ext.copyFromRealm
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import okhttp3.MultipartBody
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -170,37 +169,23 @@ object ApiRepository : ApiRepositoryCore() {
}

fun saveDraft(mailboxUuid: String, draft: Draft, okHttpClient: OkHttpClient): ApiResponse<SaveDraftResult> {

val body = getDraftBody(draft)

fun postDraft(): ApiResponse<SaveDraftResult> = callApi(ApiRoutes.draft(mailboxUuid), POST, body, okHttpClient)

fun putDraft(uuid: String): ApiResponse<SaveDraftResult> =
callApi(ApiRoutes.draft(mailboxUuid, uuid), PUT, body, okHttpClient)

return draft.remoteUuid?.let(::putDraft) ?: run(::postDraft)
return uploadDraft(mailboxUuid, draft, okHttpClient)
}

fun sendDraft(mailboxUuid: String, draft: Draft, okHttpClient: OkHttpClient): ApiResponse<SendDraftResult> {

val body = getDraftBody(draft)

fun postDraft(): ApiResponse<SendDraftResult> = callApi(ApiRoutes.draft(mailboxUuid), POST, body, okHttpClient)

fun putDraft(uuid: String): ApiResponse<SendDraftResult> =
callApi(ApiRoutes.draft(mailboxUuid, uuid), PUT, body, okHttpClient)

return draft.remoteUuid?.let(::putDraft) ?: run(::postDraft)
return uploadDraft(mailboxUuid, draft, okHttpClient)
}

fun scheduleDraft(mailboxUuid: String, draft: Draft, okHttpClient: OkHttpClient): ApiResponse<ScheduleDraftResult> {
return uploadDraft(mailboxUuid, draft, okHttpClient)
}

val body = getDraftBody(draft)
private fun <T> uploadDraft(mailboxUuid: String, draft: Draft, okHttpClient: OkHttpClient): ApiResponse<T> {

fun postDraft(): ApiResponse<ScheduleDraftResult> = callApi(ApiRoutes.draft(mailboxUuid), POST, body, okHttpClient)
val body = getDraftBody(draft)

fun putDraft(uuid: String): ApiResponse<ScheduleDraftResult> =
callApi(ApiRoutes.draft(mailboxUuid, uuid), PUT, body, okHttpClient)
fun putDraft(uuid: String): ApiResponse<T> = callApi(ApiRoutes.draft(mailboxUuid, uuid), PUT, body, okHttpClient)
fun postDraft(): ApiResponse<T> = callApi(ApiRoutes.draft(mailboxUuid), POST, body, okHttpClient)

return draft.remoteUuid?.let(::putDraft) ?: run(::postDraft)
}
Expand Down Expand Up @@ -252,7 +237,7 @@ object ApiRepository : ApiRepositoryCore() {
return callApi(ApiRoutes.draft(mailboxUuid, remoteDraftUuid), DELETE)
}

fun deleteScheduleDraft(scheduleAction: String): ApiResponse<Unit> {
fun deleteScheduledDraft(scheduleAction: String): ApiResponse<Unit> {
return callApi(ApiRoutes.resource(scheduleAction), DELETE)
}

Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/infomaniak/mail/ui/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ class MainViewModel @Inject constructor(

fun deleteScheduleDraft(scheduleAction: String) = viewModelScope.launch(ioCoroutineContext) {
val mailbox = currentMailbox.value!!
val apiResponse = ApiRepository.deleteScheduleDraft(scheduleAction)
val apiResponse = ApiRepository.deleteScheduledDraft(scheduleAction)

if (apiResponse.isSuccess()) {
val draftFolderId = folderController.getFolder(FolderRole.SCHEDULED_DRAFTS)!!.id
Expand Down Expand Up @@ -662,7 +662,7 @@ class MainViewModel @Inject constructor(
fun modifyDraft(scheduleAction: String, draftResource: String, onSuccess: () -> Unit) =
viewModelScope.launch(ioCoroutineContext) {
val mailbox = currentMailbox.value!!
val apiResponse = ApiRepository.deleteScheduleDraft(scheduleAction)
val apiResponse = ApiRepository.deleteScheduledDraft(scheduleAction)

if (apiResponse.isSuccess()) {
val draftFolderId = folderController.getFolder(FolderRole.SCHEDULED_DRAFTS)!!.id
Expand Down

0 comments on commit 5e55fdc

Please sign in to comment.