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 49b67dc
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 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,21 +92,22 @@ class AiViewModel @Inject constructor(
}

fun splitBodyAndSubject(proposition: String): Pair<String?, String> {
return getSubjectAndContent(MATCH_SUBJECT_REGEX.find(proposition), 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 {
splitBodyAndSubjectAfterAPI25(proposition)
}
}

private fun getSubjectAndContent(match: MatchResult?, proposition: String): Pair<String?, String> {
private fun splitBodyAndSubjectForAPI25(proposition: String): Pair<String?, String> {
val match = MATCH_SUBJECT_REGEX.find(proposition)
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)
val destructuredList = it.destructured.toList()
val subject = destructuredList.getOrNull(INDEX_AI_PROPOSITION_SUBJECT)
val content = destructuredList.getOrNull(INDEX_AI_PROPOSITION_CONTENT)?.trim()
subject to content
} ?: return null to proposition

return if (subject.isNullOrBlank() || content == null) {
null to proposition
Expand All @@ -114,6 +116,18 @@ class AiViewModel @Inject constructor(
}
}

@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 49b67dc

Please sign in to comment.