Blame view

ios-examples-13.4.9/FirstApp/FirstAppExample/EMFirstAppViewController.m 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#import "EMFirstAppViewController.h"
#import "EMFramework.h"

@interface EMFirstAppViewController () {
    IBOutlet UIActivityIndicatorView *_activityIndicator;
    NSTimer *_writeTimer;
}

@end

@implementation EMFirstAppViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_activityIndicator setHidden:YES];
    [self setTitle:[[[EMConnectionManager sharedManager] connectedDevice] name]];
    [[EMConnectionManager sharedManager] readResource:@"data" onSuccess:^(id readValue) {
        [[self valueLabel] setText:[NSString stringWithFormat:@"%d", [readValue intValue]]];
        [[self valueSlider] setValue:[readValue floatValue]];
    } onFail:^(NSError *error) {

    }];

    [[EMConnectionManager sharedManager] addObserver:self forKeyPath:@"connectionState" options:0 context:NULL];
}

-(void)dealloc {
    [[EMConnectionManager sharedManager] removeObserver:self forKeyPath:@"connectionState"];
}

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if (object == [EMConnectionManager sharedManager]) {
        if ([keyPath isEqualToString:@"connectionState"]) {
            if ([[EMConnectionManager sharedManager] connectionState] == EMConnectionStateDisrupted) {
                [[self navigationController] popToRootViewControllerAnimated:YES];
            }
        }
    }
}

#pragma mark - Interface actions

-(IBAction)sliderChanged:(UISlider *)sender {
    [[self valueLabel] setText:[NSString stringWithFormat:@"%0.0f", [sender value]]];
    if (_writeTimer) {
        [_writeTimer invalidate];
        _writeTimer = nil;
    }
    _writeTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(sliderDidFinishChanging:) userInfo:nil repeats:NO];
}

-(IBAction)sliderDidFinishChanging:(UISlider *)sender {
    [_writeTimer invalidate];
    _writeTimer = nil;
    [_activityIndicator startAnimating];
    [_activityIndicator setHidden:NO];

    [[EMConnectionManager sharedManager] writeValue:[NSNumber numberWithFloat:[[self valueSlider] value]] toResource:@"data" onSuccess:^{
        [_activityIndicator setHidden:YES];
        [_activityIndicator stopAnimating];
    } onFail:^(NSError *error) {

    }];
}
@end