Skip to content

Commit 0f5ed34

Browse files
committed
refactor(details): use sealed interface for TranslationTarget and improve translation handling
This commit refines the translation logic and search behavior while fixing some minor issues in URL parsing and clipboard detection. Key changes include converting the `TranslationTarget` to a sealed interface for better type safety and improving how translated text chunks are joined to preserve delimiters. - **refactor(details)**: Converted `TranslationTarget` from an `enum` to a `sealed interface`. - **refactor(details)**: Updated `TranslationRepositoryImpl` to correctly preserve delimiters between translated text chunks and added a fallback for parsing failures. - **fix(search)**: Hardened `GITHUB_URL_REGEX` with a negative lookbehind to prevent incorrect matches on prefixed strings. - **fix(search)**: Updated `SearchViewModel` to clear repository results and reset loading states when the query is cleared or multiple links are detected in the clipboard. - **fix(core)**: Changed the default value of `AUTO_DETECT_CLIPBOARD_KEY` from `true` to `false` in `ThemesRepositoryImpl`. - **chore**: Updated `DetailsRoot` to reflect `TranslationTarget` naming changes (`About`/`WhatsNew`).
1 parent 0cc2e2a commit 0f5ed34

6 files changed

Lines changed: 43 additions & 20 deletions

File tree

core/data/src/commonMain/kotlin/zed/rainxch/core/data/repository/ThemesRepositoryImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ThemesRepositoryImpl(
7777

7878
override fun getAutoDetectClipboardLinks(): Flow<Boolean> {
7979
return preferences.data.map { prefs ->
80-
prefs[AUTO_DETECT_CLIPBOARD_KEY] ?: true
80+
prefs[AUTO_DETECT_CLIPBOARD_KEY] ?: false
8181
}
8282
}
8383

feature/details/data/src/commonMain/kotlin/zed/rainxch/details/data/repository/TranslationRepositoryImpl.kt

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,22 @@ class TranslationRepositoryImpl(
3737
cacheMutex.withLock { cache[cacheKey] }?.let { return it }
3838

3939
val chunks = chunkText(text)
40-
val translatedChunks = mutableListOf<String>()
40+
val translatedParts = mutableListOf<Pair<String, String>>()
41+
4142
var detectedLang: String? = null
4243

43-
for ((chunkText, _) in chunks) {
44+
for ((chunkText, delimiter) in chunks) {
4445
val response = translateSingleChunk(chunkText, targetLanguage, sourceLanguage)
45-
translatedChunks.add(response.translatedText)
46+
translatedParts.add(response.translatedText to delimiter)
4647
if (detectedLang == null) {
4748
detectedLang = response.detectedSourceLanguage
4849
}
4950
}
5051

5152
val result = TranslationResult(
52-
translatedText = translatedChunks.joinToString("\n\n"),
53+
translatedText = translatedParts.dropLast(1)
54+
.joinToString("") { (text, delim) -> text + delim } +
55+
translatedParts.lastOrNull()?.first.orEmpty(),
5356
detectedSourceLanguage = detectedLang
5457
)
5558

@@ -82,7 +85,14 @@ class TranslationRepositoryImpl(
8285
parameter("q", text)
8386
}.bodyAsText()
8487

85-
return parseTranslationResponse(responseText)
88+
return try {
89+
parseTranslationResponse(responseText)
90+
} catch (_: Exception) {
91+
TranslationResult(
92+
translatedText = text,
93+
detectedSourceLanguage = null
94+
)
95+
}
8696
}
8797

8898
private fun parseTranslationResponse(responseText: String): TranslationResult {

feature/details/presentation/src/commonMain/kotlin/zed/rainxch/details/presentation/DetailsRoot.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,14 +203,14 @@ fun DetailsScreen(
203203
LanguagePicker(
204204
isVisible = state.isLanguagePickerVisible,
205205
selectedLanguageCode = when (state.languagePickerTarget) {
206-
TranslationTarget.ABOUT -> state.aboutTranslation.targetLanguageCode
207-
TranslationTarget.WHATS_NEW -> state.whatsNewTranslation.targetLanguageCode
206+
TranslationTarget.About -> state.aboutTranslation.targetLanguageCode
207+
TranslationTarget.WhatsNew -> state.whatsNewTranslation.targetLanguageCode
208208
null -> null
209209
},
210210
onLanguageSelected = { language ->
211211
when (state.languagePickerTarget) {
212-
TranslationTarget.ABOUT -> onAction(DetailsAction.TranslateAbout(language.code))
213-
TranslationTarget.WHATS_NEW -> onAction(DetailsAction.TranslateWhatsNew(language.code))
212+
TranslationTarget.About -> onAction(DetailsAction.TranslateAbout(language.code))
213+
TranslationTarget.WhatsNew -> onAction(DetailsAction.TranslateWhatsNew(language.code))
214214
null -> {}
215215
}
216216
onAction(DetailsAction.DismissLanguagePicker)
@@ -272,7 +272,7 @@ fun DetailsScreen(
272272
onAction(DetailsAction.TranslateWhatsNew(state.deviceLanguageCode))
273273
},
274274
onLanguagePickerClick = {
275-
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.WHATS_NEW))
275+
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.WhatsNew))
276276
},
277277
onToggleTranslation = {
278278
onAction(DetailsAction.ToggleWhatsNewTranslation)
@@ -292,7 +292,7 @@ fun DetailsScreen(
292292
onAction(DetailsAction.TranslateAbout(state.deviceLanguageCode))
293293
},
294294
onLanguagePickerClick = {
295-
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.ABOUT))
295+
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.About))
296296
},
297297
onToggleTranslation = {
298298
onAction(DetailsAction.ToggleAboutTranslation)
@@ -312,7 +312,7 @@ fun DetailsScreen(
312312
onAction(DetailsAction.TranslateAbout(state.deviceLanguageCode))
313313
},
314314
onLanguagePickerClick = {
315-
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.ABOUT))
315+
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.About))
316316
},
317317
onToggleTranslation = {
318318
onAction(DetailsAction.ToggleAboutTranslation)
@@ -331,7 +331,7 @@ fun DetailsScreen(
331331
onAction(DetailsAction.TranslateWhatsNew(state.deviceLanguageCode))
332332
},
333333
onLanguagePickerClick = {
334-
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.WHATS_NEW))
334+
onAction(DetailsAction.ShowLanguagePicker(TranslationTarget.WhatsNew))
335335
},
336336
onToggleTranslation = {
337337
onAction(DetailsAction.ToggleWhatsNewTranslation)

feature/details/presentation/src/commonMain/kotlin/zed/rainxch/details/presentation/DetailsState.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ data class DetailsState(
7575
}
7676
}
7777

78-
enum class TranslationTarget {
79-
ABOUT, WHATS_NEW
80-
}
78+
sealed interface TranslationTarget {
79+
data object About : TranslationTarget
80+
data object WhatsNew : TranslationTarget
81+
}

feature/search/presentation/src/commonMain/kotlin/zed/rainxch/search/presentation/SearchViewModel.kt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ class SearchViewModel(
351351
it.copy(
352352
isLoading = false,
353353
isLoadingMore = false,
354-
errorMessage = null
354+
errorMessage = null,
355+
repositories = emptyList(),
356+
totalCount = null
355357
)
356358
}
357359
return
@@ -483,12 +485,22 @@ class SearchViewModel(
483485
return@launch
484486
}
485487
if (links.size == 1) {
486-
_events.send(SearchEvent.NavigateToRepo(links.first().owner, links.first().repo))
488+
_events.send(
489+
SearchEvent.NavigateToRepo(
490+
links.first().owner,
491+
links.first().repo
492+
)
493+
)
487494
} else {
488495
_state.update {
489496
it.copy(
490497
query = clipText,
491498
detectedLinks = links,
499+
repositories = emptyList(),
500+
totalCount = null,
501+
isLoading = false,
502+
isLoadingMore = false,
503+
errorMessage = null,
492504
)
493505
}
494506
}

feature/search/presentation/src/commonMain/kotlin/zed/rainxch/search/presentation/utils/GithubUrlParser.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ data class ParsedGithubLink(
77
)
88

99
private val GITHUB_URL_REGEX = Regex(
10-
"""(?:https?://)?(?:www\.)?github\.com/([a-zA-Z0-9\-_.]+)/([a-zA-Z0-9\-_.]+)"""
10+
"""(?<![A-Za-z0-9.-])(?:https?://)?(?:www\.)?github\.com/([a-zA-Z0-9\-_.]+)/([a-zA-Z0-9\-_.]+)"""
1111
)
1212

1313
fun parseGithubUrls(text: String): List<ParsedGithubLink> {

0 commit comments

Comments
 (0)