-
Notifications
You must be signed in to change notification settings - Fork 0
/
ViewManager.m
204 lines (159 loc) · 4.89 KB
/
ViewManager.m
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//
// ViewHandler.m
// ContentionApp
//
// Created by Tom Lodge on 25/08/2010.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "ViewManager.h"
@interface ViewManager (private)
-(CGPoint) getCoordinates:(int) position;
-(void) createViews:(NSMutableArray*)data;
-(BOOL) containsView:(NSString*) name data:(NSMutableArray*)data;
-(int) findViewIndex:(NSString *) viewname;
-(void) addNewView:(NodeTuple *) node position:(int) pos;
-(void) printTables;
-(void) newNetworkData:(NSNotification *) n;
-(void) removeOldViews:(NSMutableArray *) data;
@end
@implementation ViewManager
@synthesize view;
@synthesize viewController;
const static float IMAGEINDENT = 215;
const static float VIEWHEIGHT = 85;
-(id) initWithView:(UIView *) v data:(NSMutableArray*) data viewcontroller:(UIViewController<TouchResponse>*) vc{
if(self == [super init]){
[self setViewController:vc];
[self setView:v];
[self createViews:data];
}
return self;
}
-(BOOL) containsView:(NSString*) name data:(NSMutableArray*)data{
NSEnumerator *enumerator = [data objectEnumerator];
NodeTuple* node;
int count = 0;
while ( node = [enumerator nextObject]) {
if (count++ >= DEVICES)
break;
if ([name isEqualToString:[node identifier]])
return YES;
}
return NO;
}
-(void) update:(NSMutableArray *) data{
NSEnumerator *enumerator = [data objectEnumerator];
NodeTuple* node;
//remove old views here..
[self removeOldViews:data];
int position = 0;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:2.0];
[UIView setAnimationDelegate:self];
while ( node = [enumerator nextObject]) {
if (position >= DEVICES)
break;
int index = [self findViewIndex:[node identifier]];
if (index == -1){
[self addNewView:node position:position];
index = [self findViewIndex:[node identifier]];
}
CGPoint newPosition = [self getCoordinates:position];
myviews[index].center = newPosition;
[myviews[index] update:position bandwidth:[viewController getBandwidthProportion:[node identifier]]];
CGRect bounds;
CGAffineTransform transform;
if (position < 3){
transform = CGAffineTransformMakeScale(1.0, 1.0);
bounds = CGRectMake(0.0, 0.0, 320, 85);
}
else{
transform = CGAffineTransformMakeScale(0.5, 0.5);
bounds= CGRectMake(0.0, 0.0, 100, 85);
}
myviews[index].bounds = bounds;
myviews[index].transform = transform;
position++;
}
[UIView commitAnimations];
}
-(void) removeOldViews:(NSMutableArray *) data{
for (int i = 0; i < DEVICES; i++){
if (myviews[i] != NULL){
DeviceView *current = (DeviceView*) myviews[i];
if (![self containsView:current.identifier data:data]){
[current removeFromSuperview];
myviews[i] = NULL;
}
}
}
}
-(DeviceView*) viewForName:(NSString *) identifier{
int index = [self findViewIndex:identifier];
if (index != -1){
return myviews[index];
}
return NULL;
}
-(int) findViewIndex:(NSString *) identifier{
for (int i = 0; i < DEVICES; i++){
DeviceView *current = (DeviceView *) myviews[i];
if (current != NULL){
if (current.identifier != nil){
if ([current.identifier isEqualToString:identifier])
return i;
}
}
}
return -1;
}
-(CGPoint) getCoordinates:(int) position{
int spacer = 80;
int INDENT =55;
if (position < 3){
return CGPointMake(self.view.bounds.size.width/2, (10 + (position)*100) + VIEWHEIGHT/2);
}else{
return CGPointMake( (INDENT + (self.view.bounds.size.width/4 + (spacer * (position-3)))- (IMAGEINDENT/2)), 330);
}
}
-(void) addNewView:(NodeTuple *) node position:(int) pos{
NSString* myImage = [self.viewController getImage:[node identifier]];
CGRect frame;
if (pos < 3)
frame = CGRectMake(0.0, 0.0, self.view.bounds.size.width, VIEWHEIGHT);
else
frame = CGRectMake(0.0, self.view.bounds.size.height, 100, 85);
float bandwidth = [viewController getBandwidthProportion:[node identifier]];
DeviceView *pview = [[[DeviceView alloc] initWithValues:[node identifier] name:[node name] position:pos bandwidth:bandwidth frame:frame image:myImage imageindent:IMAGEINDENT] retain];
[pview setBackgroundColor:[UIColor clearColor]];
[pview setTouchDelegate:[self viewController]];
pview.center = [self getCoordinates:pos];
if (pos >=3){
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.5];
[UIView setAnimationDelegate:self];
CGAffineTransform transform = CGAffineTransformMakeScale(0.5, 0.5);
pview.transform = transform;
pview.bounds= CGRectMake(0.0, 0.0, 100, 85);
[UIView commitAnimations];
}
[self.view addSubview:pview];
for (int i = 0; i < DEVICES; i++){
if (myviews[i] == NULL){
myviews[i] = pview;
break;
}
}
}
-(void) createViews:(NSMutableArray*)data{
NSEnumerator *enumerator = [data objectEnumerator];
NodeTuple* node;
int count = 0;
while ( (node = [enumerator nextObject])) {
if (count >= DEVICES)
break;
[self addNewView:node position: count];
count +=1;
}
}
@end