Blame view

ios-examples-13.4.9/Ex_Compound/Compound Example/EMCompoundExampleViewController.m 3.26 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
//
//  EMCompoundExampleViewController.m
//  Compound Example
//
//  Created by Dexter Weiss on 10/15/13.
//  Copyright (c) 2013 Emmoco. All rights reserved.
//

#import "EMCompoundExampleViewController.h"
#import "EMFramework.h"
#import "EMCompoundResourceCell.h"

@interface EMCompoundExampleViewController () <UITableViewDataSource, UITableViewDelegate, EMCompoundResourceCellDelegate>

@property (nonatomic, strong) NSArray *readValue;

@end

@implementation EMCompoundExampleViewController

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

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[EMConnectionManager sharedManager] readResource:@"infoArray" onSuccess:^(id readValue) {
        if ([readValue isKindOfClass:[NSArray class]]) {
            [self setReadValue:readValue];
            [[self tableView] reloadData];
        }
    } onFail:^(NSError *error) {
        NSLog(@"%@", [error localizedDescription]);
    }];

    [[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];
            }
        }
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table Methods

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *reuseIdentifier = @"ResourceCell";
    NSDictionary *value = [[self readValue] objectAtIndex:[indexPath section]];
    EMCompoundResourceCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    [cell setCompoundResourceValue:value];
    [cell setDelegate:self];
    [cell setIndex:[indexPath section]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Cell Delegate

-(void)cellDidProcessEditing:(NSDictionary *)dictionary forIndex:(NSUInteger)index {
    NSMutableArray *mutable = [[self readValue] mutableCopy];
    [mutable replaceObjectAtIndex:index withObject:dictionary];
    [self setReadValue:mutable];
    [[EMConnectionManager sharedManager] writeValue:[self readValue] toResource:@"infoArray" onSuccess:^{
        NSLog(@"Successfully wrote resource");
    } onFail:^(NSError *error) {
        NSLog(@"Failed to write value: %@", [error localizedDescription]);
    }];
}

@end