diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt index 57588d4..c72b88a 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ScreenSolfa.kt @@ -103,9 +103,20 @@ object ScreenSolfa : Screen { val hasSongBorder by sharedScreenModel.hasSongBorder.collectAsState() val playlistActive by sharedScreenModel.playlistActive.collectAsState() - LaunchedEffect(playlistStanzas, activeFilePath) { - if (playlistStanzas.isNotEmpty() && stanza !in playlistStanzas) { - sharedScreenModel.setStanza(playlistStanzas.first()) + val nbStanzas by sharedScreenModel.nbStanzas.collectAsState() + val isParsing by sharedScreenModel.isParsing.collectAsState() + + LaunchedEffect(activeFilePath, nbStanzas, playlistStanzas, isParsing, stanza) { + if (isParsing || activeFilePath.isBlank() || nbStanzas <= 0) return@LaunchedEffect + + val effectiveAllowed = if (playlistStanzas.isEmpty()) { + (1..nbStanzas).toList() + } else { + playlistStanzas.filter { it in 1..nbStanzas } + } + + if (effectiveAllowed.isNotEmpty() && stanza !in effectiveAllowed) { + sharedScreenModel.setStanza(effectiveAllowed.first()) scrollState.scrollTo(0) } } @@ -311,7 +322,7 @@ object ScreenSolfa : Screen { if (dragPressed.isEmpty()) { val totalDragX = currentX - startPos.x - val threshold = screenWidthPx * 0.30f + val threshold = screenWidthPx * 0.10f // println("DEBUG END DRAG -> totalDragX: ${totalDragX}f, threshold requis: ${threshold}f") diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt index bd99e97..89e4188 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/solfa/Solfa.kt @@ -279,76 +279,81 @@ class Solfa(val sharedScreenModel: SharedScreenModel, private val fileRepository //println("37: lFL bien reçu $sourceFile") currentJob?.cancel() currentJob = solfaScope.launch { - val lines = try { - fileRepository.readFileLines(sourceFile) - } catch (e: Exception) { - println("Opening $sourceFile raised exception {${e.message}") - emptyList() - } - if (!hasRequiredM0Metadata(lines)) { - withContext(Dispatchers.Main) { - sharedScreenModel.showMissingMetadataModal(MISSING_M0_METADATA_MESSAGE) - } - return@launch - } - - currentFile = sourceFile - val contentString = lines.joinToString("\n") - sharedScreenModel.setFileContent(contentString, sourceFile) - O.clear() - T.clear() - N.clear() - L.clear() - globalDComments.clear() - meta.clear() - if (pitches.isNotEmpty()) { - pitches[0].reset() - } - pitches.clear() - refrainBeginsAt = -1 - refrainEndsAt = -1 - refrainMaxStanza = -1 - unparsedNote.clear() - voiceMarkers.clear() - templateString = "" - nextTIndex = -1 - nextNIndex = -1 - nextLIndex = -1 - lyricsComment.clear() - if (sharedScreenModel.stanza.value == 0) { - sharedScreenModel.setStanza(1) - } - sharedScreenModel.setNbStanzas(0) - TimeUnitObject.hasMarker(false) - lines.forEach { line -> - parseOneLine(line) - } - unparsedNote.forEach { - preloadN(it) - } - val localAccumulator = mutableListOf() + sharedScreenModel.setParsing(true) try { - withContext(Dispatchers.Main) { - sharedScreenModel.updateFullTUOList(emptyList()) + val lines = try { + fileRepository.readFileLines(sourceFile) + } catch (e: Exception) { + println("Opening $sourceFile raised exception {${e.message}") + emptyList() + } + if (!hasRequiredM0Metadata(lines)) { + withContext(Dispatchers.Main) { + sharedScreenModel.showMissingMetadataModal(MISSING_M0_METADATA_MESSAGE) + } + return@launch } - nextTimeUnitObject(localAccumulator) - } catch (e: Exception) { - println("Erreur parseScope Solfa:150 : ${e.message} iter: ${TimeUnitObject.nbBlock}") - } - attachLyricsToPitches(localAccumulator) - val pitches = pitches.sortedWith(compareBy({ it.tick }, { it.voiceNumber })) - val midiWriter = MidiWriterKotlin(fileRepository) - val markers = finalizeMarkers(localAccumulator, sharedScreenModel) - val originalKey = sharedScreenModel.songKey.value.ifEmpty { meta["C"] ?: "C" } - midiWriter.process(pitches, markers, originalKey) - midiWriter.save("whawyd3.mid") - val allowed = sharedScreenModel.allowedStanzas.value - val currentStz = sharedScreenModel.stanza.value - if (currentStz !in allowed && allowed.isNotEmpty()) { - sharedScreenModel.setStanza(allowed.first()) - } else if (currentStz == 0) { - sharedScreenModel.setStanza(1) + currentFile = sourceFile + val contentString = lines.joinToString("\n") + sharedScreenModel.setFileContent(contentString, sourceFile) + O.clear() + T.clear() + N.clear() + L.clear() + globalDComments.clear() + meta.clear() + if (pitches.isNotEmpty()) { + pitches[0].reset() + } + pitches.clear() + refrainBeginsAt = -1 + refrainEndsAt = -1 + refrainMaxStanza = -1 + unparsedNote.clear() + voiceMarkers.clear() + templateString = "" + nextTIndex = -1 + nextNIndex = -1 + nextLIndex = -1 + lyricsComment.clear() + if (sharedScreenModel.stanza.value == 0) { + sharedScreenModel.setStanza(1) + } + sharedScreenModel.setNbStanzas(0) + TimeUnitObject.hasMarker(false) + lines.forEach { line -> + parseOneLine(line) + } + unparsedNote.forEach { + preloadN(it) + } + val localAccumulator = mutableListOf() + try { + withContext(Dispatchers.Main) { + sharedScreenModel.updateFullTUOList(emptyList()) + } + nextTimeUnitObject(localAccumulator) + } catch (e: Exception) { + println("Erreur parseScope Solfa:150 : ${e.message} iter: ${TimeUnitObject.nbBlock}") + } + attachLyricsToPitches(localAccumulator) + val pitches = pitches.sortedWith(compareBy({ it.tick }, { it.voiceNumber })) + val midiWriter = MidiWriterKotlin(fileRepository) + val markers = finalizeMarkers(localAccumulator, sharedScreenModel) + val originalKey = sharedScreenModel.songKey.value.ifEmpty { meta["C"] ?: "C" } + + midiWriter.process(pitches, markers, originalKey) + midiWriter.save("whawyd3.mid") + val allowed = sharedScreenModel.allowedStanzas.value + val currentStz = sharedScreenModel.stanza.value + if (currentStz !in allowed && allowed.isNotEmpty()) { + sharedScreenModel.setStanza(allowed.first()) + } else if (currentStz == 0) { + sharedScreenModel.setStanza(1) + } + } finally { + sharedScreenModel.setParsing(false) } } } diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt index e04fa24..cf4e740 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/DrawerUI.kt @@ -232,7 +232,8 @@ fun MainScreenWithDrawer( scope.launch { drawerState.close() } } - ModalNavigationDrawer(drawerState = drawerState, drawerContent = { + val playlistActive by sharedScreenModel.playlistActive.collectAsState() + ModalNavigationDrawer(drawerState = drawerState, gesturesEnabled = playlistActive == null, drawerContent = { SimpleDrawerContent( items, solfaScreenModel, diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/SimpleDrawerContent.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/SimpleDrawerContent.kt index e385543..54e6f45 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/SimpleDrawerContent.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/ui/SimpleDrawerContent.kt @@ -214,6 +214,7 @@ fun SimpleDrawerContent( onClick = { scope.launch { drawerState.close() } sharedScreenModel.reset() + sharedScreenModel.setPlaylistActive(null) solfaScreenModel.loadFromFile(item.path) onSongSelected(midi) }, @@ -269,6 +270,7 @@ fun SimpleDrawerContent( onClick = { scope.launch { drawerState.close() } sharedScreenModel.reset() + sharedScreenModel.setPlaylistActive(null) solfaScreenModel.loadFromFile(item.path) onSongSelected(midi) }, @@ -404,6 +406,7 @@ fun SimpleDrawerContent( onClick = { scope.launch { drawerState.close() } sharedScreenModel.reset() + sharedScreenModel.setPlaylistActive(null) solfaScreenModel.loadFromFile(item.path) onSongSelected(midi) }, diff --git a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt index eb73240..3e1483f 100644 --- a/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt +++ b/composeApp/src/commonMain/kotlin/mg/dot/feufaro/viewmodel/SharedScreenModel.kt @@ -783,15 +783,19 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode val activeFilePath: State = _activeFilePath fun setFileContent(content: String?, path: String) { + val isNewSong = !samePath(_activeFilePathFlow.value, path) _fileContent.value = content _activeFilePath.value = path _activeFilePathFlow.value = path val active = _activePlaylist.value val entry = active.find { samePath(it.path, path) } + if (isNewSong) { + _nbStanzas.value = 0 + } if (entry != null && entry.stanzas.isNotEmpty()) { _stanza.value = entry.stanzas.sorted().first() - } else if (_stanza.value == 0) { + } else if (isNewSong || _stanza.value <= 0) { _stanza.value = 1 } @@ -1287,6 +1291,7 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode _showSearchMenu.value = false _sourceModeState.value = false _harmonyView.value = false + _stanza.value = 0 setTranspositionInterval(0) setTranspToC(0) setSilentDurBefr(0) @@ -1321,6 +1326,12 @@ class SharedScreenModel(private val fileRepository: FileRepository) : ScreenMode _songTitle.value = theTitle } + private val _isParsing = MutableStateFlow(false) + val isParsing: StateFlow = _isParsing.asStateFlow() + + fun setParsing(parsing: Boolean) { + _isParsing.value = parsing + } fun setStanza(theStanza: Int) { try { _stanza.value = theStanza