diff --git a/src/main/kotlin/com/bolenpad/photophetch/ui/AppState.kt b/src/main/kotlin/com/bolenpad/photophetch/ui/AppState.kt index 74ed322..1b72595 100644 --- a/src/main/kotlin/com/bolenpad/photophetch/ui/AppState.kt +++ b/src/main/kotlin/com/bolenpad/photophetch/ui/AppState.kt @@ -220,10 +220,19 @@ class AppState( fun prepareImport() { val device = _selectedDevice.value ?: return + val cfg = _config.value val today = LocalDate.now() - val folderName = _config.value.folderNameTemplate + val baseName = cfg.folderNameTemplate .replace("{date}", today.toString()) .replace("{device}", StagingPath.deviceLabel(device)) + + // Auto-increment if the folder already exists: 2026-07-28_iPhone15Pro_2, _3, … + val folderName = if (cfg.stagingRoot.isNotBlank()) { + StagingPath.uniqueFolderName(Paths.get(cfg.stagingRoot), baseName) + } else { + baseName + } + _importConfig.update { it.copy(folderName = folderName) } navigateTo(Screen.IMPORT_CONFIG) } diff --git a/src/main/kotlin/com/bolenpad/photophetch/util/StagingPath.kt b/src/main/kotlin/com/bolenpad/photophetch/util/StagingPath.kt index 9da58bc..7e56dc2 100644 --- a/src/main/kotlin/com/bolenpad/photophetch/util/StagingPath.kt +++ b/src/main/kotlin/com/bolenpad/photophetch/util/StagingPath.kt @@ -49,6 +49,26 @@ object StagingPath { return stagingRoot.resolve(folderName) } + /** + * Returns a folder name that doesn't already exist under [stagingRoot]. + * If the base name is free, returns it as-is. + * If it already exists, appends _2, _3, … until a free name is found. + * + * Examples: + * "2026-07-28_iPhone15Pro" → free → returns as-is + * "2026-07-28_iPhone15Pro" taken → returns "2026-07-28_iPhone15Pro_2" + * "2026-07-28_iPhone15Pro_2" taken → returns "2026-07-28_iPhone15Pro_3" + */ + fun uniqueFolderName(stagingRoot: Path, baseName: String): String { + if (!stagingRoot.resolve(baseName).exists()) return baseName + var counter = 2 + while (true) { + val candidate = "${baseName}_$counter" + if (!stagingRoot.resolve(candidate).exists()) return candidate + counter++ + } + } + /** * Create the staging directory if it doesn't already exist. * @throws IllegalStateException if the parent is not writable