Skip to content

Commit

Permalink
Code review
Browse files Browse the repository at this point in the history
  • Loading branch information
tevincent committed Jul 11, 2024
1 parent 9f3be64 commit 5d3a11f
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions app/src/main/java/com/infomaniak/mail/ui/newMessage/AiViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package com.infomaniak.mail.ui.newMessage

import android.os.Build
import androidx.annotation.IdRes
import androidx.annotation.RequiresApi
import androidx.annotation.StringRes
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -91,29 +92,36 @@ class AiViewModel @Inject constructor(
}

fun splitBodyAndSubject(proposition: String): Pair<String?, String> {
return getSubjectAndContent(MATCH_SUBJECT_REGEX.find(proposition), proposition)
}

private fun getSubjectAndContent(match: MatchResult?, proposition: String): Pair<String?, String> {
val (subject, content) = match?.let {
// The method get on MatchGroupCollection is not available on API25
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
val destructuredList = it.destructured.toList()
destructuredList[INDEX_AI_PROPOSITION_SUBJECT] to destructuredList[INDEX_AI_PROPOSITION_CONTENT].trim()
} else {
val subject = it.groups["subject"]?.value?.trim()
val content = it.groups["content"]?.value
subject to content
}
} ?: (null to proposition)

return if (subject.isNullOrBlank() || content == null) {
null to proposition
// The method get on MatchGroupCollection is not available on API25
return if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
splitBodyAndSubjectForAPI25(proposition)
} else {
subject to content
splitBodyAndSubjectAfterAPI25(proposition)
}
}

private fun splitBodyAndSubjectForAPI25(proposition: String): Pair<String?, String> {
val match = MATCH_SUBJECT_REGEX.find(proposition)
val destructuredList = match?.destructured?.toList()
val content = destructuredList?.getOrNull(INDEX_AI_PROPOSITION_CONTENT) ?: return null to proposition
val subject = destructuredList.getOrNull(INDEX_AI_PROPOSITION_SUBJECT)?.trim()

if (subject.isNullOrBlank()) return null to proposition

return subject to content
}

@RequiresApi(Build.VERSION_CODES.O)
private fun splitBodyAndSubjectAfterAPI25(proposition: String): Pair<String?, String> {
val match = MATCH_SUBJECT_REGEX.find(proposition)
val content = match?.groups?.get("content")?.value ?: return null to proposition
val subject = match.groups["subject"]?.value?.trim()

if (subject.isNullOrBlank()) return null to proposition

return subject to content
}

private fun handleAiResult(
apiResponse: ApiResponse<AiResult>,
promptMessage: AiMessage?,
Expand Down

0 comments on commit 5d3a11f

Please sign in to comment.