From 5cbd5e61a01f37734763966329a26104df0134bc Mon Sep 17 00:00:00 2001 From: Akbar Date: Wed, 6 Oct 2021 23:18:58 +0500 Subject: [PATCH] caching is added --- packages/weather/lib/src/weather_factory.dart | 37 +++++++++++++++++++ packages/weather/lib/weather.dart | 1 + packages/weather/pubspec.yaml | 1 + 3 files changed, 39 insertions(+) diff --git a/packages/weather/lib/src/weather_factory.dart b/packages/weather/lib/src/weather_factory.dart index a840c5623..d5498bd4b 100644 --- a/packages/weather/lib/src/weather_factory.dart +++ b/packages/weather/lib/src/weather_factory.dart @@ -1,5 +1,8 @@ part of weather_library; +const _weatherKey = "cached-weather-data"; +const _forecastKey = "cached-forecast-data"; + /// Plugin for fetching weather data in JSON. class WeatherFactory { String _apiKey; @@ -10,8 +13,11 @@ class WeatherFactory { late http.Client _httpClient; + final Completer _sharedPreferenceCompleter = Completer(); + WeatherFactory(this._apiKey, {this.language = Language.ENGLISH}) { _httpClient = http.Client(); + SharedPreferences.getInstance().then((value) => _sharedPreferenceCompleter.complete(value)); } /// Fetch current weather based on geographical coordinates @@ -48,6 +54,33 @@ class WeatherFactory { return forecasts; } + Future getCachedWeather() async { + final _prefs = await _sharedPreferenceCompleter.future; + final _restoredData = _prefs.getString(_weatherKey); + + if(_restoredData != null) { + Map? jsonBody = json.decode(_restoredData); + return Weather(jsonBody!); + } else { + throw OpenWeatherAPIException("No cached data for weather"); + } + } + + Future?> getCachedForecast() async { + final _prefs = await _sharedPreferenceCompleter.future; + final _restoredData = _prefs.getString(_forecastKey); + + if(_restoredData != null){ + Map? jsonBody = json.decode(_restoredData); + List forecasts = _parseForecast(jsonBody!); + return forecasts; + + } else { + throw OpenWeatherAPIException("No cached data for forecast"); + } + } + + Future?> _sendRequest(String tag, {double? lat, double? lon, String? cityName}) async { /// Build HTTP get url by passing the required parameters String url = _buildUrl(tag, cityName, lat, lon); @@ -58,6 +91,10 @@ class WeatherFactory { /// Perform error checking on response: /// Status code 200 means everything went well if (response.statusCode == STATUS_OK) { + + final _prefs = await _sharedPreferenceCompleter.future; + _prefs.setString(tag == FIVE_DAY_FORECAST ? _forecastKey : _weatherKey, response.body); + Map? jsonBody = json.decode(response.body); return jsonBody; } diff --git a/packages/weather/lib/weather.dart b/packages/weather/lib/weather.dart index 235b17dd0..cffe0dfc6 100644 --- a/packages/weather/lib/weather.dart +++ b/packages/weather/lib/weather.dart @@ -10,6 +10,7 @@ library weather_library; import 'dart:async'; import 'dart:convert'; import 'package:http/http.dart' as http; +import 'package:shared_preferences/shared_preferences.dart'; part 'package:weather/src/weather_domain.dart'; part 'package:weather/src/weather_factory.dart'; diff --git a/packages/weather/pubspec.yaml b/packages/weather/pubspec.yaml index cc739fc4e..3eb23f69d 100644 --- a/packages/weather/pubspec.yaml +++ b/packages/weather/pubspec.yaml @@ -10,6 +10,7 @@ dependencies: flutter: sdk: flutter http: ^0.13.1 + shared_preferences: ^2.0.8 dev_dependencies: test: ^1.6.1