Support screen orientation change Settings on Android
This commit is contained in:
parent
aaec06944c
commit
ea4975df9b
6 changed files with 104 additions and 19 deletions
|
|
@ -17,7 +17,8 @@
|
|||
<activity
|
||||
android:exported="true"
|
||||
android:name=".MainActivity"
|
||||
android:windowSoftInputMode="adjustResize">
|
||||
android:windowSoftInputMode="adjustResize"
|
||||
android:configChanges="orientation|screenSize|screenLayout|keyboardHidden">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import SharedScreenModel
|
|||
import android.Manifest
|
||||
import android.app.ComponentCaller
|
||||
import android.content.Intent
|
||||
import android.content.pm.ActivityInfo
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.view.WindowManager
|
||||
|
|
@ -32,6 +33,16 @@ class MainActivity : ComponentActivity() {
|
|||
private val solfaScreenModel: SolfaScreenModel by inject()
|
||||
private var lastBackTime = 0L
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
ActivityProvider.currentActivity = this
|
||||
|
||||
val prefs = provideSettings()
|
||||
val rotationMode = prefs.getString("screen_rotation_mode", "LANDSCAPE")
|
||||
requestedOrientation = when (rotationMode) {
|
||||
"PORTRAIT" -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
||||
"AUTO" -> ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
|
||||
else -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
|
||||
}
|
||||
|
||||
ActivityCompat.requestPermissions(
|
||||
this,
|
||||
arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE),
|
||||
|
|
@ -88,6 +99,14 @@ class MainActivity : ComponentActivity() {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
super.onDestroy()
|
||||
if (ActivityProvider.currentActivity == this) {
|
||||
ActivityProvider.currentActivity = null
|
||||
}
|
||||
}
|
||||
|
||||
fun updateSystemBarsVisibility(isFullScreen: Boolean) {
|
||||
val windowInsetsController = WindowCompat.getInsetsController(window, window.decorView)
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,7 @@
|
|||
package mg.dot.feufaro
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.pm.ActivityInfo
|
||||
import com.russhwolf.settings.Settings
|
||||
import com.russhwolf.settings.set
|
||||
|
||||
|
|
@ -13,4 +15,17 @@ actual class SaveSettings actual constructor() {
|
|||
actual fun loadWindowState(): WindowState {
|
||||
return WindowState(0, 0, 0, 0)
|
||||
}
|
||||
|
||||
actual fun setScreenOrientation(key: String) {
|
||||
val activity = ActivityProvider.currentActivity as? Activity
|
||||
activity?.requestedOrientation = when (key) {
|
||||
"PORTRAIT" -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
|
||||
"AUTO" -> ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
|
||||
else -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
object ActivityProvider {
|
||||
var currentActivity: Activity? = null
|
||||
}
|
||||
|
|
@ -15,4 +15,5 @@ expect class SaveSettings() {
|
|||
|
||||
fun loadWindowState(): WindowState
|
||||
fun saveWindowState(state: WindowState)
|
||||
fun setScreenOrientation(key: String)
|
||||
}
|
||||
|
|
@ -34,10 +34,13 @@ import feufaro.composeapp.generated.resources.Res
|
|||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import mg.dot.feufaro.DisplayConfigManager
|
||||
import mg.dot.feufaro.getPlatform
|
||||
import mg.dot.feufaro.SaveSettings
|
||||
import mg.dot.feufaro.midi.Dynamic
|
||||
import mg.dot.feufaro.midi.MidiInstrument
|
||||
import mg.dot.feufaro.provideSettings
|
||||
import mg.dot.feufaro.ui.MyVerticalScrollbar
|
||||
import mg.dot.feufaro.ui.ThemeDefaults
|
||||
import mg.dot.feufaro.ui.toColorOrDefault
|
||||
import org.jetbrains.compose.resources.Font
|
||||
import org.koin.compose.koinInject
|
||||
|
||||
|
|
@ -52,11 +55,10 @@ fun Settings(
|
|||
val prefs: Settings = provideSettings()
|
||||
var hasCountIn by remember { mutableStateOf(prefs.getBoolean("isCountIn", true)) }
|
||||
var isSmartPointBLoop by remember { mutableStateOf(prefs.getBoolean("smartBpoint", true)) }
|
||||
var rotationMode by remember { mutableStateOf(prefs.getString("screen_rotation_mode", "LANDSCAPE")) }
|
||||
var isDynamicEnabled by remember { mutableStateOf(Dynamic.isGloballyEnabled) }
|
||||
var refreshTrigger by remember { mutableStateOf(0) }
|
||||
val isFullScreenEnabled by sharedScreenModel.isFullScreen.collectAsState()
|
||||
val platform = getPlatform()
|
||||
val isAndroid = platform.name.startsWith("Android")
|
||||
|
||||
// Load Configurations
|
||||
val displayConfigManager = koinInject<DisplayConfigManager>()
|
||||
|
|
@ -94,20 +96,7 @@ fun Settings(
|
|||
val selectedInstrument = instruments.find { it.program == globalInstrumentProgram }
|
||||
?: MidiInstrument(program = globalInstrumentProgram, name = "Instrument $globalInstrumentProgram")
|
||||
|
||||
val menuScrollState = rememberScrollState()
|
||||
var expandedDropdown by remember { mutableStateOf(true) }
|
||||
|
||||
LaunchedEffect(expandedDropdown, globalInstrumentProgram) {
|
||||
if (expandedDropdown) {
|
||||
val selectedIndex = instruments.indexOfFirst { it.program == globalInstrumentProgram }
|
||||
if (selectedIndex != -1) {
|
||||
val itemHeightPx = 16 * 3
|
||||
|
||||
kotlinx.coroutines.yield()
|
||||
menuScrollState.scrollTo(selectedIndex * itemHeightPx)
|
||||
}
|
||||
}
|
||||
}
|
||||
val saveSettings = remember { SaveSettings() }
|
||||
|
||||
AlertDialog(
|
||||
properties = DialogProperties(
|
||||
|
|
@ -175,6 +164,64 @@ fun Settings(
|
|||
}
|
||||
}
|
||||
|
||||
if (isAndroid) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(vertical = 4.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
Text(
|
||||
text = "Orientation de l'écran",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurface
|
||||
)
|
||||
|
||||
val rotationOptions = listOf(
|
||||
Triple("LANDSCAPE", "Paysage", Icons.Default.StayCurrentLandscape),
|
||||
Triple("PORTRAIT", "Portrait", Icons.Default.StayCurrentPortrait),
|
||||
Triple("AUTO", "Auto", Icons.Default.ScreenRotation)
|
||||
)
|
||||
|
||||
Row(
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
horizontalArrangement = Arrangement.spacedBy(8.dp)
|
||||
) {
|
||||
rotationOptions.forEach { (key, label, icon) ->
|
||||
val isSelected = rotationMode == key
|
||||
IconButton(
|
||||
onClick = {
|
||||
rotationMode = key
|
||||
prefs.putString("screen_rotation_mode", key)
|
||||
saveSettings.setScreenOrientation(key)
|
||||
onDismissRequest()
|
||||
},
|
||||
modifier = Modifier
|
||||
.weight(1f)
|
||||
.height(50.dp),
|
||||
colors = IconButtonColors(
|
||||
containerColor = if (isSelected) MaterialTheme.colorScheme.primary.copy(alpha = 0.15f) else Color.Transparent,
|
||||
contentColor = if (isSelected) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.onSurface,
|
||||
disabledContainerColor = MaterialTheme.colorScheme.onSecondary,
|
||||
disabledContentColor = MaterialTheme.colorScheme.onSecondary
|
||||
)
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center
|
||||
) {
|
||||
Icon(
|
||||
imageVector = icon,
|
||||
contentDescription = label,
|
||||
modifier = Modifier.size(25.dp)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
HorizontalDivider(color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f))
|
||||
}
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
|
|
|
|||
|
|
@ -42,4 +42,6 @@ actual class SaveSettings actual constructor() {
|
|||
state.y
|
||||
)
|
||||
}
|
||||
|
||||
actual fun setScreenOrientation(key: String) { }
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue