diff --git a/lib/main.dart b/lib/main.dart index 59909c6..3a29cac 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -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(); diff --git a/lib/src/accounts/controllers/folder_controller.dart b/lib/src/accounts/controllers/folder_controller.dart index 89eecb5..a42bf48 100644 --- a/lib/src/accounts/controllers/folder_controller.dart +++ b/lib/src/accounts/controllers/folder_controller.dart @@ -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'; @@ -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 toggleDeletionOnSync(FolderModel model) async { @@ -120,8 +120,8 @@ class Folder extends _$Folder { ) ]; - // TODO: Update - await _box.add(model); + await _box.clear(); + await _box.addAll(state); } Future toggleTwoWaySync(FolderModel model) async { @@ -135,8 +135,8 @@ class Folder extends _$Folder { ) ]; - // TODO: Update - await _box.add(model); + await _box.clear(); + await _box.addAll(state); } Future createFolder({ @@ -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 upload( @@ -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 delete(FolderModel model, Option path) async { @@ -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 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 (); } } diff --git a/lib/src/home/services/rclone.dart b/lib/src/home/services/rclone.dart index 4df819d..3c0929f 100644 --- a/lib/src/home/services/rclone.dart +++ b/lib/src/home/services/rclone.dart @@ -120,6 +120,17 @@ class RCloneAuthService { return TaskEither.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) { @@ -164,10 +175,9 @@ class RCloneAuthService { // Wait for process to finish await process.exitCode; - print(output); - print(errorOutput); final Map authJson = jsonDecode( - RegExp(r'\{.+\}').stringMatch(output.toString())!); + RegExp(r'\{.+\}').stringMatch(output.toString())!, + ); if (authJson case { 'access_token': String accessToken, @@ -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); diff --git a/lib/src/settings/views/settings_view.dart b/lib/src/settings/views/settings_view.dart index 378c5a0..6a86571 100644 --- a/lib/src/settings/views/settings_view.dart +++ b/lib/src/settings/views/settings_view.dart @@ -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 { @@ -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( @@ -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(); }