Merge pull request #3030 from nextcloud/issue-2931-followup

issue 2931 - follow up fixes
This commit is contained in:
Andy Scherzinger 2023-05-17 23:17:06 +02:00 committed by GitHub
commit f3c9721d21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 39 deletions

View File

@ -60,26 +60,38 @@ class TranslateActivity : BaseActivity() {
@Inject @Inject
lateinit var userManager: UserManager lateinit var userManager: UserManager
var fromLanguages = arrayOf<String>() private var fromLanguages = arrayOf<String>()
private var toLanguages = arrayOf<String>()
var toLanguages = arrayOf<String>() private var text: String? = null
var text: String? = null
var check: Int = 0
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this) NextcloudTalkApplication.sharedApplication!!.componentApplication.inject(this)
binding = ActivityTranslateBinding.inflate(layoutInflater) binding = ActivityTranslateBinding.inflate(layoutInflater)
setupActionBar() setupActionBar()
setContentView(binding.root) setContentView(binding.root)
setupSystemColors() setupSystemColors()
setupTextViews() setupTextViews()
setupSpinners() setupSpinners()
getLanguageOptions() getLanguageOptions()
translate(null, Locale.getDefault().language)
if (savedInstanceState == null) {
translate(null, Locale.getDefault().language)
} else {
binding.translatedMessageTextview.text = savedInstanceState.getString(BundleKeys.SAVED_TRANSLATED_MESSAGE)
}
}
override fun onResume() {
super.onResume()
setItems()
}
override fun onSaveInstanceState(outState: Bundle) {
outState.run {
putString(BundleKeys.SAVED_TRANSLATED_MESSAGE, binding.translatedMessageTextview.text.toString())
}
super.onSaveInstanceState(outState)
} }
private fun setupActionBar() { private fun setupActionBar() {
@ -127,7 +139,6 @@ class TranslateActivity : BaseActivity() {
fromLanguages = fromLanguagesSet.toTypedArray() fromLanguages = fromLanguagesSet.toTypedArray()
toLanguages = toLanguagesSet.toTypedArray() toLanguages = toLanguagesSet.toTypedArray()
fillSpinners() fillSpinners()
} }
@ -146,7 +157,6 @@ class TranslateActivity : BaseActivity() {
fromLanguage fromLanguage
} }
Log.i(TAG, "Url is: $translateURL")
ncApi.translateMessage(credentials, translateURL, text, toLanguage, calculatedFromLanguage) ncApi.translateMessage(credentials, translateURL, text, toLanguage, calculatedFromLanguage)
?.subscribeOn(Schedulers.io()) ?.subscribeOn(Schedulers.io())
?.observeOn(AndroidSchedulers.mainThread()) ?.observeOn(AndroidSchedulers.mainThread())
@ -197,7 +207,7 @@ class TranslateActivity : BaseActivity() {
} }
private fun getISOFromLanguage(language: String): String { private fun getISOFromLanguage(language: String): String {
if (resources.getString(R.string.translation_device_settings).equals(language)) { if (resources.getString(R.string.translation_device_settings) == language) {
return Locale.getDefault().language return Locale.getDefault().language
} }
@ -208,7 +218,7 @@ class TranslateActivity : BaseActivity() {
val currentUser: User = userManager.currentUser.blockingGet() val currentUser: User = userManager.currentUser.blockingGet()
val json = JSONArray(CapabilitiesUtilNew.getLanguages(currentUser).toString()) val json = JSONArray(CapabilitiesUtilNew.getLanguages(currentUser).toString())
for (i in 0..json.length() - 1) { for (i in 0 until json.length()) {
val current = json.getJSONObject(i) val current = json.getJSONObject(i)
if (current.getString(FROM_LABEL) == language) { if (current.getString(FROM_LABEL) == language) {
return current.getString(FROM_ID) return current.getString(FROM_ID)
@ -223,34 +233,16 @@ class TranslateActivity : BaseActivity() {
viewThemeUtils.material.colorTextInputLayout(binding.toLanguageInputLayout) viewThemeUtils.material.colorTextInputLayout(binding.toLanguageInputLayout)
fillSpinners() fillSpinners()
binding.fromLanguage.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { binding.fromLanguage.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ ->
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { val fromLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString())
if (++check > 1) { val toLabel: String = getISOFromLanguage(binding.toLanguage.text.toString())
val fromLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString()) translate(fromLabel, toLabel)
val toLabel: String = getISOFromLanguage(binding.fromLanguage.text.toString())
Log.i(TAG, "fromLanguageSpinner :: $FROM_LABEL = $fromLabel, $TO_LABEL = $ count: $check")
translate(fromLabel, toLabel)
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
// write code to perform some action
}
} }
binding.toLanguage.onItemSelectedListener = object : AdapterView.OnItemSelectedListener { binding.toLanguage.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ ->
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) { val toLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString())
if (++check > 2) { val fromLabel: String = getISOFromLanguage(binding.fromLanguage.text.toString())
val toLabel: String = getISOFromLanguage(parent.getItemAtPosition(position).toString()) translate(fromLabel, toLabel)
val fromLabel: String = getISOFromLanguage(binding.fromLanguage.text.toString())
Log.i(TAG, "toLanguageSpinner :: $FROM_LABEL = $fromLabel, $TO_LABEL = $toLabel count: $check")
translate(fromLabel, toLabel)
}
}
override fun onNothingSelected(parent: AdapterView<*>) {
// write code to perform some action
}
} }
} }
@ -270,6 +262,11 @@ class TranslateActivity : BaseActivity() {
} }
} }
private fun setItems() {
binding.fromLanguage.setSimpleItems(fromLanguages)
binding.toLanguage.setSimpleItems(toLanguages)
}
companion object { companion object {
private val TAG = TranslateActivity::class.simpleName private val TAG = TranslateActivity::class.simpleName
private const val FROM_ID = "from" private const val FROM_ID = "from"

View File

@ -89,4 +89,5 @@ object BundleKeys {
const val KEY_SHARE_RECORDING_TO_CHAT_URL = "KEY_SHARE_RECORDING_TO_CHAT_URL" const val KEY_SHARE_RECORDING_TO_CHAT_URL = "KEY_SHARE_RECORDING_TO_CHAT_URL"
const val KEY_GEOCODING_RESULT = "KEY_GEOCODING_RESULT" const val KEY_GEOCODING_RESULT = "KEY_GEOCODING_RESULT"
const val ADD_ACCOUNT = "ADD_ACCOUNT" // temp workaround until conductor is removed const val ADD_ACCOUNT = "ADD_ACCOUNT" // temp workaround until conductor is removed
const val SAVED_TRANSLATED_MESSAGE = "SAVED_TRANSLATED_MESSAGE"
} }