diff --git a/lib/components/locations/add_location_form.dart b/lib/components/locations/add_location_form.dart index 8858972..fcbcef6 100644 --- a/lib/components/locations/add_location_form.dart +++ b/lib/components/locations/add_location_form.dart @@ -1,10 +1,11 @@ import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; class AddLocationsForm extends StatefulWidget { - final SharedPreferences prefs; - - const AddLocationsForm(this.prefs, {Key? key}) : super(key: key); + final Function saveNewLocation; + final List savedLocations; + const AddLocationsForm( + {Key? key, required this.saveNewLocation, required this.savedLocations}) + : super(key: key); @override AddLocationsFormState createState() => AddLocationsFormState(); @@ -16,12 +17,10 @@ class AddLocationsFormState extends State { String? newLocationName; double? newLatitude; double? newLongitude; - late List savedLocations; @override void initState() { super.initState(); - savedLocations = widget.prefs.getStringList('savedLocations') ?? []; } @override @@ -33,7 +32,7 @@ class AddLocationsFormState extends State { void _submitLocation() { if (_formKey.currentState!.validate()) { _formKey.currentState!.save(); - _saveLocationToPrefs(newLocationName!, newLatitude!, newLongitude!); + widget.saveNewLocation(newLocationName!, newLatitude!, newLongitude!); _formKey.currentState!.reset(); newLocationName = null; newLatitude = null; @@ -41,13 +40,6 @@ class AddLocationsFormState extends State { } } - void _saveLocationToPrefs(String name, double lat, double lon) { - savedLocations.add(name); - widget.prefs.setStringList('savedLocations', savedLocations); - widget.prefs.setDouble("$name-lat", lat); - widget.prefs.setDouble("$name-lon", lon); - } - @override Widget build(BuildContext context) { return Form( @@ -65,7 +57,7 @@ class AddLocationsFormState extends State { if (value == null || value.isEmpty) { return "Please enter a name for the new location"; } - if (savedLocations.contains(value)) { + if (widget.savedLocations.contains(value)) { return "Location name already in use"; } return null; diff --git a/lib/components/locations/manage_single_location.dart b/lib/components/locations/manage_single_location.dart new file mode 100644 index 0000000..31dfd2f --- /dev/null +++ b/lib/components/locations/manage_single_location.dart @@ -0,0 +1,21 @@ +import 'package:flutter/material.dart'; + +class ManageSingleLocation extends StatelessWidget { + final String name; + final Function delete; + const ManageSingleLocation( + {Key? key, required this.name, required this.delete}) + : super(key: key); + + @override + Widget build(BuildContext context) { + return Card( + child: Padding( + padding: const EdgeInsets.all(10), + child: Row(children: [ + Text(name, style: Theme.of(context).textTheme.headline5), + const Spacer(), + const Icon(Icons.delete_sharp) + ]))); + } +} diff --git a/lib/components/settings_form.dart b/lib/components/settings_form.dart index af77f3f..6670f81 100644 --- a/lib/components/settings_form.dart +++ b/lib/components/settings_form.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; +import 'package:wetter/services/user_preferences.dart'; class SettingsForm extends StatefulWidget { const SettingsForm({Key? key}) : super(key: key); @@ -9,20 +9,13 @@ class SettingsForm extends StatefulWidget { } class SettingsFormState extends State { - SharedPreferences? prefs; int _daysToLoad = 7; final _formKey = GlobalKey(); @override void initState() { + _daysToLoad = UserPreferences.preferences!.getInt("daysToLoad") ?? 7; super.initState(); - initializePreferences().whenComplete(() => setState(() { - _daysToLoad = prefs?.getInt("daysToLoad") ?? 7; - })); - } - - Future initializePreferences() async { - prefs = await SharedPreferences.getInstance(); } @override @@ -35,12 +28,13 @@ class SettingsFormState extends State { value: _daysToLoad.toDouble(), min: 1.0, max: 14.0, - divisions: 14, + divisions: 13, label: _daysToLoad.round().toString(), onChanged: (double value) { setState(() { _daysToLoad = value.round(); - prefs?.setInt("daysToLoad", _daysToLoad); + UserPreferences.preferences! + .setInt("daysToLoad", _daysToLoad); }); }, ) diff --git a/lib/main.dart b/lib/main.dart index f9c0d6b..2e28628 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,8 +1,13 @@ import 'package:flutter/material.dart'; import 'package:wetter/components/main_menu.dart'; import 'package:wetter/screens/main_weather_view.dart'; +import 'package:wetter/services/user_preferences.dart'; -void main() => runApp(const MyApp()); +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + await UserPreferences.init(); + runApp(const MyApp()); +} class MyApp extends StatefulWidget { const MyApp({super.key}); diff --git a/lib/screens/locations.dart b/lib/screens/locations.dart index 380821c..2bb6561 100644 --- a/lib/screens/locations.dart +++ b/lib/screens/locations.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; import 'package:wetter/components/locations/add_location_form.dart'; +import 'package:wetter/components/locations/manage_single_location.dart'; +import 'package:wetter/services/user_preferences.dart'; class Locations extends StatefulWidget { const Locations({super.key}); @@ -10,18 +11,21 @@ class Locations extends StatefulWidget { } class _LocationsState extends State { - late final SharedPreferences? prefs; - + List locations = []; @override void initState() { - SharedPreferences.getInstance().then((value) => prefs = value); + locations = UserPreferences.getSavedLocations(); super.initState(); } - Future initializePreferences() async { - prefs = await SharedPreferences.getInstance(); + void saveNewLocation(String name, double lat, double lon) { + setState(() { + locations = UserPreferences.saveNewLocation(name, lat, lon); + }); } + void deleteLocation(String name) {} + @override Widget build(BuildContext context) { return Scaffold( @@ -29,8 +33,20 @@ class _LocationsState extends State { title: const Text('Locations'), ), body: Padding( - padding: const EdgeInsets.all(20), - child: AddLocationsForm(prefs!), - )); + padding: const EdgeInsets.all(20), + child: Center( + child: Column( + mainAxisAlignment: MainAxisAlignment.spaceEvenly, + children: [ + for (String name in locations) + ManageSingleLocation(name: name, delete: deleteLocation), + ...[ + Expanded( + child: AddLocationsForm( + saveNewLocation: saveNewLocation, + savedLocations: locations), + ) + ], + ])))); } } diff --git a/lib/services/user_preferences.dart b/lib/services/user_preferences.dart new file mode 100644 index 0000000..692e80e --- /dev/null +++ b/lib/services/user_preferences.dart @@ -0,0 +1,22 @@ +import 'package:shared_preferences/shared_preferences.dart'; + +class UserPreferences { + static List _savedLocations = []; + static SharedPreferences? preferences; + static Future init() async { + preferences = await SharedPreferences.getInstance(); + _savedLocations = preferences!.getStringList('savedLocations') ?? []; + } + + static List getSavedLocations() { + return _savedLocations; + } + + static List saveNewLocation(String name, double lat, double lon) { + _savedLocations.add(name); + preferences!.setStringList('savedLocations', _savedLocations); + preferences!.setDouble("$name-lat", lat); + preferences!.setDouble("$name-lon", lon); + return getSavedLocations(); + } +}