Skip to content

Commit f5948d0

Browse files
authored
feat: Add ${uuid} special reference (#18)
2 parents b8b276f + 9868753 commit f5948d0

File tree

6 files changed

+48
-19
lines changed

6 files changed

+48
-19
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import androidx.compose.ui.text.buildAnnotatedString
88
import androidx.compose.ui.text.withStyle
99
import co.adityarajput.fileflow.R
1010
import co.adityarajput.fileflow.data.Verb
11+
import co.adityarajput.fileflow.utils.File
1112
import co.adityarajput.fileflow.utils.FileSuperlative
1213
import co.adityarajput.fileflow.utils.getGetDirectoryFromUri
1314
import co.adityarajput.fileflow.utils.toShortHumanReadableTime
1415
import kotlinx.serialization.Serializable
16+
import kotlin.uuid.ExperimentalUuidApi
17+
import kotlin.uuid.Uuid
1518

1619
@Suppress("ClassName")
1720
@Serializable
@@ -65,6 +68,19 @@ sealed class Action {
6568
withStyle(dullStyle) { append("\nas ") }
6669
append(destFileNameTemplate)
6770
}
71+
72+
@OptIn(ExperimentalUuidApi::class)
73+
fun getDestFileName(srcFile: File) =
74+
srcFile.name!!.replace(
75+
Regex(srcFileNamePattern),
76+
destFileNameTemplate.replace(
77+
$$"${uuid}",
78+
Uuid.random().toString(),
79+
).replace(
80+
$$"${folder}",
81+
srcFile.parent?.name ?: "",
82+
),
83+
)
6884
}
6985

7086
@Serializable

app/src/main/java/co/adityarajput/fileflow/services/FlowExecutor.kt

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,7 @@ class FlowExecutor(private val context: Context) {
5252
continue
5353
}
5454

55-
val destFileName = srcFile.name!!.replace(
56-
regex,
57-
rule.action.destFileNameTemplate.replace(
58-
$$"${folder}",
59-
srcFile.parent?.name ?: "",
60-
),
61-
)
55+
val destFileName = rule.action.getDestFileName(srcFile)
6256
var destFile = destSubDir.listChildren(false)
6357
.firstOrNull { it.isFile && it.name == destFileName }
6458

app/src/main/java/co/adityarajput/fileflow/viewmodels/UpsertRuleViewModel.kt

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,9 @@ class UpsertRuleViewModel(
107107
?.also { if (it.isEmpty()) warning = FormWarning.NO_MATCHES_IN_SRC }
108108

109109
if (matchingSrcFiles != null && values.destFileNameTemplate.isNotBlank()) {
110-
predictedDestFileNames = matchingSrcFiles.map {
111-
it.name!!.replace(
112-
regex,
113-
values.destFileNameTemplate.replace(
114-
$$"${folder}",
115-
it.parent?.name ?: "",
116-
),
117-
)
118-
}.distinct()
110+
predictedDestFileNames = matchingSrcFiles
111+
.map { (values.toRule().action as Action.MOVE).getDestFileName(it) }
112+
.distinct()
119113
}
120114
} catch (_: Exception) {
121115
}

app/src/main/java/co/adityarajput/fileflow/views/screens/OnboardingScreen.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package co.adityarajput.fileflow.views.screens
22

3-
import android.annotation.SuppressLint
43
import android.os.Handler
54
import android.os.Looper
65
import androidx.compose.foundation.layout.*
@@ -22,7 +21,6 @@ private val permissions = listOf(
2221
Permission.MANAGE_EXTERNAL_STORAGE,
2322
)
2423

25-
@SuppressLint("BatteryLife")
2624
@Composable
2725
fun OnboardingScreen(goToRulesScreen: () -> Unit = {}) {
2826
val context = LocalContext.current

app/src/main/java/co/adityarajput/fileflow/views/screens/UpsertRuleScreen.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,14 +324,39 @@ private fun ColumnScope.ActionPage(viewModel: UpsertRuleViewModel) {
324324
label = { Text(stringResource(R.string.file_name_template)) },
325325
placeholder = { Text(stringResource(R.string.template_placeholder)) },
326326
supportingText = {
327-
if (viewModel.state.values.predictedDestFileNames?.isNotEmpty() ?: false)
327+
if (viewModel.state.values.predictedDestFileNames.isNullOrEmpty()) {
328+
Text(
329+
buildAnnotatedString {
330+
append(stringResource(R.string.blank_template_supporting))
331+
if (viewModel.state.values.destFileNameTemplate != "$0") {
332+
withLink(
333+
LinkAnnotation.Clickable(
334+
"keep_name",
335+
TextLinkStyles(
336+
SpanStyle(
337+
MaterialTheme.colorScheme.primary,
338+
textDecoration = TextDecoration.Underline,
339+
),
340+
),
341+
) {
342+
viewModel.updateForm(
343+
context,
344+
viewModel.state.values.copy(destFileNameTemplate = "$0"),
345+
)
346+
},
347+
) { append(stringResource(R.string.keep_name)) }
348+
}
349+
},
350+
)
351+
} else {
328352
Text(
329353
stringResource(
330354
R.string.template_will_yield,
331355
viewModel.state.values.predictedDestFileNames!!
332356
.joinToString(stringResource(R.string.or), limit = 3),
333357
),
334358
)
359+
}
335360
},
336361
colors = OutlinedTextFieldDefaults.colors(
337362
focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,

app/src/main/res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,8 @@
109109
<string name="preserve_structure">Recreate source folder-structure in destination</string>
110110
<string name="file_name_template">File name template</string>
111111
<string name="template_placeholder">Enter a regex template</string>
112+
<string name="blank_template_supporting">"Template will be used to name the file in the destination. "</string>
113+
<string name="keep_name">Keep original name?</string>
112114
<string name="template_will_yield">Template will yield %1$s</string>
113115
<string name="overwrite_existing">Overwrite existing files in the destination, in case of conflict</string>
114116
<string name="retention_days">Mark stale if unmodified for</string>

0 commit comments

Comments
 (0)