Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pop-up window when translating selection with app #429

Merged
merged 1 commit into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>

</activity>

<activity
android:name=".ui.ShareActivity"
android:configChanges="orientation"
android:exported="true"
android:theme="@style/Theme.Material3.DayNight.Dialog"
android:windowSoftInputMode="adjustResize"
android:excludeFromRecents="true">

<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.TRANSLATE" />
Expand Down
54 changes: 54 additions & 0 deletions app/src/main/java/com/bnyro/translate/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.os.Bundle
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.ext.hexToColor
import com.bnyro.translate.ui.models.TranslationModel
import com.bnyro.translate.ui.theme.TranslateYouTheme
import com.bnyro.translate.util.LocaleHelper
import com.bnyro.translate.util.Preferences

open class BaseActivity: 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]

super.onCreate(savedInstanceState)
}

fun showContent(content: @Composable () -> Unit) {
setContent {
TranslateYouTheme(themeMode, accentColor?.hexToColor()) {
content()
}
}
}
}
77 changes: 5 additions & 72 deletions app/src/main/java/com/bnyro/translate/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,89 +17,22 @@

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.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.compose.rememberNavController
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.nav.NavigationHost
import com.bnyro.translate.ui.theme.TranslateYouTheme
import com.bnyro.translate.util.JsonHelper
import com.bnyro.translate.util.LocaleHelper
import com.bnyro.translate.util.Preferences
import kotlinx.serialization.encodeToString

class MainActivity : ComponentActivity() {
private lateinit var mainModel: TranslationModel
var themeMode by mutableStateOf(Preferences.getThemeMode())
var accentColor by mutableStateOf(Preferences.getAccentColor())

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

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

super.onCreate(savedInstanceState)

setContent {
TranslateYouTheme(themeMode, accentColor?.hexToColor()) {
val navController = rememberNavController()
NavigationHost(navController, mainModel)
}
showContent {
val navController = rememberNavController()
NavigationHost(navController, translationModel)
}

handleIntentData()
}

override fun onStop() {
mainModel.saveSelectedLanguages()
translationModel.saveSelectedLanguages()
super.onStop()
}

@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 {
mainModel.insertedText = it
mainModel.translateNow()
}
// 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()
mainModel.sourceLanguage = Language(source, source)
mainModel.targetLanguage = Language(target, target)
mainModel.insertedText = intent.data?.getQueryParameter("text").orEmpty()
mainModel.translateNow()
}
if (intent.type?.startsWith("image/") != true) return

(intent.parcelable<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
mainModel.processImage(this, it)
}
}
}
126 changes: 126 additions & 0 deletions app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/*
* 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.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.stringResource
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.TranslationComponent

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

handleIntentData()

showContent {
val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp

LaunchedEffect(Unit) {
translationModel.refresh(this@ShareActivity)
}

AlertDialog(
modifier = Modifier
.heightIn(max = screenHeight * 2 / 3)
.padding(horizontal = 10.dp),
properties = DialogProperties(
dismissOnClickOutside = false,
usePlatformDefaultWidth = false
),
onDismissRequest = { finish() },
confirmButton = {
DialogButton(
text = stringResource(R.string.okay)
) {
finish()
}
},
dismissButton = {
DialogButton(text = stringResource(R.string.clear)) {
translationModel.clearTranslation()
}
},
title = {
AppHeader()
},
text = {
TranslationComponent(
viewModel = translationModel,
showLanguageSelector = true
)
}
)
}

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()
}
// 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()
}
if (intent.type?.startsWith("image/") != true) return

(intent.parcelable<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
translationModel.processImage(this, it)
}
}
}
67 changes: 67 additions & 0 deletions app/src/main/java/com/bnyro/translate/ui/components/AppHeader.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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.components

import android.content.Intent
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
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() {
val context = LocalContext.current

Row(
modifier = Modifier.clickable(interactionSource = remember {
MutableInteractionSource()
}, indication = null) {
val intent = Intent(context, MainActivity::class.java)
context.startActivity(intent)
},
verticalAlignment = Alignment.CenterVertically
) {
Icon(
modifier = Modifier.size(70.dp),
painter = painterResource(R.drawable.ic_app_icon),
contentDescription = stringResource(R.string.app_name),
)

Spacer(modifier = Modifier.width(10.dp))

Text(
text = stringResource(id = R.string.app_name),
style = MaterialTheme.typography.headlineSmall
)
}
}
Loading
Loading