Skip to content

Commit

Permalink
Adjusting Translation Provider
Browse files Browse the repository at this point in the history
- Added 4 new model classes
- Added the new API function to NcApi
- Implemented the changes in the Repository + ViewModel
- Implemented the changes in the Activity
- Added some helper functions
  • Loading branch information
rapterjet2004 committed Oct 5, 2023
1 parent 0c2e0aa commit d2cb6ee
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 53 deletions.
5 changes: 5 additions & 0 deletions app/src/main/java/com/nextcloud/talk/api/NcApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import com.nextcloud.talk.models.json.userprofile.UserProfileFieldsOverall;
import com.nextcloud.talk.models.json.userprofile.UserProfileOverall;
import com.nextcloud.talk.polls.repositories.model.PollOverall;
import com.nextcloud.talk.translate.repositories.model.LanguagesOverall;
import com.nextcloud.talk.translate.repositories.model.TranslationsOverall;

import java.util.List;
Expand Down Expand Up @@ -673,6 +674,10 @@ Observable<TranslationsOverall> translateMessage(@Header("Authorization") String
@Query("toLanguage") String toLanguage,
@Nullable @Query("fromLanguage") String fromLanguage);

@GET
Observable<LanguagesOverall> getLanguages(@Header("Authorization") String authorization,
@Url String url);

@GET
Observable<ReminderOverall> getReminder(@Header("Authorization") String authorization,
@Url String url);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.nextcloud.talk.translate.repositories

import com.nextcloud.talk.translate.repositories.model.LanguageModel
import io.reactivex.Observable

interface TranslateRepository {
Expand All @@ -11,4 +12,9 @@ interface TranslateRepository {
toLanguage: String,
fromLanguage: String?
): Observable<String>

fun getLanguages(
authorization: String,
url: String
): Observable<Array<LanguageModel>>
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.nextcloud.talk.translate.repositories

import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.translate.repositories.model.LanguageModel
import io.reactivex.Observable
import javax.inject.Inject

Expand All @@ -15,4 +16,8 @@ class TranslateRepositoryImpl @Inject constructor(private val ncApi: NcApi) : Tr
): Observable<String> {
return ncApi.translateMessage(authorization, url, text, toLanguage, fromLanguage).map { it.ocs?.data!!.text }
}

override fun getLanguages(authorization: String, url: String): Observable<Array<LanguageModel>> {
return ncApi.getLanguages(authorization, url).map { it.ocs?.data?.languages }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Nextcloud Talk application
*
* @author Julius Linus
* Copyright (C) 2023 Julius Linus <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.translate.repositories.model

data class LanguageModel(
var from: String?,
var fromLabel: String?,
var to: String?,
var toLabel: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Nextcloud Talk application
*
* @author Julius Linus
* Copyright (C) 2023 Julius Linus <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
import kotlinx.parcelize.RawValue

@Parcelize
@JsonObject
data class LanguagesData(
@JsonField(name = ["languageDetection"])
var languageDetection: Boolean?,
@JsonField(name = ["languages"])
var languages: Array<@RawValue LanguageModel>?
) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(null, null)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Nextcloud Talk application
*
* @author Julius Linus
* Copyright (C) 2023 Julius Linus <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.translate.repositories.model

import android.os.Parcelable
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
import com.nextcloud.talk.models.json.generic.GenericMeta
import kotlinx.parcelize.Parcelize

@Parcelize
@JsonObject
data class LanguagesOCS(
@JsonField(name = ["meta"])
var meta: GenericMeta?,
@JsonField(name = ["data"])
var data: LanguagesData?
) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(null, LanguagesData())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Nextcloud Talk application
*
* @author Julius Linus
* Copyright (C) 2023 Julius Linus <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/
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
class LanguagesOverall(
@JsonField(name = ["ocs"])
var ocs: LanguagesOCS?
) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(null)
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import kotlinx.parcelize.Parcelize

@Parcelize
@JsonObject
data class TranslateOCS( // TODO finish this model
data class TranslateOCS(
@JsonField(name = ["meta"])
var meta: GenericMeta?,
@JsonField(name = ["data"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,10 @@ 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
import com.nextcloud.talk.utils.database.user.CapabilitiesUtilNew
import org.json.JSONArray
import java.util.Locale
import javax.inject.Inject

Expand All @@ -60,6 +59,7 @@ class TranslateActivity : BaseActivity() {

private var toLanguages: Array<String>? = null
private var fromLanguages: Array<String>? = null
private var languages: Array<LanguageModel>? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand All @@ -79,16 +79,25 @@ class TranslateActivity : BaseActivity() {
onTranslatedState(state.msg)
}

is TranslateViewModel.ErrorState -> {
onErrorState()
is TranslateViewModel.TranslationErrorState -> {
onTranslationErrorState()
}

is TranslateViewModel.LanguagesErrorState -> {
onLanguagesErrorState()
}

is TranslateViewModel.LanguagesRetrievedState -> {
languages = state.array
onLanguagesRetrieved()
}
}
}
setupActionBar()
setContentView(binding.root)
setupSystemColors()
setupTextViews()
getLanguageOptions()
viewModel.getLanguages()
setupSpinners()
setupCopyButton()

Expand Down Expand Up @@ -154,20 +163,17 @@ class TranslateActivity : BaseActivity() {
binding.originalMessageTextview.text = text
}

private fun getLanguageOptions() {
val currentUser = userManager.currentUser.blockingGet()
val json = JSONArray((CapabilitiesUtilNew.getLanguages(currentUser) as ArrayList<*>).toArray())

private fun onLanguagesRetrieved() {
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 (i in languages!!.indices) {
val current = languages!![i]
if (current.from != Locale.getDefault().language) {
current.fromLabel?.let { toLanguagesSet.add(it) }
}

fromLanguagesSet.add(current.getString(TO_LABEL))
current.toLabel?.let { fromLanguagesSet.add(it) }
}

toLanguages = toLanguagesSet.toTypedArray()
Expand All @@ -179,16 +185,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 @@ -203,25 +209,19 @@ class TranslateActivity : BaseActivity() {
}

private fun getISOFromLanguage(language: String): String {
var result = ""

if (resources.getString(R.string.translation_device_settings) == language) {
return Locale.getDefault().language
result = Locale.getDefault().language
}

return getISOFromServer(language)
}

private fun getISOFromServer(language: String): String {
val currentUser = userManager.currentUser.blockingGet()
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)
for (current in languages!!) {
if (current.fromLabel == language) {
result = current.from!!
}
}

return ""
return result
}

private fun setupSpinners() {
Expand Down Expand Up @@ -279,10 +279,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
Loading

0 comments on commit d2cb6ee

Please sign in to comment.