Skip to content

Commit

Permalink
feat: landscape orientation support (closes #402)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Sep 21, 2024
1 parent 3a41689 commit 1309296
Show file tree
Hide file tree
Showing 6 changed files with 266 additions and 206 deletions.
1 change: 0 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
android:name=".ui.MainActivity"
android:configChanges="orientation"
android:exported="true"
android:screenOrientation="userPortrait"
android:theme="@style/Theme.TranslateYou"
android:windowSoftInputMode="adjustResize"
tools:ignore="LockedOrientationActivity">
Expand Down
57 changes: 26 additions & 31 deletions app/src/main/java/com/bnyro/translate/ui/ShareActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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
Expand All @@ -31,6 +32,7 @@ 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.unit.dp
import androidx.compose.ui.window.DialogProperties
import com.bnyro.translate.R
Expand All @@ -54,38 +56,31 @@ class ShareActivity : BaseActivity() {
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(
modifier = Modifier.fillMaxSize(),
viewModel = translationModel,
showLanguageSelector = true
)
AlertDialog(modifier = Modifier
.heightIn(
max = if (configuration.orientation == Configuration.ORIENTATION_PORTRAIT) screenHeight * 2 / 3 else Dp.Unspecified
)
.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(
modifier = Modifier.fillMaxSize(),
viewModel = translationModel,
showLanguageSelector = true
)
})
}

setFinishOnTouchOutside(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ fun AppHeader() {
verticalAlignment = Alignment.CenterVertically
) {
Icon(
modifier = Modifier.size(70.dp),
modifier = Modifier.size(50.dp),
painter = painterResource(R.drawable.ic_app_icon),
contentDescription = stringResource(R.string.app_name),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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.res.Configuration
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import com.bnyro.translate.R
import com.bnyro.translate.obj.Translation
import com.bnyro.translate.ui.models.TranslationModel

@Composable
fun LanguageSelectionComponent(viewModel: TranslationModel) {
val orientation = LocalConfiguration.current.orientation

if (orientation == Configuration.ORIENTATION_PORTRAIT) {
Row(
modifier = Modifier
.padding(10.dp)
.padding(top = 5.dp),
verticalAlignment = Alignment.CenterVertically
) {
Box(
modifier = Modifier.weight(1f),
contentAlignment = Alignment.Center
) {
LanguageSelector(
viewModel.availableLanguages,
viewModel.sourceLanguage,
autoLanguageEnabled = viewModel.engine.autoLanguageCode != null,
viewModel = viewModel
) {
if (it == viewModel.targetLanguage) {
viewModel.targetLanguage = viewModel.sourceLanguage
}
viewModel.sourceLanguage = it
viewModel.translateNow()
}
}

SwapLanguagesButton(viewModel)

Box(
modifier = Modifier.weight(1f),
contentAlignment = Alignment.Center
) {
LanguageSelector(
viewModel.availableLanguages,
viewModel.targetLanguage,
viewModel = viewModel
) {
if (it == viewModel.sourceLanguage) {
viewModel.sourceLanguage = viewModel.targetLanguage
}
viewModel.targetLanguage = it
viewModel.translateNow()
}
}
}
} else {
Column(
modifier = Modifier
.padding(10.dp)
.padding(top = 5.dp),
horizontalAlignment = Alignment.CenterHorizontally
) {
Box(
contentAlignment = Alignment.Center
) {
LanguageSelector(
viewModel.availableLanguages,
viewModel.sourceLanguage,
autoLanguageEnabled = viewModel.engine.autoLanguageCode != null,
viewModel = viewModel
) {
if (it == viewModel.targetLanguage) {
viewModel.targetLanguage = viewModel.sourceLanguage
}
viewModel.sourceLanguage = it
viewModel.translateNow()
}
}

SwapLanguagesButton(viewModel)

Box(
contentAlignment = Alignment.Center
) {
LanguageSelector(
viewModel.availableLanguages,
viewModel.targetLanguage,
viewModel = viewModel
) {
if (it == viewModel.sourceLanguage) {
viewModel.sourceLanguage = viewModel.targetLanguage
}
viewModel.targetLanguage = it
viewModel.translateNow()
}
}
}
}
}

@Composable
fun SwapLanguagesButton(viewModel: TranslationModel) {
val switchBtnEnabled by remember {
mutableStateOf(viewModel.sourceLanguage.code.isNotEmpty())
}

IconButton(
onClick = {
if (viewModel.availableLanguages.isEmpty()) return@IconButton
if (!switchBtnEnabled) return@IconButton
val temp = viewModel.sourceLanguage
viewModel.sourceLanguage = viewModel.targetLanguage
viewModel.targetLanguage = temp

if (viewModel.translation.translatedText.isNotEmpty()) {
viewModel.insertedText = viewModel.translation.translatedText
viewModel.translation = Translation("")
}

viewModel.translateNow()
}
) {
Icon(
painterResource(R.drawable.ic_switch),
null,
modifier = Modifier
.size(18.dp),
tint = if (switchBtnEnabled) MaterialTheme.colorScheme.onSurface else Color.Gray
)
}
}

This file was deleted.

Loading

0 comments on commit 1309296

Please sign in to comment.