diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..86d6e4f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "wetter", + "request": "launch", + "type": "dart" + }, + { + "name": "wetter (profile mode)", + "request": "launch", + "type": "dart", + "flutterMode": "profile" + }, + { + "name": "wetter (release mode)", + "request": "launch", + "type": "dart", + "flutterMode": "release" + } + ] +} \ No newline at end of file diff --git a/lib/components/settings_form.dart b/lib/components/settings_form.dart new file mode 100644 index 0000000..af77f3f --- /dev/null +++ b/lib/components/settings_form.dart @@ -0,0 +1,50 @@ +import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +class SettingsForm extends StatefulWidget { + const SettingsForm({Key? key}) : super(key: key); + + @override + SettingsFormState createState() => SettingsFormState(); +} + +class SettingsFormState extends State { + SharedPreferences? prefs; + int _daysToLoad = 7; + final _formKey = GlobalKey(); + + @override + void initState() { + super.initState(); + initializePreferences().whenComplete(() => setState(() { + _daysToLoad = prefs?.getInt("daysToLoad") ?? 7; + })); + } + + Future initializePreferences() async { + prefs = await SharedPreferences.getInstance(); + } + + @override + Widget build(BuildContext context) { + return Form( + key: _formKey, + child: Column( + children: [ + Slider( + value: _daysToLoad.toDouble(), + min: 1.0, + max: 14.0, + divisions: 14, + label: _daysToLoad.round().toString(), + onChanged: (double value) { + setState(() { + _daysToLoad = value.round(); + prefs?.setInt("daysToLoad", _daysToLoad); + }); + }, + ) + ], + )); + } +} diff --git a/lib/main.dart b/lib/main.dart index 40fee04..2ed6092 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:shared_preferences/shared_preferences.dart'; import 'package:wetter/components/compact_weather_data.dart'; import 'package:wetter/components/main_menu.dart'; import 'package:wetter/models/daily_weather_model.dart'; @@ -14,13 +15,23 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { + SharedPreferences? prefs; + int _daysToLoad = 7; late Future> dailyWeather; @override void initState() { super.initState(); - dailyWeather = BrightSkyAPI.fetchForecastForTimeRange( - DateTime.now(), DateTime.now().add(const Duration(days: 7))); + initializePreferences(); + dailyWeather = + BrightSkyAPI.fetchForecastForTimeRange(DateTime.now(), _daysToLoad); + } + + Future initializePreferences() async { + prefs = await SharedPreferences.getInstance(); + setState(() { + _daysToLoad = prefs!.getInt("daysToLoad") ?? 7; + }); } @override @@ -44,9 +55,9 @@ class _MyAppState extends State { onRefresh: () { return Future(() { setState(() { + _daysToLoad = prefs?.getInt("daysToLoad") ?? 7; dailyWeather = BrightSkyAPI.fetchForecastForTimeRange( - DateTime.now(), - DateTime.now().add(const Duration(days: 7))); + DateTime.now(), _daysToLoad); }); }); }, @@ -54,7 +65,8 @@ class _MyAppState extends State { physics: const AlwaysScrollableScrollPhysics(), child: Center( child: FutureBuilder>( - future: dailyWeather, + future: BrightSkyAPI.fetchForecastForTimeRange( + DateTime.now(), _daysToLoad), builder: (context, snapshot) { if (snapshot.hasData) { return Column( diff --git a/lib/screens/settings.dart b/lib/screens/settings.dart index 3aef8e2..707ede3 100644 --- a/lib/screens/settings.dart +++ b/lib/screens/settings.dart @@ -1,4 +1,5 @@ import 'package:flutter/material.dart'; +import 'package:wetter/components/settings_form.dart'; class Settings extends StatelessWidget { const Settings({super.key}); @@ -6,10 +7,12 @@ class Settings extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( - appBar: AppBar( - title: const Text('Settings'), - ), - body: const Center(child: Text("Settings here")), - ); + appBar: AppBar( + title: const Text('Settings'), + ), + body: const Padding( + padding: EdgeInsets.all(20), + child: Center(child: SettingsForm()), + )); } } diff --git a/lib/services/brightsky_api_service.dart b/lib/services/brightsky_api_service.dart index 7f79ad2..07344d4 100644 --- a/lib/services/brightsky_api_service.dart +++ b/lib/services/brightsky_api_service.dart @@ -17,7 +17,8 @@ class BrightSkyAPI { } static Future> fetchForecastForTimeRange( - DateTime from, DateTime until) async { + DateTime from, int noOfDays) async { + DateTime until = from.add(Duration(days: noOfDays)); String fromDay = "${from.year}-${from.month}-${from.day}"; String untilDay = "${until.year}-${until.month}-${until.day}"; final response = await http.get( diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index cccf817..287b6a9 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,8 @@ import FlutterMacOS import Foundation +import shared_preferences_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) } diff --git a/pubspec.lock b/pubspec.lock index f0f9207..bf871bc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -57,6 +57,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.3.0" + ffi: + dependency: transitive + description: + name: ffi + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" + file: + dependency: transitive + description: + name: file + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.4" flutter: dependency: "direct main" description: flutter @@ -74,6 +88,11 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_web_plugins: + dependency: transitive + description: flutter + source: sdk + version: "0.0.0" http: dependency: "direct main" description: @@ -95,6 +114,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.17.0" + js: + dependency: transitive + description: + name: js + url: "https://pub.dartlang.org" + source: hosted + version: "0.6.4" lints: dependency: transitive description: @@ -130,6 +156,104 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.8.1" + path_provider_linux: + dependency: transitive + description: + name: path_provider_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.7" + path_provider_platform_interface: + dependency: transitive + description: + name: path_provider_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + path_provider_windows: + dependency: transitive + description: + name: path_provider_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.2" + platform: + dependency: transitive + description: + name: platform + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" + plugin_platform_interface: + dependency: transitive + description: + name: plugin_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.2" + process: + dependency: transitive + description: + name: process + url: "https://pub.dartlang.org" + source: hosted + version: "4.2.4" + shared_preferences: + dependency: "direct main" + description: + name: shared_preferences + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.15" + shared_preferences_android: + dependency: transitive + description: + name: shared_preferences_android + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.12" + shared_preferences_ios: + dependency: transitive + description: + name: shared_preferences_ios + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + shared_preferences_linux: + dependency: transitive + description: + name: shared_preferences_linux + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" + shared_preferences_macos: + dependency: transitive + description: + name: shared_preferences_macos + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + shared_preferences_platform_interface: + dependency: transitive + description: + name: shared_preferences_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + shared_preferences_web: + dependency: transitive + description: + name: shared_preferences_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.4" + shared_preferences_windows: + dependency: transitive + description: + name: shared_preferences_windows + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.1" sky_engine: dependency: transitive description: flutter @@ -198,5 +322,20 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.0.0" + win32: + dependency: transitive + description: + name: win32 + url: "https://pub.dartlang.org" + source: hosted + version: "2.7.0" + xdg_directories: + dependency: transitive + description: + name: xdg_directories + url: "https://pub.dartlang.org" + source: hosted + version: "0.2.0+2" sdks: dart: ">=2.17.6 <3.0.0" + flutter: ">=3.0.0" diff --git a/pubspec.yaml b/pubspec.yaml index 9742165..8b978a1 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -37,6 +37,7 @@ dependencies: http: ^0.13.5 weather_icons: ^3.0.0 intl: ^0.17.0 + shared_preferences: ^2.0.15 dev_dependencies: flutter_test: