-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow to easily override setting entries
- Loading branch information
1 parent
038d3a5
commit 1b6fdf4
Showing
18 changed files
with
394 additions
and
236 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,10 @@ | ||
export 'account/account.dart'; | ||
export 'contact/contact.dart'; | ||
export 'localization/app_localizations.g.dart'; | ||
export 'localization/extension.dart'; | ||
export 'logger.dart'; | ||
export 'main.dart'; | ||
export 'routes/provider.dart'; | ||
export 'routes/routes.dart'; | ||
export 'settings/settings.dart'; | ||
export 'util/localized_dialog_helper.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export 'model.dart'; | ||
export 'provider.dart'; | ||
export 'screen.dart'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import 'package:flutter/widgets.dart'; | ||
|
||
/// Standard setting entries | ||
enum UiSettingsType { | ||
divider, | ||
security, | ||
accounts, | ||
swipe, | ||
signature, | ||
defaultSender, | ||
design, | ||
language, | ||
folders, | ||
readReceipts, | ||
reply, | ||
feedback, | ||
about, | ||
welcome, | ||
development, | ||
} | ||
|
||
/// A UI element for the settings screen | ||
class UiSettingsElement { | ||
/// Creates a new [UiSettingsElement] | ||
const UiSettingsElement({ | ||
required this.title, | ||
required this.onTap, | ||
this.type, | ||
this.subtitle, | ||
this.icon, | ||
}); | ||
|
||
/// Creates a new [UiSettingsElement] as a divider | ||
UiSettingsElement.divider() | ||
: this( | ||
title: '', | ||
onTap: null, | ||
type: UiSettingsType.divider, | ||
); | ||
|
||
/// The title of the element | ||
final String title; | ||
|
||
/// The standard type of the element | ||
final UiSettingsType? type; | ||
|
||
/// The subtitle of the element | ||
final String? subtitle; | ||
|
||
/// The icon of the element | ||
final IconData? icon; | ||
|
||
/// The action when the element is tapped | ||
final VoidCallback? onTap; | ||
|
||
/// Is this element a divider? | ||
bool get isDivider => type == UiSettingsType.divider; | ||
} | ||
|
||
/// Eases custom elements | ||
extension UiSettingsElementsExtension on List<UiSettingsElement> { | ||
/// Inserts an element after the element with the given type | ||
void insertAfter(UiSettingsType type, UiSettingsElement element) { | ||
final index = indexWhere((e) => e.type == type); | ||
if (index == -1) { | ||
add(element); | ||
} else { | ||
insert(index + 1, element); | ||
} | ||
} | ||
|
||
/// Inserts an element before the element with the given type | ||
void insertBefore(UiSettingsType type, UiSettingsElement element) { | ||
final index = indexWhere((e) => e.type == type); | ||
if (index == -1) { | ||
insert(0, element); | ||
} else { | ||
insert(index, element); | ||
} | ||
} | ||
|
||
/// Removes the element with the given type | ||
void removeType(UiSettingsType type) { | ||
removeWhere((e) => e.type == type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
|
||
import '../../localization/extension.dart'; | ||
import '../../routes/routes.dart'; | ||
import '../../util/localized_dialog_helper.dart'; | ||
import 'model.dart'; | ||
|
||
part 'provider.g.dart'; | ||
|
||
@Riverpod(keepAlive: true) | ||
class SettingsUiElements extends _$SettingsUiElements { | ||
@override | ||
void build() {} | ||
|
||
/// Generates the shown setting entries | ||
List<UiSettingsElement> generate( | ||
BuildContext context, | ||
) => | ||
buildStandardElements(context); | ||
|
||
static List<UiSettingsElement> buildStandardElements(BuildContext context) { | ||
final text = context.text; | ||
|
||
return [ | ||
UiSettingsElement( | ||
type: UiSettingsType.security, | ||
title: text.securitySettingsTitle, | ||
onTap: () => context.pushNamed(Routes.settingsSecurity), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.accounts, | ||
title: text.settingsActionAccounts, | ||
onTap: () => context.pushNamed(Routes.settingsAccounts), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.swipe, | ||
title: text.swipeSettingTitle, | ||
onTap: () => context.pushNamed(Routes.settingsSwipe), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.signature, | ||
title: text.signatureSettingsTitle, | ||
onTap: () => context.pushNamed(Routes.settingsSignature), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.defaultSender, | ||
title: text.defaultSenderSettingsTitle, | ||
onTap: () => context.pushNamed(Routes.settingsDefaultSender), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.design, | ||
title: text.settingsActionDesign, | ||
onTap: () => context.pushNamed(Routes.settingsDesign), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.language, | ||
title: text.languageSettingTitle, | ||
onTap: () => context.pushNamed(Routes.settingsLanguage), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.folders, | ||
title: text.settingsFolders, | ||
onTap: () => context.pushNamed(Routes.settingsFolders), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.readReceipts, | ||
title: text.settingsReadReceipts, | ||
onTap: () => context.pushNamed(Routes.settingsReadReceipts), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.reply, | ||
title: text.replySettingsTitle, | ||
onTap: () => context.pushNamed(Routes.settingsReplyFormat), | ||
), | ||
UiSettingsElement.divider(), | ||
UiSettingsElement( | ||
type: UiSettingsType.feedback, | ||
title: text.settingsActionFeedback, | ||
onTap: () => context.pushNamed(Routes.settingsFeedback), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.about, | ||
title: text.drawerEntryAbout, | ||
onTap: () => LocalizedDialogHelper.showAbout(context), | ||
), | ||
UiSettingsElement( | ||
type: UiSettingsType.welcome, | ||
title: text.settingsActionWelcome, | ||
onTap: () => context.pushNamed(Routes.welcome), | ||
), | ||
]; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'package:enough_platform_widgets/enough_platform_widgets.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:hooks_riverpod/hooks_riverpod.dart'; | ||
|
||
import '../../localization/extension.dart'; | ||
import '../../screens/base.dart'; | ||
import 'model.dart'; | ||
import 'provider.dart'; | ||
|
||
/// Allows to personalize the app settings | ||
class SettingsScreen extends ConsumerWidget { | ||
/// Creates a new [SettingsScreen] | ||
const SettingsScreen({super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context, WidgetRef ref) { | ||
final uiSettingElementsNotifier = ref.watch( | ||
settingsUiElementsProvider.notifier, | ||
); | ||
final settingEntries = uiSettingElementsNotifier.generate(context); | ||
final localizations = context.text; | ||
|
||
Widget buildEntry(UiSettingsElement entry) { | ||
if (entry.isDivider) return const Divider(); | ||
final subtitle = entry.subtitle; | ||
final icon = entry.icon; | ||
|
||
return PlatformListTile( | ||
title: Text(entry.title), | ||
subtitle: subtitle != null ? Text(subtitle) : null, | ||
leading: icon != null ? Icon(icon) : null, | ||
onTap: entry.onTap, | ||
); | ||
} | ||
|
||
return BasePage( | ||
title: localizations.settingsTitle, | ||
content: SingleChildScrollView( | ||
child: SafeArea( | ||
child: Padding( | ||
padding: const EdgeInsets.all(8), | ||
child: Column( | ||
crossAxisAlignment: CrossAxisAlignment.start, | ||
children: settingEntries.map(buildEntry).toList(), | ||
), | ||
), | ||
), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export 'home/home.dart'; | ||
export 'model.dart'; | ||
export 'provider.dart'; |
Oops, something went wrong.