Blame view

ios-examples-13.4.9/Ex_Indicator/Indicator Example/EMIndicatorExampleViewController.m 3.57 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
//  EMIndicatorExampleViewController.m
//  Indicator Example
//
//  Created by Dexter Weiss on 10/15/13.
//  Copyright (c) 2013 Emmoco. All rights reserved.
//

#import "EMIndicatorExampleViewController.h"
#import "EMFramework.h"

@interface EMIndicatorExampleViewController () {
    IBOutlet UISegmentedControl *_segmentedControl;
    IBOutlet UILabel *_indicatorLabel;
    NSTimer *_fadeTimer;
}

@end

@implementation EMIndicatorExampleViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setTitle:[[[EMConnectionManager sharedManager] connectedDevice] name]];
    [[EMConnectionManager sharedManager] readResource:@"eventSelector" onSuccess:^(id readValue) {
        [self _updateSegmentedControlSelectedIndex:readValue];
    } onFail:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];

    [_indicatorLabel setAlpha:0.0f];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:kEMConnectionDidReceiveIndicatorNotificationName object:nil];
}

-(void)didReceiveNotification:(NSNotification *)notification {
    id notificationValue = [[notification userInfo] objectForKey:kEMIndicatorResourceValueKey];
    NSString *resourceName = [[notification userInfo] objectForKey:kEMIndicatorNameKey];
    NSString *indicatorString = [NSString stringWithFormat:@"%@ indicator received\nValue: %@", resourceName, [notificationValue stringValue]];
    [_indicatorLabel setAlpha:0.0f];
    [_indicatorLabel setText:indicatorString];
    [UIView animateWithDuration:0.2 animations:^{
        [_indicatorLabel setAlpha:1.0f];
    } completion:^(BOOL finished) {
        [self _startLabelTimer];
    }];
}

-(void)_startLabelTimer {
    [_fadeTimer invalidate];
    _fadeTimer = [NSTimer timerWithTimeInterval:3.0f target:self selector:@selector(fadeLabel) userInfo:nil repeats:NO];
    [[NSRunLoop currentRunLoop] addTimer:_fadeTimer forMode:NSDefaultRunLoopMode];
}

-(void)fadeLabel {
    [_fadeTimer invalidate];
    [UIView animateWithDuration:0.3 animations:^{
        [_indicatorLabel setAlpha:0.0f];
    }];
}

-(void)_updateSegmentedControlSelectedIndex:(NSString *)eventValue {
    if ([eventValue isEqualToString:@"NONE"]) {
        [_segmentedControl setSelectedSegmentIndex:0];
    }
    else if ([eventValue isEqualToString:@"VOID_EVENT"]) {
        [_segmentedControl setSelectedSegmentIndex:1];
    }
    else if ([eventValue isEqualToString:@"UINT8_EVENT"]) {
        [_segmentedControl setSelectedSegmentIndex:2];
    }
    else if ([eventValue isEqualToString:@"INT32_EVENT"]) {
        [_segmentedControl setSelectedSegmentIndex:3];
    }
}

#pragma mark - Interface actions

-(IBAction)segmentedControlDidChange:(UISegmentedControl *)sender {
    NSString *indicatorType = nil;
    switch ([sender selectedSegmentIndex]) {
        case 0:
            indicatorType = @"NONE";
            break;
        case 1:
            indicatorType = @"VOID_EVENT";
            break;
        case 2:
            indicatorType = @"UINT8_EVENT";
            break;
        case 3:
            indicatorType = @"INT32_EVENT";
            break;
    }

    [[EMConnectionManager sharedManager] writeValue:indicatorType toResource:@"eventSelector" onSuccess:^{
        NSLog(@"Successfully wrote event type");
    } onFail:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];
}

@end