improved new album creation dialog
This commit is contained in:
parent
51952f7699
commit
dcbd95be01
1 changed files with 52 additions and 22 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
import 'dart:io';
|
||||||
|
|
||||||
import 'package:aves/utils/android_file_utils.dart';
|
import 'package:aves/utils/android_file_utils.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/widgets.dart';
|
import 'package:flutter/widgets.dart';
|
||||||
|
@ -10,19 +12,17 @@ class CreateAlbumDialog extends StatefulWidget {
|
||||||
|
|
||||||
class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
final TextEditingController _nameController = TextEditingController();
|
final TextEditingController _nameController = TextEditingController();
|
||||||
Set<StorageVolume> allVolumes;
|
final ValueNotifier<bool> _existsNotifier = ValueNotifier(false);
|
||||||
StorageVolume primaryVolume, selectedVolume;
|
Set<StorageVolume> _allVolumes;
|
||||||
|
StorageVolume _primaryVolume, _selectedVolume;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void initState() {
|
void initState() {
|
||||||
super.initState();
|
super.initState();
|
||||||
|
_allVolumes = androidFileUtils.storageVolumes;
|
||||||
// TODO TLAD improve new album default name
|
_primaryVolume = _allVolumes.firstWhere((volume) => volume.isPrimary, orElse: () => _allVolumes.first);
|
||||||
_nameController.text = 'Album 1';
|
_selectedVolume = _primaryVolume;
|
||||||
|
_initAlbumName();
|
||||||
allVolumes = androidFileUtils.storageVolumes;
|
|
||||||
primaryVolume = allVolumes.firstWhere((volume) => volume.isPrimary, orElse: () => allVolumes.first);
|
|
||||||
selectedVolume = primaryVolume;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
@ -38,7 +38,7 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
content: Column(
|
content: Column(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
if (allVolumes.length > 1) ...[
|
if (_allVolumes.length > 1) ...[
|
||||||
Row(
|
Row(
|
||||||
mainAxisSize: MainAxisSize.min,
|
mainAxisSize: MainAxisSize.min,
|
||||||
children: [
|
children: [
|
||||||
|
@ -47,7 +47,7 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
Expanded(
|
Expanded(
|
||||||
child: DropdownButton<StorageVolume>(
|
child: DropdownButton<StorageVolume>(
|
||||||
isExpanded: true,
|
isExpanded: true,
|
||||||
items: allVolumes
|
items: _allVolumes
|
||||||
.map((volume) => DropdownMenuItem(
|
.map((volume) => DropdownMenuItem(
|
||||||
value: volume,
|
value: volume,
|
||||||
child: Text(
|
child: Text(
|
||||||
|
@ -58,18 +58,30 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
),
|
),
|
||||||
))
|
))
|
||||||
.toList(),
|
.toList(),
|
||||||
value: selectedVolume,
|
value: _selectedVolume,
|
||||||
onChanged: (volume) => setState(() => selectedVolume = volume),
|
onChanged: (volume) {
|
||||||
|
_selectedVolume = volume;
|
||||||
|
_checkAlbumExists();
|
||||||
|
setState(() {});
|
||||||
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
const SizedBox(height: 16),
|
const SizedBox(height: 16),
|
||||||
],
|
],
|
||||||
TextField(
|
ValueListenableBuilder<bool>(
|
||||||
controller: _nameController,
|
valueListenable: _existsNotifier,
|
||||||
// autofocus: true,
|
builder: (context, exists, child) {
|
||||||
),
|
return TextField(
|
||||||
|
controller: _nameController,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
helperText: exists ? 'Album already exists' : '',
|
||||||
|
),
|
||||||
|
onChanged: (_) => _checkAlbumExists(),
|
||||||
|
onSubmitted: (_) => _submit(context),
|
||||||
|
);
|
||||||
|
}),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
contentPadding: const EdgeInsets.fromLTRB(24, 20, 24, 0),
|
contentPadding: const EdgeInsets.fromLTRB(24, 20, 24, 0),
|
||||||
|
@ -79,16 +91,34 @@ class _CreateAlbumDialogState extends State<CreateAlbumDialog> {
|
||||||
child: Text('Cancel'.toUpperCase()),
|
child: Text('Cancel'.toUpperCase()),
|
||||||
),
|
),
|
||||||
FlatButton(
|
FlatButton(
|
||||||
onPressed: () => Navigator.pop(context, _buildAlbumPath()),
|
onPressed: () => _submit(context),
|
||||||
child: Text('Create'.toUpperCase()),
|
child: Text('Create'.toUpperCase()),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
String _buildAlbumPath() {
|
String _buildAlbumPath(String name) {
|
||||||
final newName = _nameController.text;
|
if (name == null || name.isEmpty) return '';
|
||||||
if (newName == null || newName.isEmpty) return null;
|
return join(_selectedVolume.path, 'Pictures', name);
|
||||||
return join(selectedVolume == primaryVolume ? androidFileUtils.dcimPath : selectedVolume.path, newName);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<void> _initAlbumName() async {
|
||||||
|
var count = 1;
|
||||||
|
while (true) {
|
||||||
|
var name = 'Album $count';
|
||||||
|
if (!await Directory(_buildAlbumPath(name)).exists()) {
|
||||||
|
_nameController.text = name;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<void> _checkAlbumExists() async {
|
||||||
|
final path = _buildAlbumPath(_nameController.text);
|
||||||
|
_existsNotifier.value = path.isEmpty ? false : await Directory(path).exists();
|
||||||
|
}
|
||||||
|
|
||||||
|
void _submit(BuildContext context) => Navigator.pop(context, _buildAlbumPath(_nameController.text));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue