From a9329e38247797ba803203a60c77873a62876162 Mon Sep 17 00:00:00 2001 From: vjrj Date: Fri, 29 Jun 2018 08:44:06 +0200 Subject: [PATCH] Added simple injector --- lib/activeFires.dart | 57 +++++++++++++----------------- lib/globalFiresBottomStats.dart | 6 ++-- lib/globals.dart | 6 ---- lib/locationUtils.dart | 4 +-- lib/mainCommon.dart | 12 ++++--- lib/placesAutocompleteUtils.dart | 7 ++-- lib/redux/fetchDataMiddleware.dart | 4 +-- pubspec.yaml | 8 +++-- 8 files changed, 49 insertions(+), 55 deletions(-) diff --git a/lib/activeFires.dart b/lib/activeFires.dart index 020df11..01928df 100644 --- a/lib/activeFires.dart +++ b/lib/activeFires.dart @@ -37,11 +37,10 @@ class _ViewModel { identical(this, other) || other is _ViewModel && runtimeType == other.runtimeType && - yourLocations == other.yourLocations ; + yourLocations == other.yourLocations; @override - int get hashCode => - yourLocations.hashCode; + int get hashCode => yourLocations.hashCode; } class ActiveFiresPage extends StatelessWidget { @@ -104,38 +103,32 @@ class ActiveFiresPage extends StatelessWidget { padding: const EdgeInsets.all(16.0), itemCount: yl.length, itemBuilder: (BuildContext _context, int i) { - return _buildItem( - context, yl.elementAt(i), onDeleted, onToggle, onTap); + final ThemeData theme = Theme.of(context); + return new Dismissible( + key: new ObjectKey(yl.elementAt(i)), + direction: DismissDirection.horizontal, + onDismissed: (DismissDirection direction) { + onDeleted(yl.elementAt(i)); + }, + background: new Container( + color: theme.primaryColor, + child: const ListTile( + leading: const Icon(Icons.delete, + color: Colors.white, size: 36.0))), + secondaryBackground: new Container( + color: theme.primaryColor, + child: const ListTile( + trailing: const Icon(Icons.delete, + color: Colors.white, size: 36.0))), + child: new Container( + decoration: new BoxDecoration( + color: theme.canvasColor, + border: new Border( + bottom: new BorderSide(color: theme.dividerColor))), + child: _buildRow(context, yl.elementAt(i), onToggle, onTap))); }); } - Widget _buildItem( - BuildContext context, YourLocation item, onDelete, onToggle, onTap) { - final ThemeData theme = Theme.of(context); - return new Dismissible( - key: new ObjectKey(item), - direction: DismissDirection.horizontal, - onDismissed: (DismissDirection direction) { - onDelete(item); - }, - background: new Container( - color: theme.primaryColor, - child: const ListTile( - leading: - const Icon(Icons.delete, color: Colors.white, size: 36.0))), - secondaryBackground: new Container( - color: theme.primaryColor, - child: const ListTile( - trailing: - const Icon(Icons.delete, color: Colors.white, size: 36.0))), - child: new Container( - decoration: new BoxDecoration( - color: theme.canvasColor, - border: new Border( - bottom: new BorderSide(color: theme.dividerColor))), - child: _buildRow(context, item, onToggle, onTap))); - } - @override Widget build(BuildContext context) { return new StoreConnector( diff --git a/lib/globalFiresBottomStats.dart b/lib/globalFiresBottomStats.dart index 1c775d3..fbed8dd 100644 --- a/lib/globalFiresBottomStats.dart +++ b/lib/globalFiresBottomStats.dart @@ -7,6 +7,7 @@ import 'customBottomAppBar.dart'; import 'colors.dart'; import 'generated/i18n.dart'; import 'customMoment.dart'; +import 'package:flutter_simple_dependency_injection/injector.dart'; class GlobalFiresBottomStats extends StatefulWidget { @override @@ -16,11 +17,12 @@ class GlobalFiresBottomStats extends StatefulWidget { class _GlobalFiresBottomStatsState extends State { String lastCheck; int activeFires = 0; + final firesApiUrl = Injector.getInjector().get(String, "firesApiUrl"); @override void initState() { super.initState(); - http.read('${globals.firesApiUrl}status/last-fire-check').then((result) { + http.read('${firesApiUrl}status/last-fire-check').then((result) { try { var now = Moment.now(); var last = DateTime.parse(json.decode(result)['value']); @@ -31,7 +33,7 @@ class _GlobalFiresBottomStatsState extends State { print('Cannot get the last fire check'); } }); - http.read('${globals.firesApiUrl}status/active-fires-count').then((result) { + http.read('${firesApiUrl}status/active-fires-count').then((result) { try { int count = json.decode(result)['total']; setState(() { diff --git a/lib/globals.dart b/lib/globals.dart index 2ce4a4c..7a63559 100644 --- a/lib/globals.dart +++ b/lib/globals.dart @@ -1,14 +1,9 @@ library fires.globals; import 'package:flutter/material.dart'; -import 'package:get_it/get_it.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'dart:async'; -// FIXME remove this later -String gmapKey; -String firesApiKey; -String firesApiUrl; final String appVersion = '0.0.1'; final Widget appMediumIcon = @@ -16,5 +11,4 @@ final Widget appMediumIcon = final Widget appIcon = Image.asset('images/logo-200.png', width: 24.0, height: 24.0); final Future prefs = SharedPreferences.getInstance(); -final GetIt getIt = new GetIt(); bool isDevelopment = false; diff --git a/lib/locationUtils.dart b/lib/locationUtils.dart index 14e3a54..fe6eaee 100644 --- a/lib/locationUtils.dart +++ b/lib/locationUtils.dart @@ -4,8 +4,8 @@ import 'dart:async'; import 'package:fires_flutter/models/yourLocation.dart'; import 'package:flutter/material.dart'; import 'package:geocoder/geocoder.dart'; -import 'globals.dart' as globals; import 'generated/i18n.dart'; +import 'package:flutter_simple_dependency_injection/injector.dart'; Future getUserLocation( GlobalKey scaffoldKey) async { @@ -47,7 +47,7 @@ Future getUserLocation( Future getReverseLocation(YourLocation loc, [bool external = false]) async { final coordinates = new Coordinates(loc.lat, loc.lon); - var geoCoder = external ? Geocoder.google(globals.gmapKey) : Geocoder.local; + var geoCoder = external ? Geocoder.google(Injector.getInjector().get(String, "gmapKey")) : Geocoder.local; var addresses = await geoCoder.findAddressesFromCoordinates(coordinates); var first = addresses.first; print("${first.featureName} : ${first.addressLine}"); diff --git a/lib/mainCommon.dart b/lib/mainCommon.dart index a629ef8..a52bdad 100644 --- a/lib/mainCommon.dart +++ b/lib/mainCommon.dart @@ -12,13 +12,16 @@ import 'models/firesApi.dart'; import 'redux/fetchDataMiddleware.dart'; import 'redux/reducers.dart'; import 'package:fires_flutter/models/yourLocationPersist.dart'; +import 'package:flutter_simple_dependency_injection/injector.dart'; Future> loadSecrets() async { return await SecretLoader(secretPath: 'assets/private-settings.json').load(); } void mainCommon(List> otherMiddleware) { - globals.getIt.registerSingleton(new FiresApi()); + + final injector = Injector.getInjector(); + injector.map(FiresApi, (i) => new FiresApi(), isSingleton: true); loadSecrets().then((secrets) { final store = new Store(appStateReducer, @@ -28,10 +31,9 @@ void mainCommon(List> otherMiddleware) { firesApiUrl: secrets['firesApiUrl'] + "api/v1/"), middleware: List.from(otherMiddleware)..add(fetchYourLocationsMiddleware)); - // FIXME remove this later - globals.firesApiKey = store.state.firesApiKey; - globals.firesApiUrl = store.state.firesApiUrl; - globals.gmapKey = store.state.gmapKey; + injector.map(String, (i) => store.state.firesApiUrl, key: "firesApiUrl"); + injector.map(String, (i) => store.state.firesApiKey, key: "firesApiKey"); + injector.map(String, (i) => store.state.gmapKey, key: "gmapKey"); globals.prefs.then((prefs) { loadYourLocationsWithPrefs(prefs); diff --git a/lib/placesAutocompleteUtils.dart b/lib/placesAutocompleteUtils.dart index bf66e52..1aff869 100644 --- a/lib/placesAutocompleteUtils.dart +++ b/lib/placesAutocompleteUtils.dart @@ -5,15 +5,16 @@ import 'package:flutter_google_places_autocomplete/flutter_google_places_autocom import 'package:fires_flutter/models/yourLocation.dart'; import 'generated/i18n.dart'; -import 'globals.dart' as globals; +import 'package:flutter_simple_dependency_injection/injector.dart'; Future openPlacesDialog(GlobalKey sc) async { Mode _mode = Mode.overlay; - GoogleMapsPlaces _places = new GoogleMapsPlaces(globals.gmapKey); + String gmapKey = Injector.getInjector().get(String, "gmapKey"); + GoogleMapsPlaces _places = new GoogleMapsPlaces(gmapKey); Prediction p = await showGooglePlacesAutocomplete( context: sc.currentContext, hint: S.of(sc.currentContext).typeTheNameOfAPlace, - apiKey: globals.gmapKey, + apiKey: gmapKey, onError: (res) { sc.currentState .showSnackBar(new SnackBar(content: new Text(res.errorMessage))); diff --git a/lib/redux/fetchDataMiddleware.dart b/lib/redux/fetchDataMiddleware.dart index 108d73c..32b0eb9 100644 --- a/lib/redux/fetchDataMiddleware.dart +++ b/lib/redux/fetchDataMiddleware.dart @@ -1,9 +1,9 @@ import 'package:bson_objectid/bson_objectid.dart'; import 'package:fires_flutter/models/yourLocation.dart'; +import 'package:flutter_simple_dependency_injection/injector.dart'; import 'package:just_debounce_it/just_debounce_it.dart'; import 'package:redux/redux.dart'; -import '../globals.dart' as globals; import '../models/appState.dart'; import '../models/firesApi.dart'; import '../models/yourLocationPersist.dart'; @@ -18,7 +18,7 @@ import 'actions.dart'; // Middleware do not return any values themselves. They simply forward // actions on to the Reducer or swallow actions in some special cases. -FiresApi api = globals.getIt.get(); +FiresApi api = Injector.getInjector().get(FiresApi); void fetchYourLocationsMiddleware( Store store, action, NextDispatcher next) { diff --git a/pubspec.yaml b/pubspec.yaml index 7c16418..6c57ce3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -14,10 +14,12 @@ dependencies: # utils simple_moment: "^0.0.3" just_debounce_it: "^1.0.4" + flutter_simple_dependency_injection: "^0.0.2" # net http: "^0.11.3+16" jaguar_resty: "^2.5.5" + connectivity: "^0.3.1" # data json_annotation: ^0.2.3 @@ -34,7 +36,7 @@ dependencies: redux_logging: "^0.3.0" # maps, geo, etc - flutter_map: "^0.0.1" + flutter_map: "^0.0.10" flutter_google_places_autocomplete: "^0.0.4" location: "^1.3.4" geocoder: "^0.0.1" @@ -53,11 +55,11 @@ dependencies: # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: "^0.1.2" # not using yet - get_it: "^1.0.1+1" + # community_material_icon: "^0.1.2" # url_launcher: "^3.0.2" # share: "^0.5.2" - # connectivity: "^0.3.1" + # package_info: "^0.3.2" dev_dependencies: