diff --git a/lib/models/friendica_version_requirement.dart b/lib/models/friendica_version_requirement.dart new file mode 100644 index 0000000..bb38983 --- /dev/null +++ b/lib/models/friendica_version_requirement.dart @@ -0,0 +1,32 @@ +import 'friendica_version.dart'; + +final FriendicaVersionRequirement unknownRequirement = + FriendicaVersionRequirement(unknown); + +class FriendicaVersionRequirement { + final FriendicaVersion minimumVersion; + final FriendicaVersion? maxVersion; + + const FriendicaVersionRequirement(this.minimumVersion, {this.maxVersion}); + + bool versionMeetsRequirement(FriendicaVersion version) { + if (version < minimumVersion) { + return false; + } + + if (maxVersion == null) { + return true; + } + + return version <= maxVersion!; + } + + @override + String toString() { + if (maxVersion == null) { + return 'requires at least Friendica $minimumVersion'; + } + + return 'works only on Friendica $minimumVersion to $maxVersion'; + } +} diff --git a/lib/services/feature_version_checker.dart b/lib/services/feature_version_checker.dart index a62af2b..f833cef 100644 --- a/lib/services/feature_version_checker.dart +++ b/lib/services/feature_version_checker.dart @@ -6,6 +6,7 @@ import 'package:result_monad/result_monad.dart'; import '../globals.dart'; import '../models/exec_error.dart'; import '../models/friendica_version.dart'; +import '../models/friendica_version_requirement.dart'; enum RelaticaFeatures { diasporaReshare('Resharing Diaspora Posts'), @@ -29,8 +30,8 @@ class FriendicaVersionChecker { const FriendicaVersionChecker(); bool canUseFeature(RelaticaFeatures feature) { - final neededVersion = featureVersionRequirement[feature]; - if (neededVersion == null) { + final requirement = featureVersionRequirement[feature]; + if (requirement == null) { _logger.severe( 'Return false since no minimum version data in table for: $feature', ); @@ -39,7 +40,9 @@ class FriendicaVersionChecker { return getIt>() .activeEntry - .andThenSuccess((info) => info.friendicaVersion >= neededVersion) + .andThenSuccess( + (info) => requirement.versionMeetsRequirement(info.friendicaVersion), + ) .fold( onSuccess: (versionMet) => versionMet, onError: (error) { @@ -61,17 +64,20 @@ class FriendicaVersionChecker { ); } - FriendicaVersion getVersionRequirement(RelaticaFeatures feature) => - featureVersionRequirement[feature] ?? unknown; + FriendicaVersionRequirement getVersionRequirement(RelaticaFeatures feature) => + featureVersionRequirement[feature] ?? unknownRequirement; String versionErrorString(RelaticaFeatures feature) => - "${feature.label} requires at least Friendica ${getVersionRequirement(feature).toVersionString()}"; + "${feature.label} ${getVersionRequirement(feature)}"; - static final featureVersionRequirement = { - RelaticaFeatures.diasporaReshare: v2023_04, - RelaticaFeatures.directMessageCreation: v2023_04, - RelaticaFeatures.postSpoilerText: v2023_04, - RelaticaFeatures.statusEditing: v2023_04, - RelaticaFeatures.usingActualFollowRequests: v2023_04, + static final featureVersionRequirement = + { + RelaticaFeatures.diasporaReshare: FriendicaVersionRequirement(v2023_04), + RelaticaFeatures.directMessageCreation: + FriendicaVersionRequirement(v2023_04), + RelaticaFeatures.postSpoilerText: FriendicaVersionRequirement(v2023_04), + RelaticaFeatures.statusEditing: FriendicaVersionRequirement(v2023_04), + RelaticaFeatures.usingActualFollowRequests: + FriendicaVersionRequirement(v2023_04), }; }