Commit df98ac28 by Maurits van Beusekom Committed by GitHub

Merge pull request #515 from Baseflow/equality

Implement equality for Permission
parents 28b3caae 9c849ea9
## 3.1.3
* Implemented equality operator for `Permission` class;
* Reverted services status check for notification permission. Turns out implementation does not fit with idea's of permission_handler plugin.
## 3.1.2
* Allow checking serviceStatus for notification permission.
......
......@@ -11,6 +11,7 @@ class PermissionWithService extends Permission {
}
/// Defines the permissions which can be checked and requested.
@immutable
class Permission {
const Permission._(this.value);
factory Permission.byValue(int value) => values[value];
......@@ -92,7 +93,7 @@ class Permission {
/// Android: Notification
/// iOS: Notification
static const notification = PermissionWithService._(17);
static const notification = Permission._(17);
/// Android: Allows an application to access any geographic locations
/// persisted in the user's shared collection.
......@@ -163,4 +164,18 @@ class Permission {
@override
String toString() => 'Permission.${_names[value]}';
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
if (other.runtimeType != runtimeType) {
return false;
}
return other is Permission && other.value == value;
}
@override
int get hashCode => value.hashCode;
}
......@@ -3,7 +3,7 @@ description: A common platform interface for the permission_handler plugin.
homepage: https://github.com/baseflow/flutter-permission-handler/tree/master/permission_handler_platform_interface
# NOTE: We strongly prefer non-breaking changes, even at the expense of a
# less-clean API. See https://flutter.dev/go/platform-interface-breaking-changes
version: 3.1.2
version: 3.1.3
dependencies:
flutter:
......
......@@ -22,4 +22,59 @@ void main() {
var permissionName = permissionWithService.toString();
expect(permissionName, 'Permission.calendar');
});
test(
// ignore: lines_longer_than_80_chars
'equality operator should return true for two instances with the same values',
() {
// Arrange
final firstPermission = Permission.byValue(1);
final secondPermission = Permission.byValue(1);
// Act & Assert
expect(
firstPermission == secondPermission,
true,
);
});
test(
// ignore: lines_longer_than_80_chars
'equality operator should return false for two instances with different values',
() {
// Arrange
final firstPermission = Permission.byValue(1);
final secondPermission = Permission.byValue(2);
// Act & Assert
expect(
firstPermission == secondPermission,
false,
);
});
test('hashCode should be the same for two instances with the same values',
() {
// Arrange
final firstPermission = Permission.byValue(1);
final secondPermission = Permission.byValue(1);
// Act & Assert
expect(
firstPermission.hashCode,
secondPermission.hashCode,
);
});
test('hashCode should not match for two instances with different values', () {
// Arrange
final firstPermission = Permission.byValue(1);
final secondPermission = Permission.byValue(2);
// Act & Assert
expect(
firstPermission.hashCode == secondPermission.hashCode,
false,
);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment