Revert "Add yandex translate support"

This reverts commit 0fc88a4076.
This commit is contained in:
Tlaster 2020-12-22 17:05:12 +08:00
parent 4f137ad827
commit cf270b72fc
11 changed files with 17 additions and 130 deletions

View File

@ -67,8 +67,6 @@ public interface TwidereConstants extends SharedPreferenceConstants, IntentConst
String TWITTER_CONSUMER_KEY = "MUUBibXUognm6e9vbzrUIqPkt"; String TWITTER_CONSUMER_KEY = "MUUBibXUognm6e9vbzrUIqPkt";
String TWITTER_CONSUMER_SECRET = "l2uWAgQkoHvDfM2PrRFx2WN4h7QIUIktmxyeTAqRo6TkGCtNKy"; String TWITTER_CONSUMER_SECRET = "l2uWAgQkoHvDfM2PrRFx2WN4h7QIUIktmxyeTAqRo6TkGCtNKy";
String YANDEX_KEY = "trnsl.1.1.20200513T065609Z.8e72845b632aa04f.fe1297e42c152de9e8773e1bc71162b1e498e2a8";
String DEFAULT_TWITTER_API_URL_FORMAT = "https://[DOMAIN].twitter.com/"; String DEFAULT_TWITTER_API_URL_FORMAT = "https://[DOMAIN].twitter.com/";
String SCHEME_HTTP = "http"; String SCHEME_HTTP = "http";

View File

@ -285,8 +285,6 @@ public interface SharedPreferenceConstants {
String KEY_OVERRIDE_LANGUAGE = "override_language"; String KEY_OVERRIDE_LANGUAGE = "override_language";
@ExportablePreference(STRING) @ExportablePreference(STRING)
String KEY_TAB_POSITION = "tab_position"; String KEY_TAB_POSITION = "tab_position";
@ExportablePreference(STRING)
String KEY_YANDEX_KEY = "yandex_key";
@ExportablePreference(BOOLEAN) @ExportablePreference(BOOLEAN)
String KEY_AUTO_HIDE_TABS = "auto_hide_tabs"; String KEY_AUTO_HIDE_TABS = "auto_hide_tabs";
@ExportablePreference(BOOLEAN) @ExportablePreference(BOOLEAN)

View File

@ -1,11 +0,0 @@
package org.mariotaku.yandex
import org.mariotaku.restfu.annotation.method.POST
import org.mariotaku.restfu.annotation.param.Query
import org.mariotaku.yandex.model.YandexTranslateResult
interface YandexAPI {
@POST("/api/v1.5/tr.json/translate")
fun search(@Query("text") text: String,
@Query("lang") lang: String): YandexTranslateResult
}

View File

@ -1,50 +0,0 @@
package org.mariotaku.yandex
import org.mariotaku.restfu.RestAPIFactory
import org.mariotaku.restfu.RestConverter
import org.mariotaku.restfu.RestMethod
import org.mariotaku.restfu.RestRequest
import org.mariotaku.restfu.http.Endpoint
import org.mariotaku.restfu.http.RestHttpClient
import org.mariotaku.restfu.http.ValueMap
import org.mariotaku.restfu.logansqaure.LoganSquareConverterFactory
class YandexAPIFactory(apiKey: String, endpoint: String) {
private val factory: RestAPIFactory<YandexException> = RestAPIFactory<YandexException>()
init {
factory.setEndpoint(Endpoint(endpoint))
factory.setExceptionFactory { cause, _, _ ->
cause?.let { YandexException(it) }
?: YandexException()
}
factory.setRestConverterFactory(LoganSquareConverterFactory())
factory.setRestRequestFactory(object : RestRequest.DefaultFactory<YandexException?>() {
override fun create(restMethod: RestMethod<YandexException?>,
factory: RestConverter.Factory<YandexException?>, valuePool: ValueMap?): RestRequest {
val method = restMethod.method
val path = restMethod.path
val headers = restMethod.getHeaders(valuePool)
val queries = restMethod.getQueries(valuePool)
val params = restMethod.getParams(factory, valuePool)
val rawValue = restMethod.rawValue
val bodyType = restMethod.bodyType
val extras = restMethod.extras
queries.add("key", apiKey)
return RestRequest(method.value, method.allowBody, path, headers, queries,
params, rawValue, bodyType, extras)
}
})
}
fun setHttpClient(restClient: RestHttpClient): YandexAPIFactory {
factory.setHttpClient(restClient)
return this
}
fun build(): YandexAPI {
return factory.build(YandexAPI::class.java)
}
}

View File

@ -1,8 +0,0 @@
package org.mariotaku.yandex
class YandexException : Exception {
constructor() : super()
constructor(message: String?) : super(message)
constructor(message: String?, cause: Throwable?) : super(message, cause)
constructor(cause: Throwable?) : super(cause)
}

View File

@ -1,14 +0,0 @@
package org.mariotaku.yandex.model
import com.bluelinelabs.logansquare.annotation.JsonField
import com.bluelinelabs.logansquare.annotation.JsonObject
@JsonObject
data class YandexTranslateResult(
@JsonField(name = ["code"])
var code: Int? = null,
@JsonField(name = ["lang"])
var lang: String? = null,
@JsonField(name = ["text"])
var text: List<String>? = null
)

View File

@ -10,7 +10,6 @@ import org.mariotaku.ktextension.bcp47Tag
import org.mariotaku.ktextension.toLongOr import org.mariotaku.ktextension.toLongOr
import org.mariotaku.twidere.BuildConfig import org.mariotaku.twidere.BuildConfig
import org.mariotaku.twidere.Constants.* import org.mariotaku.twidere.Constants.*
import org.mariotaku.twidere.TwidereConstants
import org.mariotaku.twidere.TwidereConstants.KEY_MEDIA_PRELOAD import org.mariotaku.twidere.TwidereConstants.KEY_MEDIA_PRELOAD
import org.mariotaku.twidere.annotation.AccountType import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.annotation.ImageShapeStyle import org.mariotaku.twidere.annotation.ImageShapeStyle
@ -87,7 +86,6 @@ val lastLaunchTimeKey = KLongKey("last_launch_time", -1)
val promotionsEnabledKey = KBooleanKey("promotions_enabled", false) val promotionsEnabledKey = KBooleanKey("promotions_enabled", false)
val translationDestinationKey = KNullableStringKey(KEY_TRANSLATION_DESTINATION, null) val translationDestinationKey = KNullableStringKey(KEY_TRANSLATION_DESTINATION, null)
val tabPositionKey = KStringKey(KEY_TAB_POSITION, SharedPreferenceConstants.DEFAULT_TAB_POSITION) val tabPositionKey = KStringKey(KEY_TAB_POSITION, SharedPreferenceConstants.DEFAULT_TAB_POSITION)
val yandexKeyKey = KStringKey(SharedPreferenceConstants.KEY_YANDEX_KEY, TwidereConstants.YANDEX_KEY)
val autoHideTabs = KBooleanKey(SharedPreferenceConstants.KEY_AUTO_HIDE_TABS, true) val autoHideTabs = KBooleanKey(SharedPreferenceConstants.KEY_AUTO_HIDE_TABS, true)
val hideCardNumbersKey = KBooleanKey(KEY_HIDE_CARD_NUMBERS, false) val hideCardNumbersKey = KBooleanKey(KEY_HIDE_CARD_NUMBERS, false)
val showLinkPreviewKey = KBooleanKey(KEY_SHOW_LINK_PREVIEW, false) val showLinkPreviewKey = KBooleanKey(KEY_SHOW_LINK_PREVIEW, false)

View File

@ -32,19 +32,19 @@ import android.nfc.NdefMessage
import android.nfc.NdefRecord import android.nfc.NdefRecord
import android.nfc.NfcAdapter.CreateNdefMessageCallback import android.nfc.NfcAdapter.CreateNdefMessageCallback
import android.os.Bundle import android.os.Bundle
import android.text.TextUtils
import android.view.*
import android.widget.Toast
import androidx.appcompat.app.AlertDialog
import androidx.loader.app.LoaderManager import androidx.loader.app.LoaderManager
import androidx.loader.app.LoaderManager.LoaderCallbacks import androidx.loader.app.LoaderManager.LoaderCallbacks
import androidx.loader.app.hasRunningLoadersSafe import androidx.loader.app.hasRunningLoadersSafe
import androidx.loader.content.FixedAsyncTaskLoader import androidx.loader.content.FixedAsyncTaskLoader
import androidx.loader.content.Loader import androidx.loader.content.Loader
import androidx.appcompat.app.AlertDialog
import androidx.recyclerview.widget.FixedLinearLayoutManager import androidx.recyclerview.widget.FixedLinearLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.ViewHolder import androidx.recyclerview.widget.RecyclerView.ViewHolder
import android.text.TextUtils
import android.view.*
import android.widget.Toast
import com.squareup.otto.Subscribe import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.fragment_status.* import kotlinx.android.synthetic.main.fragment_status.*
import kotlinx.android.synthetic.main.layout_content_fragment_common.* import kotlinx.android.synthetic.main.layout_content_fragment_common.*
@ -72,11 +72,13 @@ import org.mariotaku.twidere.annotation.AccountType
import org.mariotaku.twidere.constant.KeyboardShortcutConstants.* import org.mariotaku.twidere.constant.KeyboardShortcutConstants.*
import org.mariotaku.twidere.constant.displaySensitiveContentsKey import org.mariotaku.twidere.constant.displaySensitiveContentsKey
import org.mariotaku.twidere.constant.newDocumentApiKey import org.mariotaku.twidere.constant.newDocumentApiKey
import org.mariotaku.twidere.constant.yandexKeyKey
import org.mariotaku.twidere.extension.* import org.mariotaku.twidere.extension.*
import org.mariotaku.twidere.extension.model.*
import org.mariotaku.twidere.extension.model.api.key import org.mariotaku.twidere.extension.model.api.key
import org.mariotaku.twidere.extension.model.api.toParcelable import org.mariotaku.twidere.extension.model.api.toParcelable
import org.mariotaku.twidere.extension.model.getAccountType
import org.mariotaku.twidere.extension.model.media_type
import org.mariotaku.twidere.extension.model.newMicroBlogInstance
import org.mariotaku.twidere.extension.model.originalId
import org.mariotaku.twidere.extension.view.calculateSpaceItemHeight import org.mariotaku.twidere.extension.view.calculateSpaceItemHeight
import org.mariotaku.twidere.fragment.AbsStatusesFragment import org.mariotaku.twidere.fragment.AbsStatusesFragment
import org.mariotaku.twidere.fragment.AbsStatusesFragment.Companion.handleActionClick import org.mariotaku.twidere.fragment.AbsStatusesFragment.Companion.handleActionClick
@ -99,14 +101,12 @@ import org.mariotaku.twidere.util.*
import org.mariotaku.twidere.util.ContentScrollHandler.ContentListSupport import org.mariotaku.twidere.util.ContentScrollHandler.ContentListSupport
import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback import org.mariotaku.twidere.util.KeyboardShortcutsHandler.KeyboardShortcutCallback
import org.mariotaku.twidere.util.RecyclerViewScrollHandler.RecyclerViewCallback import org.mariotaku.twidere.util.RecyclerViewScrollHandler.RecyclerViewCallback
import org.mariotaku.twidere.util.dagger.DependencyHolder
import org.mariotaku.twidere.view.CardMediaContainer.OnMediaClickListener import org.mariotaku.twidere.view.CardMediaContainer.OnMediaClickListener
import org.mariotaku.twidere.view.ExtendedRecyclerView import org.mariotaku.twidere.view.ExtendedRecyclerView
import org.mariotaku.twidere.view.holder.GapViewHolder import org.mariotaku.twidere.view.holder.GapViewHolder
import org.mariotaku.twidere.view.holder.StatusViewHolder import org.mariotaku.twidere.view.holder.StatusViewHolder
import org.mariotaku.twidere.view.holder.iface.IStatusViewHolder import org.mariotaku.twidere.view.holder.iface.IStatusViewHolder
import org.mariotaku.twidere.view.holder.iface.IStatusViewHolder.StatusClickListener import org.mariotaku.twidere.view.holder.iface.IStatusViewHolder.StatusClickListener
import org.mariotaku.yandex.YandexAPIFactory
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import kotlin.math.max import kotlin.math.max
import kotlin.math.min import kotlin.math.min
@ -692,8 +692,8 @@ class StatusFragment : BaseFragment(), LoaderCallbacks<SingleResponse<Parcelable
private val weakFragment = WeakReference(fragment) private val weakFragment = WeakReference(fragment)
override fun onExecute(account: AccountDetails, params: Any?): TranslationResult { override fun onExecute(account: AccountDetails, params: Any?): TranslationResult {
val prefDest = preferences.getString(KEY_TRANSLATION_DESTINATION, null).orEmpty()
val twitter = account.newMicroBlogInstance(context, MicroBlog::class.java) val twitter = account.newMicroBlogInstance(context, MicroBlog::class.java)
val prefDest = preferences.getString(KEY_TRANSLATION_DESTINATION, null).orEmpty()
val dest: String val dest: String
if (TextUtils.isEmpty(prefDest)) { if (TextUtils.isEmpty(prefDest)) {
dest = twitter.accountSettings.language dest = twitter.accountSettings.language
@ -703,21 +703,7 @@ class StatusFragment : BaseFragment(), LoaderCallbacks<SingleResponse<Parcelable
} else { } else {
dest = prefDest dest = prefDest
} }
return if (account.isOfficial(context)) { return twitter.showTranslation(status.originalId, dest)
twitter.showTranslation(status.originalId, dest)
} else {
val holder = DependencyHolder.get(context)
val api = YandexAPIFactory(preferences[yandexKeyKey], "https://translate.yandex.net/")
.setHttpClient(holder.restHttpClient)
.build()
val result = api.search(status.text_plain, "${status.lang}-${dest.split('-').firstOrNull()}")
TranslationResult().also {
it[TranslationResult::class.java.getDeclaredField("text")] = result.text?.firstOrNull()
it[TranslationResult::class.java.getDeclaredField("id")] = status.originalId
it[TranslationResult::class.java.getDeclaredField("translatedLang")] = result.lang?.split('-')?.lastOrNull()
it[TranslationResult::class.java.getDeclaredField("lang")] = result.lang?.split('-')?.firstOrNull()
}
}
} }
override fun onSucceed(callback: Any?, result: TranslationResult) { override fun onSucceed(callback: Any?, result: TranslationResult) {

View File

@ -22,7 +22,12 @@ package org.mariotaku.twidere.view.holder.status
import android.content.Context import android.content.Context
import android.content.SharedPreferences import android.content.SharedPreferences
import android.graphics.Rect import android.graphics.Rect
import android.net.Uri import androidx.annotation.UiThread
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.appcompat.widget.ActionMenuView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.text.SpannableString import android.text.SpannableString
import android.text.SpannableStringBuilder import android.text.SpannableStringBuilder
import android.text.Spanned import android.text.Spanned
@ -33,13 +38,6 @@ import android.view.LayoutInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import androidx.annotation.UiThread
import androidx.appcompat.widget.ActionMenuView
import androidx.core.content.ContextCompat
import androidx.core.view.ViewCompat
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.adapter_item_status_count_label.view.* import kotlinx.android.synthetic.main.adapter_item_status_count_label.view.*
import kotlinx.android.synthetic.main.header_status.view.* import kotlinx.android.synthetic.main.header_status.view.*
import org.mariotaku.abstask.library.TaskStarter import org.mariotaku.abstask.library.TaskStarter
@ -351,7 +349,7 @@ class DetailStatusViewHolder(
val lang = status.lang val lang = status.lang
if (CheckUtils.isValidLocale(lang) /* && account.isOfficial(context)*/) { if (CheckUtils.isValidLocale(lang) && account.isOfficial(context)) {
translateContainer.visibility = View.VISIBLE translateContainer.visibility = View.VISIBLE
if (translation != null) { if (translation != null) {
val locale = Locale(translation.translatedLang) val locale = Locale(translation.translatedLang)

View File

@ -326,8 +326,6 @@
<string name="dns_server">DNS Server</string> <string name="dns_server">DNS Server</string>
<string name="dns_server_summary">Set DNS Server for network requests.</string> <string name="dns_server_summary">Set DNS Server for network requests.</string>
<string name="yandex_key">Yandex Api Key</string>
<string name="draft_saved">Draft saved</string> <string name="draft_saved">Draft saved</string>
<string name="drafts_hint_messages">Your unsent tweets will save here</string> <string name="drafts_hint_messages">Your unsent tweets will save here</string>

View File

@ -36,12 +36,6 @@
android:summary="@string/custom_host_mapping_summary" android:summary="@string/custom_host_mapping_summary"
android:title="@string/custom_host_mapping"/> android:title="@string/custom_host_mapping"/>
<org.mariotaku.twidere.preference.ThemedEditTextPreference
android:dialogTitle="@string/yandex_key"
android:key="yandex_key"
android:singleLine="true"
android:title="@string/yandex_key"/>
<org.mariotaku.twidere.preference.TintedPreferenceCategory <org.mariotaku.twidere.preference.TintedPreferenceCategory
android:key="category_proxy" android:key="category_proxy"
android:title="@string/proxy"> android:title="@string/proxy">