refactor: ChangeCollectionNameDialog to generic RenameDialog

This commit is contained in:
Neeraj Gupta 2021-11-09 12:28:37 +05:30
parent 9cd11351d2
commit 7196ab1e6f
2 changed files with 21 additions and 24 deletions

View file

@ -12,8 +12,8 @@ import 'package:photos/models/collection.dart';
import 'package:photos/models/magic_metadata.dart';
import 'package:photos/models/selected_files.dart';
import 'package:photos/services/collections_service.dart';
import 'package:photos/ui/change_collection_name_dialog.dart';
import 'package:photos/ui/create_collection_page.dart';
import 'package:photos/ui/rename_dialog.dart';
import 'package:photos/ui/share_collection_widget.dart';
import 'package:photos/utils/delete_file_util.dart';
import 'package:photos/utils/dialog_util.dart';
@ -122,15 +122,15 @@ class _GalleryAppBarWidgetState extends State<GalleryAppBarWidget> {
if (widget.type != GalleryAppBarType.owned_collection) {
return;
}
final result = await showDialog(
final result = await showDialog<String>(
context: context,
builder: (BuildContext context) {
return ChangeCollectionNameDialog(name: _appBarTitle);
return RenameDialog(_appBarTitle, 'album');
},
barrierColor: Colors.black.withOpacity(0.85),
);
// indicates user cancelled the rename request
if (result == null) {
if (result == null || result.trim() == _appBarTitle.trim()) {
return;
}

View file

@ -2,24 +2,25 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:photos/utils/dialog_util.dart';
class ChangeCollectionNameDialog extends StatefulWidget {
class RenameDialog extends StatefulWidget {
final String name;
final String type;
final int maxLength;
const ChangeCollectionNameDialog({Key key, this.name}) : super(key: key);
const RenameDialog(this.name, this.type, {Key key, this.maxLength = 100})
: super(key: key);
@override
_ChangeCollectionNameDialogState createState() =>
_ChangeCollectionNameDialogState();
_RenameDialogState createState() => _RenameDialogState();
}
class _ChangeCollectionNameDialogState
extends State<ChangeCollectionNameDialog> {
String _newCollectionName;
class _RenameDialogState extends State<RenameDialog> {
String _newName;
@override
void initState() {
super.initState();
_newCollectionName = widget.name;
_newName = widget.name;
}
@override
@ -33,7 +34,7 @@ class _ChangeCollectionNameDialogState
children: [
TextFormField(
decoration: InputDecoration(
hintText: 'album name',
hintText: '${widget.type} name',
hintStyle: TextStyle(
color: Colors.white30,
),
@ -41,12 +42,12 @@ class _ChangeCollectionNameDialogState
),
onChanged: (value) {
setState(() {
_newCollectionName = value;
_newName = value;
});
},
autocorrect: false,
keyboardType: TextInputType.text,
initialValue: _newCollectionName,
initialValue: _newName,
autofocus: true,
),
],
@ -72,21 +73,17 @@ class _ChangeCollectionNameDialogState
),
),
onPressed: () {
if (_newCollectionName.trim().isEmpty) {
if (_newName.trim().isEmpty) {
showErrorDialog(
context, "empty name", "album name cannot be empty");
context, "empty name", "${widget.type} name cannot be empty");
return;
}
if (_newCollectionName.trim().length > 100) {
if (_newName.trim().length > widget.maxLength) {
showErrorDialog(context, "name too large",
"album name should be less than 100 characters");
"${widget.type} name should be less than ${widget.maxLength} characters");
return;
}
if (_newCollectionName.trim() == widget.name.trim()) {
Navigator.of(context).pop(null);
return;
}
Navigator.of(context).pop(_newCollectionName.trim());
Navigator.of(context).pop(_newName.trim());
},
),
],