| 1 |
// Copyright (C) 2016 The Qt Company Ltd. |
| 2 |
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 |
|
| 4 |
#import "InterfaceController.h" |
| 5 |
|
| 6 |
@interface InterfaceController() <WCSessionDelegate> |
| 7 |
|
| 8 |
@end |
| 9 |
|
| 10 |
@implementation InterfaceController |
| 11 |
|
| 12 |
- (id)init { |
| 13 |
if ((self = [super init])) { |
| 14 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 15 |
NSString *host = [defaults stringForKey:@"host"]; |
| 16 |
int port = [defaults integerForKey:@"port"]; |
| 17 |
|
| 18 |
if (host == nil) { |
| 19 |
self.host = @"127.0.0.1"; |
| 20 |
} else { |
| 21 |
self.host = host; |
| 22 |
} |
| 23 |
|
| 24 |
if (port == 0) { |
| 25 |
self.port = [NSNumber numberWithInt:8080]; |
| 26 |
} else { |
| 27 |
self.port = [NSNumber numberWithInt:port]; |
| 28 |
} |
| 29 |
|
| 30 |
self.planets = @[@"Sun", |
| 31 |
@"Mercury", |
| 32 |
@"Venus", |
| 33 |
@"Earth", |
| 34 |
@"Mars", |
| 35 |
@"Jupiter", |
| 36 |
@"Saturn", |
| 37 |
@"Uranus", |
| 38 |
@"Neptune", |
| 39 |
@"Solar System"]; |
| 40 |
|
| 41 |
self.minimumValue = 1.0f; |
| 42 |
self.maximumValue = 10.0f; |
| 43 |
} |
| 44 |
|
| 45 |
return self; |
| 46 |
} |
| 47 |
|
| 48 |
- (void)awakeWithContext:(id)context { |
| 49 |
[super awakeWithContext:context]; |
| 50 |
|
| 51 |
int planetsCount = [self.planets count]; |
| 52 |
|
| 53 |
NSMutableArray *pickerItems = [[NSMutableArray alloc] init]; |
| 54 |
|
| 55 |
for (int i = 0; i < planetsCount; i++) { |
| 56 |
WKPickerItem *item = [WKPickerItem alloc]; |
| 57 |
item.title = self.planets[i]; |
| 58 |
[pickerItems addObject:item]; |
| 59 |
} |
| 60 |
|
| 61 |
[self.planetPicker setItems:pickerItems]; |
| 62 |
|
| 63 |
[self.hostLabel setText:[NSString stringWithFormat:@"%@:%@", self.host, [NSString stringWithFormat:@"%d", [self.port intValue]]]]; |
| 64 |
} |
| 65 |
|
| 66 |
- (void)willActivate { |
| 67 |
[super willActivate]; |
| 68 |
|
| 69 |
if ([WCSession isSupported]) { |
| 70 |
WCSession *session = [WCSession defaultSession]; |
| 71 |
session.delegate = self; |
| 72 |
[session activateSession]; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
- (IBAction)selectedPlanetChanged:(NSInteger)value { |
| 77 |
NSString *command = [@[@"selectPlanet", self.planets[value]] componentsJoinedByString:@"/"]; |
| 78 |
|
| 79 |
[self sendCommand:command]; |
| 80 |
} |
| 81 |
|
| 82 |
- (IBAction)rotationSpeedChanged:(float)value { |
| 83 |
NSString* formattedValue = [NSString stringWithFormat:@"%.02f", (value - self.minimumValue) / |
| 84 |
(self.maximumValue - self.minimumValue)]; |
| 85 |
NSString *command = [@[@"setRotationSpeed", formattedValue] componentsJoinedByString:@"/"]; |
| 86 |
|
| 87 |
[self sendCommand:command]; |
| 88 |
} |
| 89 |
|
| 90 |
- (IBAction)viewingDistanceChanged:(float)value { |
| 91 |
NSString* formattedValue = [NSString stringWithFormat:@"%.02f", (value - self.minimumValue) / |
| 92 |
(self.maximumValue - self.minimumValue)]; |
| 93 |
NSString *command = [@[@"setViewingDistance", formattedValue] componentsJoinedByString:@"/"]; |
| 94 |
|
| 95 |
[self sendCommand:command]; |
| 96 |
} |
| 97 |
|
| 98 |
- (IBAction)planetSizeChanged:(float)value { |
| 99 |
NSString* formattedValue = [NSString stringWithFormat:@"%.02f", (value - self.minimumValue) / |
| 100 |
(self.maximumValue - self.minimumValue)]; |
| 101 |
NSString *command = [@[@"setPlanetSize", formattedValue] componentsJoinedByString:@"/"]; |
| 102 |
|
| 103 |
[self sendCommand:command]; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
- (void)sendCommand:(NSString *)command { |
| 108 |
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; |
| 109 |
|
| 110 |
NSURLComponents *urlComponents = [[NSURLComponents alloc] init]; |
| 111 |
urlComponents.scheme = @"http"; |
| 112 |
urlComponents.host = self.host; |
| 113 |
urlComponents.port = self.port; |
| 114 |
urlComponents.path = [NSString stringWithFormat:@"/%@", command]; |
| 115 |
|
| 116 |
[request setURL:urlComponents.URL]; |
| 117 |
[request setHTTPMethod:@"GET"]; |
| 118 |
|
| 119 |
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; |
| 120 |
[[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, |
| 121 |
NSError *error) {}] resume]; |
| 122 |
} |
| 123 |
|
| 124 |
- (void)session:(nonnull WCSession *)session didReceiveMessage:(nonnull NSDictionary *)message replyHandler:(nonnull void (^)(NSDictionary<NSString *,id> *))replyHandler { |
| 125 |
NSString *host = [message objectForKey:@"host"]; |
| 126 |
NSString *port = [message objectForKey:@"port"]; |
| 127 |
|
| 128 |
NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; |
| 129 |
numberFormatter.numberStyle = NSNumberFormatterNoStyle; |
| 130 |
|
| 131 |
self.host = host; |
| 132 |
self.port = [numberFormatter numberFromString:port]; |
| 133 |
|
| 134 |
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |
| 135 |
[defaults setObject:host forKey:@"host"]; |
| 136 |
[defaults setInteger:[self.port integerValue] forKey:@"port"]; |
| 137 |
[defaults synchronize]; |
| 138 |
|
| 139 |
[self.hostLabel setText:[NSString stringWithFormat:@"%@:%@", host, port]]; |
| 140 |
} |
| 141 |
|
| 142 |
- (void)session:(WCSession *)session activationDidCompleteWithState:(WCSessionActivationState)activationState error:(NSError *)error { |
| 143 |
|
| 144 |
} |
| 145 |
|
| 146 |
@end |