FMCPlanner2/FMC Planner 2/ViewControllerScreen.m

459 lines
18 KiB
Objective-C

//
// ViewControllerScreen.m
// FMC Planner 2
//
// Created by Kilian Hofmann on 20.03.16.
// Copyright © 2016 Kilian Hofmann. All rights reserved.
//
#import "ViewController.h"
#import "ViewControllerScreen.h"
@implementation ViewControllerScreen
NavDataParser *parser = nil;
#pragma mark - View management and navigation
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Init Start Page
[self clearScreen];
_Header.text = @"START";
_Scratchpad.text = @"";
_numRoutePage = 1;
// ADJUST BASED ON SAVE
_maxNumRoutePages = 1;
// Tap gesture shit
for (UILabel *label in _LSK) {
label.adjustsFontSizeToFitWidth = YES;
UITapGestureRecognizer *reg = [[UITapGestureRecognizer alloc]
initWithTarget:self
action:@selector(enterLSK:)];
reg.numberOfTapsRequired = 1;
reg.numberOfTouchesRequired = 1;
[label addGestureRecognizer:reg];
}
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self loadScreen:@"MENU" withOptions:nil];
}
- (void)viewDidLayoutSubviews
{
_FrameHeight.constant = self.view.frame.size.height / 14.5;
// Size and LineBreak
_Header.font =
[SharedDeclerations findAdaptiveFontForUILabelSize:_Header.frame.size
withMinimumSize:5
withMod:0.7];
_Header.adjustsFontSizeToFitWidth = YES;
_Scratchpad.font = [SharedDeclerations
findAdaptiveFontForUILabelSize:_Scratchpad.frame.size
withMinimumSize:5
withMod:0.7];
_Scratchpad.adjustsFontSizeToFitWidth = YES;
for (UILabel *label in _LSK) {
label.font = [SharedDeclerations
findAdaptiveFontForUILabelSize:_Header.frame.size
withMinimumSize:5
withMod:0.6];
}
}
#pragma mark - LSK action
- (void)enterLSK:(UITapGestureRecognizer *)tapReg
{
parser = _main.navDataParser;
if ([_Scratchpad.text isEqualToString:INVALID] ||
[_Scratchpad.text isEqualToString:NOTREADY]) {
// DO NOTHING AS OF YET
}
else if ([_Scratchpad.text isEqualToString:@"DELETE"]) {
((UILabel *)tapReg.view).text = @"";
_Scratchpad.text = @"";
}
else if ([_Header.text isEqualToString:@"MENU"]) {
if ([((UILabel *)tapReg.view)
.restorationIdentifier isEqualToString:@"LSKR1"]) {
if ([((UILabel *)tapReg.view).text isEqualToString:@""]) {
_main.firstLoad = true;
}
else if (!([((UILabel *)tapReg.view)
.text isEqualToString:_Scratchpad.text])) {
_main.firstLoad = true;
}
_main.backupCoRte = ((UILabel *)tapReg.view).text;
((UILabel *)tapReg.view).text = _Scratchpad.text;
_Scratchpad.text = @"";
}
else if ([((UILabel *)tapReg.view)
.restorationIdentifier isEqualToString:@"LSKR3"]) {
ViewControllerRouteRequest *rq = [self.storyboard
instantiateViewControllerWithIdentifier:@"RTEFINDER"];
rq.main = _main;
[self presentViewController:rq animated:YES completion:nil];
}
else if ([((UILabel *)tapReg.view)
.restorationIdentifier isEqualToString:@"LSKR4"]) {
[_main saveToFile:_main.save];
_main.save = [[NSMutableDictionary alloc] init];
[self clearScreen];
_numRoutePage = 1;
_maxNumRoutePages = 1;
NSArray *optional = [[[NSDictionary alloc]
initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"LSKs"
ofType:@"plist"]]
valueForKey:_Header.text];
for (UILabel *label in _LSK) {
for (int i = 0; i < optional.count; i++) {
if (label.tag == i && [label.text isEqualToString:@""]) {
label.text = optional[i];
if (!([optional[i] isEqualToString:@""])) {
label.userInteractionEnabled = YES;
}
}
}
}
}
else if ([((UILabel *)tapReg.view)
.restorationIdentifier isEqualToString:@"LSKR5"]) {
// TODO: Export to PMDG route format
}
else if ([((UILabel *)tapReg.view)
.restorationIdentifier isEqualToString:@"LSKR6"]) {
UINavigationController *sm = [self.storyboard
instantiateViewControllerWithIdentifier:@"ServiceMenu"];
((ViewControllerServiceMenu *)(sm.childViewControllers).firstObject)
.main = _main;
[self presentViewController:sm animated:YES completion:nil];
}
else {
((UILabel *)tapReg.view).text = _Scratchpad.text;
_Scratchpad.text = @"";
}
}
else if ([[_Header.text substringToIndex:3] isEqualToString:@"ROU"]) {
if (parser.parserReady) {
// Required entries for checks
UILabel *opposite = nil;
UILabel *precedingWaypoint = nil;
UILabel *precedingAirway = nil;
for (UILabel *label in _LSK) {
// General population
if (tapReg.view.tag > 5) {
// Right hand LSKs
if (label.tag == tapReg.view.tag - 6) {
opposite = label;
}
if (label.tag == tapReg.view.tag - 7) {
precedingAirway = label;
}
if (label.tag == tapReg.view.tag - 1) {
precedingWaypoint = label;
}
}
else {
// Left hand LSKs
if (label.tag == tapReg.view.tag + 6) {
opposite = label;
}
if (label.tag == tapReg.view.tag + 5) {
precedingWaypoint = label;
}
if (label.tag == tapReg.view.tag - 1) {
precedingAirway = label;
}
}
// Specials for page transitioning querries
if (tapReg.view.tag == 6 || tapReg.view.tag == 0) {
// First waypoint or airway on page
precedingAirway = [[UILabel alloc] init];
precedingWaypoint = [[UILabel alloc] init];
precedingAirway.text = (_main.save)[[NSString
stringWithFormat:@"ROUTE %lu/@LSKL6",
(unsigned long)_numRoutePage - 1]];
precedingWaypoint.text = (_main.save)[[NSString
stringWithFormat:@"ROUTE %lu/@LSKR6",
(unsigned long)_numRoutePage - 1]];
precedingAirway.restorationIdentifier = @"LSKL6";
precedingWaypoint.restorationIdentifier = @"LSKR6";
if (precedingWaypoint.text == nil) {
precedingWaypoint.text = @"";
}
if (precedingAirway.text == nil) {
precedingAirway.text = @"";
}
}
}
NSArray *finds = nil;
// Checks
if (tapReg.view.tag <= 5) {
// Left hand LSKs, aka airway entry
if (tapReg.view.tag == 0 && [[_Header.text substringToIndex:8]
isEqualToString:@"ROUTE 1/"]) {
// Not allowed
NSLog(@"NOT ALLOWED");
}
else if ([precedingWaypoint.text isEqualToString:@""] &&
[opposite.text isEqualToString:@""]) {
if ([precedingAirway.text
isEqualToString:_Scratchpad.text]) {
// Case : L2 == T104, L3 == T104, R1 == DKB
// : INVALID
NSLog(@"NOT ALLOWED 2");
}
else {
finds = [parser
findCrosspointBetween:precedingAirway.text
and:_Scratchpad.text
withStart:precedingWaypoint.text];
if (finds.count > 0) {
precedingWaypoint.text = finds.firstObject;
if (tapReg.view.tag == 0) {
(_main.save)[[NSString
stringWithFormat:
@"ROUTE %lu/@%@",
(unsigned long)_numRoutePage - 1,
precedingWaypoint
.restorationIdentifier]] =
precedingWaypoint.text;
}
else {
(_main.save)[
[NSString stringWithFormat:
@"ROUTE %lu/@%@",
(unsigned long)_numRoutePage,
precedingWaypoint
.restorationIdentifier]] =
precedingWaypoint.text;
}
}
}
}
else if (![precedingWaypoint.text isEqualToString:@""] &&
[opposite.text isEqualToString:@""]) {
// Case : L2 == T104, R1 == DKB
// : Check if R1 on L2
finds = [parser findWaypoint:precedingWaypoint.text
onAirway:_Scratchpad.text];
}
else if (!([precedingWaypoint.text isEqualToString:@""] &&
[opposite.text isEqualToString:@""])) {
// Case : L2 == T104, L3 == DIRECT, R2 == DKB, R3 == WLD
// : Check for validity on R2 and R3, L3 =!= T104
finds = [parser findWaypoint:precedingWaypoint.text
onAirway:_Scratchpad.text];
if (finds.count > 0) {
finds = [parser findWaypoint:opposite.text
onAirway:_Scratchpad.text];
}
}
}
else {
// Right hand LSKs, aka waypoint entry
if ([precedingWaypoint.text isEqualToString:@""] &&
![[_Header.text substringToIndex:7]
isEqualToString:@"ROUTE 1"]) {
// Not allowed
NSLog(@"NOT ALLOWED 3");
}
else if ([opposite.text isEqualToString:@""]) {
// Special case if no airway has been entered (DIRECT gets
// enterd only if waypoint exists)
finds = [parser findWaypoint:_Scratchpad.text];
}
else {
// Check if waypoint is on airway
finds = [parser findWaypoint:_Scratchpad.text
onAirway:opposite.text];
}
}
// Writing to LSK if check are done
if (finds.count > 0) {
((UILabel *)tapReg.view).text = _Scratchpad.text;
_Scratchpad.text = @"";
if (tapReg.view.tag > 5) {
if ([opposite.text isEqualToString:@""]) {
opposite.text = @"DIRECT";
(_main.save)[[NSString
stringWithFormat:@"ROUTE %lu/@%@",
(unsigned long)_numRoutePage,
opposite.restorationIdentifier]] =
opposite.text;
}
}
}
else {
_Scratchpad.text = INVALID;
}
}
else if (!parser.parserReady) {
_Scratchpad.text = NOTREADY;
}
else if (parser.notActive) {
((UILabel *)tapReg.view).text = _Scratchpad.text;
_Scratchpad.text = @"";
}
}
else {
((UILabel *)tapReg.view).text = _Scratchpad.text;
_Scratchpad.text = @"";
}
if ([[_Header.text substringToIndex:4] isEqualToString:@"ROUT"]) {
(_main.save)[[NSString
stringWithFormat:@"ROUTE %lu/@%@", (unsigned long)_numRoutePage,
((UILabel *)tapReg.view).restorationIdentifier]] =
((UILabel *)tapReg.view).text;
}
else {
(_main.save)[[NSString
stringWithFormat:@"%@@%@", _Header.text,
((UILabel *)tapReg.view).restorationIdentifier]] =
((UILabel *)tapReg.view).text;
}
if (([((UILabel *)tapReg.view)
.restorationIdentifier isEqualToString:@"LSKR6"] ||
tapReg.view.tag == 5) &&
_numRoutePage == _maxNumRoutePages &&
[[_Header.text substringToIndex:4] isEqualToString:@"ROUT"]) {
_maxNumRoutePages += 1;
NSRange indexSlash = [_Header.text rangeOfString:@"/"];
_Header.text = [_Header.text
stringByReplacingCharactersInRange:NSMakeRange(
indexSlash.location,
_Header.text.length -
indexSlash.location)
withString:[NSString
stringWithFormat:
@"/%lu",
(unsigned long)
_maxNumRoutePages]];
}
(_main.save)[@"maxRte"] = @(_maxNumRoutePages);
[_main saveToFile:_main.save];
}
#pragma mark - Screen actions
- (void)clearScreen
{
for (UILabel *label in _LSK) {
label.text = @"";
}
}
- (void)loadScreen:(NSString *)screenName withOptions:(NSArray *)options
{
[self clearScreen];
_Header.text = screenName;
NSArray *screen =
[[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:screenName
ofType:@"plist"]];
for (UILabel *label in _Desc) {
for (int j = 0; j < screen.count; j++) {
if ((label.restorationIdentifier).intValue == j) {
label.text = screen[j];
}
if (![screen[j] isEqualToString:@""] ||
[_Header.text isEqualToString:@"ROUTE"]) {
for (UILabel *lsk in _LSK) {
if (lsk.tag == j) {
lsk.userInteractionEnabled = YES;
}
}
}
}
}
for (NSString *opt in options) {
if ([opt isEqualToString:FMCNEXTRTEPAGE]) {
_numRoutePage += 1;
}
else if ([opt isEqualToString:FMCFIRSTRTEPAGE]) {
_numRoutePage = 1;
}
_Header.text = [_Header.text
stringByAppendingString:
[NSString stringWithFormat:@" %lu/%lu",
(unsigned long)_numRoutePage,
(unsigned long)_maxNumRoutePages]];
}
[self loadDataToScreen];
NSArray *optional = [[[NSDictionary alloc]
initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"LSKs"
ofType:@"plist"]]
valueForKey:_Header.text];
for (UILabel *label in _LSK) {
for (int i = 0; i < optional.count; i++) {
if (label.tag == i && [label.text isEqualToString:@""]) {
label.text = optional[i];
if (!([optional[i] isEqualToString:@""])) {
label.userInteractionEnabled = YES;
}
}
}
}
}
- (void)loadDataToScreen
{
for (NSString *ident in _main.save) {
NSRange range = [ident rangeOfString:@"@"];
NSRange range2 = [ident rangeOfString:@"/"];
if ((range.location != NSNotFound &&
[_Header.text
isEqualToString:[ident substringToIndex:range.location]]) ||
(range2.location != NSNotFound &&
_Header.text.length > range2.location &&
[[ident substringToIndex:range2.location]
isEqualToString:[_Header.text
substringToIndex:range2.location]])) {
for (UILabel *label in _LSK) {
if ([label.restorationIdentifier
isEqualToString:[ident
substringFromIndex:range.location +
1]]) {
label.text = [_main.save valueForKey:ident];
}
}
}
NSArray *optional = [[[NSDictionary alloc]
initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"LSKs"
ofType:@"plist"]]
valueForKey:_Header.text];
for (UILabel *label in _LSK) {
for (int i = 0; i < optional.count; i++) {
if (label.tag == i && [label.text isEqualToString:@""]) {
label.text = optional[i];
if (!([optional[i] isEqualToString:@""])) {
label.userInteractionEnabled = YES;
}
}
}
}
}
if ([[_main.save valueForKey:@"maxRte"] unsignedIntegerValue] > 0) {
_maxNumRoutePages =
[[_main.save valueForKey:@"maxRte"] unsignedIntegerValue];
}
}
@end