From e96a33f0a350ff37ed4f3556e10d4f60001fdcc2 Mon Sep 17 00:00:00 2001 From: Konstantin Kollar Date: Mon, 5 Sep 2022 11:59:52 +0200 Subject: [PATCH] Add locations and save them, remove locations and delete them --- .../locations/manage_single_location.dart | 4 +- lib/components/main_menu.dart | 6 +- lib/main.dart | 75 +++++++++++-------- lib/screens/locations.dart | 6 +- lib/screens/main_weather_view.dart | 16 +--- lib/services/user_preferences.dart | 16 ++++ 6 files changed, 75 insertions(+), 48 deletions(-) diff --git a/lib/components/locations/manage_single_location.dart b/lib/components/locations/manage_single_location.dart index 31dfd2f..3a93f7c 100644 --- a/lib/components/locations/manage_single_location.dart +++ b/lib/components/locations/manage_single_location.dart @@ -15,7 +15,9 @@ class ManageSingleLocation extends StatelessWidget { child: Row(children: [ Text(name, style: Theme.of(context).textTheme.headline5), const Spacer(), - const Icon(Icons.delete_sharp) + GestureDetector( + onTap: () => delete(name), + child: const Icon(Icons.delete_sharp)) ]))); } } diff --git a/lib/components/main_menu.dart b/lib/components/main_menu.dart index 35272af..bb86692 100644 --- a/lib/components/main_menu.dart +++ b/lib/components/main_menu.dart @@ -28,8 +28,10 @@ class NavBar extends StatelessWidget { leading: const Icon(Icons.public), title: const Text("Locations"), onTap: (() { - Navigator.push(context, - MaterialPageRoute(builder: (context) => const Locations())); + Navigator.push( + context, + MaterialPageRoute(builder: (context) => const Locations()), + ); }), ) ], diff --git a/lib/main.dart b/lib/main.dart index 2e28628..17f245d 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -17,41 +17,52 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { + List _locations = []; + @override - Widget build(BuildContext context) { - return MaterialApp( - title: 'Wetter', - theme: ThemeData( - primarySwatch: Colors.blue, - textTheme: const TextTheme( - headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold), - headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic), - bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'), - ), - ), - home: DefaultTabController( - length: 3, - child: Scaffold( - appBar: AppBar( - title: const Text('Wetter'), - bottom: const TabBar( - tabs: [ - Tab(icon: Text("Hamburg")), - Tab(icon: Text("Frankfurt")), - Tab(icon: Text("Freiburg")), - ], - ), - ), - drawer: const NavBar(), - body: const TabBarView( - children: [ - MainWeatherView(53.46, 9.98), - MainWeatherView(50.15, 8.70), - MainWeatherView(47.99, 7.85) - ], - ), + void initState() { + _locations = UserPreferences.getSavedLocations(); + super.initState(); + } + + Widget buildContent() { + return DefaultTabController( + length: _locations.length, + child: Scaffold( + appBar: AppBar( + title: const Text('Wetter'), + bottom: (_locations.isEmpty) + ? null + : TabBar( + tabs: [for (String name in _locations) Tab(icon: Text(name))], + ), ), + drawer: const NavBar(), + body: (_locations.isEmpty) + ? const Text("No locations") + : TabBarView( + children: [ + for (String name in _locations) + MainWeatherView(UserPreferences.getLat(name), + UserPreferences.getLon(name)) + ], + ), ), ); } + + @override + Widget build(BuildContext context) { + return MaterialApp( + title: 'Wetter', + theme: ThemeData( + primarySwatch: Colors.blue, + textTheme: const TextTheme( + headline1: TextStyle(fontSize: 72.0, fontWeight: FontWeight.bold), + headline6: TextStyle(fontSize: 36.0, fontStyle: FontStyle.italic), + bodyText2: TextStyle(fontSize: 14.0, fontFamily: 'Hind'), + ), + ), + home: buildContent()); + } } diff --git a/lib/screens/locations.dart b/lib/screens/locations.dart index 2bb6561..1571398 100644 --- a/lib/screens/locations.dart +++ b/lib/screens/locations.dart @@ -24,7 +24,11 @@ class _LocationsState extends State { }); } - void deleteLocation(String name) {} + void deleteLocation(String name) { + setState(() { + locations = UserPreferences.deleteLocation(name); + }); + } @override Widget build(BuildContext context) { diff --git a/lib/screens/main_weather_view.dart b/lib/screens/main_weather_view.dart index e456bf5..547995f 100644 --- a/lib/screens/main_weather_view.dart +++ b/lib/screens/main_weather_view.dart @@ -1,5 +1,5 @@ import 'package:flutter/material.dart'; -import 'package:shared_preferences/shared_preferences.dart'; +import 'package:wetter/services/user_preferences.dart'; import '../components/compact_weather_data.dart'; import '../models/daily_weather_model.dart'; @@ -17,8 +17,7 @@ class MainWeatherView extends StatefulWidget { class _MainWeatherViewState extends State with AutomaticKeepAliveClientMixin { - SharedPreferences? prefs; - int _daysToLoad = 7; + int _daysToLoad = UserPreferences.preferences!.getInt("daysToLoad") ?? 7; late Future> dailyWeather; @override @@ -27,18 +26,10 @@ class _MainWeatherViewState extends State @override void initState() { super.initState(); - initializePreferences(); dailyWeather = BrightSkyAPI.fetchForecastForTimeRange( DateTime.now(), _daysToLoad, widget.lat, widget.lon); } - Future initializePreferences() async { - prefs = await SharedPreferences.getInstance(); - setState(() { - _daysToLoad = prefs!.getInt("daysToLoad") ?? 7; - }); - } - @override Widget build(BuildContext context) { super.build(context); @@ -46,7 +37,8 @@ class _MainWeatherViewState extends State onRefresh: () { return Future(() { setState(() { - _daysToLoad = prefs?.getInt("daysToLoad") ?? 7; + _daysToLoad = + UserPreferences.preferences!.getInt("daysToLoad") ?? 7; dailyWeather = BrightSkyAPI.fetchForecastForTimeRange( DateTime.now(), _daysToLoad, widget.lat, widget.lon); }); diff --git a/lib/services/user_preferences.dart b/lib/services/user_preferences.dart index 692e80e..eb0a61e 100644 --- a/lib/services/user_preferences.dart +++ b/lib/services/user_preferences.dart @@ -19,4 +19,20 @@ class UserPreferences { preferences!.setDouble("$name-lon", lon); return getSavedLocations(); } + + static List deleteLocation(String name) { + preferences!.remove("$name-lat"); + preferences!.remove("$name-lon"); + _savedLocations.remove(name); + preferences!.remove(name); + return getSavedLocations(); + } + + static getLat(String name) { + return preferences!.getDouble("$name-lat"); + } + + static getLon(String name) { + return preferences!.getDouble("$name-lon"); + } }