Skip to content

Commit

Permalink
Implement local/remote folder delete
Browse files Browse the repository at this point in the history
  • Loading branch information
dhzdhd committed Jan 8, 2025
1 parent 9bae038 commit ebf2862
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 32 deletions.
35 changes: 14 additions & 21 deletions lib/src/accounts/controllers/folder_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CreateFolderController extends _$CreateFolderController {

state = const AsyncLoading();
state = await AsyncValue.guard(
() => folderNotifier.createFolder(
() => folderNotifier.create(
authModel: authModel,
folderName: folderName,
folderPath: folderPath,
Expand Down Expand Up @@ -59,18 +59,12 @@ class UploadDeleteController extends _$UploadDeleteController {
);
}

Future<void> delete(
FolderModel folderModel,
Option<String> filePath,
) async {
Future<void> delete(FolderModel folderModel, bool deleteRemote) async {
final folderNotifier = ref.read(folderProvider.notifier);

state = const AsyncLoading();
state = await AsyncValue.guard(
() => folderNotifier.delete(
folderModel,
filePath,
),
() => folderNotifier.delete(folderModel, deleteRemote),
);
}
}
Expand Down Expand Up @@ -139,7 +133,7 @@ class Folder extends _$Folder {
await _box.addAll(state);
}

Future<void> createFolder({
Future<void> create({
required DriveProviderModel authModel,
required String folderPath,
required String folderName,
Expand All @@ -159,8 +153,7 @@ class Folder extends _$Folder {

state = [...state, model];

await _box.clear();
await _box.addAll(state);
await _box.add(model);
}

Future<void> upload(
Expand All @@ -183,18 +176,18 @@ class Folder extends _$Folder {
// TODO: Implement per file
}

Future<void> delete(FolderModel model, Option<String> path) async {
// TODO: Implement per file
final providerModels = ref.watch(authProvider);
final providerModel =
providerModels.filter((t) => t.remoteName == model.remoteName).first;
Future<void> delete(FolderModel model, bool deleteRemote) async {
if (deleteRemote) {
final providerModels = ref.watch(authProvider);
final providerModel =
providerModels.filter((t) => t.remoteName == model.remoteName).first;

await RCloneDriveService()
.delete(providerModel: providerModel, folderModel: model)
.run();
await RCloneDriveService()
.delete(providerModel: providerModel, folderModel: model)
.run();
}

state = state.where((element) => element != model).toList();

await _box.clear();
await _box.addAll(state);
}
Expand Down
50 changes: 50 additions & 0 deletions lib/src/home/components/delete_folder_dialog.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:syncvault/helpers.dart';
import 'package:syncvault/src/accounts/controllers/folder_controller.dart';
import 'package:syncvault/src/accounts/models/folder_model.dart';

class DeleteFolderDialogWidget extends ConsumerWidget {
const DeleteFolderDialogWidget({super.key, required this.model});

final FolderModel model;

@override
Widget build(BuildContext context, WidgetRef ref) {
final folderNotifier = ref.read(uploadDeleteControllerProvider.notifier);

return AlertDialog(
title: Text('Delete ${model.folderName}?'),
content: const Text(
'Remote delete deletes the local state and uploaded files.\nLocal delete deletes only local state.\nThis does not affect your local files.',
textAlign: TextAlign.left,
),
actions: [
TextButton(
onPressed: () async {
if (context.mounted) {
Navigator.of(context).pop();
}
await folderNotifier.delete(model, true);
},
child: const Text('Remote Delete')),
TextButton(
onPressed: () async {
if (context.mounted) {
Navigator.of(context).pop();
}
await folderNotifier.delete(model, false);
},
child: const Text('Local Delete')),
ElevatedButton(
onPressed: () {
if (context.mounted) {
Navigator.of(context).pop();
}
},
child: const Text('Cancel'),
)
],
);
}
}
19 changes: 8 additions & 11 deletions lib/src/home/views/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:syncvault/src/accounts/controllers/auth_controller.dart';
import 'package:syncvault/src/accounts/controllers/folder_controller.dart';
import 'package:syncvault/src/accounts/views/account_view.dart';
import 'package:syncvault/helpers.dart';
import 'package:syncvault/src/home/components/delete_folder_dialog.dart';
import 'package:syncvault/src/home/components/expandable_card_widget.dart';
import 'package:syncvault/src/home/components/new_folder_dialog_widget.dart';
import 'package:syncvault/src/home/components/tree_view_sheet_widget.dart';
Expand Down Expand Up @@ -292,20 +293,16 @@ class _HomeViewState extends ConsumerState<HomeView> {
.isLoading) {
return;
}
// TODO: Perhaps make loadingIndex an array
currentLoadingIndex.value = index;

if (!uploadDeleteController
.isLoading) {
await ref
.read(
uploadDeleteControllerProvider
.notifier,
)
.delete(e, none());

if (context.mounted) {
if (context.mounted) {
context.showSuccessSnackBar(
content: 'Success',
await showDialog(
context: context,
builder: (ctx) =>
DeleteFolderDialogWidget(
model: e),
);
}
}
Expand Down

0 comments on commit ebf2862

Please sign in to comment.