jld3103
1 year ago
10 changed files with 201 additions and 52 deletions
@ -1,2 +1,42 @@ |
|||||||
/// The result of a version check. |
import 'package:meta/meta.dart'; |
||||||
typedef VersionSupported<T> = ({bool isSupported, T minimumVersion}); |
import 'package:version/version.dart'; |
||||||
|
|
||||||
|
/// Holds the [versions], [minimumVersion] and [maximumMajor] of an app. |
||||||
|
@immutable |
||||||
|
class VersionCheck { |
||||||
|
/// Creates a new [VersionCheck]. |
||||||
|
/// |
||||||
|
/// If the [maximumMajor] is `null` the compatibility of the major of the [minimumVersion] is checked. |
||||||
|
VersionCheck({ |
||||||
|
required this.versions, |
||||||
|
required this.minimumVersion, |
||||||
|
required final int? maximumMajor, |
||||||
|
}) : maximumMajor = maximumMajor ?? minimumVersion.major; |
||||||
|
|
||||||
|
/// Current version of the app. |
||||||
|
final List<Version>? versions; |
||||||
|
|
||||||
|
/// Minimum version of the app. |
||||||
|
final Version minimumVersion; |
||||||
|
|
||||||
|
/// Maximum major version of the app. |
||||||
|
late final int maximumMajor; |
||||||
|
|
||||||
|
/// Whether the [versions] is allowed by the [minimumVersion] and [maximumMajor]. |
||||||
|
/// |
||||||
|
/// If [versions] is `null` or empty it is assumed that the app is supported. |
||||||
|
/// Only one of the [versions] has to be supported to return `true`. |
||||||
|
bool get isSupported { |
||||||
|
if (versions == null || versions!.isEmpty) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
|
||||||
|
for (final version in versions!) { |
||||||
|
if (version >= minimumVersion && version.major <= maximumMajor) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return false; |
||||||
|
} |
||||||
|
} |
||||||
|
@ -1,25 +1,22 @@ |
|||||||
import 'package:collection/collection.dart'; |
|
||||||
import 'package:nextcloud/src/api/core.openapi.dart' as core; |
import 'package:nextcloud/src/api/core.openapi.dart' as core; |
||||||
import 'package:nextcloud/src/api/notes.openapi.dart' as notes; |
import 'package:nextcloud/src/api/notes.openapi.dart' as notes; |
||||||
import 'package:nextcloud/src/helpers/common.dart'; |
import 'package:nextcloud/src/helpers/common.dart'; |
||||||
import 'package:version/version.dart'; |
import 'package:version/version.dart'; |
||||||
|
|
||||||
/// API version of the notes app supported |
/// Minimum API version of the notes app supported |
||||||
const supportedVersion = 1; |
final minVersion = Version(1, 3, 0); |
||||||
|
|
||||||
// ignore: public_member_api_docs |
// ignore: public_member_api_docs |
||||||
extension NotesVersionSupported on notes.Client { |
extension NotesVersionCheck on notes.Client { |
||||||
/// Check if the notes app version is supported by this client |
/// Check if the notes app version is supported by this client |
||||||
/// |
/// |
||||||
/// Also returns the supported API version number |
/// Also returns the supported API version number |
||||||
VersionSupported<int> isSupported( |
VersionCheck getVersionCheck(final core.OcsGetCapabilitiesResponseApplicationJson_Ocs_Data capabilities) { |
||||||
final core.OcsGetCapabilitiesResponseApplicationJson_Ocs_Data capabilities, |
final versions = capabilities.capabilities.notesCapabilities?.notes.apiVersion; |
||||||
) => |
return VersionCheck( |
||||||
( |
versions: versions?.map(Version.parse).toList(), |
||||||
isSupported: capabilities.capabilities.notesCapabilities?.notes.apiVersion |
minimumVersion: minVersion, |
||||||
?.map(Version.parse) |
maximumMajor: null, |
||||||
.firstWhereOrNull((final version) => version.major == supportedVersion) != |
|
||||||
null, |
|
||||||
minimumVersion: supportedVersion, |
|
||||||
); |
); |
||||||
} |
} |
||||||
|
} |
||||||
|
@ -0,0 +1,100 @@ |
|||||||
|
import 'package:nextcloud/nextcloud.dart'; |
||||||
|
import 'package:test/test.dart'; |
||||||
|
import 'package:version/version.dart'; |
||||||
|
|
||||||
|
void main() { |
||||||
|
group('Version check', () { |
||||||
|
test('Null versions', () { |
||||||
|
final check = VersionCheck( |
||||||
|
versions: null, |
||||||
|
// Invalid constraints to avoid accidental validation |
||||||
|
minimumVersion: Version(2, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isTrue); |
||||||
|
}); |
||||||
|
|
||||||
|
test('Empty versions', () { |
||||||
|
final check = VersionCheck( |
||||||
|
versions: const [], |
||||||
|
// Invalid constraints to avoid accidental validation |
||||||
|
minimumVersion: Version(2, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isTrue); |
||||||
|
}); |
||||||
|
|
||||||
|
test('Multiple versions', () { |
||||||
|
final check = VersionCheck( |
||||||
|
versions: [ |
||||||
|
Version(0, 9, 9), |
||||||
|
Version(1, 5, 0), |
||||||
|
Version(2, 0, 0), |
||||||
|
], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isTrue); |
||||||
|
}); |
||||||
|
|
||||||
|
test('With maximumMajor', () { |
||||||
|
var check = VersionCheck( |
||||||
|
versions: [Version(0, 9, 9)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isFalse); |
||||||
|
|
||||||
|
check = VersionCheck( |
||||||
|
versions: [Version(1, 0, 0)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isTrue); |
||||||
|
|
||||||
|
check = VersionCheck( |
||||||
|
versions: [Version(1, 5, 0)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isTrue); |
||||||
|
|
||||||
|
check = VersionCheck( |
||||||
|
versions: [Version(1, 9, 9)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isTrue); |
||||||
|
|
||||||
|
check = VersionCheck( |
||||||
|
versions: [Version(2, 0, 0)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: 1, |
||||||
|
); |
||||||
|
expect(check.isSupported, isFalse); |
||||||
|
}); |
||||||
|
|
||||||
|
test('Without maximumMajor', () { |
||||||
|
var check = VersionCheck( |
||||||
|
versions: [Version(0, 9, 9)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: null, |
||||||
|
); |
||||||
|
expect(check.isSupported, isFalse); |
||||||
|
|
||||||
|
check = VersionCheck( |
||||||
|
versions: [Version(1, 5, 0)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: null, |
||||||
|
); |
||||||
|
expect(check.isSupported, isTrue); |
||||||
|
|
||||||
|
check = VersionCheck( |
||||||
|
versions: [Version(2, 0, 0)], |
||||||
|
minimumVersion: Version(1, 0, 0), |
||||||
|
maximumMajor: null, |
||||||
|
); |
||||||
|
expect(check.isSupported, isFalse); |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
Loading…
Reference in new issue