123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- import 'package:flutter/material.dart';
- import 'package:flutter/services.dart';
- import 'package:flutter/widgets.dart';
- import 'package:photos/core/constants.dart';
- import 'package:photos/core/event_bus.dart';
- import 'package:photos/events/user_details_changed_event.dart';
- import 'package:photos/models/duplicate_files.dart';
- import 'package:photos/models/file.dart';
- import 'package:photos/ui/detail_page.dart';
- import 'package:photos/ui/thumbnail_widget.dart';
- import 'package:photos/utils/data_util.dart';
- import 'package:photos/utils/delete_file_util.dart';
- import 'package:photos/utils/navigation_util.dart';
- class DeduplicatePage extends StatefulWidget {
- final List<DuplicateFiles> duplicates;
- DeduplicatePage(this.duplicates, {Key key}) : super(key: key);
- @override
- _DeduplicatePageState createState() => _DeduplicatePageState();
- }
- class _DeduplicatePageState extends State<DeduplicatePage> {
- static final kDeleteIconOverlay = Container(
- decoration: BoxDecoration(
- gradient: LinearGradient(
- begin: Alignment.topCenter,
- end: Alignment.bottomCenter,
- colors: [
- Colors.transparent,
- Colors.black.withOpacity(0.6),
- ],
- stops: const [0.75, 1],
- ),
- ),
- child: Align(
- alignment: Alignment.bottomRight,
- child: Padding(
- padding: const EdgeInsets.only(right: 8, bottom: 4),
- child: Icon(
- Icons.delete_forever,
- size: 18,
- color: Colors.red[700],
- ),
- ),
- ),
- );
- final Set<File> _selectedFiles = <File>{};
- final Map<int, int> _fileSizeMap = {};
- SortKey sortKey = SortKey.size;
- @override
- void initState() {
- super.initState();
- for (final duplicate in widget.duplicates) {
- for (int index = 0; index < duplicate.files.length; index++) {
- if (index != 0) {
- // _selectedFiles.add(duplicate.files[index]);
- }
- _fileSizeMap[duplicate.files[index].uploadedFileID] = duplicate.size;
- }
- }
- }
- @override
- Widget build(BuildContext context) {
- _sortDuplicates();
- return Scaffold(
- appBar: AppBar(
- title: Hero(
- tag: "deduplicate",
- child: Material(
- type: MaterialType.transparency,
- child: Text(
- "deduplicate files",
- style: TextStyle(
- fontSize: 18,
- ),
- ),
- ),
- ),
- ),
- body: _getBody(),
- );
- }
- void _sortDuplicates() {
- widget.duplicates.sort((first, second) {
- if (sortKey == SortKey.size) {
- final aSize = first.files.length * first.size;
- final bSize = second.files.length * second.size;
- return bSize - aSize;
- } else if (sortKey == SortKey.count) {
- return second.files.length - first.files.length;
- } else {
- return second.files.first.creationTime - first.files.first.creationTime;
- }
- });
- }
- Widget _getBody() {
- return Column(
- mainAxisAlignment: MainAxisAlignment.center,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- Expanded(
- child: ListView.builder(
- itemBuilder: (context, index) {
- if (index == 0) {
- return _getHeader();
- } else if (index == 1) {
- return _getSortMenu();
- }
- return Padding(
- padding: const EdgeInsets.only(top: 10, bottom: 10),
- child: _getGridView(widget.duplicates[index - 2], index - 2),
- );
- },
- itemCount: widget.duplicates.length,
- shrinkWrap: true,
- ),
- ),
- _selectedFiles.isEmpty ? Container() : _getDeleteButton(),
- ],
- );
- }
- Padding _getHeader() {
- return Padding(
- padding: EdgeInsets.fromLTRB(16, 12, 16, 8),
- child: Column(
- children: [
- Text(
- "the following files were clubbed based on their sizes and timestamps",
- style: TextStyle(
- color: Colors.white.withOpacity(0.6),
- height: 1.2,
- ),
- ),
- Padding(
- padding: EdgeInsets.all(4),
- ),
- Text(
- "please review and delete the items you believe are duplicates",
- style: TextStyle(
- color: Colors.white.withOpacity(0.6),
- height: 1.2,
- ),
- ),
- ],
- ),
- );
- }
- Widget _getSortMenu() {
- Text sortOptionText(SortKey key) {
- String text = key.toString();
- switch (key) {
- case SortKey.count:
- text = "count";
- break;
- case SortKey.size:
- text = "total size";
- break;
- case SortKey.time:
- text = "time";
- break;
- }
- return Text(
- text,
- style: TextStyle(
- fontWeight: FontWeight.bold,
- fontSize: 13,
- color: Colors.white.withOpacity(0.6),
- ),
- );
- }
- return Row(
- // h4ck to align PopupMenuItems to end
- mainAxisAlignment: MainAxisAlignment.spaceBetween,
- crossAxisAlignment: CrossAxisAlignment.end,
- children: [
- Container(),
- PopupMenuButton(
- initialValue: sortKey?.index ?? 0,
- child: Padding(
- padding: const EdgeInsets.fromLTRB(24, 6, 24, 6),
- child: Row(
- mainAxisAlignment: MainAxisAlignment.end,
- crossAxisAlignment: CrossAxisAlignment.center,
- children: [
- sortOptionText(sortKey),
- Padding(padding: EdgeInsets.only(left: 5.0)),
- Icon(
- Icons.sort,
- color: Theme.of(context).buttonColor,
- size: 20,
- ),
- ],
- ),
- ),
- onSelected: (int index) {
- setState(() {
- sortKey = SortKey.values[index];
- });
- },
- itemBuilder: (context) {
- return List.generate(SortKey.values.length, (index) {
- return PopupMenuItem(
- value: index,
- child: Align(
- alignment: Alignment.topRight,
- child: sortOptionText(SortKey.values[index]),
- ),
- );
- });
- },
- ),
- ],
- );
- }
- Widget _getDeleteButton() {
- String text;
- if (_selectedFiles.length == 1) {
- text = "delete 1 item";
- } else {
- text = "delete " + _selectedFiles.length.toString() + " items";
- }
- int size = 0;
- for (final file in _selectedFiles) {
- size += _fileSizeMap[file.uploadedFileID];
- }
- return SizedBox(
- width: double.infinity,
- child: TextButton(
- style: OutlinedButton.styleFrom(
- backgroundColor: Colors.red[700],
- ),
- child: Column(
- children: [
- Padding(padding: EdgeInsets.all(2)),
- Text(
- text,
- style: TextStyle(
- fontWeight: FontWeight.bold,
- fontSize: 14,
- color: Colors.white,
- ),
- textAlign: TextAlign.center,
- ),
- Padding(padding: EdgeInsets.all(2)),
- Text(
- formatBytes(size),
- style: TextStyle(
- color: Colors.white.withOpacity(0.7),
- fontSize: 12,
- ),
- ),
- Padding(padding: EdgeInsets.all(2)),
- ],
- ),
- onPressed: () async {
- await deleteFilesFromRemoteOnly(context, _selectedFiles.toList());
- Bus.instance.fire(UserDetailsChangedEvent());
- Navigator.of(context)
- .pop(DeduplicationResult(_selectedFiles.length, size));
- },
- ),
- );
- }
- Widget _getGridView(DuplicateFiles duplicates, int itemIndex) {
- return Column(
- children: [
- Padding(
- padding: const EdgeInsets.fromLTRB(16, 8, 4, 4),
- child: Text(
- duplicates.files.length.toString() +
- " files, " +
- formatBytes(duplicates.size) +
- " each",
- style: TextStyle(
- color: Colors.white.withOpacity(0.9),
- ),
- ),
- ),
- GridView.builder(
- shrinkWrap: true,
- physics:
- NeverScrollableScrollPhysics(), // to disable GridView's scrolling
- itemBuilder: (context, index) {
- return _buildFile(context, duplicates.files[index], itemIndex);
- },
- itemCount: duplicates.files.length,
- gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
- crossAxisCount: 4,
- ),
- padding: EdgeInsets.all(0),
- ),
- ],
- );
- }
- Widget _buildFile(BuildContext context, File file, int index) {
- return GestureDetector(
- onTap: () {
- if (_selectedFiles.contains(file)) {
- _selectedFiles.remove(file);
- } else {
- _selectedFiles.add(file);
- }
- setState(() {});
- },
- onLongPress: () {
- HapticFeedback.lightImpact();
- final files = widget.duplicates[index].files;
- routeToPage(
- context,
- DetailPage(
- DetailPageConfiguration(
- files,
- null,
- files.indexOf(file),
- "deduplicate_",
- mode: DetailPageMode.minimalistic,
- ),
- ),
- );
- },
- child: Container(
- margin: const EdgeInsets.all(2.0),
- decoration: BoxDecoration(
- border: _selectedFiles.contains(file)
- ? Border.all(
- width: 3,
- color: Colors.red[700],
- )
- : null,
- ),
- child: Stack(children: [
- Hero(
- tag: "deduplicate_" + file.tag(),
- child: ThumbnailWidget(
- file,
- diskLoadDeferDuration: kThumbnailDiskLoadDeferDuration,
- serverLoadDeferDuration: kThumbnailServerLoadDeferDuration,
- shouldShowLivePhotoOverlay: true,
- key: Key("deduplicate_" + file.tag()),
- ),
- ),
- _selectedFiles.contains(file) ? kDeleteIconOverlay : Container(),
- ]),
- ),
- );
- }
- }
- enum SortKey {
- size,
- count,
- time,
- }
- class DeduplicationResult {
- final int count;
- final int size;
- DeduplicationResult(this.count, this.size);
- }
|