mixed_collection 0.1.5 copy "mixed_collection: ^0.1.5" to clipboard
mixed_collection: ^0.1.5 copied to clipboard

A mixed collection, which supports filtering and sorting based on configurable filters and sorters.

example/example.dart

// Copyright (c) 2019, iMeshAcademy authors.  Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// MIT-style license that can be found in the LICENSE file.

import 'package:mixed_collection/mixed_collection.dart';
import 'stringentry.dart';

bool filterByNameStartsWith(dynamic entry, dynamic data) {
  if (null != entry && null != data) {
    return (entry.getValue('') as String).startsWith(data);
  }

  return false;
}

bool containsFilter(dynamic entry, dynamic data) {
  if (null != entry && null != data) {
    return (entry.getValue('') as String).contains(data);
  }

  return false;
}

int sortByStringAsc(dynamic a, dynamic b) {
  if (a == null) return -1;
  if (b == null) return 0;
  return a.toString().compareTo(b.toString());
}

int sortByStringdesc(dynamic a, dynamic b) {
  if (a == null) return -1;
  if (b == 1) return 0;
  return b.toString().compareTo(a.toString());
}

void main() {
  var items = ["John", "Harry", "Albert", "Paul", "Emilie", "Frank"];

  Collection collection = new Collection();
  var entries = items.map((item) => new StringCollectionEntry(item)).toList();
  collection.load(entries, (res, err) {
    print(collection.count);
  });

  collection.filter(filterByNameStartsWith, true, true, "A");
  assert(collection.records.length == 1);

  collection.clearFilters();
  collection.filter(containsFilter, true, true, "rr");
  assert(collection.records.length == 1);
  var filters = [
    {"callback": filterByNameStartsWith, "value": "A"},
    {"callback": containsFilter, "value": "er"}
  ];

  // Perform this operation if you have filtered the collection already.
  collection.suspendFilter();
  collection.clearFilters();

  // Add the new filter to the collection.
  // This is one way you can change filtering at run time.
  collection.filters.addAll(filters);

  // Let collection resume filtering. A next call to filter API will perfrom filtering.
  collection.resumeFilters();
  collection.filter();
  assert(collection.records.length == 1);

// Remove all filters.
  collection.clearFilters();

  print("Actual Collection");
  collection.records.forEach((f) => print(f.item));

  print("Sort By Ascending");
  collection.sort(sortByStringAsc);
  collection.records.forEach((f) => print(f.item));

  collection.clearSorters();
  print("Sort By Descending");

  // Sort by using a custom comparer.
  collection.sort(sortByStringdesc);
  collection.records.forEach((f) => print(f.item));

  collection.clearSorters();

  ///
  /// Configuration for sorting.
  ///
  /// The sorting is not stable, which means, the indexes of "equal" elements could be
  /// juggled.
  ///
  ///
  var sorter = {
    "caseSensitive": false,
    "direction": "desc",
    "enabled": true,
    "property": ''
  };

  print("Case insensitive sort - config.");
  collection.sort(sorter);
  collection.records.forEach((f) => print(f.item));
}
2
likes
40
points
15
downloads

Publisher

unverified uploader

Weekly Downloads

A mixed collection, which supports filtering and sorting based on configurable filters and sorters.

Homepage
Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

eventify, meta

More

Packages that depend on mixed_collection