Skip to content

Commit

Permalink
Migrate to null-safety. (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
CaramelDunes authored Aug 26, 2021
1 parent 2a91985 commit 5322d40
Show file tree
Hide file tree
Showing 19 changed files with 540 additions and 342 deletions.
15 changes: 3 additions & 12 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

<application
android:name="io.flutter.app.FlutterApplication"
android:label="No Fuss PPG"
android:label="VFR Light"
android:icon="@mipmap/launcher_icon">
<activity
android:name=".MainActivity"
Expand All @@ -25,15 +27,6 @@
android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme"
/>
<!-- Displays an Android View that continues showing the launch screen
Drawable until Flutter paints its first frame, then this splash
screen fades out. A splash screen is useful to avoid any visual
gap between the end of Android's launch screen and the painting of
Flutter's first frame. -->
<meta-data
android:name="io.flutter.embedding.android.SplashScreenDrawable"
android:resource="@drawable/launch_background"
/>
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
Expand All @@ -44,7 +37,5 @@
<meta-data
android:name="flutterEmbedding"
android:value="2"/>

<service android:name="changjoopark.com.flutter_foreground_plugin.FlutterForegroundService"/>
</application>
</manifest>
2 changes: 1 addition & 1 deletion android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.2-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.3-all.zip
Empty file.
2 changes: 1 addition & 1 deletion bin/download_oaci_vfr.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Future<bool> downloadTile(http.Client client, int x, int y, int zoom) async {
String url =
'https://wxs.ign.fr/an7nvfzojv5wa96dsga5nk8w/geoportail/wmts?layer=GEOGRAPHICALGRIDSYSTEMS.MAPS.SCAN-OACI&style=normal&tilematrixset=PM&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/jpeg&TileMatrix=$zoom&TileCol=$y&TileRow=$x';

var response = await client.get(url);
var response = await client.get(Uri.parse(url));

if (response.statusCode == 200) {
print('Downloaded $zoom/$x-$y.');
Expand Down
9 changes: 4 additions & 5 deletions lib/flight_recorder.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import 'dart:io';
import 'package:latlong/latlong.dart';
import 'package:latlong2/latlong.dart';

import 'package:vfr_light/instruments_data_source.dart';

class FlightRecorder {
IOSink _outputSink;

LatLng _lastPosition;
LatLng? _lastPosition;

FlightRecorder(File outputFile) {
_outputSink = outputFile.openWrite();
FlightRecorder(File outputFile) : _outputSink = outputFile.openWrite() {
_outputSink.writeln(
'longitude,latitude,altitudeInM,heightInM,speedInKmH,datetime');
}
Expand All @@ -18,7 +17,7 @@ class FlightRecorder {
if (_lastPosition == null || _lastPosition != data.position) {
_lastPosition = data.position;
_outputSink.writeln(
'${data.position.longitude},${data.position.latitude},${data.altitudeInM.round()},${data.heightInM.round()},${data.speedInKmH.round()},${data.time.toIso8601String()}');
'${data.position.longitude},${data.position.latitude},${data.altitudeInM?.round() ?? ''},${data.heightInM?.round() ?? ''},${data.speedInKmH.round()},${data.time.toIso8601String()}');
}
}

Expand Down
29 changes: 8 additions & 21 deletions lib/instruments.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Instruments extends StatelessWidget {

final InstrumentsData data;

const Instruments({Key key, @required this.data}) : super(key: key);
const Instruments({Key? key, required this.data}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand All @@ -25,17 +25,14 @@ class Instruments extends StatelessWidget {
style: Theme.of(context).textTheme.subtitle2),
Text(
data.heightInM != null
? StandardAtmosphere.feetFromMeters(data.heightInM)
? StandardAtmosphere.feetFromMeters(data.heightInM!)
.round()
.toString()
: '???',
style: commonStyle),
Text('Height (m)',
style: Theme.of(context).textTheme.subtitle2),
Text(
data.heightInM != null
? data.heightInM.round().toString()
: '???',
Text(data.heightInM?.round().toString() ?? '???',
style: commonStyle),
],
),
Expand All @@ -46,17 +43,15 @@ class Instruments extends StatelessWidget {
style: Theme.of(context).textTheme.subtitle2),
Text(
data.altitudeInM != null
? StandardAtmosphere.feetFromMeters(data.altitudeInM)
? StandardAtmosphere.feetFromMeters(data.altitudeInM!)
.round()
.toString()
: '???',
style: commonStyle),
Text('Altitude (m)',
style: Theme.of(context).textTheme.subtitle2),
Text(
data.altitudeInM != null
? data.altitudeInM.round().toString()
: '???',
Text(data.altitudeInM?.round().toString() ?? '???',
// FIXME only one ?.?
style: commonStyle),
],
),
Expand All @@ -65,18 +60,10 @@ class Instruments extends StatelessWidget {
children: [
Text('Heading (°)',
style: Theme.of(context).textTheme.subtitle2),
Text(
data.headingInDeg != null
? data.headingInDeg.round().toString()
: '???',
style: commonStyle),
Text(data.headingInDeg.round().toString(), style: commonStyle),
Text('Speed (km/h)',
style: Theme.of(context).textTheme.subtitle2),
Text(
data.speedInKmH != null
? data.speedInKmH.round().toString()
: '???',
style: commonStyle),
Text(data.speedInKmH.round().toString(), style: commonStyle),
],
),
],
Expand Down
85 changes: 46 additions & 39 deletions lib/instruments_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
import 'dart:async';

import 'package:enviro_sensors/enviro_sensors.dart';
import 'package:environment_sensors/environment_sensors.dart';
import 'package:geolocator/geolocator.dart';
import 'package:meta/meta.dart';
import 'standard_atmosphere.dart';
import 'package:latlong/latlong.dart';
import 'package:latlong2/latlong.dart';

class InstrumentsData {
final LatLng position;
final double speedInKmH;
final double altitudeInM;
final double heightInM;
final double headingInDeg;
final DateTime time;
final double speedInKmH;
final double? altitudeInM;
final double? heightInM;

InstrumentsData(
{@required this.position,
@required this.speedInKmH,
@required this.altitudeInM,
@required this.heightInM,
@required this.headingInDeg,
@required this.time});
{required this.position,
required this.headingInDeg,
required this.time,
required this.speedInKmH,
required this.altitudeInM,
required this.heightInM});
}

class InstrumentsDataSource {
Expand All @@ -31,66 +30,74 @@ class InstrumentsDataSource {
final StreamController<InstrumentsData> _instrumentsDataStream =
StreamController.broadcast();

StreamSubscription _pressureSubscription;
StreamSubscription _positionSubscription;
late StreamSubscription _pressureSubscription;
late StreamSubscription _positionSubscription;

LatLng _lastPosition;
double _lastSpeedInKmH;
double _lastAltitudeInM;
double _lastHeightInM;
double _lastHeadingInDeg;
LatLng? _lastPosition;
double? _lastSpeedInKmH;
double? _lastAltitudeInM;
double? _lastHeightInM;
double? _lastHeadingInDeg;
double? _lastPressure;

InstrumentsDataSource(
{@required this.qnh, @required this.surfaceAltitudeInM}) {
InstrumentsDataSource({required this.qnh, required this.surfaceAltitudeInM}) {
_positionSubscription =
getPositionStream(desiredAccuracy: LocationAccuracy.high)
Geolocator.getPositionStream(desiredAccuracy: LocationAccuracy.high)
.listen(_onNewPosition);

_pressureSubscription =
barometerEvents.asBroadcastStream().listen(_onNewPressure);
_pressureSubscription = EnvironmentSensors()
.pressure
.asBroadcastStream()
.listen(_onNewPressure);
}

void dispose() {
_pressureSubscription.cancel();
_positionSubscription.cancel();
}

Future<void> recalibrateAtSfc({@required double surfaceAltitudeInM}) async {
Future<void> recalibrateAtSfc({required double surfaceAltitudeInM}) async {
this.surfaceAltitudeInM = surfaceAltitudeInM;

qnh = StandardAtmosphere.qnh(
pressure: (await barometerEvents.asBroadcastStream().first).reading,
altitude: surfaceAltitudeInM);
if (_lastPressure != null) {
qnh = StandardAtmosphere.qnh(
pressure: _lastPressure!, altitude: surfaceAltitudeInM);
}
}

Stream<InstrumentsData> get data => _instrumentsDataStream.stream;

LatLng get lastPosition => _lastPosition;
LatLng? get lastPosition => _lastPosition;

void _sendData() {
_instrumentsDataStream.add(InstrumentsData(
position: _lastPosition,
speedInKmH: _lastSpeedInKmH,
position: _lastPosition!,
speedInKmH: _lastSpeedInKmH!,
headingInDeg: _lastHeadingInDeg!,
time: DateTime.now(),
altitudeInM: _lastAltitudeInM,
heightInM: _lastHeightInM,
headingInDeg: _lastHeadingInDeg,
time: DateTime.now()));
heightInM: _lastHeightInM));
}

void _onNewPosition(Position position) {
this._lastPosition = LatLng(position.latitude, position.longitude);
_lastPosition = LatLng(position.latitude, position.longitude);
_lastSpeedInKmH = position.speed * 3.6;
_lastHeadingInDeg = position.heading;

_sendData();
}

void _onNewPressure(BarometerEvent event) {
void _onNewPressure(double pressure) {
_lastAltitudeInM =
StandardAtmosphere.altitude(qnh: qnh, pressure: event.reading);
StandardAtmosphere.altitude(qnh: qnh, pressure: pressure);

_lastHeightInM = _lastAltitudeInM - surfaceAltitudeInM;
_lastHeightInM = _lastAltitudeInM! - surfaceAltitudeInM;
_lastPressure = pressure;

_sendData();
if (_lastPosition != null &&
_lastSpeedInKmH != null &&
_lastHeadingInDeg != null) {
_sendData();
}
}
}
Loading

0 comments on commit 5322d40

Please sign in to comment.