RenameDialog: Move logic to Repo class

I want all the domain logic to be in the Repo class, this way it can be
performed over another thread / connection. Also, it's far easier to
test this way.
This commit is contained in:
Vishesh Handa
2022-01-22 10:19:22 +01:00
parent 901e4b5cb9
commit 4219adfc30
2 changed files with 17 additions and 3 deletions

View File

@ -914,6 +914,13 @@ class GitJournalRepo with ChangeNotifier {
return Result(null);
}
Result<bool> fileExists(String path) {
return catchAllSync(() {
var type = io.FileSystemEntity.typeSync(path);
return Result(type != io.FileSystemEntityType.notFound);
});
}
}
Future<void> _copyDirectory(String source, String destination) async {

View File

@ -9,9 +9,10 @@ import 'package:flutter/material.dart';
import 'package:easy_localization/easy_localization.dart';
import 'package:path/path.dart' as p;
import 'package:path/path.dart';
import 'package:universal_io/io.dart';
import 'package:provider/provider.dart';
import 'package:gitjournal/generated/locale_keys.g.dart';
import 'package:gitjournal/repository.dart';
class RenameDialog extends StatefulWidget {
final String oldPath;
@ -87,8 +88,14 @@ class _RenameDialogState extends State<RenameDialog> {
});
var newPath = join(dirname(widget.oldPath), value);
if (FileSystemEntity.typeSync(newPath) !=
FileSystemEntityType.notFound) {
var repo = context.read<GitJournalRepo>();
var r = repo.fileExists(newPath);
if (r.isFailure) {
return r.error.toString();
}
var exists = r.getOrThrow();
if (exists) {
return tr(LocaleKeys.widgets_rename_validator_exists);
}