validate form to rename entry
This commit is contained in:
parent
a56ed27d0e
commit
c72fb94330
3 changed files with 41 additions and 13 deletions
|
@ -10,16 +10,14 @@ import 'package:aves/utils/time_utils.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:geocoder/geocoder.dart';
|
import 'package:geocoder/geocoder.dart';
|
||||||
import 'package:path/path.dart';
|
import 'package:path/path.dart' as ppath;
|
||||||
import 'package:tuple/tuple.dart';
|
import 'package:tuple/tuple.dart';
|
||||||
|
|
||||||
import 'mime_types.dart';
|
import 'mime_types.dart';
|
||||||
|
|
||||||
class ImageEntry {
|
class ImageEntry {
|
||||||
String uri;
|
String uri;
|
||||||
String _path;
|
String _path, _directory, _filename, _extension;
|
||||||
String _directory;
|
|
||||||
String _filename;
|
|
||||||
int contentId;
|
int contentId;
|
||||||
final String sourceMimeType;
|
final String sourceMimeType;
|
||||||
int width;
|
int width;
|
||||||
|
@ -131,20 +129,26 @@ class ImageEntry {
|
||||||
_path = path;
|
_path = path;
|
||||||
_directory = null;
|
_directory = null;
|
||||||
_filename = null;
|
_filename = null;
|
||||||
|
_extension = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
String get path => _path;
|
String get path => _path;
|
||||||
|
|
||||||
String get directory {
|
String get directory {
|
||||||
_directory ??= path != null ? dirname(path) : null;
|
_directory ??= path != null ? ppath.dirname(path) : null;
|
||||||
return _directory;
|
return _directory;
|
||||||
}
|
}
|
||||||
|
|
||||||
String get filenameWithoutExtension {
|
String get filenameWithoutExtension {
|
||||||
_filename ??= path != null ? basenameWithoutExtension(path) : null;
|
_filename ??= path != null ? ppath.basenameWithoutExtension(path) : null;
|
||||||
return _filename;
|
return _filename;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String get extension {
|
||||||
|
_extension ??= path != null ? ppath.extension(path) : null;
|
||||||
|
return _extension;
|
||||||
|
}
|
||||||
|
|
||||||
// the MIME type reported by the Media Store is unreliable
|
// the MIME type reported by the Media Store is unreliable
|
||||||
// so we use the one found during cataloguing if possible
|
// so we use the one found during cataloguing if possible
|
||||||
String get mimeType => catalogMetadata?.mimeType ?? sourceMimeType;
|
String get mimeType => catalogMetadata?.mimeType ?? sourceMimeType;
|
||||||
|
@ -318,7 +322,7 @@ class ImageEntry {
|
||||||
Future<bool> rename(String newName) async {
|
Future<bool> rename(String newName) async {
|
||||||
if (newName == filenameWithoutExtension) return true;
|
if (newName == filenameWithoutExtension) return true;
|
||||||
|
|
||||||
final newFields = await ImageFileService.rename(this, '$newName${extension(this.path)}');
|
final newFields = await ImageFileService.rename(this, '$newName$extension');
|
||||||
if (newFields.isEmpty) return false;
|
if (newFields.isEmpty) return false;
|
||||||
|
|
||||||
final uri = newFields['uri'];
|
final uri = newFields['uri'];
|
||||||
|
|
|
@ -77,6 +77,7 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
helperText: exists ? 'Album already exists' : '',
|
helperText: exists ? 'Album already exists' : '',
|
||||||
hintText: 'Album name',
|
hintText: 'Album name',
|
||||||
),
|
),
|
||||||
|
autofocus: _allVolumes.length == 1,
|
||||||
onChanged: (_) => _validate(),
|
onChanged: (_) => _validate(),
|
||||||
onSubmitted: (_) => _submit(context),
|
onSubmitted: (_) => _submit(context),
|
||||||
);
|
);
|
||||||
|
@ -96,7 +97,7 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
child: Text('Create'.toUpperCase()),
|
child: Text('Create'.toUpperCase()),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
)
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:aves/model/image_entry.dart';
|
import 'package:aves/model/image_entry.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import 'package:path/path.dart';
|
||||||
|
|
||||||
import '../aves_dialog.dart';
|
import '../aves_dialog.dart';
|
||||||
|
|
||||||
|
@ -14,11 +17,13 @@ class RenameEntryDialog extends StatefulWidget {
|
||||||
|
|
||||||
class _RenameEntryDialogState extends State<RenameEntryDialog> {
|
class _RenameEntryDialogState extends State<RenameEntryDialog> {
|
||||||
final TextEditingController _nameController = TextEditingController();
|
final TextEditingController _nameController = TextEditingController();
|
||||||
|
final ValueNotifier<bool> _isValidNotifier = ValueNotifier(false);
|
||||||
|
|
||||||
|
ImageEntry get entry => widget.entry;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
final entry = widget.entry;
|
|
||||||
_nameController.text = entry.filenameWithoutExtension ?? entry.sourceTitle;
|
_nameController.text = entry.filenameWithoutExtension ?? entry.sourceTitle;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,17 +39,35 @@ class _RenameEntryDialogState extends State<RenameEntryDialog> {
|
||||||
content: TextField(
|
content: TextField(
|
||||||
controller: _nameController,
|
controller: _nameController,
|
||||||
autofocus: true,
|
autofocus: true,
|
||||||
|
onChanged: (_) => _validate(),
|
||||||
|
onSubmitted: (_) => _submit(context),
|
||||||
),
|
),
|
||||||
actions: [
|
actions: [
|
||||||
FlatButton(
|
FlatButton(
|
||||||
onPressed: () => Navigator.pop(context),
|
onPressed: () => Navigator.pop(context),
|
||||||
child: Text('Cancel'.toUpperCase()),
|
child: Text('Cancel'.toUpperCase()),
|
||||||
),
|
),
|
||||||
FlatButton(
|
ValueListenableBuilder<bool>(
|
||||||
onPressed: () => Navigator.pop(context, _nameController.text),
|
valueListenable: _isValidNotifier,
|
||||||
child: Text('Apply'.toUpperCase()),
|
builder: (context, isValid, child) {
|
||||||
),
|
return FlatButton(
|
||||||
|
onPressed: isValid ? () => _submit(context) : null,
|
||||||
|
child: Text('Apply'.toUpperCase()),
|
||||||
|
);
|
||||||
|
},
|
||||||
|
)
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _validate() async {
|
||||||
|
var newName = _nameController.text ?? '';
|
||||||
|
if (newName.isNotEmpty) {
|
||||||
|
newName += entry.extension;
|
||||||
|
}
|
||||||
|
final type = await FileSystemEntity.type(join(entry.directory, newName));
|
||||||
|
_isValidNotifier.value = type == FileSystemEntityType.notFound;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _submit(BuildContext context) => Navigator.pop(context, _nameController.text);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue