Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions app/src/main/java/co/adityarajput/fileflow/data/models/Action.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.withStyle
import co.adityarajput.fileflow.R
import co.adityarajput.fileflow.data.Verb
import co.adityarajput.fileflow.utils.File
import co.adityarajput.fileflow.utils.FileSuperlative
import co.adityarajput.fileflow.utils.getGetDirectoryFromUri
import co.adityarajput.fileflow.utils.toShortHumanReadableTime
import kotlinx.serialization.Serializable
import kotlin.uuid.ExperimentalUuidApi
import kotlin.uuid.Uuid

@Suppress("ClassName")
@Serializable
Expand Down Expand Up @@ -65,6 +68,19 @@ sealed class Action {
withStyle(dullStyle) { append("\nas ") }
append(destFileNameTemplate)
}

@OptIn(ExperimentalUuidApi::class)
fun getDestFileName(srcFile: File) =
srcFile.name!!.replace(
Regex(srcFileNamePattern),
destFileNameTemplate.replace(
$$"${uuid}",
Uuid.random().toString(),
).replace(
$$"${folder}",
srcFile.parent?.name ?: "",
),
)
}

@Serializable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@ class FlowExecutor(private val context: Context) {
continue
}

val destFileName = srcFile.name!!.replace(
regex,
rule.action.destFileNameTemplate.replace(
$$"${folder}",
srcFile.parent?.name ?: "",
),
)
val destFileName = rule.action.getDestFileName(srcFile)
var destFile = destSubDir.listChildren(false)
.firstOrNull { it.isFile && it.name == destFileName }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,9 @@ class UpsertRuleViewModel(
?.also { if (it.isEmpty()) warning = FormWarning.NO_MATCHES_IN_SRC }

if (matchingSrcFiles != null && values.destFileNameTemplate.isNotBlank()) {
predictedDestFileNames = matchingSrcFiles.map {
it.name!!.replace(
regex,
values.destFileNameTemplate.replace(
$$"${folder}",
it.parent?.name ?: "",
),
)
}.distinct()
predictedDestFileNames = matchingSrcFiles
.map { (values.toRule().action as Action.MOVE).getDestFileName(it) }
.distinct()
}
} catch (_: Exception) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package co.adityarajput.fileflow.views.screens

import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import androidx.compose.foundation.layout.*
Expand All @@ -22,7 +21,6 @@ private val permissions = listOf(
Permission.MANAGE_EXTERNAL_STORAGE,
)

@SuppressLint("BatteryLife")
@Composable
fun OnboardingScreen(goToRulesScreen: () -> Unit = {}) {
val context = LocalContext.current
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,39 @@ private fun ColumnScope.ActionPage(viewModel: UpsertRuleViewModel) {
label = { Text(stringResource(R.string.file_name_template)) },
placeholder = { Text(stringResource(R.string.template_placeholder)) },
supportingText = {
if (viewModel.state.values.predictedDestFileNames?.isNotEmpty() ?: false)
if (viewModel.state.values.predictedDestFileNames.isNullOrEmpty()) {
Text(
buildAnnotatedString {
append(stringResource(R.string.blank_template_supporting))
if (viewModel.state.values.destFileNameTemplate != "$0") {
withLink(
LinkAnnotation.Clickable(
"keep_name",
TextLinkStyles(
SpanStyle(
MaterialTheme.colorScheme.primary,
textDecoration = TextDecoration.Underline,
),
),
) {
viewModel.updateForm(
context,
viewModel.state.values.copy(destFileNameTemplate = "$0"),
)
},
) { append(stringResource(R.string.keep_name)) }
}
},
)
} else {
Text(
stringResource(
R.string.template_will_yield,
viewModel.state.values.predictedDestFileNames!!
.joinToString(stringResource(R.string.or), limit = 3),
),
)
}
},
colors = OutlinedTextFieldDefaults.colors(
focusedContainerColor = MaterialTheme.colorScheme.secondaryContainer,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
<string name="preserve_structure">Recreate source folder-structure in destination</string>
<string name="file_name_template">File name template</string>
<string name="template_placeholder">Enter a regex template</string>
<string name="blank_template_supporting">"Template will be used to name the file in the destination. "</string>
<string name="keep_name">Keep original name?</string>
<string name="template_will_yield">Template will yield %1$s</string>
<string name="overwrite_existing">Overwrite existing files in the destination, in case of conflict</string>
<string name="retention_days">Mark stale if unmodified for</string>
Expand Down
Loading