Files
buzz/mobile/lib/features/settings/settings_page/notifications_section.dart

72 lines
2.4 KiB
Dart

part of '../settings_page.dart';
bool _notificationsVisible(WidgetRef ref) {
if (defaultTargetPlatform != TargetPlatform.iOS) return false;
final community = ref.watch(activeCommunityProvider).value;
if (community == null) return false;
final capability = ref.watch(currentRelayPushDescriptorProvider);
return (!capability.isLoading &&
!capability.hasError &&
capability.value != null) ||
community.pushNotificationsEnabled ||
community.pushSubscriptionState.pendingTombstoneGeneration != null;
}
class _NotificationsSection extends ConsumerWidget {
const _NotificationsSection();
@override
Widget build(BuildContext context, WidgetRef ref) {
if (!_notificationsVisible(ref)) return const SizedBox.shrink();
final community = ref.watch(activeCommunityProvider).value!;
final capability = ref.watch(currentRelayPushDescriptorProvider);
final hasCapability =
!capability.isLoading &&
!capability.hasError &&
capability.value != null;
final canToggle = hasCapability || community.pushNotificationsEnabled;
void setEnabled(bool enabled) {
unawaited(
ref
.read(communityListProvider.notifier)
.setPushNotificationsEnabled(community.id, enabled),
);
}
return AppListCard(
verticalPadding: Grid.twelve,
children: [
MergeSemantics(
key: const ValueKey('push-notifications-setting'),
child: Stack(
alignment: Alignment.centerRight,
children: [
AppListRow(
key: const ValueKey('push-notifications-enabled'),
title: 'Notifications',
// Reserve horizontal room without letting the switch's 48dp
// layout height expand this single-line row.
trailing: const SizedBox(width: 60),
onTap: canToggle
? () => setEnabled(!community.pushNotificationsEnabled)
: null,
),
Positioned.fill(
right: Grid.xs,
child: Align(
alignment: Alignment.centerRight,
child: Switch.adaptive(
value: community.pushNotificationsEnabled,
onChanged: canToggle ? setEnabled : null,
),
),
),
],
),
),
],
);
}
}