Skip to content

Commit

Permalink
fix: Make factorized uploadDraft() works
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinBoulongne committed Jan 30, 2025
1 parent e389ca9 commit d52e821
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions app/src/main/java/com/infomaniak/mail/data/api/ApiRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,24 @@ object ApiRepository : ApiRepositoryCore() {
return uploadDraft(mailboxUuid, draft, okHttpClient)
}

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

private inline fun <reified T> uploadDraft(mailboxUuid: String, draft: Draft, okHttpClient: OkHttpClient): ApiResponse<T> {
val body = getDraftBody(draft)
return draft.remoteUuid?.let { putDraft(mailboxUuid, body, okHttpClient, it) }
?: run { postDraft(mailboxUuid, 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)
private inline fun <reified T> putDraft(
mailboxUuid: String,
body: String,
okHttpClient: OkHttpClient,
uuid: String,
): ApiResponse<T> = callApi(ApiRoutes.draft(mailboxUuid, uuid), PUT, body, okHttpClient)

return draft.remoteUuid?.let(::putDraft) ?: run(::postDraft)
}
private inline fun <reified T> postDraft(
mailboxUuid: String,
body: String,
okHttpClient: OkHttpClient,
): ApiResponse<T> = callApi(ApiRoutes.draft(mailboxUuid), POST, body, okHttpClient)

private fun getDraftBody(draft: Draft): String {
val updatedDraft = if (draft.identityId == Draft.NO_IDENTITY.toString()) {
Expand Down

0 comments on commit d52e821

Please sign in to comment.