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 0d7190d5d28..6749944729f 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 @@ -29,7 +29,6 @@ import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.text.method.ScrollingMovementMethod import android.view.View -import android.widget.AdapterView import android.widget.ArrayAdapter import androidx.lifecycle.ViewModelProvider import autodagger.AutoInjector @@ -38,6 +37,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.LanguageModel import com.nextcloud.talk.translate.viewmodels.TranslateViewModel import com.nextcloud.talk.users.UserManager import com.nextcloud.talk.utils.bundle.BundleKeys @@ -78,15 +78,20 @@ class TranslateActivity : BaseActivity() { } is TranslateViewModel.TranslationErrorState -> { - onErrorState() + onTranslationErrorState() } + + is TranslateViewModel.LanguagesErrorState -> { + onLanguagesErrorState() + } + is TranslateViewModel.LanguagesRetrievedState -> TODO() } } setupActionBar() setContentView(binding.root) setupSystemColors() setupTextViews() - getLanguageOptions() + viewModel.getLanguages() setupSpinners() setupCopyButton() @@ -152,14 +157,11 @@ class TranslateActivity : BaseActivity() { binding.originalMessageTextview.text = text } - private fun getLanguageOptions() { - val currentUser = userManager.currentUser.blockingGet() - // TODO replace commented out logic with api call to get languages - // val json = JSONArray((CapabilitiesUtilNew.getLanguages(currentUser) as ArrayList<*>).toArray()) - + private fun onLanguagesRetrieved(array: Array) { val fromLanguagesSet = mutableSetOf(resources.getString(R.string.translation_detect_language)) val toLanguagesSet = mutableSetOf(resources.getString(R.string.translation_device_settings)) + // TODO finish this logic // for (i in 0 until json.length()) { // val current = json.getJSONObject(i) // if (current.getString(FROM_ID) != Locale.getDefault().language) { @@ -178,7 +180,7 @@ class TranslateActivity : BaseActivity() { binding.toLanguageInputLayout.isEnabled = value } - private fun showDialog() { + private fun showDialog(errorTitleRes: Int, errorMessageRes: Int) { val dialogBuilder = MaterialAlertDialogBuilder(this@TranslateActivity) .setIcon( viewThemeUtils.dialog.colorMaterialAlertDialogIcon( @@ -186,8 +188,8 @@ class TranslateActivity : BaseActivity() { R.drawable.ic_warning_white ) ) - .setTitle(R.string.translation_error_title) - .setMessage(R.string.translation_error_message) + .setTitle(errorTitleRes) + .setMessage(errorMessageRes) .setPositiveButton(R.string.nc_ok) { dialog, _ -> dialog.dismiss() } @@ -201,27 +203,27 @@ class TranslateActivity : BaseActivity() { ) } - private fun getISOFromLanguage(language: String): String { - if (resources.getString(R.string.translation_device_settings) == language) { - return Locale.getDefault().language - } - - return getISOFromServer(language) - } - - private fun getISOFromServer(language: String): String { - val currentUser = userManager.currentUser.blockingGet() - // TODO replace commented out logic with API calls - // val json = JSONArray((CapabilitiesUtilNew.getLanguages(currentUser) as ArrayList<*>).toArray()) - // for (i in 0 until json.length()) { - // val current = json.getJSONObject(i) - // if (current.getString(FROM_LABEL) == language) { - // return current.getString(FROM_ID) - // } - // } - - return "" - } + // private fun getISOFromLanguage(language: String): String { + // if (resources.getString(R.string.translation_device_settings) == language) { + // return Locale.getDefault().language + // } + // + // return getISOFromServer(language) + // } + + // private fun getISOFromServer(language: String): String { + // val currentUser = userManager.currentUser.blockingGet() + // // TODO replace commented out logic with API calls + // val json = JSONArray((CapabilitiesUtilNew.getLanguages(currentUser) as ArrayList<*>).toArray()) + // for (i in 0 until json.length()) { + // val current = json.getJSONObject(i) + // if (current.getString(FROM_LABEL) == language) { + // return current.getString(FROM_ID) + // } + // } + // + // return "" + // } private fun setupSpinners() { viewThemeUtils.material.colorTextInputLayout(binding.fromLanguageInputLayout) @@ -229,17 +231,17 @@ class TranslateActivity : BaseActivity() { fillSpinners() 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!!) - } - - 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!!) - } + // 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!!) + // } + // + // 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!!) + // } } private fun fillSpinners() { @@ -278,10 +280,16 @@ class TranslateActivity : BaseActivity() { enableSpinners(true) } - private fun onErrorState() { + private fun onTranslationErrorState() { + binding.progressBar.visibility = View.GONE + enableSpinners(true) + showDialog(R.string.translation_error_title, R.string.translation_error_message) + } + + private fun onLanguagesErrorState() { binding.progressBar.visibility = View.GONE enableSpinners(true) - showDialog() + showDialog(R.string.languages_error_title, R.string.languages_error_message) } companion object { diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8fa0ef657e9..1761849be1b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -710,5 +710,7 @@ How to translate with transifex: Audio Call started a call Error 429 Too Many Requests + Retrieval failed + Languages could not be retrieved