Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

caching is added #426

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
37 changes: 37 additions & 0 deletions packages/weather/lib/src/weather_factory.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,8 +13,11 @@ class WeatherFactory {

late http.Client _httpClient;

final Completer<SharedPreferences> _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
Expand Down Expand Up @@ -48,6 +54,33 @@ class WeatherFactory {
return forecasts;
}

Future<Weather> getCachedWeather() async {
final _prefs = await _sharedPreferenceCompleter.future;
final _restoredData = _prefs.getString(_weatherKey);

if(_restoredData != null) {
Map<String, dynamic>? jsonBody = json.decode(_restoredData);
return Weather(jsonBody!);
} else {
throw OpenWeatherAPIException("No cached data for weather");
}
}

Future<List<Weather>?> getCachedForecast() async {
final _prefs = await _sharedPreferenceCompleter.future;
final _restoredData = _prefs.getString(_forecastKey);

if(_restoredData != null){
Map<String, dynamic>? jsonBody = json.decode(_restoredData);
List<Weather> forecasts = _parseForecast(jsonBody!);
return forecasts;

} else {
throw OpenWeatherAPIException("No cached data for forecast");
}
}


Future<Map<String, dynamic>?> _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);
Expand All @@ -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<String, dynamic>? jsonBody = json.decode(response.body);
return jsonBody;
}
Expand Down
1 change: 1 addition & 0 deletions packages/weather/lib/weather.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
1 change: 1 addition & 0 deletions packages/weather/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies:
flutter:
sdk: flutter
http: ^0.13.1
shared_preferences: ^2.0.8

dev_dependencies:
test: ^1.6.1
Expand Down