Skip to content

Commit

Permalink
fix: keep inserted text when going to fullscreen app from translation…
Browse files Browse the repository at this point in the history
… dialog
  • Loading branch information
Bnyro committed Feb 4, 2025
1 parent e335f79 commit 15455a0
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 118 deletions.
54 changes: 0 additions & 54 deletions app/src/main/java/com/bnyro/translate/ui/BaseActivity.kt

This file was deleted.

7 changes: 1 addition & 6 deletions app/src/main/java/com/bnyro/translate/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import android.os.Bundle
import androidx.navigation.compose.rememberNavController
import com.bnyro.translate.ui.nav.NavigationHost

class MainActivity : BaseActivity() {
class MainActivity : TranslationActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

Expand All @@ -30,9 +30,4 @@ class MainActivity : BaseActivity() {
NavigationHost(navController, translationModel)
}
}

override fun onStop() {
translationModel.saveSelectedLanguages()
super.onStop()
}
}
54 changes: 6 additions & 48 deletions app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,9 @@

package com.bnyro.translate.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.Configuration
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -39,20 +35,15 @@ import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties
import com.bnyro.translate.R
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.ext.parcelable
import com.bnyro.translate.ui.components.AppHeader
import com.bnyro.translate.ui.components.DialogButton
import com.bnyro.translate.ui.views.SimTranslationDialogComponent
import com.bnyro.translate.ui.views.TranslationComponent
import com.bnyro.translate.util.ImageHelper

class ShareActivity : BaseActivity() {
class ShareActivity : TranslationActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

handleIntentData()

showContent {
val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp
Expand Down Expand Up @@ -90,7 +81,11 @@ class ShareActivity : BaseActivity() {
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
AppHeader(modifier = Modifier.weight(1f))
AppHeader(modifier = Modifier.weight(1f)) {
val intent = Intent(this@ShareActivity, MainActivity::class.java)
.putExtra(Intent.EXTRA_TEXT, translationModel.insertedText)
this@ShareActivity.startActivity(intent)
}

if (translationModel.simTranslationEnabled) {
SimTranslationDialogComponent(translationModel)
Expand All @@ -109,41 +104,4 @@ class ShareActivity : BaseActivity() {

setFinishOnTouchOutside(false)
}

@SuppressLint("InlinedApi")
private fun getIntentText(): String? {
return intent.getCharSequenceExtra(Intent.EXTRA_TEXT)?.toString()
?: intent.takeIf { Build.VERSION.SDK_INT > Build.VERSION_CODES.M }
?.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT)?.toString()
?: intent.getCharSequenceExtra(Intent.ACTION_SEND)?.toString()
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent
handleIntentData()
}

private fun handleIntentData() {
getIntentText()?.let {
translationModel.insertedText = it
translationModel.translateNow(this)
}
// open links from Google Translate
if (intent.data?.host == "translate.google.com") {
val source = intent.data?.getQueryParameter("sl").orEmpty()
val target = intent.data?.getQueryParameter("tl").orEmpty()
translationModel.sourceLanguage = Language(source, source)
translationModel.targetLanguage = Language(target, target)
translationModel.insertedText = intent.data?.getQueryParameter("text").orEmpty()
translationModel.translateNow(this)
}
if (intent.type?.startsWith("image/") != true) return

(intent.parcelable<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
ImageHelper.getImage(this, it)?.let { bm ->
translationModel.processImage(this, bm)
}
}
}
}
105 changes: 105 additions & 0 deletions app/src/main/java/com/bnyro/translate/ui/TranslationActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2024 You Apps
*
* 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 <https://www.gnu.org/licenses/>.
*/

package com.bnyro.translate.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModelProvider
import com.bnyro.translate.db.obj.Language
import com.bnyro.translate.ext.hexToColor
import com.bnyro.translate.ext.parcelable
import com.bnyro.translate.ui.models.TranslationModel
import com.bnyro.translate.ui.theme.TranslateYouTheme
import com.bnyro.translate.util.ImageHelper
import com.bnyro.translate.util.LocaleHelper
import com.bnyro.translate.util.Preferences

open class TranslationActivity: ComponentActivity() {
lateinit var translationModel: TranslationModel
var themeMode by mutableStateOf(Preferences.getThemeMode())
var accentColor by mutableStateOf(Preferences.getAccentColor())

override fun onCreate(savedInstanceState: Bundle?) {
LocaleHelper.updateLanguage(this)

translationModel = ViewModelProvider(this)[TranslationModel::class.java]
handleIntentData()

super.onCreate(savedInstanceState)
}

fun showContent(content: @Composable () -> Unit) {
setContent {
TranslateYouTheme(themeMode, accentColor?.hexToColor()) {
content()
}
}
}

@SuppressLint("InlinedApi")
private fun getIntentText(): String? {
return intent.getCharSequenceExtra(Intent.EXTRA_TEXT)?.toString()
?: intent.takeIf { Build.VERSION.SDK_INT > Build.VERSION_CODES.M }
?.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT)?.toString()
?: intent.getCharSequenceExtra(Intent.ACTION_SEND)?.toString()
}

override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
this.intent = intent
handleIntentData()
}

private fun handleIntentData() {
getIntentText()?.let {
translationModel.insertedText = it
translationModel.translateNow(this)
}
// open links from Google Translate
if (intent.data?.host == "translate.google.com") {
val source = intent.data?.getQueryParameter("sl").orEmpty()
val target = intent.data?.getQueryParameter("tl").orEmpty()
translationModel.sourceLanguage = Language(source, source)
translationModel.targetLanguage = Language(target, target)
translationModel.insertedText = intent.data?.getQueryParameter("text").orEmpty()
translationModel.translateNow(this)
}
if (intent.type?.startsWith("image/") != true) return

(intent.parcelable<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
ImageHelper.getImage(this, it)?.let { bm ->
translationModel.processImage(this, bm)
}
}
}

override fun onStop() {
translationModel.saveSelectedLanguages()
super.onStop()
}
}
15 changes: 5 additions & 10 deletions app/src/main/java/com/bnyro/translate/ui/components/AppHeader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package com.bnyro.translate.ui.components

import android.content.Intent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Row
Expand All @@ -31,24 +30,20 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import com.bnyro.translate.R
import com.bnyro.translate.ui.MainActivity

@Composable
fun AppHeader(modifier: Modifier = Modifier) {
val context = LocalContext.current

fun AppHeader(
modifier: Modifier = Modifier,
onClick: () -> Unit
) {
Row(
modifier = modifier.clickable(interactionSource = remember {
MutableInteractionSource()
}, indication = null) {
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
},
}, indication = null, onClick = onClick),
verticalAlignment = Alignment.CenterVertically
) {
Icon(
Expand Down

0 comments on commit 15455a0

Please sign in to comment.