Added simple injector

This commit is contained in:
vjrj 2018-06-29 08:44:06 +02:00
parent de340c0664
commit a9329e3824
8 changed files with 49 additions and 55 deletions

View file

@ -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<AppState, _ViewModel>(

View file

@ -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<GlobalFiresBottomStats> {
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<GlobalFiresBottomStats> {
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(() {

View file

@ -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<SharedPreferences> prefs = SharedPreferences.getInstance();
final GetIt getIt = new GetIt();
bool isDevelopment = false;

View file

@ -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<YourLocation> getUserLocation(
GlobalKey<ScaffoldState> scaffoldKey) async {
@ -47,7 +47,7 @@ Future<YourLocation> getUserLocation(
Future<String> 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}");

View file

@ -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<Map<String, dynamic>> loadSecrets() async {
return await SecretLoader(secretPath: 'assets/private-settings.json').load();
}
void mainCommon(List<Middleware<AppState>> otherMiddleware) {
globals.getIt.registerSingleton<FiresApi>(new FiresApi());
final injector = Injector.getInjector();
injector.map(FiresApi, (i) => new FiresApi(), isSingleton: true);
loadSecrets().then((secrets) {
final store = new Store<AppState>(appStateReducer,
@ -28,10 +31,9 @@ void mainCommon(List<Middleware<AppState>> 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);

View file

@ -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<YourLocation> openPlacesDialog(GlobalKey<ScaffoldState> 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)));

View file

@ -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>();
FiresApi api = Injector.getInjector().get<FiresApi>(FiresApi);
void fetchYourLocationsMiddleware(
Store<AppState> store, action, NextDispatcher next) {

View file

@ -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: