Skip to content

Commit

Permalink
better procedure load
Browse files Browse the repository at this point in the history
  • Loading branch information
apps4av committed Sep 30, 2024
1 parent 17b7b52 commit ea3a1ce
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 55 deletions.
7 changes: 3 additions & 4 deletions lib/data/main_database_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class MainDatabaseHelper {
String name = "$lid.$id${transition.isEmpty ? '' : '.'}$transition"; // transition is optional
return Destination(locationID: name,
facilityName: name,
type: Destination.typeAirway,
type: Destination.typeProcedure,
coordinate: da.coordinate);
});
ret.addAll(d);
Expand Down Expand Up @@ -333,7 +333,7 @@ class MainDatabaseHelper {
}

// procedure is same as airway as its a bunch of points
Future<AirwayDestination?> findProcedure(String procedureName) async {
Future<ProcedureDestination?> findProcedure(String procedureName) async {
List<Map<String, dynamic>> maps1 = [];
List<Map<String, dynamic>> maps2 = [];
List<String> segments = procedureName.split(".");
Expand Down Expand Up @@ -385,13 +385,12 @@ class MainDatabaseHelper {
}
Map<String, dynamic> m2 = Map.from(m);
// name is required so is lat/lon
m2["name"] = d.locationID;
m2["Latitude"] = d.coordinate.latitude;
m2["Longitude"] = d.coordinate.longitude;
mapsCombined.add(m2);
lastId = id;
}
return AirwayDestination.fromMap(procedureName, mapsCombined);
return ProcedureDestination.fromMap(procedureName, mapsCombined);
}

}
Expand Down
50 changes: 46 additions & 4 deletions lib/destination/destination.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,16 @@ class Destination {

static const String typeGps = "GPS";
static const String typeAirway = "Airway";
static const String typeProcedure = "Procedure";

static bool isAirway(String type) {
return type == typeAirway;
}

static bool isProcedure(String type) {
return type == typeProcedure;
}

static bool isNav(String type) {
return type == "TACAN" ||
type == "NDB/DME" ||
Expand Down Expand Up @@ -312,6 +317,40 @@ class AirwayDestination extends Destination {
}
}


class ProcedureDestination extends Destination {

List<Destination> points; // this has many waypoints

ProcedureDestination({
required super.locationID,
required super.type,
required super.facilityName,
required super.coordinate,
required this.points
});

factory ProcedureDestination.fromMap(String name, List<Map<String, dynamic>> maps) {

// airway has multiple entries for sequences
List<Destination> ret = List.generate(maps.length, (i) {
return Destination(
locationID: (maps[i]['fix_identifier'] as String).trim(),
facilityName: (maps[i]['fix_identifier'] as String).trim(),
type: Destination.typeProcedure,
coordinate: LatLng(maps[i]['Latitude'] as double, maps[i]['Longitude'] as double),
);
});

return ProcedureDestination(
locationID: name,
type: ret[0].type,
facilityName: name,
coordinate: ret[0].coordinate,
points: ret);
}
}

class DestinationFactory {

// make a destination from a generic destination through db query.
Expand All @@ -332,14 +371,14 @@ class DestinationFactory {
FixDestination? destination = await MainDatabaseHelper.db.findFix(d.locationID);
ret = destination ?? d;
}
else if (Destination.isAirway(type) && d.locationID.contains(".")) {
AirwayDestination? destination = await MainDatabaseHelper.db.findProcedure(d.locationID);
ret = destination ?? d;
}
else if (Destination.isAirway(type)) {
AirwayDestination? destination = await MainDatabaseHelper.db.findAirway(d.locationID);
ret = destination ?? d;
}
else if (Destination.isProcedure(type)) {
ProcedureDestination? destination = await MainDatabaseHelper.db.findProcedure(d.locationID);
ret = destination ?? d;
}
else if (Destination.isGps(type)) {
ret = GpsDestination(locationID: d.locationID, type: d.type, facilityName: d.facilityName, coordinate: d.coordinate);
}
Expand All @@ -363,6 +402,9 @@ class DestinationFactory {
else if(Destination.isAirway(type)) {
return Icon(MdiIcons.rayStartVertexEnd, color: color);
}
else if(Destination.isProcedure(type)) {
return Icon(MdiIcons.mapMarkerPath, color: color);
}
return Icon(MdiIcons.help, color: color);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/instrument_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class InstrumentListState extends State<InstrumentList> {
if(Destination.typeGps == d.type) {
_destination = _truncate(d.facilityName);
}
else if(Destination.typeAirway == d.type && d.secondaryName != null) {
else if((Destination.isAirway(d.type) || (Destination.isProcedure(d.type))) && d.secondaryName != null) {
_destination = _truncate(d.secondaryName!);
}
else {
Expand Down
3 changes: 3 additions & 0 deletions lib/longpress_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ class LongPressFuture {
else if(showDestination is AirwayDestination) {
pages.add(const Text(Destination.typeAirway));
}
else if(showDestination is ProcedureDestination) {
pages.add(const Text(Destination.typeProcedure));
}
// SUA for every press
saa = await MainDatabaseHelper.db.getSaa(destination.coordinate);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/plan/plan_item_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class PlanItemWidgetState extends State<PlanItemWidget> {
Widget build(BuildContext context) {

// different tiles for airways
return Destination.isAirway(widget.waypoint.destination.type) ?
return Destination.isAirway(widget.waypoint.destination.type) || Destination.isProcedure(widget.waypoint.destination.type) ?

Column(children: [
ExpansionTile(
leading: DestinationFactory.getIcon(widget.waypoint.destination.type, widget.waypoint.airwayDestinationsOnRoute.isEmpty ? Colors.red : Theme.of(context).colorScheme.primary),
leading: DestinationFactory.getIcon(widget.waypoint.destination.type, widget.waypoint.destinationsOnRoute.isEmpty ? Colors.red : Theme.of(context).colorScheme.primary),
title: Text(widget.waypoint.destination.locationID, style: TextStyle(color: widget.current ? Constants.planCurrentColor : Theme.of(context).colorScheme.primary),),
subtitle: Text(widget.waypoint.destination.secondaryName != null ? widget.waypoint.destination.secondaryName! : widget.waypoint.destination.locationID),
children: <Widget>[
Expand Down Expand Up @@ -56,17 +56,17 @@ class PlanItemWidgetState extends State<PlanItemWidget> {
List<Widget> _buildExpandableContent() {
List<Widget> columnContent = [];

List<Destination> destinations = widget.waypoint.airwayDestinationsOnRoute;
List<Destination> destinations = widget.waypoint.destinationsOnRoute;

for (int index = 0; index < destinations.length; index++) {
columnContent.add(
ListTile(
title: Text(destinations[index].secondaryName != null ? destinations[index].secondaryName! : destinations[index].locationID, style : TextStyle(color : (destinations[index] == widget.waypoint.airwayDestinationsOnRoute[widget.waypoint.currentAirwayDestinationIndex] && widget.current) ? Constants.planCurrentColor : Theme.of(context).colorScheme.primary)),
title: Text(destinations[index].secondaryName != null ? destinations[index].secondaryName! : destinations[index].locationID, style : TextStyle(color : (destinations[index] == widget.waypoint.destinationsOnRoute[widget.waypoint.currentDestinationIndex] && widget.current) ? Constants.planCurrentColor : Theme.of(context).colorScheme.primary)),
subtitle: PlanLineWidget(destination: destinations[index],),
leading: DestinationFactory.getIcon(widget.waypoint.destination.type, Theme.of(context).colorScheme.primary),
onTap: () {
setState(() {
widget.waypoint.currentAirwayDestinationIndex = widget.waypoint.airwayDestinationsOnRoute.indexOf(destinations[index]);
widget.waypoint.currentDestinationIndex = widget.waypoint.destinationsOnRoute.indexOf(destinations[index]);
widget.onTap();// go to this point in airway
});
}
Expand Down
73 changes: 44 additions & 29 deletions lib/plan/plan_route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,30 +39,42 @@ class PlanRoute {
int get length => _waypoints.length;
bool get isNotEmpty => _waypoints.isNotEmpty;

void _procedureAdjust(Waypoint waypoint) {
waypoint.currentDestinationIndex = 0; // on change to airway, reset it
waypoint.destinationsOnRoute = [];
List<Destination> points = [];

// adjust airways, nothing to do when airway is not in the middle of points
int index = _waypoints.indexOf(waypoint);
ProcedureDestination procedure = _waypoints[index].destination as ProcedureDestination;

// procedure
for(Destination d in procedure.points) {
Destination n = Destination(locationID: procedure.locationID,
type: d.type,
facilityName: d.facilityName,
coordinate: d.coordinate);
n.secondaryName = d.locationID;
points.add(n);
}

if(points.isNotEmpty) {
waypoint.destinationsOnRoute = points;
}

}

void _airwayAdjust(Waypoint waypoint) {

waypoint.currentAirwayDestinationIndex = 0; // on change to airway, reset it
waypoint.airwayDestinationsOnRoute = [];
waypoint.currentDestinationIndex = 0; // on change to airway, reset it
waypoint.destinationsOnRoute = [];
List<Destination> points = [];

// adjust airways, nothing to do when airway is not in the middle of points
int index = _waypoints.indexOf(waypoint);
AirwayDestination airway = _waypoints[index].destination as AirwayDestination;

if(airway.locationID.contains(".")) {
// procedure
for(Destination d in airway.points) {
Destination n = Destination(locationID: airway.locationID,
type: d.type,
facilityName: d.facilityName,
coordinate: d.coordinate);
n.secondaryName = d.locationID;
points.add(n);
}
}
// need a start and end for airway
else if(index > 0 && index < _waypoints.length - 1) {
if(index > 0 && index < _waypoints.length - 1) {
// replace the airway with the new airway with the right points
points = Airway.find(
_waypoints[index - 1].destination,
Expand All @@ -71,7 +83,7 @@ class PlanRoute {
}

if(points.isNotEmpty) {
waypoint.airwayDestinationsOnRoute = points;
waypoint.destinationsOnRoute = points;
}
}

Expand All @@ -82,12 +94,12 @@ class PlanRoute {
for(int index = 0; index < waypoints.length; index++) {
Destination destination = waypoints[index].destination;
// expand airways
if(Destination.isAirway(destination.type)) {
if(Destination.isAirway(destination.type) || Destination.isProcedure(destination.type)) {
// skip empty airways
if(waypoints[index].airwayDestinationsOnRoute.isEmpty) {
if(waypoints[index].destinationsOnRoute.isEmpty) {
continue;
}
destinationsExpanded.addAll(waypoints[index].airwayDestinationsOnRoute);
destinationsExpanded.addAll(waypoints[index].destinationsOnRoute);
}
else {
destinationsExpanded.add(destination);
Expand Down Expand Up @@ -146,6 +158,9 @@ class PlanRoute {
if (Destination.isAirway(destination.type)) {
_airwayAdjust(_waypoints[index]); // add all airways
}
else if (Destination.isProcedure(destination.type)) {
_procedureAdjust(_waypoints[index]); // add all airways
}
}
}

Expand All @@ -161,7 +176,7 @@ class PlanRoute {
// current
List<Destination> destinationsCurrent = [];
Destination destination = current.destination;
if(Destination.isAirway(destination.type)) {
if(Destination.isAirway(destination.type) || Destination.isProcedure(destination.type)) {
destinationsPassed.addAll(current.getDestinationsPassed());
destinationsNext.insertAll(0, current.getDestinationsNext());
destinationsCurrent.addAll(current.getDestinationsCurrent());
Expand Down Expand Up @@ -262,10 +277,10 @@ class PlanRoute {

void advance() {
if(_current != null) {
if(Destination.isAirway(_current!.destination.type)) {
if(_current!.currentAirwayDestinationIndex < _current!.airwayDestinationsOnRoute.length - 1) {
if(Destination.isAirway(_current!.destination.type) || Destination.isProcedure(_current!.destination.type)) {
if(_current!.currentDestinationIndex < _current!.destinationsOnRoute.length - 1) {
// flying on airway and not done
_current!.currentAirwayDestinationIndex++;
_current!.currentDestinationIndex++;
update();
return;
}
Expand Down Expand Up @@ -374,11 +389,11 @@ class PlanRoute {
// set waypoint based on destination object
void setCurrentWaypointFromDestination(Destination d) {
for(Waypoint w in _waypoints) {
if(Destination.isAirway(w.destination.type)) {
int index = w.airwayDestinationsOnRoute.indexOf(d);
if(Destination.isAirway(w.destination.type) || Destination.isProcedure(w.destination.type)) {
int index = w.destinationsOnRoute.indexOf(d);
if(index >= 0) {
_setCurrent(w);
w.currentAirwayDestinationIndex = index;
w.currentDestinationIndex = index;
return;
}
}
Expand Down Expand Up @@ -591,7 +606,7 @@ class PlanRoute {
void insertWaypoint(Waypoint waypoint) {
Storage().realmHelper.addRecent(waypoint.destination);

if(Destination.isAirway(waypoint.destination.type) || _waypoints.isEmpty) {
if(Destination.isAirway(waypoint.destination.type) || Destination.isProcedure(waypoint.destination.type) || _waypoints.isEmpty) {
addWaypoint(waypoint); // airways cannot be added in the middle. that's confusing
return;
}
Expand Down Expand Up @@ -637,7 +652,7 @@ class PlanRoute {
// for rubber banding
void replaceDestination(int index, LatLng ll) {
if(index >= 0 && index < _allDestinations.length) {
if(Destination.isAirway(_allDestinations[index].type)) {
if(Destination.isAirway(_allDestinations[index].type) || Destination.isProcedure(_allDestinations[index].type)) {
return;
}
for(int d = 0; d < _waypoints.length; d++) {
Expand All @@ -654,7 +669,7 @@ class PlanRoute {
// also for rubber banding
void replaceDestinationFromDb(int index, LatLng ll) {
if(index >= 0 && index < _allDestinations.length) {
if(Destination.isAirway(_allDestinations[index].type)) {
if(Destination.isAirway(_allDestinations[index].type) || Destination.isProcedure(_allDestinations[index].type)) {
return;
}
MainDatabaseHelper.db.findNear(ll, factor: 0.0001).then((onValue) { // snap but not too far
Expand Down
24 changes: 12 additions & 12 deletions lib/plan/waypoint.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ import 'package:avaremp/destination/destination.dart';
class Waypoint {

final Destination _destination;
List<Destination> airwayDestinationsOnRoute = [];
int currentAirwayDestinationIndex = 0;
List<Destination> destinationsOnRoute = [];
int currentDestinationIndex = 0;

Waypoint(this._destination);

Destination get destination {
return airwayDestinationsOnRoute.isNotEmpty ?
airwayDestinationsOnRoute[currentAirwayDestinationIndex] : _destination;
return destinationsOnRoute.isNotEmpty ?
destinationsOnRoute[currentDestinationIndex] : _destination;
}

// return points passed, current, next
List<Destination> getDestinationsNext() {
if(airwayDestinationsOnRoute.isNotEmpty) {
return currentAirwayDestinationIndex == (airwayDestinationsOnRoute.length - 1) ?
[] : airwayDestinationsOnRoute.sublist(currentAirwayDestinationIndex + 1, airwayDestinationsOnRoute.length);
if(destinationsOnRoute.isNotEmpty) {
return currentDestinationIndex == (destinationsOnRoute.length - 1) ?
[] : destinationsOnRoute.sublist(currentDestinationIndex + 1, destinationsOnRoute.length);
}
return [];
}

// return points passed, current, next
List<Destination> getDestinationsPassed() {
if(airwayDestinationsOnRoute.isNotEmpty) {
return currentAirwayDestinationIndex == 0 ?
[] : airwayDestinationsOnRoute.sublist(0, currentAirwayDestinationIndex);
if(destinationsOnRoute.isNotEmpty) {
return currentDestinationIndex == 0 ?
[] : destinationsOnRoute.sublist(0, currentDestinationIndex);
}
return [];
}

// return points passed, current, next
List<Destination> getDestinationsCurrent() {
if(airwayDestinationsOnRoute.isNotEmpty) {
return [airwayDestinationsOnRoute[currentAirwayDestinationIndex]];
if(destinationsOnRoute.isNotEmpty) {
return [destinationsOnRoute[currentDestinationIndex]];
}
return [];
}
Expand Down

0 comments on commit ea3a1ce

Please sign in to comment.