Auto-increment folder name if staging folder already exists

StagingPath.uniqueFolderName() checks whether the base folder name is
already taken and appends _2, _3, etc. until a free name is found.
AppState.prepareImport() now uses this so the ImportScreen always
pre-fills a unique folder name.
This commit is contained in:
Kyle Bolen
2026-07-28 03:48:30 +00:00
parent 05897e6d62
commit 35b98e1857
2 changed files with 30 additions and 1 deletions

View File

@@ -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)
}

View File

@@ -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