Skip to content

Commit

Permalink
WIP progress on the activity
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Oct 4, 2023
1 parent bd0687a commit 6d8a823
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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<LanguageModel>) {
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) {
Expand All @@ -178,16 +180,16 @@ 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(
context,
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()
}
Expand All @@ -201,45 +203,45 @@ 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)
viewThemeUtils.material.colorTextInputLayout(binding.toLanguageInputLayout)
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() {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -710,5 +710,7 @@ How to translate with transifex:
<string name="audio_call">Audio Call</string>
<string name="started_a_call">started a call</string>
<string name="nc_settings_phone_book_integration_phone_number_dialog_429">Error 429 Too Many Requests</string>
<string name="languages_error_title">Retrieval failed</string>
<string name="languages_error_message">Languages could not be retrieved</string>

</resources>

0 comments on commit 6d8a823

Please sign in to comment.