share folders and details
This commit is contained in:
+161
@@ -0,0 +1,161 @@
|
||||
package com.vaultdrop.mobile.data.repository
|
||||
|
||||
import androidx.room.withTransaction
|
||||
import com.vaultdrop.mobile.data.local.AppDatabase
|
||||
import com.vaultdrop.mobile.data.local.dao.FileDao
|
||||
import com.vaultdrop.mobile.data.local.dao.FolderDao
|
||||
import com.vaultdrop.mobile.data.local.entity.FileEntity
|
||||
import com.vaultdrop.mobile.data.local.entity.FolderEntity
|
||||
import com.vaultdrop.mobile.data.remote.ApiClient
|
||||
import com.vaultdrop.mobile.data.remote.dto.ResourcePermissionDto
|
||||
import com.vaultdrop.mobile.domain.DeviceIdentity
|
||||
import com.vaultdrop.mobile.domain.SyncPlacement
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
||||
/**
|
||||
* Hydratation des ressources partagées dans Room depuis le snapshot serveur.
|
||||
*
|
||||
* Le snapshot (`GET /sync/permissions`) contient les permissions effectives
|
||||
* de l'utilisateur. Les ressources non possédées (viewer/commenter/editor)
|
||||
* sont importées en tant que lignes cloud-only (`uri = NULL`, `sync_status = "cloud"`).
|
||||
*
|
||||
* Un pull complet (`after=0`) est effectué au login et à la restauration de
|
||||
* session pour garantir la convergence (révocations = disparition du snapshot).
|
||||
*/
|
||||
@Singleton
|
||||
class ShareRepository @Inject constructor(
|
||||
private val apiClient: ApiClient,
|
||||
private val appDatabase: AppDatabase,
|
||||
private val deviceIdentity: DeviceIdentity,
|
||||
) {
|
||||
|
||||
/**
|
||||
* Récupère le snapshot complet des permissions et hydrate Room.
|
||||
*
|
||||
* Pour chaque permission non-owner :
|
||||
* - Folders : upsert cloud-only (uri=null, name, parentId, ownerId)
|
||||
* - Files : upsert cloud-only (uri=null, name, folderResourceId, ownerId, processed=true)
|
||||
*
|
||||
* Convergence : les lignes cloud-only absentes du snapshot complet sont
|
||||
* marquées `exists = 0`.
|
||||
*/
|
||||
suspend fun syncSnapshot() {
|
||||
val currentUserId = deviceIdentity.getOrCreate()
|
||||
val permissions = apiClient.syncPermissions()
|
||||
|
||||
val now = System.currentTimeMillis()
|
||||
val folderDao = appDatabase.folderDao()
|
||||
val fileDao = appDatabase.fileDao()
|
||||
|
||||
val snapshotFolderIds = mutableSetOf<String>()
|
||||
val snapshotFileIds = mutableSetOf<String>()
|
||||
|
||||
appDatabase.withTransaction {
|
||||
for (perm in permissions) {
|
||||
when {
|
||||
perm.effectiveAccess == "owner" -> Unit
|
||||
perm.resourceType == "folder" -> {
|
||||
snapshotFolderIds += perm.resourceId
|
||||
upsertSharedFolder(perm, now, folderDao)
|
||||
}
|
||||
perm.resourceType == "file" -> {
|
||||
snapshotFileIds += perm.resourceId
|
||||
upsertSharedFile(perm, now, fileDao)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Convergence : marquer les lignes cloud-only absentes du snapshot
|
||||
// comme disparues (révocation / expiration côté serveur).
|
||||
for (folder in folderDao.getAll()) {
|
||||
if (isCloudOnlyForOtherUser(folder.uri, folder.ownerId, currentUserId, folder.exists)
|
||||
&& folder.resourceId !in snapshotFolderIds
|
||||
) {
|
||||
folderDao.markMissing(folder.resourceId, now)
|
||||
}
|
||||
}
|
||||
for (file in fileDao.getAll()) {
|
||||
if (isCloudOnlyForOtherUser(file.uri, file.ownerId, currentUserId, file.exists)
|
||||
&& file.resourceId !in snapshotFileIds
|
||||
) {
|
||||
fileDao.markMissing(file.resourceId, now)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Timber.d(
|
||||
"syncSnapshot: hydrated %d folders, %d files",
|
||||
snapshotFolderIds.size,
|
||||
snapshotFileIds.size,
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Ligne hébergée cloud (uri NULL), non possédée localement, encore visible.
|
||||
* Absente du snapshot → ressource partagée révoquée/expirée → marquer disparue.
|
||||
*/
|
||||
private fun isCloudOnlyForOtherUser(
|
||||
uri: String?,
|
||||
ownerId: String?,
|
||||
currentNodeId: String,
|
||||
exists: Int?,
|
||||
): Boolean = uri == null && ownerId != null && ownerId != currentNodeId && exists != 0
|
||||
|
||||
private suspend fun upsertSharedFolder(
|
||||
perm: ResourcePermissionDto,
|
||||
now: Long,
|
||||
folderDao: FolderDao,
|
||||
) {
|
||||
val existing = folderDao.getByResourceId(perm.resourceId)
|
||||
if (existing != null && existing.uri != null) {
|
||||
// Une copie physique locale existe déjà — ne jamais écraser l'uri.
|
||||
return
|
||||
}
|
||||
val entity = FolderEntity(
|
||||
id = existing?.id ?: 0L,
|
||||
resourceId = perm.resourceId,
|
||||
uri = null,
|
||||
name = perm.name.ifBlank { existing?.name ?: "" },
|
||||
exists = 1,
|
||||
parentResourceId = perm.parentId ?: existing?.parentResourceId,
|
||||
ownerId = perm.ownerId ?: existing?.ownerId,
|
||||
syncStatus = SyncPlacement.confirmed(false),
|
||||
addedAt = existing?.addedAt ?: now,
|
||||
updatedAt = now,
|
||||
)
|
||||
folderDao.upsert(entity)
|
||||
}
|
||||
|
||||
private suspend fun upsertSharedFile(
|
||||
perm: ResourcePermissionDto,
|
||||
now: Long,
|
||||
fileDao: FileDao,
|
||||
) {
|
||||
val existing = fileDao.getByResourceId(perm.resourceId)
|
||||
if (existing != null && existing.uri != null) {
|
||||
// Une copie physique locale existe déjà — ne jamais écraser l'uri.
|
||||
return
|
||||
}
|
||||
val entity = FileEntity(
|
||||
id = existing?.id ?: 0L,
|
||||
resourceId = perm.resourceId,
|
||||
uri = null,
|
||||
name = perm.name.ifBlank { existing?.name ?: "" },
|
||||
folderResourceId = perm.parentId ?: existing?.folderResourceId ?: "",
|
||||
extension = existing?.extension,
|
||||
size = existing?.size ?: 0L,
|
||||
mimeType = existing?.mimeType,
|
||||
category = existing?.category,
|
||||
exists = 1,
|
||||
lastModified = existing?.lastModified,
|
||||
ownerId = perm.ownerId ?: existing?.ownerId,
|
||||
syncStatus = SyncPlacement.confirmed(false),
|
||||
processed = true,
|
||||
addedAt = existing?.addedAt ?: now,
|
||||
updatedAt = now,
|
||||
)
|
||||
fileDao.upsert(entity)
|
||||
}
|
||||
}
|
||||
+31
-9
@@ -4,6 +4,7 @@ import androidx.activity.compose.BackHandler
|
||||
import androidx.compose.foundation.ExperimentalFoundationApi
|
||||
import androidx.compose.foundation.combinedClickable
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.PaddingValues
|
||||
import androidx.compose.foundation.layout.Row
|
||||
@@ -18,10 +19,12 @@ import androidx.compose.material.icons.automirrored.filled.ArrowBack
|
||||
import androidx.compose.material.icons.filled.Close
|
||||
import androidx.compose.material.icons.filled.CreateNewFolder
|
||||
import androidx.compose.material.icons.filled.Folder
|
||||
import androidx.compose.material.icons.filled.MoreVert
|
||||
import androidx.compose.material.icons.filled.Refresh
|
||||
import androidx.compose.material.icons.filled.Share
|
||||
import androidx.compose.material3.Card
|
||||
import androidx.compose.material3.CardDefaults
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.IconButton
|
||||
@@ -92,6 +95,7 @@ fun FolderDetailScreen(
|
||||
var showDeleteWarning by remember { mutableStateOf(false) }
|
||||
var showDeleteConfirm by remember { mutableStateOf(false) }
|
||||
var showShareSheet by remember { mutableStateOf(false) }
|
||||
var showMoreMenu by remember { mutableStateOf(false) }
|
||||
var pendingDeleteMode by remember { mutableStateOf<FileDeleter.DeleteMode?>(null) }
|
||||
var pendingDeleteReview by remember { mutableStateOf<DeleteReview?>(null) }
|
||||
val snackbarHostState = remember { SnackbarHostState() }
|
||||
@@ -178,14 +182,32 @@ fun FolderDetailScreen(
|
||||
status = connectionStatus,
|
||||
onClick = connectionStatusViewModel::checkNow,
|
||||
)
|
||||
IconButton(onClick = { showShareSheet = true }) {
|
||||
Icon(
|
||||
Icons.Filled.Share,
|
||||
contentDescription = stringResource(R.string.share_content_description),
|
||||
)
|
||||
}
|
||||
IconButton(onClick = viewModel::refresh) {
|
||||
Icon(Icons.Filled.Refresh, contentDescription = stringResource(R.string.refresh))
|
||||
Box {
|
||||
IconButton(onClick = { showMoreMenu = true }) {
|
||||
Icon(
|
||||
Icons.Filled.MoreVert,
|
||||
contentDescription = stringResource(R.string.more_actions),
|
||||
)
|
||||
}
|
||||
DropdownMenu(
|
||||
expanded = showMoreMenu,
|
||||
onDismissRequest = { showMoreMenu = false },
|
||||
) {
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(R.string.share_action)) },
|
||||
onClick = {
|
||||
showMoreMenu = false
|
||||
showShareSheet = true
|
||||
},
|
||||
)
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(R.string.refresh)) },
|
||||
onClick = {
|
||||
showMoreMenu = false
|
||||
viewModel.refresh()
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
package com.vaultdrop.mobile.ui.share
|
||||
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.Spacer
|
||||
import androidx.compose.foundation.layout.fillMaxWidth
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.foundation.layout.size
|
||||
import androidx.compose.foundation.layout.width
|
||||
import androidx.compose.material.icons.Icons
|
||||
import androidx.compose.material.icons.filled.KeyboardArrowDown
|
||||
import androidx.compose.material.icons.filled.Share
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.CircularProgressIndicator
|
||||
import androidx.compose.material3.DropdownMenu
|
||||
import androidx.compose.material3.DropdownMenuItem
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.Icon
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.ModalBottomSheet
|
||||
import androidx.compose.material3.OutlinedButton
|
||||
import androidx.compose.material3.OutlinedTextField
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.rememberModalBottomSheetState
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.text.font.FontWeight
|
||||
import androidx.compose.ui.text.style.TextAlign
|
||||
import androidx.compose.ui.unit.dp
|
||||
import com.vaultdrop.mobile.R
|
||||
|
||||
/** Niveau d'accès sélectionnable — miroir de `ShareAccessLevel`. */
|
||||
private data class AccessOption(
|
||||
val labelRes: Int,
|
||||
val value: String,
|
||||
)
|
||||
|
||||
private val ACCESS_OPTIONS = listOf(
|
||||
AccessOption(R.string.share_access_viewer, ShareAccessLevel.VIEWER),
|
||||
AccessOption(R.string.share_access_commenter, ShareAccessLevel.COMMENTER),
|
||||
AccessOption(R.string.share_access_editor, ShareAccessLevel.EDITOR),
|
||||
)
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ShareBottomSheet(
|
||||
resourceName: String,
|
||||
onDismiss: () -> Unit,
|
||||
onShare: (username: String, access: String) -> Unit,
|
||||
sharing: Boolean,
|
||||
error: String?,
|
||||
enqueued: Boolean,
|
||||
) {
|
||||
var username by remember { mutableStateOf("") }
|
||||
var access by remember { mutableStateOf(ShareAccessLevel.VIEWER) }
|
||||
val sheetState = rememberModalBottomSheetState()
|
||||
|
||||
LaunchedEffect(enqueued) {
|
||||
if (enqueued) onDismiss()
|
||||
}
|
||||
|
||||
ModalBottomSheet(
|
||||
onDismissRequest = onDismiss,
|
||||
sheetState = sheetState,
|
||||
) {
|
||||
Column(
|
||||
modifier = Modifier
|
||||
.fillMaxWidth()
|
||||
.padding(horizontal = 16.dp)
|
||||
.padding(bottom = 24.dp),
|
||||
verticalArrangement = Arrangement.spacedBy(12.dp),
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(R.string.share_title),
|
||||
style = MaterialTheme.typography.titleMedium,
|
||||
fontWeight = FontWeight.SemiBold,
|
||||
)
|
||||
Text(
|
||||
text = resourceName,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
)
|
||||
|
||||
OutlinedTextField(
|
||||
value = username,
|
||||
onValueChange = { username = it },
|
||||
label = { Text(stringResource(R.string.share_username_hint)) },
|
||||
singleLine = true,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
|
||||
Text(
|
||||
text = stringResource(R.string.share_access_label),
|
||||
style = MaterialTheme.typography.labelLarge,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
AccessDropdown(
|
||||
selected = access,
|
||||
onSelect = { access = it },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
|
||||
error?.let { code ->
|
||||
Text(
|
||||
text = stringResource(shareErrorResFor(code)),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
color = MaterialTheme.colorScheme.error,
|
||||
textAlign = TextAlign.Center,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
)
|
||||
}
|
||||
|
||||
Button(
|
||||
onClick = { onShare(username.trim(), access) },
|
||||
enabled = username.isNotBlank() && !sharing,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
if (sharing) {
|
||||
CircularProgressIndicator(
|
||||
modifier = Modifier.size(20.dp),
|
||||
strokeWidth = 2.dp,
|
||||
)
|
||||
} else {
|
||||
Icon(Icons.Filled.Share, contentDescription = null)
|
||||
Spacer(Modifier.width(8.dp))
|
||||
Text(stringResource(R.string.share_submit))
|
||||
}
|
||||
}
|
||||
OutlinedButton(
|
||||
onClick = onDismiss,
|
||||
enabled = !sharing,
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Text(stringResource(R.string.share_cancel))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AccessDropdown(
|
||||
selected: String,
|
||||
onSelect: (String) -> Unit,
|
||||
modifier: Modifier = Modifier,
|
||||
) {
|
||||
var expanded by remember { mutableStateOf(false) }
|
||||
val selectedLabel = ACCESS_OPTIONS.firstOrNull { it.value == selected }?.labelRes
|
||||
?: ACCESS_OPTIONS.first().labelRes
|
||||
|
||||
Box(modifier = modifier) {
|
||||
OutlinedButton(
|
||||
onClick = { expanded = true },
|
||||
modifier = Modifier.fillMaxWidth(),
|
||||
) {
|
||||
Text(
|
||||
text = stringResource(selectedLabel),
|
||||
modifier = Modifier.weight(1f),
|
||||
textAlign = TextAlign.Start,
|
||||
)
|
||||
Icon(
|
||||
imageVector = Icons.Filled.KeyboardArrowDown,
|
||||
contentDescription = null,
|
||||
)
|
||||
}
|
||||
DropdownMenu(
|
||||
expanded = expanded,
|
||||
onDismissRequest = { expanded = false },
|
||||
) {
|
||||
ACCESS_OPTIONS.forEach { option ->
|
||||
DropdownMenuItem(
|
||||
text = { Text(stringResource(option.labelRes)) },
|
||||
onClick = {
|
||||
onSelect(option.value)
|
||||
expanded = false
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun shareErrorResFor(code: String): Int = when (code) {
|
||||
"USER_NOT_FOUND" -> R.string.share_error_not_found
|
||||
else -> R.string.share_error_generic
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package com.vaultdrop.mobile.ui.share
|
||||
|
||||
import android.content.Context
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import com.vaultdrop.mobile.data.remote.ApiClient
|
||||
import com.vaultdrop.mobile.data.remote.ApiException
|
||||
import com.vaultdrop.mobile.data.repository.OutboxRepository
|
||||
import com.vaultdrop.mobile.features.sync.OutboxSyncWorker
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import kotlinx.coroutines.launch
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
|
||||
data class ShareUiState(
|
||||
val sharing: Boolean = false,
|
||||
/** Code d'erreur contractuel (`USER_NOT_FOUND`, `SHARE_FAILED`) ou null. */
|
||||
val error: String? = null,
|
||||
/** L'op `share` a été enqueued avec succès (prête pour le drain). */
|
||||
val enqueued: Boolean = false,
|
||||
)
|
||||
|
||||
/** Accès partage — valeur du payload `access` (docs/api-v1.md §6.1). */
|
||||
object ShareAccessLevel {
|
||||
const val VIEWER = "viewer"
|
||||
const val COMMENTER = "commenter"
|
||||
const val EDITOR = "editor"
|
||||
}
|
||||
|
||||
/**
|
||||
* Partage d'une ressource locale avec un autre utilisateur : résout le
|
||||
* destinataire par username (exact), puis enqueue l'op `share` dans l'outbox
|
||||
* (drainée par [OutboxSyncWorker]).
|
||||
*/
|
||||
@HiltViewModel
|
||||
class ShareViewModel @Inject constructor(
|
||||
private val apiClient: ApiClient,
|
||||
private val outboxRepository: OutboxRepository,
|
||||
@ApplicationContext private val appContext: Context,
|
||||
) : ViewModel() {
|
||||
|
||||
private val _uiState = MutableStateFlow(ShareUiState())
|
||||
val uiState: StateFlow<ShareUiState> = _uiState.asStateFlow()
|
||||
|
||||
/**
|
||||
* Partage [resourceId] avec l'utilisateur [username] au niveau [access].
|
||||
*
|
||||
* 1. `GET /users/resolve` (username exact) → user id ;
|
||||
* 2. enqueue `share` dans l'outbox ;
|
||||
* 3. déclenche le drain du worker.
|
||||
*/
|
||||
fun share(resourceId: String, resourceType: String, username: String, access: String) {
|
||||
if (_uiState.value.sharing) return
|
||||
viewModelScope.launch {
|
||||
_uiState.update { it.copy(sharing = true, error = null, enqueued = false) }
|
||||
runCatching {
|
||||
val user = apiClient.resolveUser(username)
|
||||
outboxRepository.enqueueShare(
|
||||
resourceId = resourceId,
|
||||
resourceType = resourceType,
|
||||
granteeUserId = user.id,
|
||||
access = access,
|
||||
)
|
||||
}.onSuccess {
|
||||
OutboxSyncWorker.enqueue(appContext)
|
||||
Timber.d("share: op enqueued for %s", resourceId)
|
||||
_uiState.update { it.copy(sharing = false, enqueued = true) }
|
||||
}.onFailure { e ->
|
||||
val error = when {
|
||||
e is ApiException && e.code == "NOT_FOUND" -> "USER_NOT_FOUND"
|
||||
else -> "SHARE_FAILED"
|
||||
}
|
||||
Timber.w(e, "share failed")
|
||||
_uiState.update { it.copy(sharing = false, error = error) }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/** Consomme l'état clone (succès/erreur affiché) avant de rouvrir le sheet. */
|
||||
fun reset() {
|
||||
_uiState.value = ShareUiState()
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
}
|
||||
}
|
||||
@@ -242,4 +242,6 @@
|
||||
<string name="share_error_not_found">Aucun utilisateur connu sous ce nom.</string>
|
||||
<string name="share_error_generic">Impossible de partager pour le moment.</string>
|
||||
<string name="share_content_description">Partager ce dossier</string>
|
||||
<string name="share_action">Partager</string>
|
||||
<string name="more_actions">Plus d\'actions</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user