mirror of
https://github.com/NekoX-Dev/NekoX.git
synced 2024-12-13 21:10:43 +01:00
Improve batch translate
This commit is contained in:
parent
5d67144209
commit
1b37387043
@ -5,11 +5,11 @@ import org.telegram.messenger.LocaleController
|
||||
import org.telegram.messenger.R
|
||||
import org.telegram.tgnet.TLRPC
|
||||
import org.telegram.ui.ArticleViewer
|
||||
import tw.nekomimi.nekogram.NekoConfig
|
||||
import tw.nekomimi.nekogram.transtale.TranslateDb
|
||||
import tw.nekomimi.nekogram.transtale.Translator
|
||||
import tw.nekomimi.nekogram.utils.AlertUtil
|
||||
import tw.nekomimi.nekogram.utils.UIUtil
|
||||
import tw.nekomimi.nekogram.utils.uUpdate
|
||||
import java.lang.Runnable
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
@ -50,21 +50,15 @@ fun HashSet<Any>.filterBaseTexts(): HashSet<Any> {
|
||||
|
||||
fun ArticleViewer.doTransLATE() {
|
||||
|
||||
val dialog = AlertUtil.showProgress(parentActivity)
|
||||
val status = AlertUtil.showProgress(parentActivity)
|
||||
|
||||
dialog.show()
|
||||
|
||||
fun update(message: String) {
|
||||
|
||||
UIUtil.runOnUIThread(Runnable { dialog.setMessage(message) })
|
||||
|
||||
}
|
||||
status.show()
|
||||
|
||||
val transPool = newFixedThreadPoolContext(5, "Article Trans Pool")
|
||||
|
||||
val cancel = AtomicBoolean(false)
|
||||
|
||||
dialog.setOnCancelListener {
|
||||
status.setOnCancelListener {
|
||||
|
||||
adapter[0].trans = false
|
||||
transMenu.setTextAndIcon(LocaleController.getString("Translate", R.string.Translate), R.drawable.ic_translate)
|
||||
@ -85,13 +79,14 @@ fun ArticleViewer.doTransLATE() {
|
||||
val all = array.size
|
||||
val taskCount = AtomicInteger(array.size)
|
||||
|
||||
update("0 / $all")
|
||||
status.uUpdate("0 / $all")
|
||||
|
||||
array.forEach { item ->
|
||||
|
||||
when (item) {
|
||||
|
||||
is TLRPC.RichText -> getText(adapter[0], null, item, item, copy[item] ?: copy[item.parentRichText], 1000, true).takeIf { it.isNotBlank() }?.toString()
|
||||
is TLRPC.RichText -> getText(adapter[0], null, item, item, copy[item]
|
||||
?: copy[item.parentRichText], 1000, true).takeIf { it.isNotBlank() }?.toString()
|
||||
is String -> item
|
||||
else -> null
|
||||
|
||||
@ -101,7 +96,7 @@ fun ArticleViewer.doTransLATE() {
|
||||
|
||||
if (TranslateDb.currentTarget().contains(str)) {
|
||||
|
||||
update("${all - taskCount.get()} / $all")
|
||||
status.uUpdate("${all - taskCount.get()} / $all")
|
||||
|
||||
if (taskCount.decrementAndGet() % 10 == 0) UIUtil.runOnUIThread(Runnable {
|
||||
|
||||
@ -119,7 +114,7 @@ fun ArticleViewer.doTransLATE() {
|
||||
|
||||
Translator.translate(str)
|
||||
|
||||
update((all - taskCount.get()).toString() + " / " + all)
|
||||
status.uUpdate((all - taskCount.get()).toString() + " / " + all)
|
||||
|
||||
if (taskCount.decrementAndGet() % 10 == 0) UIUtil.runOnUIThread(Runnable {
|
||||
|
||||
@ -138,12 +133,13 @@ fun ArticleViewer.doTransLATE() {
|
||||
UIUtil.runOnUIThread(Runnable {
|
||||
|
||||
cancel.set(true)
|
||||
dialog.dismiss()
|
||||
status.dismiss()
|
||||
updatePaintSize()
|
||||
adapter[0].trans = false
|
||||
transMenu.setTextAndIcon(LocaleController.getString("Translate", R.string.Translate), R.drawable.ic_translate)
|
||||
|
||||
AlertUtil.showTransFailedDialog(parentActivity, it is UnsupportedOperationException,it.message ?: it.javaClass.simpleName, Runnable {
|
||||
AlertUtil.showTransFailedDialog(parentActivity, it is UnsupportedOperationException, it.message
|
||||
?: it.javaClass.simpleName, Runnable {
|
||||
doTransLATE()
|
||||
})
|
||||
|
||||
@ -164,20 +160,15 @@ fun ArticleViewer.doTransLATE() {
|
||||
}
|
||||
|
||||
deferreds.awaitAll()
|
||||
|
||||
UIUtil.runOnUIThread(Runnable {
|
||||
|
||||
transPool.cancel()
|
||||
|
||||
if (!cancel.get()) {
|
||||
if (!cancel.get()) UIUtil.runOnUIThread {
|
||||
|
||||
updatePaintSize()
|
||||
dialog.dismiss()
|
||||
status.dismiss()
|
||||
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,9 +1,6 @@
|
||||
package tw.nekomimi.nekogram.parts
|
||||
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.GlobalScope
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import kotlinx.coroutines.*
|
||||
import org.telegram.messenger.LocaleController
|
||||
import org.telegram.messenger.MessageObject
|
||||
import org.telegram.tgnet.TLRPC
|
||||
@ -13,10 +10,12 @@ import tw.nekomimi.nekogram.transtale.TranslateDb
|
||||
import tw.nekomimi.nekogram.transtale.Translator
|
||||
import tw.nekomimi.nekogram.transtale.code2Locale
|
||||
import tw.nekomimi.nekogram.utils.AlertUtil
|
||||
import tw.nekomimi.nekogram.utils.UIUtil
|
||||
import tw.nekomimi.nekogram.utils.uDismiss
|
||||
import tw.nekomimi.nekogram.utils.uUpdate
|
||||
import java.util.*
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
|
||||
fun MessageObject.toRawString(): String {
|
||||
|
||||
@ -109,45 +108,50 @@ fun ChatActivity.translateMessages(target: Locale = NekoConfig.translateToLang?.
|
||||
}
|
||||
|
||||
val status = AlertUtil.showProgress(parentActivity)
|
||||
val canceled = AtomicBoolean()
|
||||
|
||||
val cancel = AtomicBoolean()
|
||||
|
||||
status.setOnCancelListener {
|
||||
|
||||
canceled.set(true)
|
||||
|
||||
cancel.set(true)
|
||||
}
|
||||
|
||||
status.show()
|
||||
|
||||
val deferreds = LinkedList<Deferred<Unit>>()
|
||||
val taskCount = AtomicInteger(messages.size)
|
||||
val transPool = newFixedThreadPoolContext(5, "Message Trans Pool")
|
||||
|
||||
suspend fun next() {
|
||||
val index = taskCount.decrementAndGet()
|
||||
if (index == 0) {
|
||||
status.uDismiss()
|
||||
} else if (messages.size > 1) {
|
||||
status.uUpdate("${messages.size - index} / ${messages.size}")
|
||||
}
|
||||
}
|
||||
|
||||
GlobalScope.launch(Dispatchers.IO) {
|
||||
|
||||
messages.forEachIndexed { i, selectedObject ->
|
||||
|
||||
val isEnd = i == messages.size - 1
|
||||
messages.forEachIndexed { _, selectedObject ->
|
||||
|
||||
val state = selectedObject.translateFinished(target)
|
||||
|
||||
if (messages.size > 1) status.uUpdate("${i + 1} / ${messages.size}")
|
||||
|
||||
if (state == 1) {
|
||||
next()
|
||||
return@forEachIndexed
|
||||
} else if (state == 2) {
|
||||
next()
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
|
||||
selectedObject.messageOwner.translated = true
|
||||
|
||||
messageHelper.resetMessageContent(dialogId, selectedObject)
|
||||
|
||||
if (isEnd) status.dismiss()
|
||||
|
||||
}
|
||||
|
||||
return@forEachIndexed
|
||||
|
||||
}
|
||||
|
||||
withContext(Dispatchers.IO) trans@{
|
||||
deferreds.add(async(transPool) trans@{
|
||||
|
||||
val db = TranslateDb.forLocale(target)
|
||||
|
||||
@ -159,7 +163,7 @@ fun ChatActivity.translateMessages(target: Locale = NekoConfig.translateToLang?.
|
||||
|
||||
if (question == null) {
|
||||
|
||||
if (canceled.get()) return@trans
|
||||
if (cancel.get()) return@trans
|
||||
|
||||
runCatching {
|
||||
|
||||
@ -171,7 +175,7 @@ fun ChatActivity.translateMessages(target: Locale = NekoConfig.translateToLang?.
|
||||
|
||||
val parentActivity = parentActivity
|
||||
|
||||
if (parentActivity != null && !canceled.get()) {
|
||||
if (parentActivity != null && !cancel.get()) {
|
||||
|
||||
AlertUtil.showTransFailedDialog(parentActivity, it is UnsupportedOperationException, it.message
|
||||
?: it.javaClass.simpleName) {
|
||||
@ -196,7 +200,7 @@ fun ChatActivity.translateMessages(target: Locale = NekoConfig.translateToLang?.
|
||||
|
||||
if (answer == null) {
|
||||
|
||||
if (canceled.get()) return@trans
|
||||
if (cancel.get()) return@trans
|
||||
|
||||
runCatching {
|
||||
|
||||
@ -208,7 +212,7 @@ fun ChatActivity.translateMessages(target: Locale = NekoConfig.translateToLang?.
|
||||
|
||||
val parentActivity = parentActivity
|
||||
|
||||
if (parentActivity != null && !canceled.get()) {
|
||||
if (parentActivity != null && !cancel.get()) {
|
||||
|
||||
AlertUtil.showTransFailedDialog(parentActivity, e is UnsupportedOperationException, e.message
|
||||
?: e.javaClass.simpleName) {
|
||||
@ -245,7 +249,7 @@ fun ChatActivity.translateMessages(target: Locale = NekoConfig.translateToLang?.
|
||||
|
||||
val parentActivity = parentActivity
|
||||
|
||||
if (parentActivity != null && !canceled.get()) {
|
||||
if (parentActivity != null && !cancel.get()) {
|
||||
|
||||
AlertUtil.showTransFailedDialog(parentActivity, it is UnsupportedOperationException, it.message
|
||||
?: it.javaClass.simpleName) {
|
||||
@ -267,22 +271,31 @@ fun ChatActivity.translateMessages(target: Locale = NekoConfig.translateToLang?.
|
||||
|
||||
}
|
||||
|
||||
if (!canceled.get()) {
|
||||
if (!cancel.get()) {
|
||||
|
||||
selectedObject.messageOwner.translated = true
|
||||
|
||||
next()
|
||||
|
||||
withContext(Dispatchers.Main) {
|
||||
|
||||
messageHelper.resetMessageContent(dialogId, selectedObject)
|
||||
|
||||
if (isEnd) status.dismiss()
|
||||
|
||||
}
|
||||
|
||||
} else return@trans
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
deferreds.awaitAll()
|
||||
transPool.cancel()
|
||||
|
||||
UIUtil.runOnUIThread {
|
||||
|
||||
if (!cancel.get()) status.uDismiss()
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user