offerToActivateFeature function
Future<bool>
offerToActivateFeature({ - required dynamic context,
- required dynamic ref,
- required String spaceId,
- required dynamic feature,
})
Implementation
Future<bool> offerToActivateFeature({
required BuildContext context,
required WidgetRef ref,
required String spaceId,
required SpaceFeature feature,
}) async {
final lang = L10n.of(context);
EasyLoading.showInfo(lang.loading);
try {
final space = await ref.read(spaceProvider(spaceId).future);
final appSettingsAndMembership =
await ref.read(spaceAppSettingsProvider(spaceId).future);
EasyLoading.dismiss();
if (!context.mounted) {
return false;
}
final featureTitle = switch (feature) {
SpaceFeature.boosts => lang.boosts,
SpaceFeature.pins => lang.pins,
SpaceFeature.events => lang.events,
SpaceFeature.tasks => lang.tasks,
};
final appSettings = appSettingsAndMembership.settings;
final powerLevels = appSettingsAndMembership.powerLevels;
final maxPowerLevel = powerLevels.maxPowerLevel();
final currentPowerLevel = switch (feature) {
SpaceFeature.boosts => powerLevels.news(),
SpaceFeature.events => powerLevels.events(),
SpaceFeature.pins => powerLevels.pins(),
SpaceFeature.tasks => powerLevels.tasks(),
};
final isAlreadyActive = switch (feature) {
SpaceFeature.boosts => appSettings.news().active(),
SpaceFeature.events => appSettings.events().active(),
SpaceFeature.pins => appSettings.pins().active(),
SpaceFeature.tasks => appSettings.tasks().active(),
};
final levelKey = switch (feature) {
SpaceFeature.boosts => powerLevels.newsKey(),
SpaceFeature.pins => powerLevels.pinsKey(),
SpaceFeature.events => powerLevels.eventsKey(),
SpaceFeature.tasks => powerLevels.tasksKey(),
};
if (isAlreadyActive) {
// feature is already activated, forward to the power level changer
return await updateFeatureLevelChangeDialog(
context,
maxPowerLevel,
currentPowerLevel,
space,
powerLevels,
levelKey,
featureTitle,
);
}
int? setPowerLevel;
EasyLoading.dismiss();
final shouldActivate = await showDialog<bool?>(
context: context,
builder: (BuildContext context) => _ActivateFeatureDialog(
featureName: featureTitle,
currentPowerLevelName:
maxPowerLevel == 100 ? powerLevelName(currentPowerLevel) : 'Custom',
currentPowerLevel: currentPowerLevel,
onPowerLevelChange: (newValue) => setPowerLevel = newValue,
),
);
if (shouldActivate != true || !context.mounted) {
return false;
}
await setActerFeature(
context,
true,
appSettings,
space,
feature,
featureTitle,
);
if (setPowerLevel != currentPowerLevel) {
await updateFeatureLevel(
// ignore: use_build_context_synchronously
lang,
space,
levelKey,
featureTitle,
setPowerLevel,
);
}
return true;
} catch (error, stack) {
_log.severe('Failed to activate space feature', error, stack);
EasyLoading.showToast(lang.error(error));
}
return false;
}