Skip to content

Commit 0ab69c4

Browse files
committed
Add custom replacements: ${extension}, ${date}, ${time}, ${datetime:<*>}
1 parent b3b926f commit 0ab69c4

File tree

4 files changed

+36
-8
lines changed

4 files changed

+36
-8
lines changed

app/src/main/java/co/adityarajput/fileflow/data/models/Action.kt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ import co.adityarajput.fileflow.utils.*
1313
import co.adityarajput.fileflow.views.dullStyle
1414
import kotlinx.serialization.Serializable
1515
import java.nio.file.FileAlreadyExistsException
16-
import kotlin.uuid.ExperimentalUuidApi
17-
import kotlin.uuid.Uuid
1816

1917
@Suppress("ClassName")
2018
@Serializable
@@ -69,16 +67,15 @@ sealed class Action {
6967
append(destFileNameTemplate)
7068
}
7169

72-
@OptIn(ExperimentalUuidApi::class)
7370
fun getDestFileName(srcFile: File) =
7471
srcFile.name!!.replace(
7572
Regex(srcFileNamePattern),
76-
destFileNameTemplate.replace(
77-
$$"${uuid}",
78-
Uuid.random().toString(),
79-
).replace(
73+
destFileNameTemplate.applyCustomReplacements().replace(
8074
$$"${folder}",
8175
srcFile.parent?.name ?: "",
76+
).replace(
77+
$$"${extension}",
78+
srcFile.extension,
8279
),
8380
)
8481

app/src/main/java/co/adityarajput/fileflow/utils/Files.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ sealed class File {
122122
}
123123
}
124124

125+
val extension
126+
get() = when (this) {
127+
is SAFFile -> documentFile.name?.substringAfterLast('.', "").orEmpty()
128+
is FSFile -> ioFile.extension
129+
}
130+
125131
val name
126132
get() = when (this) {
127133
is SAFFile -> documentFile.name

app/src/main/java/co/adityarajput/fileflow/utils/String.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import androidx.compose.runtime.Composable
44
import androidx.compose.ui.res.pluralStringResource
55
import androidx.compose.ui.res.stringResource
66
import co.adityarajput.fileflow.R
7+
import java.time.ZonedDateTime
8+
import java.time.format.DateTimeFormatter
9+
import kotlin.uuid.ExperimentalUuidApi
10+
import kotlin.uuid.Uuid
711

812
@Composable
913
fun Long.toShortHumanReadableTime(): String {
@@ -42,3 +46,24 @@ fun Long.toAccurateHumanReadableTime(): String {
4246
@Composable
4347
fun Boolean.getToggleString(): String =
4448
stringResource(if (this) R.string.disable else R.string.enable)
49+
50+
@OptIn(ExperimentalUuidApi::class)
51+
fun String.applyCustomReplacements() = this
52+
.replace(
53+
$$"${uuid}",
54+
Uuid.random().toString(),
55+
)
56+
.replace(
57+
$$"${date}",
58+
ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE),
59+
)
60+
.replace(
61+
$$"${time}",
62+
ZonedDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME),
63+
)
64+
.replace(
65+
Regex("\\$\\{datetime:([^}]+)}"),
66+
{ result ->
67+
ZonedDateTime.now().format(DateTimeFormatter.ofPattern(result.groupValues[1]))
68+
},
69+
)

app/src/main/java/co/adityarajput/fileflow/utils/Time.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ val ZonedDateTime.isToday get() = toLocalDate() == ZonedDateTime.now().toLocalDa
3434

3535
private val cronParser = CronParser(CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX))
3636

37-
fun String.getExecutionTimes(count: Int = 1): List<ZonedDateTime>? {
37+
fun String.getExecutionTimes(count: Int): List<ZonedDateTime>? {
3838
try {
3939
val schedule = cronParser.parse(this)
4040
val executionTime = ExecutionTime.forCron(schedule)

0 commit comments

Comments
 (0)