Skip to content

Commit

Permalink
got it to work, now need to implement onclick
Browse files Browse the repository at this point in the history
Signed-off-by: rapterjet2004 <[email protected]>
  • Loading branch information
rapterjet2004 committed Nov 15, 2023
1 parent 08c8aae commit 578ed07
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 25 deletions.
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.Language
import io.reactivex.Observable

interface TranslateRepository {
Expand All @@ -15,5 +16,5 @@ interface TranslateRepository {
fun getLanguages(
authorization: String,
url: String
): Observable<Array<String>>
): Observable<List<Language>>
}
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.Language
import io.reactivex.Observable
import javax.inject.Inject

Expand All @@ -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<Array<String>> {
override fun getLanguages(authorization: String, url: String): Observable<List<Language>> {
return ncApi.getLanguages(authorization, url).map { it.ocs?.data?.languages }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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
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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ data class LanguagesData(
@JsonField(name = ["languageDetection"])
var languageDetection: Boolean?,
@JsonField(name = ["languages"])
var languages: Array<String>?
var languages: List<Language>?
) : Parcelable {
// This constructor is added to work with the 'com.bluelinelabs.logansquare.annotation.JsonObject'
constructor() : this(null, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -59,7 +61,7 @@ class TranslateActivity : BaseActivity() {

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

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -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()
}
}
}
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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!!)
}
}

Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,7 +25,7 @@ class TranslateViewModel @Inject constructor(
data object StartState : ViewState
class TranslatedState(val msg: String) : ViewState

class LanguagesRetrievedState(val array: Array<String>) : ViewState
class LanguagesRetrievedState(val list: List<Language>) : ViewState

data object LanguagesErrorState : ViewState

Expand Down Expand Up @@ -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<Array<String>> {
?.subscribe(object : Observer<List<Language>> {
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<String>) {
_viewState.value = LanguagesRetrievedState(array)
override fun onNext(list: List<Language>) {
_viewState.value = LanguagesRetrievedState(list)
Log.d(TAG, "Languages retrieved: $list")
}
})
}
Expand Down

0 comments on commit 578ed07

Please sign in to comment.