Skip to content

Commit

Permalink
floating window WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yamin8000 committed Sep 5, 2024
1 parent ac28ed9 commit 0ec7570
Show file tree
Hide file tree
Showing 4 changed files with 191 additions and 79 deletions.
9 changes: 9 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,15 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<activity
android:name=".ui.FloatingActivity"
android:configChanges="orientation|screenSize|keyboardHidden|keyboard|locale|screenLayout|screenSize"
android:excludeFromRecents="true"
android:exported="true"
android:theme="@style/Theme.AppCompat.Dialog"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
Expand Down
88 changes: 88 additions & 0 deletions app/src/main/java/io/github/yamin8000/owl/ui/BaseActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* freeDictionaryApp/freeDictionaryApp.app.main
* BaseActivity.kt Copyrighted by Yamin Siahmargooei at 2024/9/5
* BaseActivity.kt Last modified at 2024/9/5
* This file is part of freeDictionaryApp/freeDictionaryApp.app.main.
* Copyright (C) 2024 Yamin Siahmargooei
*
* freeDictionaryApp/freeDictionaryApp.app.main 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.
*
* freeDictionaryApp/freeDictionaryApp.app.main 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 freeDictionaryApp. If not, see <https://www.gnu.org/licenses/>.
*/

package io.github.yamin8000.owl.ui

import android.content.res.Configuration
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 dagger.hilt.android.AndroidEntryPoint
import io.github.yamin8000.owl.common.ui.theme.AppTheme
import io.github.yamin8000.owl.datastore.domain.model.ThemeType
import io.github.yamin8000.owl.datastore.domain.usecase.settings.SettingUseCases
import io.github.yamin8000.owl.util.log
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

@AndroidEntryPoint
internal open class BaseActivity : ComponentActivity() {

@Inject
lateinit var settings: SettingUseCases

var appTheme by mutableStateOf<ThemeType>(ThemeType.System)

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

appTheme = findTheme()
}

protected fun showContent(
content: @Composable () -> Unit,
) {
setContent {
val isSystemInDarkTheme = (resources.configuration.uiMode
.and(Configuration.UI_MODE_NIGHT_MASK)) == Configuration.UI_MODE_NIGHT_YES
AppTheme(
isDarkTheme = isDarkTheme(appTheme, isSystemInDarkTheme),
isOledTheme = appTheme == ThemeType.Darker,
isDynamicColor = appTheme == ThemeType.System,
content = content
)
}
}

private fun findTheme(): ThemeType {
return try {
runBlocking {
settings.getTheme()
}
} catch (e: InterruptedException) {
log(e.stackTraceToString())
ThemeType.System
}
}

private fun isDarkTheme(
theme: ThemeType,
isSystemInDarkTheme: Boolean
) = when (theme) {
ThemeType.Light -> false
ThemeType.System -> isSystemInDarkTheme
ThemeType.Dark, ThemeType.Darker -> true
}
}
90 changes: 90 additions & 0 deletions app/src/main/java/io/github/yamin8000/owl/ui/FloatingActivity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* freeDictionaryApp/freeDictionaryApp.app.main
* FloatingActivity.kt Copyrighted by Yamin Siahmargooei at 2024/9/5
* FloatingActivity.kt Last modified at 2024/9/5
* This file is part of freeDictionaryApp/freeDictionaryApp.app.main.
* Copyright (C) 2024 Yamin Siahmargooei
*
* freeDictionaryApp/freeDictionaryApp.app.main 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.
*
* freeDictionaryApp/freeDictionaryApp.app.main 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 freeDictionaryApp. If not, see <https://www.gnu.org/licenses/>.
*/

package io.github.yamin8000.owl.ui

import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties

internal class FloatingActivity : BaseActivity() {

@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val data = handleOutsideInputIntent()

showContent {
val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp.dp
BasicAlertDialog(
onDismissRequest = { finish() },
properties = DialogProperties(
dismissOnClickOutside = false,
usePlatformDefaultWidth = false
),
content = {
Surface(
modifier = Modifier
.size(screenHeight / 2)
.padding(horizontal = 10.dp),
content = {
Text("is this what you want? $data")
}
)
}
)
}
}

private fun handleOutsideInputIntent(): String? {
return if (intent.type == "text/plain") {
when (intent.action) {
Intent.ACTION_TRANSLATE, Intent.ACTION_DEFINE, Intent.ACTION_SEND -> {
intent.getStringExtra(Intent.EXTRA_TEXT)
}

Intent.ACTION_PROCESS_TEXT -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)
else null
}

else -> {
null
}
}
} else {
null
}
}
}
83 changes: 4 additions & 79 deletions app/src/main/java/io/github/yamin8000/owl/ui/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,51 +22,33 @@
package io.github.yamin8000.owl.ui

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import dagger.hilt.android.AndroidEntryPoint
import io.github.yamin8000.karlancer.feature_history.ui.HistoryScreen
import io.github.yamin8000.owl.common.ui.theme.AppTheme
import io.github.yamin8000.owl.datastore.domain.model.ThemeType
import io.github.yamin8000.owl.datastore.domain.usecase.settings.SettingUseCases
import io.github.yamin8000.owl.feature_favourites.FavouritesScreen
import io.github.yamin8000.owl.feature_home.di.HomeViewModelFactory
import io.github.yamin8000.owl.feature_home.ui.HomeScreen
import io.github.yamin8000.owl.feature_home.ui.HomeViewModel
import io.github.yamin8000.owl.feature_settings.ui.SettingsScreen
import io.github.yamin8000.owl.ui.navigation.Nav
import io.github.yamin8000.owl.util.log
import kotlinx.coroutines.runBlocking
import javax.inject.Inject

@AndroidEntryPoint
internal class MainActivity : ComponentActivity() {
internal class MainActivity : BaseActivity() {

private var intentSearch: String? = null

@Inject
lateinit var settings: SettingUseCases

private lateinit var initTheme: ThemeType

@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
@ExperimentalMaterial3Api
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -76,68 +58,11 @@ internal class MainActivity : ComponentActivity() {

enableEdgeToEdge()

intentSearch = handleOutsideInputIntent()

handleTheme()

setContent {
val (theme, onThemeChanged) = remember { mutableStateOf(initTheme) }
AppTheme(
isDarkTheme = isDarkTheme(theme, isSystemInDarkTheme()),
isOledTheme = theme == ThemeType.Darker,
isDynamicColor = theme == ThemeType.System,
content = {
Scaffold {
MainNav(onThemeChanged = onThemeChanged)
}
}
)
}
}

private fun handleTheme() {
try {
runBlocking {
initTheme = settings.getTheme()
showContent {
Scaffold {
MainNav(onThemeChanged = { appTheme = it })
}
} catch (e: InterruptedException) {
log(e.stackTraceToString())
}

if (!this::initTheme.isInitialized) {
initTheme = ThemeType.System
}
}

private fun handleOutsideInputIntent(): String? {
return if (intent.type == "text/plain") {
when (intent.action) {
Intent.ACTION_TRANSLATE, Intent.ACTION_DEFINE, Intent.ACTION_SEND -> {
intent.getStringExtra(Intent.EXTRA_TEXT)
}

Intent.ACTION_PROCESS_TEXT -> {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT)
else null
}

else -> {
null
}
}
} else {
null
}
}

private fun isDarkTheme(
theme: ThemeType,
isSystemInDarkTheme: Boolean
) = when (theme) {
ThemeType.Light -> false
ThemeType.System -> isSystemInDarkTheme
ThemeType.Dark, ThemeType.Darker -> true
}

@Composable
Expand Down

0 comments on commit 0ec7570

Please sign in to comment.