Press back to hide EditSourceCompose. Double back to quit app - Android

This commit is contained in:
Hasinjato 2026-08-11 15:00:23 +03:00
parent b82fc6a048
commit 1f4c0d4829
4 changed files with 36 additions and 3 deletions

View file

@ -1,5 +1,6 @@
package mg.dot.feufaro
import SharedScreenModel
import android.Manifest
import android.app.ComponentCaller
import android.content.Intent
@ -10,7 +11,9 @@ import android.os.Environment
import android.util.Base64
import android.view.View
import android.view.WindowManager
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.OnBackPressedCallback
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.runtime.Composable
@ -22,11 +25,14 @@ import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import mg.dot.feufaro.ui.LocalFullscreenController
import org.koin.android.ext.android.inject
import org.koin.androidx.compose.KoinAndroidContext
import java.io.File
import java.util.zip.GZIPInputStream
class MainActivity : ComponentActivity() {
private val sharedScreenModel: SharedScreenModel by inject()
private var lastBackTime = 0L
override fun onCreate(savedInstanceState: Bundle?) {
ActivityCompat.requestPermissions(
this,
@ -43,6 +49,27 @@ class MainActivity : ComponentActivity() {
WindowCompat.setDecorFitsSystemWindows(window, false)
intent?.let { handleDeepLinkIntent(it) }
onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
if (sharedScreenModel.sourceModeState.value) {
sharedScreenModel.toggleSourceMode()
return
}
val currentTime = System.currentTimeMillis()
if (currentTime - lastBackTime < 2000) {
finish()
} else {
lastBackTime = currentTime
Toast.makeText(
this@MainActivity,
"Appuyez à nouveau pour quitter",
Toast.LENGTH_SHORT
).show()
}
}
})
setContent {
KoinAndroidContext {
@ -65,7 +92,6 @@ class MainActivity : ComponentActivity() {
windowInsetsController.show(WindowInsetsCompat.Type.statusBars())
windowInsetsController.systemBarsBehavior =
WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
window.statusBarColor = android.graphics.Color.TRANSPARENT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
window.attributes.layoutInDisplayCutoutMode =

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/ic_launcher_background"/>
<background android:drawable="@drawable/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
<background android:drawable="@mipmap/ic_launcher_background"/>
<background android:drawable="@drawable/ic_launcher_background"/>
<foreground android:drawable="@drawable/ic_launcher_foreground"/>
</adaptive-icon>

View file

@ -44,6 +44,8 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.*
import androidx.compose.ui.window.Popup
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import cafe.adriel.voyager.core.annotation.InternalVoyagerApi
import cafe.adriel.voyager.navigator.internal.BackHandler
import feufaro.composeapp.generated.resources.Emmentaler
import feufaro.composeapp.generated.resources.PTSerif_Bold
import feufaro.composeapp.generated.resources.PT_Serif_Bold_Italic
@ -1421,12 +1423,17 @@ private suspend fun expandInclusions(
return result.toString().trimEnd()
}
@OptIn(InternalVoyagerApi::class)
@Composable
fun EditSourceCompose(
sharedScreenModel: SharedScreenModel,
solfaScreenModel: SolfaScreenModel
) {
var isEditorVisible = sharedScreenModel.sourceModeState.value
BackHandler(enabled = isEditorVisible) {
sharedScreenModel.toggleSourceMode()
}
var sourcePath = sharedScreenModel.activeFilePath.value
var sourceContent = sharedScreenModel.fileContent.value ?: ""
var sourceTitle = sharedScreenModel.songTitle.value