Favor functions in package:collection

This commit is contained in:
Ming Ming 2022-07-25 13:51:52 +08:00
parent 0c06fe0607
commit 2af31a0357
16 changed files with 32 additions and 53 deletions

View file

@ -1,7 +1,7 @@
import 'package:bloc/bloc.dart';
import 'package:collection/collection.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/iterable_extension.dart';
abstract class AlbumSearchBlocEvent {
const AlbumSearchBlocEvent();

View file

@ -1,4 +1,5 @@
import 'package:bloc/bloc.dart';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/ci_string.dart';

View file

@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:equatable/equatable.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/entity/album.dart';
@ -5,7 +6,6 @@ import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/iterable_extension.dart' as iterable_extension;
import 'package:nc_photos/type.dart';
abstract class AlbumCoverProvider with EquatableMixin {

View file

@ -1,5 +1,5 @@
import 'package:collection/collection.dart';
import 'package:nc_photos/entity/album.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:tuple/tuple.dart';
enum AlbumSort {

View file

@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:drift/drift.dart' as sql;
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
@ -9,7 +10,6 @@ import 'package:nc_photos/entity/sqlite_table.dart' as sql;
import 'package:nc_photos/entity/sqlite_table_converter.dart';
import 'package:nc_photos/entity/sqlite_table_extension.dart' as sql;
import 'package:nc_photos/exception.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/list_util.dart' as list_util;
import 'package:nc_photos/object_extension.dart';
import 'package:nc_photos/remote_storage_util.dart' as remote_storage_util;
@ -164,7 +164,8 @@ class FileSqliteCacheUpdater {
..orderBy([sql.OrderingTerm.asc(db.dirFiles.rowId)]);
final dirChildRowIds =
await dirChildRowIdQuery.map((r) => r.read(db.dirFiles.child)!).get();
final diff = list_util.diff(dirChildRowIds, _childRowIds.sorted());
final diff = list_util.diff(
dirChildRowIds, _childRowIds.sorted(Comparable.compare));
if (diff.item1.isNotEmpty) {
await db.batch((batch) {
// insert new children

View file

@ -1,14 +1,12 @@
import 'dart:async';
import 'package:collection/collection.dart';
import 'package:flutter/foundation.dart';
import 'package:nc_photos/list_extension.dart';
import 'package:nc_photos/override_comparator.dart';
import 'package:tuple/tuple.dart';
extension IterableExtension<T> on Iterable<T> {
/// Return a new sorted list
List<T> sorted([int Function(T a, T b)? compare]) => toList()..sort(compare);
/// Return a new stable sorted list
List<T> stableSorted([int Function(T a, T b)? compare]) =>
toList()..stableSort(compare);
@ -17,14 +15,7 @@ extension IterableExtension<T> on Iterable<T> {
/// toString for each items
String toReadableString() => "[${join(', ')}]";
Iterable<U> mapWithIndex<U>(U Function(int index, T element) fn) sync* {
int i = 0;
for (final e in this) {
yield fn(i++, e);
}
}
Iterable<Tuple2<int, T>> withIndex() => mapWithIndex((i, e) => Tuple2(i, e));
Iterable<Tuple2<int, T>> withIndex() => mapIndexed((i, e) => Tuple2(i, e));
/// Whether the collection contains an element equal to [element] using the
/// equality function [equalFn]

View file

@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:drift/drift.dart' as sql;
import 'package:event_bus/event_bus.dart';
import 'package:kiwi/kiwi.dart';
@ -8,7 +9,6 @@ import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/sqlite_table.dart' as sql;
import 'package:nc_photos/entity/sqlite_table_extension.dart' as sql;
import 'package:nc_photos/event/event.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/list_util.dart' as list_util;
import 'package:nc_photos/use_case/list_favorite_offline.dart';

View file

@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:kiwi/kiwi.dart';

View file

@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:kiwi/kiwi.dart';
@ -17,7 +18,6 @@ import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/event/event.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/object_extension.dart';
import 'package:nc_photos/or_null.dart';
@ -465,7 +465,7 @@ class _DynamicAlbumBrowserState extends State<DynamicAlbumBrowser>
));
}
if (successes.isNotEmpty) {
final indexes = successes.map((e) => e.index).sorted();
final indexes = successes.map((e) => e.index).sorted(Comparable.compare);
setState(() {
for (final i in indexes.reversed) {
_sortedItems.removeAt(i);

View file

@ -1,6 +1,7 @@
import 'dart:math' as math;
import 'package:android_intent_plus/android_intent.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/account.dart';
@ -9,7 +10,6 @@ import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file_util.dart' as file_util;
import 'package:nc_photos/help_utils.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/mobile/android/android_info.dart';
import 'package:nc_photos/mobile/android/content_uri_image_provider.dart';
@ -423,7 +423,7 @@ class _StylePickerState extends State<_StylePicker> {
runSpacing: 8,
spacing: 8,
children: [
..._bundledStyles.mapWithIndex((i, e) => _buildItem(
..._bundledStyles.mapIndexed((i, e) => _buildItem(
i,
Image(
image: ResizeImage.resizeIfNeeded(

View file

@ -1,5 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
@ -14,7 +15,6 @@ import 'package:nc_photos/entity/person.dart';
import 'package:nc_photos/exception.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/help_utils.dart' as help_utils;
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/snack_bar_manager.dart';
import 'package:nc_photos/theme.dart';

View file

@ -1,5 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
@ -429,7 +430,7 @@ class _PersonBrowserState extends State<PersonBrowser>
.toList();
setState(() {
itemStreamListItems = _backingFiles!
.mapWithIndex((i, f) => _ListItem(
.mapIndexed((i, f) => _ListItem(
index: i,
file: f,
account: widget.account,

View file

@ -1,3 +1,4 @@
import 'package:collection/collection.dart';
import 'package:logging/logging.dart';
import 'package:nc_photos/date_time_extension.dart';
import 'package:nc_photos/entity/album.dart';
@ -5,7 +6,6 @@ import 'package:nc_photos/entity/album/cover_provider.dart';
import 'package:nc_photos/entity/album/provider.dart';
import 'package:nc_photos/entity/album/sort_provider.dart';
import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/iterable_extension.dart';
class DateGroupHelper {
DateGroupHelper({

View file

@ -1,5 +1,6 @@
import 'package:cached_network_image/cached_network_image.dart';
import 'package:cached_network_image_platform_interface/cached_network_image_platform_interface.dart';
import 'package:collection/collection.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:intl/intl.dart';
@ -18,7 +19,6 @@ import 'package:nc_photos/entity/file.dart';
import 'package:nc_photos/entity/file/data_source.dart';
import 'package:nc_photos/entity/share.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
import 'package:nc_photos/iterable_extension.dart';
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/object_extension.dart';
import 'package:nc_photos/or_null.dart';

View file

@ -1,7 +1,7 @@
import 'package:collection/collection.dart';
import 'package:nc_photos/ci_string.dart';
import 'package:nc_photos/entity/album/item.dart';
import 'package:nc_photos/entity/album/sort_provider.dart';
import 'package:nc_photos/iterable_extension.dart';
import 'package:test/test.dart';
import '../../test_util.dart' as util;
@ -85,7 +85,7 @@ void _timeFileAscending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex((i, f) => AlbumFileItem(
.mapIndexed((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -113,7 +113,7 @@ void _timeFileDescending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex((i, f) => AlbumFileItem(
.mapIndexed((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -141,7 +141,7 @@ void _timeNonFileAscending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex<AlbumItem>((i, f) => AlbumFileItem(
.mapIndexed<AlbumItem>((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -177,7 +177,7 @@ void _timeNonFileDescending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex<AlbumItem>((i, f) => AlbumFileItem(
.mapIndexed<AlbumItem>((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -213,7 +213,7 @@ void _timeNonFileHead() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex<AlbumItem>((i, f) => AlbumFileItem(
.mapIndexed<AlbumItem>((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -249,7 +249,7 @@ void _filenameFileAscending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex((i, f) => AlbumFileItem(
.mapIndexed((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -277,7 +277,7 @@ void _filenameFileDescending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex((i, f) => AlbumFileItem(
.mapIndexed((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -317,7 +317,7 @@ void _filenameFileNatural() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 1),
))
.build()
.mapWithIndex((i, f) => AlbumFileItem(
.mapIndexed((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -348,7 +348,7 @@ void _filenameNonFileAscending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex<AlbumItem>((i, f) => AlbumFileItem(
.mapIndexed<AlbumItem>((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -384,7 +384,7 @@ void _filenameNonFileDescending() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex<AlbumItem>((i, f) => AlbumFileItem(
.mapIndexed<AlbumItem>((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,
@ -420,7 +420,7 @@ void _filenameNonFileHead() {
lastModified: DateTime.utc(2020, 1, 2, 3, 4, 2),
))
.build()
.mapWithIndex<AlbumItem>((i, f) => AlbumFileItem(
.mapIndexed<AlbumItem>((i, f) => AlbumFileItem(
addedBy: CiString("admin"),
addedAt: f.lastModified!,
file: f,

View file

@ -5,22 +5,6 @@ import 'package:tuple/tuple.dart';
void main() {
group("IterableExtension", () {
test("sorted", () {
final src = [1, 4, 5, 2, 3, 8, 6, 7];
expect(src.sorted(), [1, 2, 3, 4, 5, 6, 7, 8]);
});
test("mapWithIndex", () {
final src = [1, 4, 5, 2, 3];
final result =
src.mapWithIndex((index, element) => Tuple2(index, element)).toList();
expect(result[0], const Tuple2(0, 1));
expect(result[1], const Tuple2(1, 4));
expect(result[2], const Tuple2(2, 5));
expect(result[3], const Tuple2(3, 2));
expect(result[4], const Tuple2(4, 3));
});
test("withIndex", () {
final src = [1, 4, 5, 2, 3];
final result = src.withIndex().toList();