v1.0.23: functional Files tab, background service persistence, startup indexer, curved icon
- FilesScreen: per-file context menu (Open, Share, Rename, Delete), rename dialog, delete confirmation, FileProvider-based open/share intents, Snackbar error feedback - FilesViewModel: FileAction sealed class + SharedFlow; openFile, shareFile, deleteFile, renameFile with DB cleanup; resolveFile handles SAF primary: URIs - FileWatchService: stopWithTask=false keeps watcher alive after app swipe-away; catchupScan on startup detects changes missed while service was not running; SyncFileStateDao injected; FileObserver used for real-path SAF URIs - BootReceiver: handles MY_PACKAGE_REPLACED to restart service after app update - file_paths.xml: added external-path so FileProvider can serve /storage/emulated/0 files - ic_launcher_foreground: three curved stroke-based arrows (quadratic bezier, round caps) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -66,13 +66,16 @@
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED" />
|
||||
<action android:name="android.intent.action.MY_PACKAGE_REPLACED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
|
||||
<!-- File watcher for ON_CHANGE sync pairs -->
|
||||
<!-- stopWithTask=false keeps the service alive when the user swipes the app away -->
|
||||
<service
|
||||
android:name=".worker.FileWatchService"
|
||||
android:foregroundServiceType="dataSync|shortService"
|
||||
android:stopWithTask="false"
|
||||
android:exported="false" />
|
||||
|
||||
<!-- Required on API 29+ so WorkManager can start a typed foreground service -->
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package com.syncflow.ui.files
|
||||
|
||||
import android.content.Intent
|
||||
import android.webkit.MimeTypeMap
|
||||
import androidx.compose.foundation.layout.*
|
||||
import androidx.compose.foundation.lazy.LazyColumn
|
||||
import androidx.compose.foundation.lazy.items
|
||||
@@ -10,10 +12,14 @@ import androidx.compose.material3.*
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.vector.ImageVector
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.text.style.TextOverflow
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.content.FileProvider
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import com.syncflow.data.db.entities.SyncFileStateEntity
|
||||
import kotlinx.coroutines.launch
|
||||
import java.time.ZoneId
|
||||
import java.time.format.DateTimeFormatter
|
||||
import java.time.format.FormatStyle
|
||||
@@ -27,156 +33,222 @@ fun FilesScreen(
|
||||
val pairs by vm.pairs.collectAsState()
|
||||
val selectedPair by vm.selectedPair.collectAsState()
|
||||
val files by vm.files.collectAsState()
|
||||
val context = LocalContext.current
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
val scope = rememberCoroutineScope()
|
||||
|
||||
Column(modifier = modifier.fillMaxSize()) {
|
||||
// Pair selector chips
|
||||
if (pairs.size > 1) {
|
||||
ScrollableTabRow(
|
||||
selectedTabIndex = pairs.indexOfFirst { it.id == selectedPair?.id }.coerceAtLeast(0),
|
||||
edgePadding = 16.dp,
|
||||
containerColor = MaterialTheme.colorScheme.surface,
|
||||
divider = {},
|
||||
) {
|
||||
pairs.forEach { pair ->
|
||||
Tab(
|
||||
selected = pair.id == selectedPair?.id,
|
||||
onClick = { vm.selectPair(pair.id) },
|
||||
text = {
|
||||
Text(
|
||||
pair.name,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
HorizontalDivider()
|
||||
}
|
||||
|
||||
if (pairs.isEmpty()) {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.FolderOpen,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(72.dp),
|
||||
tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.35f),
|
||||
)
|
||||
Text("No sync pairs yet", style = MaterialTheme.typography.titleMedium)
|
||||
Text(
|
||||
"Create a sync pair to browse its files",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
} else if (files.isEmpty()) {
|
||||
Box(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.FolderOpen,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(72.dp),
|
||||
tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.35f),
|
||||
)
|
||||
Text("No synced files yet", style = MaterialTheme.typography.titleMedium)
|
||||
Text(
|
||||
"Run a sync to populate this view",
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Summary row
|
||||
Surface(
|
||||
color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f),
|
||||
) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(
|
||||
"${files.size} file${if (files.size != 1) "s" else ""}",
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
val totalBytes = files.sumOf { it.localSizeBytes }
|
||||
Text(
|
||||
totalBytes.toDisplaySize(),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
LazyColumn(
|
||||
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(0.dp),
|
||||
) {
|
||||
// Group by top-level directory
|
||||
val grouped = files.groupBy { f ->
|
||||
val slashIdx = f.relativePath.indexOf('/')
|
||||
if (slashIdx < 0) "" else f.relativePath.substring(0, slashIdx)
|
||||
}
|
||||
grouped.forEach { (dir, dirFiles) ->
|
||||
if (dir.isNotEmpty()) {
|
||||
item(key = "dir_$dir") {
|
||||
Row(
|
||||
modifier = Modifier.padding(top = 12.dp, bottom = 4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.Folder,
|
||||
contentDescription = null,
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text(
|
||||
dir,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
LaunchedEffect(Unit) {
|
||||
vm.fileAction.collect { action ->
|
||||
when (action) {
|
||||
is FileAction.Open -> {
|
||||
try {
|
||||
val uri = FileProvider.getUriForFile(
|
||||
context, "${context.packageName}.fileprovider", action.file
|
||||
)
|
||||
val intent = Intent(Intent.ACTION_VIEW).apply {
|
||||
setDataAndType(uri, action.file.name.mimeType())
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
context.startActivity(
|
||||
Intent.createChooser(intent, "Open with").apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
snackbarHostState.showSnackbar("Cannot open file: ${e.message}")
|
||||
}
|
||||
items(dirFiles, key = { "${it.syncPairId}_${it.relativePath}" }) { file ->
|
||||
FileRow(file, isInSubDir = dir.isNotEmpty())
|
||||
HorizontalDivider(
|
||||
color = MaterialTheme.colorScheme.outline.copy(alpha = 0.25f),
|
||||
modifier = Modifier.padding(start = if (dir.isNotEmpty()) 38.dp else 16.dp),
|
||||
}
|
||||
is FileAction.Share -> {
|
||||
try {
|
||||
val uri = FileProvider.getUriForFile(
|
||||
context, "${context.packageName}.fileprovider", action.file
|
||||
)
|
||||
val intent = Intent(Intent.ACTION_SEND).apply {
|
||||
type = action.file.name.mimeType()
|
||||
putExtra(Intent.EXTRA_STREAM, uri)
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
context.startActivity(
|
||||
Intent.createChooser(intent, "Share via").apply {
|
||||
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
||||
}
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
snackbarHostState.showSnackbar("Cannot share file: ${e.message}")
|
||||
}
|
||||
}
|
||||
is FileAction.Error -> scope.launch {
|
||||
snackbarHostState.showSnackbar(action.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Box(modifier = modifier.fillMaxSize()) {
|
||||
Column(modifier = Modifier.fillMaxSize()) {
|
||||
if (pairs.size > 1) {
|
||||
ScrollableTabRow(
|
||||
selectedTabIndex = pairs.indexOfFirst { it.id == selectedPair?.id }.coerceAtLeast(0),
|
||||
edgePadding = 16.dp,
|
||||
containerColor = MaterialTheme.colorScheme.surface,
|
||||
divider = {},
|
||||
) {
|
||||
pairs.forEach { pair ->
|
||||
Tab(
|
||||
selected = pair.id == selectedPair?.id,
|
||||
onClick = { vm.selectPair(pair.id) },
|
||||
text = { Text(pair.name, maxLines = 1, overflow = TextOverflow.Ellipsis) },
|
||||
)
|
||||
}
|
||||
}
|
||||
item { Spacer(Modifier.height(80.dp)) }
|
||||
HorizontalDivider()
|
||||
}
|
||||
|
||||
when {
|
||||
pairs.isEmpty() -> FilesEmptyState(
|
||||
icon = Icons.Default.FolderOpen,
|
||||
title = "No sync pairs yet",
|
||||
subtitle = "Create a sync pair to browse its files",
|
||||
)
|
||||
files.isEmpty() -> FilesEmptyState(
|
||||
icon = Icons.Default.FolderOpen,
|
||||
title = "No synced files yet",
|
||||
subtitle = "Run a sync to populate this view",
|
||||
)
|
||||
else -> {
|
||||
Surface(color = MaterialTheme.colorScheme.surfaceVariant.copy(alpha = 0.5f)) {
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp, vertical = 8.dp),
|
||||
horizontalArrangement = Arrangement.SpaceBetween,
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Text(
|
||||
"${files.size} file${if (files.size != 1) "s" else ""}",
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
Text(
|
||||
files.sumOf { it.localSizeBytes }.toDisplaySize(),
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
LazyColumn(
|
||||
contentPadding = PaddingValues(horizontal = 16.dp, vertical = 8.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(0.dp),
|
||||
) {
|
||||
val grouped = files.groupBy { f ->
|
||||
val idx = f.relativePath.indexOf('/')
|
||||
if (idx < 0) "" else f.relativePath.substring(0, idx)
|
||||
}
|
||||
grouped.forEach { (dir, dirFiles) ->
|
||||
if (dir.isNotEmpty()) {
|
||||
item(key = "dir_$dir") {
|
||||
Row(
|
||||
modifier = Modifier.padding(top = 12.dp, bottom = 4.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
Icon(
|
||||
Icons.Default.Folder, contentDescription = null,
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
Spacer(Modifier.width(6.dp))
|
||||
Text(
|
||||
dir,
|
||||
style = MaterialTheme.typography.labelMedium,
|
||||
color = MaterialTheme.colorScheme.primary,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
items(dirFiles, key = { "${it.syncPairId}_${it.relativePath}" }) { file ->
|
||||
FileRow(file = file, isInSubDir = dir.isNotEmpty(), vm = vm)
|
||||
HorizontalDivider(
|
||||
color = MaterialTheme.colorScheme.outline.copy(alpha = 0.25f),
|
||||
modifier = Modifier.padding(
|
||||
start = if (dir.isNotEmpty()) 38.dp else 16.dp
|
||||
),
|
||||
)
|
||||
}
|
||||
}
|
||||
item { Spacer(Modifier.height(80.dp)) }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
SnackbarHost(
|
||||
hostState = snackbarHostState,
|
||||
modifier = Modifier.align(Alignment.BottomCenter),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FilesEmptyState(icon: ImageVector, title: String, subtitle: String) {
|
||||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.spacedBy(8.dp),
|
||||
) {
|
||||
Icon(
|
||||
icon, contentDescription = null,
|
||||
modifier = Modifier.size(72.dp),
|
||||
tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.35f),
|
||||
)
|
||||
Text(title, style = MaterialTheme.typography.titleMedium)
|
||||
Text(
|
||||
subtitle,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun FileRow(file: SyncFileStateEntity, isInSubDir: Boolean) {
|
||||
private fun FileRow(file: SyncFileStateEntity, isInSubDir: Boolean, vm: FilesViewModel) {
|
||||
val name = file.relativePath.substringAfterLast('/')
|
||||
val timeFmt = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)
|
||||
val syncedAt = file.lastSyncedAt.atZone(ZoneId.systemDefault()).let { timeFmt.format(it) }
|
||||
|
||||
var menuExpanded by remember { mutableStateOf(false) }
|
||||
var showRenameDialog by remember { mutableStateOf(false) }
|
||||
var showDeleteDialog by remember { mutableStateOf(false) }
|
||||
|
||||
if (showRenameDialog) {
|
||||
RenameDialog(
|
||||
currentName = name,
|
||||
onConfirm = { newName ->
|
||||
vm.renameFile(file, newName)
|
||||
showRenameDialog = false
|
||||
},
|
||||
onDismiss = { showRenameDialog = false },
|
||||
)
|
||||
}
|
||||
|
||||
if (showDeleteDialog) {
|
||||
AlertDialog(
|
||||
onDismissRequest = { showDeleteDialog = false },
|
||||
icon = { Icon(Icons.Default.Delete, contentDescription = null) },
|
||||
title = { Text("Delete file?") },
|
||||
text = { Text("\"$name\" will be removed from this device.") },
|
||||
confirmButton = {
|
||||
TextButton(onClick = { vm.deleteFile(file); showDeleteDialog = false }) {
|
||||
Text("Delete", color = MaterialTheme.colorScheme.error)
|
||||
}
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = { showDeleteDialog = false }) { Text("Cancel") }
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
@@ -194,8 +266,7 @@ private fun FileRow(file: SyncFileStateEntity, isInSubDir: Boolean) {
|
||||
) {
|
||||
Box(contentAlignment = Alignment.Center) {
|
||||
Icon(
|
||||
fileIcon(name),
|
||||
contentDescription = null,
|
||||
fileIcon(name), contentDescription = null,
|
||||
modifier = Modifier.size(16.dp),
|
||||
tint = MaterialTheme.colorScheme.onSecondaryContainer,
|
||||
)
|
||||
@@ -215,16 +286,87 @@ private fun FileRow(file: SyncFileStateEntity, isInSubDir: Boolean) {
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
// Sync status indicator
|
||||
Icon(
|
||||
Icons.Default.CheckCircle,
|
||||
contentDescription = "Synced",
|
||||
modifier = Modifier.size(14.dp),
|
||||
tint = MaterialTheme.colorScheme.primary.copy(alpha = 0.6f),
|
||||
)
|
||||
Box {
|
||||
IconButton(onClick = { menuExpanded = true }, modifier = Modifier.size(36.dp)) {
|
||||
Icon(
|
||||
Icons.Default.MoreVert, contentDescription = "File options",
|
||||
modifier = Modifier.size(18.dp),
|
||||
tint = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
DropdownMenu(expanded = menuExpanded, onDismissRequest = { menuExpanded = false }) {
|
||||
DropdownMenuItem(
|
||||
text = { Text("Open") },
|
||||
leadingIcon = { Icon(Icons.Default.OpenInNew, contentDescription = null) },
|
||||
onClick = { menuExpanded = false; vm.openFile(file) },
|
||||
)
|
||||
DropdownMenuItem(
|
||||
text = { Text("Share") },
|
||||
leadingIcon = { Icon(Icons.Default.Share, contentDescription = null) },
|
||||
onClick = { menuExpanded = false; vm.shareFile(file) },
|
||||
)
|
||||
HorizontalDivider()
|
||||
DropdownMenuItem(
|
||||
text = { Text("Rename") },
|
||||
leadingIcon = { Icon(Icons.Default.Edit, contentDescription = null) },
|
||||
onClick = { menuExpanded = false; showRenameDialog = true },
|
||||
)
|
||||
DropdownMenuItem(
|
||||
text = { Text("Delete", color = MaterialTheme.colorScheme.error) },
|
||||
leadingIcon = {
|
||||
Icon(
|
||||
Icons.Default.Delete, contentDescription = null,
|
||||
tint = MaterialTheme.colorScheme.error,
|
||||
)
|
||||
},
|
||||
onClick = { menuExpanded = false; showDeleteDialog = true },
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun RenameDialog(
|
||||
currentName: String,
|
||||
onConfirm: (String) -> Unit,
|
||||
onDismiss: () -> Unit,
|
||||
) {
|
||||
var newName by remember { mutableStateOf(currentName) }
|
||||
AlertDialog(
|
||||
onDismissRequest = onDismiss,
|
||||
icon = { Icon(Icons.Default.Edit, contentDescription = null) },
|
||||
title = { Text("Rename file") },
|
||||
text = {
|
||||
OutlinedTextField(
|
||||
value = newName,
|
||||
onValueChange = { newName = it },
|
||||
label = { Text("New name") },
|
||||
singleLine = true,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
},
|
||||
confirmButton = {
|
||||
TextButton(
|
||||
onClick = {
|
||||
val trimmed = newName.trim()
|
||||
if (trimmed.isNotBlank() && trimmed != currentName) onConfirm(trimmed)
|
||||
else onDismiss()
|
||||
},
|
||||
enabled = newName.isNotBlank(),
|
||||
) { Text("Rename") }
|
||||
},
|
||||
dismissButton = {
|
||||
TextButton(onClick = onDismiss) { Text("Cancel") }
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
private fun String.mimeType(): String {
|
||||
val ext = substringAfterLast('.', "").lowercase()
|
||||
return MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext) ?: "*/*"
|
||||
}
|
||||
|
||||
private fun fileIcon(name: String) = when {
|
||||
name.endsWith(".pdf", ignoreCase = true) -> Icons.Default.PictureAsPdf
|
||||
name.endsWith(".jpg", ignoreCase = true) ||
|
||||
@@ -247,7 +389,7 @@ private fun fileIcon(name: String) = when {
|
||||
}
|
||||
|
||||
private fun Long.toDisplaySize(): String = when {
|
||||
this < 1_024 -> "${this} B"
|
||||
this < 1_024 -> "$this B"
|
||||
this < 1_048_576 -> "${"%.1f".format(this / 1_024.0)} KB"
|
||||
this < 1_073_741_824 -> "${"%.1f".format(this / 1_048_576.0)} MB"
|
||||
else -> "${"%.1f".format(this / 1_073_741_824.0)} GB"
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.syncflow.ui.files
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.syncflow.data.db.SyncFileStateDao
|
||||
@@ -7,15 +8,26 @@ import com.syncflow.data.db.SyncPairDao
|
||||
import com.syncflow.data.db.entities.SyncFileStateEntity
|
||||
import com.syncflow.data.db.entities.SyncPairEntity
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||
import kotlinx.coroutines.flow.*
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
|
||||
sealed class FileAction {
|
||||
data class Open(val file: File) : FileAction()
|
||||
data class Share(val file: File) : FileAction()
|
||||
data class Error(val message: String) : FileAction()
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalCoroutinesApi::class)
|
||||
@HiltViewModel
|
||||
class FilesViewModel @Inject constructor(
|
||||
syncPairDao: SyncPairDao,
|
||||
private val syncPairDao: SyncPairDao,
|
||||
private val fileStateDao: SyncFileStateDao,
|
||||
@ApplicationContext private val context: Context,
|
||||
) : ViewModel() {
|
||||
|
||||
val pairs: StateFlow<List<SyncPairEntity>> = syncPairDao.observeAll()
|
||||
@@ -35,5 +47,71 @@ class FilesViewModel @Inject constructor(
|
||||
}
|
||||
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())
|
||||
|
||||
private val _fileAction = MutableSharedFlow<FileAction>()
|
||||
val fileAction: SharedFlow<FileAction> = _fileAction
|
||||
|
||||
fun selectPair(id: Long) { _selectedPairId.value = id }
|
||||
|
||||
fun openFile(file: SyncFileStateEntity) {
|
||||
val resolved = resolveFile(file) ?: return
|
||||
viewModelScope.launch { _fileAction.emit(FileAction.Open(resolved)) }
|
||||
}
|
||||
|
||||
fun shareFile(file: SyncFileStateEntity) {
|
||||
val resolved = resolveFile(file) ?: return
|
||||
viewModelScope.launch { _fileAction.emit(FileAction.Share(resolved)) }
|
||||
}
|
||||
|
||||
fun deleteFile(file: SyncFileStateEntity) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val resolved = resolveFile(file)
|
||||
resolved?.delete()
|
||||
fileStateDao.delete(file.syncPairId, file.relativePath)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Delete failed: ${file.relativePath}")
|
||||
_fileAction.emit(FileAction.Error("Delete failed: ${e.message}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun renameFile(file: SyncFileStateEntity, newName: String) {
|
||||
viewModelScope.launch {
|
||||
try {
|
||||
val resolved = resolveFile(file) ?: return@launch
|
||||
val parent = resolved.parentFile ?: return@launch
|
||||
val dest = File(parent, newName)
|
||||
if (!resolved.renameTo(dest)) {
|
||||
_fileAction.emit(FileAction.Error("Rename failed"))
|
||||
return@launch
|
||||
}
|
||||
// Update DB: delete old state; the next sync will re-detect as a new upload
|
||||
fileStateDao.delete(file.syncPairId, file.relativePath)
|
||||
} catch (e: Exception) {
|
||||
Timber.e(e, "Rename failed: ${file.relativePath}")
|
||||
_fileAction.emit(FileAction.Error("Rename failed: ${e.message}"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun resolveFile(file: SyncFileStateEntity): File? {
|
||||
val pair = selectedPair.value ?: return null
|
||||
val root = safTreeUriToRealPath(pair.localPath) ?: pair.localPath
|
||||
val f = File(root, file.relativePath)
|
||||
if (!f.exists()) {
|
||||
viewModelScope.launch { _fileAction.emit(FileAction.Error("File not found on device")) }
|
||||
return null
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
private fun safTreeUriToRealPath(uriString: String): String? {
|
||||
if (!uriString.startsWith("content://")) return uriString
|
||||
return try {
|
||||
val treeUri = android.net.Uri.parse(uriString)
|
||||
val docId = android.provider.DocumentsContract.getTreeDocumentId(treeUri)
|
||||
if (docId.startsWith("primary:")) "/storage/emulated/0/${docId.removePrefix("primary:")}"
|
||||
else null
|
||||
} catch (e: Exception) { null }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,7 +18,8 @@ class BootReceiver : BroadcastReceiver() {
|
||||
@Inject lateinit var syncPairDao: SyncPairDao
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
if (intent.action != Intent.ACTION_BOOT_COMPLETED) return
|
||||
val validActions = setOf(Intent.ACTION_BOOT_COMPLETED, Intent.ACTION_MY_PACKAGE_REPLACED)
|
||||
if (intent.action !in validActions) return
|
||||
val wm = WorkManager.getInstance(context)
|
||||
val pending = goAsync()
|
||||
CoroutineScope(Dispatchers.IO).launch {
|
||||
|
||||
@@ -16,6 +16,7 @@ import androidx.work.ExistingWorkPolicy
|
||||
import androidx.work.WorkManager
|
||||
import com.syncflow.MainActivity
|
||||
import com.syncflow.R
|
||||
import com.syncflow.data.db.SyncFileStateDao
|
||||
import com.syncflow.data.db.SyncPairDao
|
||||
import com.syncflow.domain.model.ScheduleType
|
||||
import dagger.hilt.android.AndroidEntryPoint
|
||||
@@ -28,6 +29,7 @@ import javax.inject.Inject
|
||||
class FileWatchService : Service() {
|
||||
|
||||
@Inject lateinit var syncPairDao: SyncPairDao
|
||||
@Inject lateinit var fileStateDao: SyncFileStateDao
|
||||
|
||||
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||
private val mainHandler = Handler(Looper.getMainLooper())
|
||||
@@ -151,6 +153,36 @@ class FileWatchService : Service() {
|
||||
observer.startWatching()
|
||||
fileObservers[pairId] = observer
|
||||
Timber.d("FileWatchService: watching filesystem path for pair $pairId: $path")
|
||||
// Check if anything changed while the service was not running
|
||||
scope.launch { catchupScan(pairId, dir, wifiOnly, chargingOnly) }
|
||||
}
|
||||
|
||||
private suspend fun catchupScan(pairId: Long, dir: File, wifiOnly: Boolean, chargingOnly: Boolean) {
|
||||
val known = fileStateDao.getForPair(pairId).associateBy { it.relativePath }
|
||||
if (known.isEmpty()) return // Never synced — first sync will be triggered manually
|
||||
|
||||
val current = mutableMapOf<String, Long>()
|
||||
dir.walk().filter { it.isFile }.forEach { f ->
|
||||
current[f.relativeTo(dir).path.replace('\\', '/')] = f.lastModified()
|
||||
}
|
||||
|
||||
val hasNew = current.any { (rel, _) -> rel !in known }
|
||||
val hasModified = current.any { (rel, mtime) ->
|
||||
val s = known[rel]; s != null && s.localModifiedAt != null &&
|
||||
s.localModifiedAt.toEpochMilli() != mtime
|
||||
}
|
||||
val hasDeleted = known.keys.any { rel -> rel !in current }
|
||||
|
||||
if (hasNew || hasModified || hasDeleted) {
|
||||
Timber.d("FileWatchService: catchup detected changes for pair $pairId, scheduling sync")
|
||||
val pair = syncPairDao.getById(pairId) ?: return
|
||||
WorkManager.getInstance(applicationContext)
|
||||
.enqueueUniqueWork(
|
||||
"catchup_$pairId",
|
||||
ExistingWorkPolicy.KEEP,
|
||||
SyncWorker.buildOneTimeRequest(pairId, wifiOnly, chargingOnly),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private fun onChangeDetected(pairId: Long, wifiOnly: Boolean, chargingOnly: Boolean) {
|
||||
|
||||
@@ -6,83 +6,90 @@
|
||||
android:viewportWidth="108"
|
||||
android:viewportHeight="108">
|
||||
|
||||
<!-- Soft ambient glow underneath everything -->
|
||||
<!-- Subtle inner glow -->
|
||||
<path
|
||||
android:pathData="M54,54m-36,0a36,36 0 1,0 72,0a36,36 0 1,0 -72,0"
|
||||
android:fillColor="#18FF6B6B"/>
|
||||
android:pathData="M54,54m-30,0a30,30 0 1,0 60,0a30,30 0 1,0 -60,0"
|
||||
android:fillColor="#0AFFFFFF"/>
|
||||
|
||||
<!-- ═══ Arrow 1: pointing RIGHT (top) — electric coral ═══ -->
|
||||
<!-- Shaft with rounded left cap -->
|
||||
<path android:pathData="M27,31 Q22,31 22,36 Q22,41 27,41 L64,41 L64,31 Z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<!-- ═══ Arrow 1: curving RIGHT (top) — coral gradient ═══ -->
|
||||
<!-- Curved shaft via quadratic bezier: left→ dips up → right -->
|
||||
<path
|
||||
android:pathData="M28,40 Q54,22 80,36"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeWidth="7"
|
||||
android:strokeLineCap="round"
|
||||
android:strokeLineJoin="round">
|
||||
<aapt:attr name="android:strokeColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="22" android:startY="36"
|
||||
android:endX="64" android:endY="36"
|
||||
android:startX="28" android:startY="36"
|
||||
android:endX="80" android:endY="36"
|
||||
android:startColor="#FF6B6B"
|
||||
android:endColor="#FF9F6B"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<!-- Arrowhead -->
|
||||
<path android:pathData="M63,27 L63,45 L86,36 Z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="63" android:startY="36"
|
||||
android:endX="86" android:endY="36"
|
||||
android:startColor="#FF9F6B"
|
||||
android:endColor="#FFB347"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
|
||||
<!-- ═══ Arrow 2: pointing LEFT (middle) — cool white/silver ═══ -->
|
||||
<!-- Shaft with rounded right cap -->
|
||||
<path android:pathData="M44,50 L81,50 Q86,50 86,55 Q86,60 81,60 L44,60 Z">
|
||||
<!-- Arrowhead at (80,36) pointing right/slightly-down -->
|
||||
<path android:pathData="M76,28 L72,44 L88,38 Z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="44" android:startY="55"
|
||||
android:endX="86" android:endY="55"
|
||||
android:startColor="#B0B8D0"
|
||||
android:endColor="#E8EDF5"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<!-- Arrowhead -->
|
||||
<path android:pathData="M45,46 L45,64 L22,55 Z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="45" android:startY="55"
|
||||
android:endX="22" android:endY="55"
|
||||
android:startColor="#B0B8D0"
|
||||
android:endColor="#8892A8"/>
|
||||
android:startX="72" android:startY="36"
|
||||
android:endX="88" android:endY="36"
|
||||
android:startColor="#FFB347"
|
||||
android:endColor="#FF8C42"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
|
||||
<!-- ═══ Arrow 3: pointing RIGHT (bottom) — electric teal ═══ -->
|
||||
<!-- Shaft with rounded left cap -->
|
||||
<path android:pathData="M27,69 Q22,69 22,74 Q22,79 27,79 L64,79 L64,69 Z">
|
||||
<!-- ═══ Arrow 2: curving LEFT (middle) — silver/white ═══ -->
|
||||
<!-- Curved shaft: right→ dips down → left -->
|
||||
<path
|
||||
android:pathData="M80,56 Q54,74 28,60"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeWidth="7"
|
||||
android:strokeLineCap="round"
|
||||
android:strokeLineJoin="round">
|
||||
<aapt:attr name="android:strokeColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="80" android:startY="60"
|
||||
android:endX="28" android:endY="60"
|
||||
android:startColor="#D0D8EE"
|
||||
android:endColor="#8892AA"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<!-- Arrowhead at (28,60) pointing left/slightly-down -->
|
||||
<path android:pathData="M32,52 L36,68 L20,62 Z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="22" android:startY="74"
|
||||
android:endX="64" android:endY="74"
|
||||
android:startX="36" android:startY="60"
|
||||
android:endX="20" android:endY="60"
|
||||
android:startColor="#D0D8EE"
|
||||
android:endColor="#8892AA"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
|
||||
<!-- ═══ Arrow 3: curving RIGHT (bottom) — teal gradient ═══ -->
|
||||
<!-- Curved shaft: left→ dips up → right -->
|
||||
<path
|
||||
android:pathData="M28,74 Q54,58 80,72"
|
||||
android:fillColor="#00000000"
|
||||
android:strokeWidth="7"
|
||||
android:strokeLineCap="round"
|
||||
android:strokeLineJoin="round">
|
||||
<aapt:attr name="android:strokeColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="28" android:startY="72"
|
||||
android:endX="80" android:endY="72"
|
||||
android:startColor="#4DD0E1"
|
||||
android:endColor="#26C6DA"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
<!-- Arrowhead -->
|
||||
<path android:pathData="M63,65 L63,83 L86,74 Z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="63" android:startY="74"
|
||||
android:endX="86" android:endY="74"
|
||||
android:startColor="#26C6DA"
|
||||
android:endColor="#00BCD4"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
|
||||
<!-- Small glow dots at arrowhead tips for sparkle -->
|
||||
<path android:pathData="M86,36m-3,0a3,3 0 1,0 6,0a3,3 0 1,0 -6,0"
|
||||
android:fillColor="#FFFFB347"/>
|
||||
<path android:pathData="M22,55m-3,0a3,3 0 1,0 6,0a3,3 0 1,0 -6,0"
|
||||
android:fillColor="#FF8892A8"/>
|
||||
<path android:pathData="M86,74m-3,0a3,3 0 1,0 6,0a3,3 0 1,0 -6,0"
|
||||
android:fillColor="#FF00BCD4"/>
|
||||
<!-- Arrowhead at (80,72) pointing right/slightly-up -->
|
||||
<path android:pathData="M76,64 L72,80 L88,74 Z">
|
||||
<aapt:attr name="android:fillColor">
|
||||
<gradient android:type="linear"
|
||||
android:startX="72" android:startY="72"
|
||||
android:endX="88" android:endY="72"
|
||||
android:startColor="#00BCD4"
|
||||
android:endColor="#00ACC1"/>
|
||||
</aapt:attr>
|
||||
</path>
|
||||
|
||||
</vector>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<paths>
|
||||
<external-path name="external_storage" path="." />
|
||||
<external-files-path name="external_files" path="." />
|
||||
<files-path name="internal_files" path="." />
|
||||
</paths>
|
||||
|
||||
Reference in New Issue
Block a user