🚀 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" android:name="ua.acclorite.book_story.DYNAMIC_RECEIVER_NOT_EXPORTED_PERMISSION"
tools:node="remove" /> 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 <application
android:name=".Application" android:name=".Application"
android:allowBackup="false" android:allowBackup="false"

View file

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

View file

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

View file

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

View file

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

View file

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