Skip to content

Commit

Permalink
Merge pull request #1961 from Infomaniak/fix-crash-ai-api-25
Browse files Browse the repository at this point in the history
Fix a crash on API25 when using the get method on MatchResult when using AI
  • Loading branch information
tevincent committed Jul 12, 2024
2 parents 831d992 + f926bb9 commit ce6fdaf
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class AiPropositionFragment : Fragment() {
return FragmentAiPropositionBinding.inflate(inflater, container, false).also { _binding = it }.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) = with(binding) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
setSystemBarsColors(statusBarColor = R.color.backgroundColor)

Expand Down Expand Up @@ -175,7 +175,7 @@ class AiPropositionFragment : Fragment() {
findNavController().popBackStack()
}

val (subject, content) = splitBodyAndSubject(getLastMessage())
val (subject, content) = aiViewModel.splitBodyAndSubject(getLastMessage())

if (subject == null || navigationArgs.isSubjectBlank) {
applyProposition(subject, content)
Expand Down Expand Up @@ -208,17 +208,6 @@ class AiPropositionFragment : Fragment() {
}
}

private fun splitBodyAndSubject(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 onMenuItemClicked(menuItemId: Int) = with(aiViewModel) {
val shortcut = Shortcut.entries.find { it.menuId == menuItemId }!!
trackAiWriterEvent(shortcut.matomoValue)
Expand Down Expand Up @@ -381,6 +370,5 @@ class AiPropositionFragment : Fragment() {

companion object {
private const val REPLACEMENT_DURATION = 150L
private val MATCH_SUBJECT_REGEX = Regex("^[^:]+:(?<subject>.+?)\\n\\s*(?<content>.+)", RegexOption.DOT_MATCHES_ALL)
}
}
38 changes: 38 additions & 0 deletions app/src/main/java/com/infomaniak/mail/ui/newMessage/AiViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
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 @@ -89,6 +91,36 @@ class AiViewModel @Inject constructor(
handleAiResult(apiResponse, userMessage, isUsingPreviousMessageAsContext = previousMessageBodyPlainText != null)
}

fun splitBodyAndSubject(proposition: String): Pair<String?, String> {
val match = MATCH_SUBJECT_REGEX.find(proposition)
// The method get on MatchGroupCollection is not available on API25
return if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
splitBodyAndSubjectForAPI25(match, proposition)
} else {
splitBodyAndSubjectAfterAPI25(match, proposition)
}
}

private fun splitBodyAndSubjectForAPI25(match: MatchResult?, proposition: String): Pair<String?, String> {
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(match: MatchResult?, proposition: String): Pair<String?, String> {
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 Expand Up @@ -162,4 +194,10 @@ class AiViewModel @Inject constructor(
RATE_LIMIT_EXCEEDED(R.string.aiErrorTooManyRequests),
MISSING_CONTENT(R.string.aiErrorUnknown),
}

companion object {
private val MATCH_SUBJECT_REGEX = Regex("^[^:]+:(?<subject>.+?)\\n\\s*(?<content>.+)", RegexOption.DOT_MATCHES_ALL)
private val INDEX_AI_PROPOSITION_SUBJECT = 0
private val INDEX_AI_PROPOSITION_CONTENT = 1
}
}

0 comments on commit ce6fdaf

Please sign in to comment.