|
1 |
| -/* |
2 |
| - * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
3 |
| - * |
4 |
| - * Licensed under the Apache License, Version 2.0 (the "License"). |
5 |
| - * You may not use this file except in compliance with the License. |
6 |
| - * A copy of the License is located at |
7 |
| - * |
8 |
| - * http://aws.amazon.com/apache2.0 |
9 |
| - * |
10 |
| - * or in the "license" file accompanying this file. This file is distributed |
11 |
| - * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
12 |
| - * express or implied. See the License for the specific language governing |
13 |
| - * permissions and limitations under the License. |
14 |
| - */ |
15 |
| - |
16 |
| -part of 'query_field.dart'; |
17 |
| - |
18 |
| -enum QuerySortOrder { ascending, descending } |
19 |
| - |
20 |
| -extension QuerySortOrderExtension on QuerySortOrder { |
21 |
| - String toShortString() { |
22 |
| - return toString().split('.').last; // toString returns the enumName.value |
23 |
| - } |
24 |
| -} |
25 |
| - |
26 |
| -/// Represents a model field and an order to sort by (ascending or descending), |
27 |
| -/// used to specify the order of results from a query operation |
28 |
| -class QuerySortBy { |
29 |
| - final QuerySortOrder order; |
30 |
| - final String field; |
31 |
| - |
32 |
| - const QuerySortBy({required this.order, required this.field}); |
33 |
| - |
34 |
| - int compare<T extends Model>(T a, T b) { |
35 |
| - String fieldName = getFieldName(field); |
36 |
| - dynamic valueA = a.toJson()[fieldName]; |
37 |
| - dynamic valueB = b.toJson()[fieldName]; |
38 |
| - int orderMultiplier = order == QuerySortOrder.ascending ? 1 : -1; |
39 |
| - if (valueA == null || valueB == null) { |
40 |
| - return orderMultiplier * _compareNull(valueA, valueB); |
41 |
| - } else if (valueA is bool && valueB is bool) { |
42 |
| - return orderMultiplier * _compareBool(valueA, valueB); |
43 |
| - } else if (valueA is Comparable && valueB is Comparable) { |
44 |
| - return orderMultiplier * valueA.compareTo(valueB); |
45 |
| - } |
46 |
| - throw const AmplifyException( |
47 |
| - 'A non-comparable field was used as a QuerySortBy', |
48 |
| - ); |
49 |
| - } |
50 |
| - |
51 |
| - int _compareBool(bool a, bool b) { |
52 |
| - if (a == b) return 0; |
53 |
| - return a ? 1 : -1; |
54 |
| - } |
55 |
| - |
56 |
| - int _compareNull(Object? a, Object? b) { |
57 |
| - if (a == b) return 0; |
58 |
| - return a != null ? 1 : -1; |
59 |
| - } |
60 |
| - |
61 |
| - Map<String, dynamic> serializeAsMap() { |
62 |
| - return <String, dynamic>{'field': field, 'order': order.toShortString()}; |
63 |
| - } |
64 |
| -} |
| 1 | +/* |
| 2 | + * Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +part of 'query_field.dart'; |
| 17 | + |
| 18 | +enum QuerySortOrder { ascending, descending } |
| 19 | + |
| 20 | +extension QuerySortOrderExtension on QuerySortOrder { |
| 21 | + String toShortString() { |
| 22 | + return toString().split('.').last; // toString returns the enumName.value |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +/// Represents a model field and an order to sort by (ascending or descending), |
| 27 | +/// used to specify the order of results from a query operation |
| 28 | +class QuerySortBy { |
| 29 | + final QuerySortOrder order; |
| 30 | + final String field; |
| 31 | + |
| 32 | + const QuerySortBy({required this.order, required this.field}); |
| 33 | + |
| 34 | + int compare<T extends Model>(T a, T b) { |
| 35 | + String fieldName = getFieldName(field); |
| 36 | + dynamic valueA = a.toJson()[fieldName]; |
| 37 | + dynamic valueB = b.toJson()[fieldName]; |
| 38 | + int orderMultiplier = order == QuerySortOrder.ascending ? 1 : -1; |
| 39 | + if (valueA == null || valueB == null) { |
| 40 | + return orderMultiplier * _compareNull(valueA, valueB); |
| 41 | + } else if (valueA is bool && valueB is bool) { |
| 42 | + return orderMultiplier * _compareBool(valueA, valueB); |
| 43 | + } else if (valueA is Comparable && valueB is Comparable) { |
| 44 | + return orderMultiplier * valueA.compareTo(valueB); |
| 45 | + } |
| 46 | + throw const AmplifyException( |
| 47 | + 'A non-comparable field was used as a QuerySortBy', |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + int _compareBool(bool a, bool b) { |
| 52 | + if (a == b) return 0; |
| 53 | + return a ? 1 : -1; |
| 54 | + } |
| 55 | + |
| 56 | + int _compareNull(Object? a, Object? b) { |
| 57 | + if (a == b) return 0; |
| 58 | + return a != null ? 1 : -1; |
| 59 | + } |
| 60 | + |
| 61 | + Map<String, dynamic> serializeAsMap() { |
| 62 | + return <String, dynamic>{ |
| 63 | + 'field': getFieldName(field), |
| 64 | + 'order': order.toShortString(), |
| 65 | + }; |
| 66 | + } |
| 67 | +} |
0 commit comments