Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: integrate show notification button option #5302

Merged
5 changes: 5 additions & 0 deletions frontend/appflowy_flutter/lib/core/config/kv_keys.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ class KVKeys {
/// {'feature_flag_1': true, 'feature_flag_2': false}
static const String featureFlag = 'featureFlag';

/// The key for saving show notification icon option
///
/// The value is a boolean string
static const String showNotificationIcon = 'showNotificationIcon';

/// The key for saving the last opened workspace id
///
/// The workspace id is a string.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import 'dart:async';

import 'package:appflowy/core/config/kv.dart';
import 'package:appflowy/core/config/kv_keys.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/user/application/user_settings_service.dart';
import 'package:appflowy_backend/log.dart';
import 'package:appflowy_backend/protobuf/flowy-user/user_setting.pb.dart';
Expand All @@ -10,23 +13,31 @@ part 'notification_settings_cubit.freezed.dart';

class NotificationSettingsCubit extends Cubit<NotificationSettingsState> {
NotificationSettingsCubit() : super(NotificationSettingsState.initial()) {
UserSettingsBackendService()
.getNotificationSettings()
.then((notificationSettings) {
_notificationSettings = notificationSettings;
emit(
state.copyWith(
isNotificationsEnabled: _notificationSettings.notificationsEnabled,
),
);
_initCompleter.complete();
});
_initialize();
}

final Completer<void> _initCompleter = Completer();

late final NotificationSettingsPB _notificationSettings;

Future<void> _initialize() async {
final settings =
await UserSettingsBackendService().getNotificationSettings();
_notificationSettings = settings;
migcarde marked this conversation as resolved.
Show resolved Hide resolved

final showNotificationSetting = await getIt<KeyValueStorage>()
.getWithFormat(KVKeys.showNotificationIcon, (v) => bool.parse(v));

emit(
state.copyWith(
isNotificationsEnabled: _notificationSettings.notificationsEnabled,
isShowNotificationsIconEnabled: showNotificationSetting ?? true,
),
);

_initCompleter.complete();
}

Future<void> toggleNotificationsEnabled() async {
await _initCompleter.future;

Expand All @@ -41,9 +52,24 @@ class NotificationSettingsCubit extends Cubit<NotificationSettingsState> {
await _saveNotificationSettings();
}

Future<void> toogleShowNotificationIconEnabled() async {
await _initCompleter.future;

emit(
state.copyWith(
isShowNotificationsIconEnabled: !state.isShowNotificationsIconEnabled,
),
);
}

Future<void> _saveNotificationSettings() async {
await _initCompleter.future;

await getIt<KeyValueStorage>().set(
KVKeys.showNotificationIcon,
state.isShowNotificationsIconEnabled.toString(),
);

final result = await UserSettingsBackendService()
.setNotificationSettings(_notificationSettings);
result.fold(
Expand All @@ -59,8 +85,12 @@ class NotificationSettingsState with _$NotificationSettingsState {

const factory NotificationSettingsState({
required bool isNotificationsEnabled,
required bool isShowNotificationsIconEnabled,
}) = _NotificationSettingsState;

factory NotificationSettingsState.initial() =>
const NotificationSettingsState(isNotificationsEnabled: true);
const NotificationSettingsState(
isNotificationsEnabled: true,
isShowNotificationsIconEnabled: true,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:appflowy/generated/locale_keys.g.dart';
import 'package:appflowy/startup/startup.dart';
import 'package:appflowy/user/application/reminder/reminder_bloc.dart';
import 'package:appflowy/workspace/application/menu/sidebar_sections_bloc.dart';
import 'package:appflowy/workspace/application/settings/notifications/notification_settings_cubit.dart';
import 'package:appflowy/workspace/presentation/notifications/notification_dialog.dart';
import 'package:appflowy_popover/appflowy_popover.dart';
import 'package:easy_localization/easy_localization.dart';
Expand All @@ -24,23 +25,32 @@ class NotificationButton extends StatelessWidget {

return BlocProvider<ReminderBloc>.value(
value: getIt<ReminderBloc>(),
child: BlocBuilder<ReminderBloc, ReminderState>(
builder: (context, state) => FlowyTooltip(
message: LocaleKeys.notificationHub_title.tr(),
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: AppFlowyPopover(
mutex: mutex,
direction: PopoverDirection.bottomWithLeftAligned,
constraints: const BoxConstraints(maxHeight: 500, maxWidth: 425),
windowPadding: EdgeInsets.zero,
margin: EdgeInsets.zero,
popupBuilder: (_) =>
NotificationDialog(views: views, mutex: mutex),
child: _buildNotificationIcon(context, state.hasUnreads),
),
),
),
child: BlocBuilder<NotificationSettingsCubit, NotificationSettingsState>(
builder: (notificationSettingsContext, notificationSettingsState) {
return BlocBuilder<ReminderBloc, ReminderState>(
builder: (context, state) => notificationSettingsState
.isShowNotificationsIconEnabled
? FlowyTooltip(
message: LocaleKeys.notificationHub_title.tr(),
child: MouseRegion(
cursor: SystemMouseCursors.click,
child: AppFlowyPopover(
mutex: mutex,
direction: PopoverDirection.bottomWithLeftAligned,
constraints:
const BoxConstraints(maxHeight: 500, maxWidth: 425),
windowPadding: EdgeInsets.zero,
margin: EdgeInsets.zero,
popupBuilder: (_) =>
NotificationDialog(views: views, mutex: mutex),
child:
_buildNotificationIcon(context, state.hasUnreads),
),
),
)
: const SizedBox.shrink(),
);
},
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ class SettingsNotificationsView extends StatelessWidget {
),
],
),
FlowySettingListTile(
label: LocaleKeys
.settings_notifications_showNotificationsIcon_label
.tr(),
hint: LocaleKeys.settings_notifications_showNotificationsIcon_hint
.tr(),
trailing: [
Switch(
value: state.isShowNotificationsIconEnabled,
splashRadius: 0,
activeColor: Theme.of(context).colorScheme.primary,
onChanged: (_) =>
context.read<NotificationSettingsCubit>()
.toogleShowNotificationIconEnabled(),
),
],
),
],
);
},
Expand Down
4 changes: 4 additions & 0 deletions frontend/resources/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,10 @@
"enableNotifications": {
"label": "Enable notifications",
"hint": "Turn off to stop local notifications from appearing."
},
"showNotificationsIcon": {
"label": "Show notifications icon",
"hint": "Turn off to hide notification icons in the app."
}
},
"appearance": {
Expand Down