diff --git a/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepository.kt b/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepository.kt index 4760f1f4c75..7692cae58aa 100644 --- a/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepository.kt +++ b/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepository.kt @@ -1,5 +1,6 @@ package com.nextcloud.talk.translate.repositories +import com.nextcloud.talk.translate.repositories.model.Language import io.reactivex.Observable interface TranslateRepository { @@ -15,5 +16,5 @@ interface TranslateRepository { fun getLanguages( authorization: String, url: String - ): Observable> + ): Observable> } diff --git a/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepositoryImpl.kt b/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepositoryImpl.kt index 288919e5305..8828f33aba8 100644 --- a/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepositoryImpl.kt +++ b/app/src/main/java/com/nextcloud/talk/translate/repositories/TranslateRepositoryImpl.kt @@ -1,6 +1,7 @@ package com.nextcloud.talk.translate.repositories import com.nextcloud.talk.api.NcApi +import com.nextcloud.talk.translate.repositories.model.Language import io.reactivex.Observable import javax.inject.Inject @@ -16,7 +17,7 @@ class TranslateRepositoryImpl @Inject constructor(private val ncApi: NcApi) : Tr return ncApi.translateMessage(authorization, url, text, toLanguage, fromLanguage).map { it.ocs?.data!!.text } } - override fun getLanguages(authorization: String, url: String): Observable> { + override fun getLanguages(authorization: String, url: String): Observable> { return ncApi.getLanguages(authorization, url).map { it.ocs?.data?.languages } } } diff --git a/app/src/main/java/com/nextcloud/talk/translate/repositories/model/Language.kt b/app/src/main/java/com/nextcloud/talk/translate/repositories/model/Language.kt new file mode 100644 index 00000000000..15287dab27e --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/translate/repositories/model/Language.kt @@ -0,0 +1,41 @@ +/* + * Nextcloud Talk application + * + * @author Julius Linus + * Copyright (C) 2023 Julius Linus + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.nextcloud.talk.translate.repositories.model + +import android.os.Parcelable +import com.bluelinelabs.logansquare.annotation.JsonField +import com.bluelinelabs.logansquare.annotation.JsonObject +import kotlinx.parcelize.Parcelize + +@Parcelize +@JsonObject +data class Language( + @JsonField(name = ["from"]) + var from: String?, + @JsonField(name = ["fromLabel"]) + var fromLabel: String?, + @JsonField(name = ["to"]) + var to: String?, + @JsonField(name = ["toLabel"]) + var toLabel: String? +) : Parcelable { + // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' + constructor() : this(null, null, null, null) +} diff --git a/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesData.kt b/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesData.kt index 626369aa4da..3cd2f220665 100644 --- a/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesData.kt +++ b/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesData.kt @@ -30,7 +30,7 @@ data class LanguagesData( @JsonField(name = ["languageDetection"]) var languageDetection: Boolean?, @JsonField(name = ["languages"]) - var languages: Array? + var languages: List? ) : Parcelable { // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' constructor() : this(null, null) diff --git a/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesOCS.kt b/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesOCS.kt index c6eff84f25c..f8c88edd83e 100644 --- a/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesOCS.kt +++ b/app/src/main/java/com/nextcloud/talk/translate/repositories/model/LanguagesOCS.kt @@ -34,5 +34,5 @@ data class LanguagesOCS( var data: LanguagesData? ) : Parcelable { // This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject' - constructor() : this(null, LanguagesData()) + constructor() : this(null, null) } diff --git a/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt b/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt index e78ef588cb0..6dcc7eb2836 100644 --- a/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/translate/ui/TranslateActivity.kt @@ -28,6 +28,7 @@ import android.content.Context import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.text.method.ScrollingMovementMethod +import android.util.Log import android.view.View import android.widget.AdapterView import android.widget.ArrayAdapter @@ -38,6 +39,7 @@ import com.nextcloud.talk.R import com.nextcloud.talk.activities.BaseActivity import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.databinding.ActivityTranslateBinding +import com.nextcloud.talk.translate.repositories.model.Language import com.nextcloud.talk.translate.viewmodels.TranslateViewModel import com.nextcloud.talk.users.UserManager import com.nextcloud.talk.utils.bundle.BundleKeys @@ -59,7 +61,7 @@ class TranslateActivity : BaseActivity() { private var toLanguages: Array? = null private var fromLanguages: Array? = null - private var languages: Array? = null + private var languages: List? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -88,9 +90,11 @@ class TranslateActivity : BaseActivity() { } is TranslateViewModel.LanguagesRetrievedState -> { - languages = state.array + Log.d(TAG, "Languages are: ${state.list}") + languages = state.list getLanguageOptions() setupSpinners() + setItems() } } } @@ -164,19 +168,16 @@ class TranslateActivity : BaseActivity() { } private fun getLanguageOptions() { - val currentUser = userManager.currentUser.blockingGet() - val json = JSONArray(languages) - val fromLanguagesSet = mutableSetOf(resources.getString(R.string.translation_detect_language)) val toLanguagesSet = mutableSetOf(resources.getString(R.string.translation_device_settings)) - for (i in 0 until json.length()) { - val current = json.getJSONObject(i) - if (current.getString(FROM_ID) != Locale.getDefault().language) { - toLanguagesSet.add(current.getString(FROM_LABEL)) + for (language in languages!!) { + val locale = Locale.getDefault().language + if (language.from != locale) { + toLanguagesSet.add(language.fromLabel!!) } - fromLanguagesSet.add(current.getString(TO_LABEL)) + fromLanguagesSet.add(language.toLabel!!) } toLanguages = toLanguagesSet.toTypedArray() @@ -240,15 +241,15 @@ class TranslateActivity : BaseActivity() { val text = intent.extras!!.getString(BundleKeys.KEY_TRANSLATE_MESSAGE) binding.fromLanguage.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ -> - val fromLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString()) - val toLabel: String = getISOFromLanguage(binding.toLanguage.text.toString()) - viewModel.translateMessage(toLabel, fromLabel, text!!) + // val fromLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString()) + // val toLabel: String = getISOFromLanguage(binding.toLanguage.text.toString()) + // viewModel.translateMessage(toLabel, fromLabel, text!!) } binding.toLanguage.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ -> - val toLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString()) - val fromLabel: String = getISOFromLanguage(binding.fromLanguage.text.toString()) - viewModel.translateMessage(toLabel, fromLabel, text!!) + // val toLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString()) + // val fromLabel: String = getISOFromLanguage(binding.fromLanguage.text.toString()) + // viewModel.translateMessage(toLabel, fromLabel, text!!) } } @@ -301,6 +302,7 @@ class TranslateActivity : BaseActivity() { } companion object { + val TAG = TranslateActivity::class.simpleName private const val FROM_ID = "from" private const val FROM_LABEL = "fromLabel" private const val TO_LABEL = "toLabel" diff --git a/app/src/main/java/com/nextcloud/talk/translate/viewmodels/TranslateViewModel.kt b/app/src/main/java/com/nextcloud/talk/translate/viewmodels/TranslateViewModel.kt index 850283094a8..dc5a439fb87 100644 --- a/app/src/main/java/com/nextcloud/talk/translate/viewmodels/TranslateViewModel.kt +++ b/app/src/main/java/com/nextcloud/talk/translate/viewmodels/TranslateViewModel.kt @@ -6,6 +6,7 @@ import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import com.nextcloud.talk.data.user.model.User import com.nextcloud.talk.translate.repositories.TranslateRepository +import com.nextcloud.talk.translate.repositories.model.Language import com.nextcloud.talk.users.UserManager import com.nextcloud.talk.utils.ApiUtils import io.reactivex.Observer @@ -24,7 +25,7 @@ class TranslateViewModel @Inject constructor( data object StartState : ViewState class TranslatedState(val msg: String) : ViewState - class LanguagesRetrievedState(val array: Array) : ViewState + class LanguagesRetrievedState(val list: List) : ViewState data object LanguagesErrorState : ViewState @@ -56,25 +57,27 @@ class TranslateViewModel @Inject constructor( val currentUser: User = userManager.currentUser.blockingGet() val authorization: String = ApiUtils.getCredentials(currentUser.username, currentUser.token) val url: String = ApiUtils.getUrlForLanguages(currentUser.baseUrl) + Log.d(TAG, "URL is: $url") repository.getLanguages(authorization, url) .subscribeOn(Schedulers.io()) ?.observeOn(AndroidSchedulers.mainThread()) - ?.subscribe(object : Observer> { + ?.subscribe(object : Observer> { override fun onSubscribe(d: Disposable) { // unused atm } override fun onError(e: Throwable) { _viewState.value = LanguagesErrorState - Log.e(TAG, "Error while retrieving languages") + Log.e(TAG, "Error while retrieving languages: $e") } override fun onComplete() { // unused atm } - override fun onNext(array: Array) { - _viewState.value = LanguagesRetrievedState(array) + override fun onNext(list: List) { + _viewState.value = LanguagesRetrievedState(list) + Log.d(TAG, "Languages retrieved: $list") } }) }