🚀 Improve Dictionary text selection action

Improved Dictionary action, now it supports most dictionary apps or redirects user to browser page. Previously if user had dictionary app he could not use it.

Partly resolves: #46
This commit is contained in:
Acclorite 2024-08-14 19:08:31 +03:00
parent 3766e4b230
commit a830be1403
6 changed files with 36 additions and 33 deletions

View file

@ -17,26 +17,6 @@
android:name="ua.acclorite.book_story.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
tools:node="remove" />
<queries>
<!-- Translator -->
<intent>
<action android:name="android.intent.action.PROCESS_TEXT" />
<data android:mimeType="text/plain" />
</intent>
<!-- Browser -->
<intent>
<action android:name="android.intent.action.WEB_SEARCH" />
</intent>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="https" />
</intent>
</queries>
<application
android:name=".Application"
android:allowBackup="false"

View file

@ -15,16 +15,18 @@ fun Double.removeDigits(digits: Int) = "%.${digits}f".format(this).replace(",",
fun Intent.launchActivity(
activity: ComponentActivity,
createChooser: Boolean = false,
openInNewWindow: Boolean = true,
success: (() -> Unit)? = null,
error: () -> Unit
) {
val intent = if (createChooser) Intent.createChooser(this, "") else this
if (openInNewWindow) {
this.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
}
try {
activity.baseContext.startActivity(this)
activity.baseContext.startActivity(intent)
} catch (e: Exception) {
error()
return

View file

@ -277,7 +277,8 @@ private fun ReaderScreen(
onTranslateRequested = { textToTranslate ->
onEvent(
ReaderEvent.OnOpenTranslator(
textToTranslate,
textToTranslate = textToTranslate,
translateWholeParagraph = false,
context,
noAppsFound = {
Toast.makeText(

View file

@ -85,6 +85,7 @@ fun LazyItemScope.ReaderTextParagraph(
onEvent(
ReaderEvent.OnOpenTranslator(
textToTranslate = line,
translateWholeParagraph = true,
context = context as ComponentActivity,
noAppsFound = {
Toast.makeText(

View file

@ -46,6 +46,7 @@ sealed class ReaderEvent {
data class OnOpenTranslator(
val textToTranslate: String,
val translateWholeParagraph: Boolean,
val context: ComponentActivity,
val noAppsFound: () -> Unit
) : ReaderEvent()

View file

@ -2,6 +2,7 @@ package ua.acclorite.book_story.presentation.screens.reader.data
import android.app.SearchManager
import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.compose.runtime.snapshotFlow
@ -337,7 +338,10 @@ class ReaderViewModel @Inject constructor(
)
var translatorFailure = false
translatorIntent.launchActivity(event.context) {
translatorIntent.launchActivity(
event.context,
createChooser = !event.translateWholeParagraph
) {
translatorFailure = true
}
if (!translatorFailure) {
@ -358,20 +362,34 @@ class ReaderViewModel @Inject constructor(
is ReaderEvent.OnOpenDictionary -> {
launch {
val dictionaryIntent = Intent()
val browserIntent = Intent()
browserIntent.action = Intent.ACTION_WEB_SEARCH
browserIntent.putExtra(
SearchManager.QUERY,
"dictionary" +
": ${event.textToDefine.trim()}"
dictionaryIntent.type = "text/plain"
dictionaryIntent.action = Intent.ACTION_PROCESS_TEXT
dictionaryIntent.putExtra(
Intent.EXTRA_PROCESS_TEXT,
event.textToDefine.trim()
)
dictionaryIntent.putExtra(Intent.EXTRA_PROCESS_TEXT_READONLY, true)
var failure = false
browserIntent.launchActivity(event.context) {
failure = true
browserIntent.action = Intent.ACTION_VIEW
val text = event.textToDefine.trim().replace(" ", "+")
browserIntent.data = Uri.parse("https://www.onelook.com/?w=$text")
var dictionaryFailure = false
dictionaryIntent.launchActivity(event.context, createChooser = true) {
dictionaryFailure = true
}
if (!failure) {
if (!dictionaryFailure) {
return@launch
}
var browserFailure = false
browserIntent.launchActivity(event.context) {
browserFailure = true
}
if (!browserFailure) {
return@launch
}