-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
191 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/main/java/io/github/yamin8000/owl/ui/BaseActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
app/src/main/java/io/github/yamin8000/owl/ui/FloatingActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters