From 4681c7395912fd8397956a2339d8949f96a8a30a Mon Sep 17 00:00:00 2001 From: AlexisDanlos <91090088+AlexisDanlos@users.noreply.github.com> Date: Fri, 15 Nov 2024 17:25:52 +0100 Subject: [PATCH] display settings in settings --- dialer/lib/features/settings/settings.dart | 8 + .../features/settings/sorting/sorting.dart | 153 ++++++++++++++++++ dialer/pubspec.yaml | 1 + 3 files changed, 162 insertions(+) create mode 100644 dialer/lib/features/settings/sorting/sorting.dart diff --git a/dialer/lib/features/settings/settings.dart b/dialer/lib/features/settings/settings.dart index bdc22f3..a35301a 100644 --- a/dialer/lib/features/settings/settings.dart +++ b/dialer/lib/features/settings/settings.dart @@ -4,6 +4,7 @@ import 'package:flutter/material.dart'; import 'package:dialer/features/settings/call/settingsCall.dart'; import 'package:dialer/features/settings/sim/settings_accounts.dart'; import 'package:dialer/features/settings/key/manage_keys_page.dart'; +import 'package:dialer/features/settings/sorting/sorting.dart'; class SettingsPage extends StatelessWidget { const SettingsPage({super.key}); @@ -28,6 +29,12 @@ class SettingsPage extends StatelessWidget { MaterialPageRoute(builder: (context) => const GestionDeClesPage()), ); break; + case 'Sorting settings': + Navigator.push( + context, + MaterialPageRoute(builder: (context) => const SortingSettingsPage()), + ); + break; // Add more cases for other settings pages default: // Handle default or unknown settings @@ -41,6 +48,7 @@ class SettingsPage extends StatelessWidget { 'Calling settings', 'Page des comptes téléphoniques', 'Gestion de clés', // Add the new option here + 'Sorting settings', ]; return Scaffold( diff --git a/dialer/lib/features/settings/sorting/sorting.dart b/dialer/lib/features/settings/sorting/sorting.dart new file mode 100644 index 0000000..dd91f26 --- /dev/null +++ b/dialer/lib/features/settings/sorting/sorting.dart @@ -0,0 +1,153 @@ +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +// store in shared preferences the sorting order of contacts + +class SortingSettings { + static const _key = 'sorting_order'; + + static Future setSortingOrder(SortingOrder order) async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setInt(_key, order.index); + } + + static Future getSortingOrder() async { + final prefs = await SharedPreferences.getInstance(); + final index = prefs.getInt(_key) ?? SortingOrder.ascending.index; + return SortingOrder.values[index]; + } + + static const _displayKey = 'contact_display'; + + static Future setContactDisplay(ContactDisplay display) async { + final prefs = await SharedPreferences.getInstance(); + await prefs.setInt(_displayKey, display.index); + } + + static Future getContactDisplay() async { + final prefs = await SharedPreferences.getInstance(); + final index = prefs.getInt(_displayKey) ?? ContactDisplay.nameFirst.index; + return ContactDisplay.values[index]; + } +} + +enum SortingOrder { ascending, descending } +enum ContactDisplay { nameFirst, firstNameFirst } + +class SortingSettingsPage extends StatefulWidget { + const SortingSettingsPage({Key? key}) : super(key: key); + + @override + _SortingSettingsPageState createState() => _SortingSettingsPageState(); +} + +class _SortingSettingsPageState extends State { + SortingOrder _sortingOrder = SortingOrder.ascending; + ContactDisplay _contactDisplay = ContactDisplay.nameFirst; + + @override + void initState() { + super.initState(); + _fetchSortingOrder(); + } + + Future _fetchSortingOrder() async { + final order = await SortingSettings.getSortingOrder(); + final display = await SortingSettings.getContactDisplay(); + setState(() { + _sortingOrder = order; + _contactDisplay = display; + }); + } + + void _onOrderSelected(SortingOrder? order) { + setState(() { + _sortingOrder = order!; + }); + } + + void _onDisplaySelected(ContactDisplay? display) { + setState(() { + _contactDisplay = display!; + }); + } + + Future _saveSortingOrder() async { + await SortingSettings.setSortingOrder(_sortingOrder); + await SortingSettings.setContactDisplay(_contactDisplay); + } + + @override + Widget build(BuildContext context) { + return Scaffold( + backgroundColor: Colors.black, + appBar: AppBar( + title: const Text('Affichage'), + ), + body: Column( + children: [ + // subtitle for the sorting order + const ListTile( + title: Text( + 'Ordre de tri', + style: TextStyle(color: Colors.white), + ), + ), + ListTile( + title: const Text('Croissant'), + leading: Radio( + value: SortingOrder.ascending, + groupValue: _sortingOrder, + onChanged: (SortingOrder? order) => _onOrderSelected(order), + ), + ), + ListTile( + title: const Text('Décroissant'), + leading: Radio( + value: SortingOrder.descending, + groupValue: _sortingOrder, + onChanged: (SortingOrder? order) => _onOrderSelected(order), + ), + ), + SizedBox(height: 16), + // subtitle for the contact display + const ListTile( + title: Text( + 'Affichage des contacts', + style: TextStyle(color: Colors.white), + ), + ), + ListTile( + title: const Text('Exemple 1'), + leading: Radio( + value: ContactDisplay.nameFirst, + groupValue: _contactDisplay, + onChanged: (ContactDisplay? display) => _onDisplaySelected(display), + ), + ), + ListTile( + title: const Text('Exemple 2'), + leading: Radio( + value: ContactDisplay.firstNameFirst, + groupValue: _contactDisplay, + onChanged: (ContactDisplay? order) => _onDisplaySelected(order), + ), + ), + const Spacer(), + Padding( + padding: const EdgeInsets.all(16), + child: ElevatedButton( + // call _saveSortingOrder then go back to previous screen + onPressed: () { + _saveSortingOrder(); + Navigator.of(context).pop(); + }, + child: const Text('Enregistrer'), + ), + ), + ], + ), + ); + } +} + diff --git a/dialer/pubspec.yaml b/dialer/pubspec.yaml index 6618f25..c340bc5 100644 --- a/dialer/pubspec.yaml +++ b/dialer/pubspec.yaml @@ -44,6 +44,7 @@ dependencies: file_picker: ^5.2.5 asn1lib: ^1.0.0 intl_utils: ^2.0.7 + shared_preferences: ^2.3.3 dev_dependencies: flutter_test: