Skip to content

Commit

Permalink
Fix duplicate folder being shown on switch toggle and fix clear stora…
Browse files Browse the repository at this point in the history
…ge setting
  • Loading branch information
dhzdhd committed Jan 8, 2025
1 parent a5b4776 commit 9bae038
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 58 deletions.
2 changes: 0 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ import 'src/settings/controllers/settings_controller.dart';
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
await Hive.initFlutter();

// final authInfo = Auth.init();
// final folderInfo = Folder.init();

Expand Down
74 changes: 22 additions & 52 deletions lib/src/accounts/controllers/folder_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:fpdart/fpdart.dart';
import 'package:get_it/get_it.dart';
import 'package:hive_ce/hive.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:syncvault/errors.dart';
import 'package:syncvault/src/accounts/controllers/auth_controller.dart';
import 'package:syncvault/src/accounts/models/file_model.dart';
import 'package:syncvault/src/accounts/models/folder_model.dart';
Expand Down Expand Up @@ -105,8 +104,9 @@ class Folder extends _$Folder {
model.copyWith(isAutoSync: !model.isAutoSync),
)
];
// TODO: Update
await _box.add(model);

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

Future<void> toggleDeletionOnSync(FolderModel model) async {
Expand All @@ -120,8 +120,8 @@ class Folder extends _$Folder {
)
];

// TODO: Update
await _box.add(model);
await _box.clear();
await _box.addAll(state);
}

Future<void> toggleTwoWaySync(FolderModel model) async {
Expand All @@ -135,8 +135,8 @@ class Folder extends _$Folder {
)
];

// TODO: Update
await _box.add(model);
await _box.clear();
await _box.addAll(state);
}

Future<void> createFolder({
Expand All @@ -157,17 +157,10 @@ class Folder extends _$Folder {
.match((l) => throw l, (r) => r)
.run();

// final files = await driveService
// .getAllItems(
// root: some(id),
// accessToken: newAuthModel.accessToken,
// filter: none(),
// )
// .match((l) => throw l, (r) => r)
// .run();

state = [...state, model];
await _box.add(model);

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

Future<void> upload(
Expand All @@ -185,27 +178,9 @@ class Folder extends _$Folder {
folderModel: folderModel,
localPath: folderModel.folderPath,
)
.run(); // TODO: Handle error

// // TODO: Implement for particular folder
// final files = await driveService
// .getAllItems(
// root: none(),
// accessToken: newAuthModel.accessToken,
// filter: none(),
// )
// .match((l) => throw l, (r) => r)
// .run();

// state = [
// ...state
// ..remove(folderModel)
// ..add(folderModel.copyWith(files: files))
// ];
// Hive.box('vault').put(
// hiveFoldersKey,
// jsonEncode(state.map((e) => e.toJson()).toList()),
// );
.run();

// TODO: Implement per file
}

Future<void> delete(FolderModel model, Option<String> path) async {
Expand All @@ -219,21 +194,16 @@ class Folder extends _$Folder {
.run();

state = state.where((element) => element != model).toList();
// TODO: Change hive storage

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

TaskEither<AppError, ()> clearCache() {
return TaskEither.tryCatch(
() async {
state = [];
await _box.clear();

return ();
},
(error, stackTrace) => error.handleError(
'Failed to clear local storage',
stackTrace,
),
);
Future<()> clearCache() async {
state = [];
await _box.clear();
await _box.flush();

return ();
}
}
17 changes: 14 additions & 3 deletions lib/src/home/services/rclone.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,17 @@ class RCloneAuthService {
return TaskEither<AppError, DriveProviderModel>.Do(($) async {
final execPath = await $(utils.getRCloneExec());
final configFile = await $(utils.getConfig());
final rCloneConfig = await $(utils.getIniConfig());

await $(TaskEither.tryCatch(
() async {
if (rCloneConfig.hasSection(payload.remoteName)) {
throw const AppError.general('Remote already exists');
}
},
(err, stackTrace) =>
err.handleError('Remote already exists', stackTrace),
));

// Authorize with rclone for given provider
final model = switch (driveProvider.backend) {
Expand Down Expand Up @@ -164,10 +175,9 @@ class RCloneAuthService {
// Wait for process to finish
await process.exitCode;

print(output);
print(errorOutput);
final Map<String, dynamic> authJson = jsonDecode(
RegExp(r'\{.+\}').stringMatch(output.toString())!);
RegExp(r'\{.+\}').stringMatch(output.toString())!,
);
if (authJson
case {
'access_token': String accessToken,
Expand Down Expand Up @@ -299,6 +309,7 @@ class RCloneAuthService {
toWrite['drive_id'] = driveId;
}

// TODO: Refactor to getIniConfig
final iniConfig =
Config.fromString(await configFile.readAsString());
iniConfig.add(model.remoteName, toWrite);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/settings/views/settings_view.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 '../controllers/settings_controller.dart';

class SettingsView extends ConsumerWidget {
Expand All @@ -12,6 +13,7 @@ class SettingsView extends ConsumerWidget {
Widget build(BuildContext context, WidgetRef ref) {
final settings = ref.watch(settingsProvider);
final settingsNotifier = ref.read(settingsProvider.notifier);
final folderNotifier = ref.read(folderProvider.notifier);

return Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -149,7 +151,7 @@ class SettingsView extends ConsumerWidget {
actions: [
OutlinedButton(
onPressed: () async {
// await folderNotifier.clearCache().run();
await folderNotifier.clearCache();
if (context.mounted) {
Navigator.of(context).pop();
}
Expand Down

0 comments on commit 9bae038

Please sign in to comment.