Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating langnames.json from local file path #1183

Merged
merged 1 commit into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,40 @@
*/
package org.wycliffeassociates.otter.common.domain.languages

import com.fasterxml.jackson.core.JsonFactory
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.reactivex.Observable
import org.wycliffeassociates.otter.common.data.primitives.Language
import org.wycliffeassociates.otter.common.persistence.ILanguageDataSource
import org.wycliffeassociates.otter.common.persistence.LanguagesApi
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.jackson.JacksonConverterFactory
import java.io.File
import javax.inject.Inject

class LanguageDataSource @Inject constructor() : ILanguageDataSource {
override fun fetchLanguageNames(url: String): Observable<List<Language>> {
return if (File(url).exists()) {
fetchLocalFile(url)
} else {
fetchEndpoint(url)
}
}

private fun fetchLocalFile(path: String): Observable<List<Language>> {
return Observable
.fromCallable {
ObjectMapper(JsonFactory())
.registerKotlinModule()
.readValue<List<Language>>(File(path))
}
}

private fun fetchEndpoint(url: String): Observable<List<Language>> {
// Using localhost as a base url is a workaround, because retrofit always requires base url to be set,
// even for full dynamic urls like in this case.
// When retrofit sees that base url and target url are different (scheme, domain),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,26 @@ import org.wycliffeassociates.otter.jvm.device.audio.AudioDeviceProvider
import org.wycliffeassociates.otter.jvm.workbookapp.di.IDependencyGraphProvider
import org.wycliffeassociates.otter.jvm.workbookapp.ui.components.drawer.ThemeColorEvent
import tornadofx.*
import java.io.File
import java.util.concurrent.TimeUnit
import javax.inject.Inject

class SettingsViewModel : ViewModel() {

private val logger = LoggerFactory.getLogger(SettingsViewModel::class.java)

@Inject lateinit var audioDeviceProvider: AudioDeviceProvider
@Inject lateinit var appPrefRepository: IAppPreferencesRepository
@Inject lateinit var pluginRepository: IAudioPluginRepository
@Inject lateinit var localeLanguage: LocaleLanguage
@Inject lateinit var theme: AppTheme
@Inject lateinit var importLanguages: ImportLanguages
@Inject
lateinit var audioDeviceProvider: AudioDeviceProvider
@Inject
lateinit var appPrefRepository: IAppPreferencesRepository
@Inject
lateinit var pluginRepository: IAudioPluginRepository
@Inject
lateinit var localeLanguage: LocaleLanguage
@Inject
lateinit var theme: AppTheme
@Inject
lateinit var importLanguages: ImportLanguages

private val audioPluginViewModel: AudioPluginViewModel by inject()
private val workbookDataStore: WorkbookDataStore by inject()
Expand Down Expand Up @@ -314,13 +321,23 @@ class SettingsViewModel : ViewModel() {
fun updateLanguageNamesUrl() {
val matchRegex = "^https?://.*".toRegex()
val replaceRegex = "^h?t?t?p?s?:?/+(.*)".toRegex()
if (!matchRegex.matches(languageNamesUrlProperty.value)) {
val cleaned = languageNamesUrlProperty.value.replace(replaceRegex, "$1")
languageNamesUrlProperty.set("https://$cleaned")
val url = languageNamesUrlProperty.value

val formattedUrl = when {
File(url).exists() -> {
File(url).invariantSeparatorsPath
}
!matchRegex.matches(url) -> {
val cleaned = url.replace(replaceRegex, "$1")
"https://$cleaned"
}
else -> url
}

languageNamesUrlProperty.set(formattedUrl)

appPrefRepository
.setLanguageNamesUrl(languageNamesUrlProperty.value)
.setLanguageNamesUrl(formattedUrl)
.subscribe()
}

Expand Down
Loading