Skip to content

Commit

Permalink
updating langnames.json from local file path (#1183)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnonymousWalker authored Sep 11, 2024
1 parent 07c74bd commit 41dabf9
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
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

0 comments on commit 41dabf9

Please sign in to comment.