Welcome to mirror list, hosted at ThFree Co, Russian Federation.

MUActionSheet.m « Classes « Source - github.com/mumble-voip/mumble-iphoneos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cdbfba8ad33ff690ad6e992abc5fe4f8e2536917 (plain)
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright 2013 The 'Mumble for iOS' Developers. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#import "MUActionSheet.h"
#import "MUActionSheetBackgroundView.h"
#import "MUActionSheetButton.h"

@interface MUActionSheet () {
    UIView                      *_fadeView;
    MUActionSheetBackgroundView *_bgView;
    UIViewController            *_viewController;
    NSString                    *_title;
    
    NSMutableArray              *_otherButtons;

    MUActionSheetButton         *_cancelButton;
    NSInteger                   _cancelButtonIndex;

    MUActionSheetButton         *_destructiveButton;
    NSInteger                   _destructiveButtonIndex;

    MUActionSheetButton         *_constructiveButton;
    NSInteger                   _constructiveButtonIndex;
    
    BOOL                        _shouldReenableScroll;
    
    UIBarButtonItem             *_oldLeftBarButtonItem;
    UIBarButtonItem             *_oldRightBarButtonItem;
    
    NSArray                     *_oldViewGestureRecognizers;
    
    id<MUActionSheetDelegate>   _delegate;
}
@end

@implementation MUActionSheet

- (id) initWithTitle:(NSString *)title delegate:(id<MUActionSheetDelegate>)delegate cancelButtonTitle:(NSString *)cancelButtonTitle destructiveButtonTitle:(NSString *)destructiveButtonTitle constructiveButtonTitle:(NSString *)constructiveButtonTitle otherButtonTitles:(NSString *)otherButtonTitles, ... {
    if ((self = [super init])) {
        _delegate = delegate;
        _title = [title copy];
        
        _otherButtons = [[NSMutableArray alloc] init];
        NSInteger idx = 0;

        {
            va_list args;
            va_start(args, otherButtonTitles);
            for (NSString *title = otherButtonTitles; title != nil; title = va_arg(args, NSString *)) {
                MUActionSheetButton *btn = [MUActionSheetButton buttonWithKind:MUActionSheetButtonKindNormal];
                [btn setTitle:title];
                [btn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
                [_otherButtons addObject:btn];
                
                idx++;
            }
        }

        _cancelButton = [[MUActionSheetButton buttonWithKind:MUActionSheetButtonKindCancel] retain];
        [_cancelButton setTitle:cancelButtonTitle ? cancelButtonTitle : @"Cancel"];
        [_cancelButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        _cancelButtonIndex = idx++;
        
        if (constructiveButtonTitle) {
            _constructiveButton = [[MUActionSheetButton buttonWithKind:MUActionSheetButtonKindConstructive] retain];
            [_constructiveButton setTitle:constructiveButtonTitle];
            [_constructiveButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            _constructiveButtonIndex = idx++;
        } else {
            _constructiveButtonIndex = -1;
        }
        
        if (destructiveButtonTitle) {
            _destructiveButton = [[MUActionSheetButton buttonWithKind:MUActionSheetButtonKindDestructive] retain];
            [_destructiveButton setTitle:destructiveButtonTitle];
            [_destructiveButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
            _destructiveButtonIndex = idx++;
        } else {
            _destructiveButtonIndex = -1;
        }
    }
    return self;
}

- (void) dealloc {
    [_title release];
    [_fadeView release];
    [_bgView release];
    [_otherButtons release];
    [_destructiveButton release];
    [_constructiveButton release];
    [_cancelButton release];
    
    [super dealloc];
}

- (NSInteger) cancelButtonIndex {
    return _cancelButtonIndex;
}

- (NSInteger) destructiveButtonIndex {
    return _destructiveButtonIndex;
}

- (NSInteger) constructiveButtonIndex {
    return _constructiveButtonIndex;
}

- (void) buttonClicked:(id)sender {
    NSInteger idx = -1;
    if (sender == _cancelButton) {
        idx = _cancelButtonIndex;
    } else if (sender == _constructiveButton) {
        idx = _constructiveButtonIndex;
    } else if (sender == _destructiveButton) {
        idx = _destructiveButtonIndex;
    } else {
        idx = [_otherButtons indexOfObject:sender];
    }
    [self removeViewAndPerformBlock:^{
        [_delegate actionSheet:self didDismissWithButtonIndex:idx];
    }];
}

- (void) showInViewController:(UIViewController *)viewController {
    _viewController = viewController;
    [self addView];
}

- (void) hideNavigationItemButtons {
    [_viewController.navigationItem setHidesBackButton:YES animated:YES];
    _oldLeftBarButtonItem = [[_viewController.navigationItem leftBarButtonItem] retain];
    [_viewController.navigationItem setLeftBarButtonItem:nil animated:YES];
    _oldRightBarButtonItem = [[_viewController.navigationItem rightBarButtonItem] retain];
    [_viewController.navigationItem setRightBarButtonItem:nil animated:YES];
}

- (void) showNavigationItemButtons {
    [_viewController.navigationItem setHidesBackButton:NO animated:YES];
    [_viewController.navigationItem setRightBarButtonItem:_oldRightBarButtonItem animated:YES];
    [_oldRightBarButtonItem release];
    [_viewController.navigationItem setLeftBarButtonItem:_oldLeftBarButtonItem animated:NO];
    [_oldLeftBarButtonItem release];
}

- (CGFloat) _viewHeightForTitleLabelHeight:(CGFloat)titleHeight {
    CGFloat padding = 10;
    CGFloat buttonHeight = 48;
    CGFloat viewHeight = 0;
    
    if (titleHeight > 0) {
        viewHeight += 1;
    }
    
    // Title adjustment
    viewHeight += padding + titleHeight + padding;
    
    // Group buttons (without cancel button)
    NSInteger numButtons = [_otherButtons count];
    if (_destructiveButton) {
        numButtons++;
    }
    if (_constructiveButton) {
        numButtons++;
    }
    viewHeight += ((buttonHeight + padding) * numButtons);
    
    // Cancel button
    viewHeight += padding + buttonHeight + 2*padding;
    
    return viewHeight;
}

- (void) addView {
    const CGFloat padding = 10;
    
    // Add a reference, so consumers of
    // the MUActionSheet can release it as soon
    // it is shown, just like UIActionSheet.
    [self retain];
    
    // Disable scrolling if we're added to something
    // that looks like a UIScrollView.
    CGPoint scrollViewOffset = CGPointMake(0, 0);
    if ([_viewController.view respondsToSelector:@selector(isScrollEnabled)]) {
        UIScrollView *scrollView = (UIScrollView *)_viewController.view;
        _shouldReenableScroll = [scrollView isScrollEnabled];
        [scrollView setScrollEnabled:NO];
        
        scrollViewOffset = [scrollView contentOffset];
    }
    
    // Store the the gesture recognizers on the view controller's view.
    // Gesture recognizers pass through even though we set exclusiveTouch
    // on our action sheet UI elements.
    _oldViewGestureRecognizers = [[[_viewController view] gestureRecognizers] retain];
    [[_viewController view] setGestureRecognizers:nil];
    
    CGFloat titleLabelHeight = 0;
    UILabel *titleLabel = nil;
    if (_title) {
        titleLabel = [[UILabel alloc] init];
        titleLabel.text = _title;
        titleLabel.font = [UIFont systemFontOfSize:[UIFont systemFontSize]];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.textColor = [UIColor whiteColor];
        titleLabel.shadowColor = [UIColor blackColor];
        titleLabel.shadowOffset = CGSizeMake(0, -1);
        titleLabel.opaque = NO;
        titleLabel.backgroundColor = [UIColor clearColor];

        CGRect textRect = [titleLabel textRectForBounds:CGRectMake(0, 0, 320, 640) limitedToNumberOfLines:1];
        titleLabelHeight = textRect.size.height;
    }
    
    CGFloat viewHeight = [self _viewHeightForTitleLabelHeight:titleLabelHeight];
    
    _fadeView = [[[UIView alloc] initWithFrame:_viewController.view.frame] autorelease];
    _fadeView.backgroundColor = [UIColor clearColor];
    [_fadeView setExclusiveTouch:YES];

    _bgView = [[[MUActionSheetBackgroundView alloc] initWithFrame:CGRectMake(0, _viewController.view.frame.size.height, 320, viewHeight)] autorelease];
    [_bgView setExclusiveTouch:YES];

    [_viewController.view addSubview:_fadeView];
    [_viewController.view addSubview:_bgView];
    
    __block CGFloat ofs = padding;
    
    if (titleLabel) {
        ofs += 1;
        [titleLabel setFrame:CGRectMake(0, ofs, 320, titleLabelHeight)];
        [_bgView addSubview:titleLabel];
    }
    
    ofs += titleLabelHeight + padding;
    
    // Destructive button
    if (_destructiveButton) {
        [_destructiveButton setFrame:CGRectMake(20, ofs, 280, 48)];
        [_bgView addSubview:_destructiveButton];
        ofs += 48 + padding;
    }

    [_otherButtons enumerateObjectsWithOptions:0 usingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        MUActionSheetButton *btn = (MUActionSheetButton *)obj;
        [btn setFrame:CGRectMake(20, ofs, 280, 48)];
        [_bgView addSubview:btn];
        ofs += 48 + padding;
    }];
    
    // Constructive button
    if (_constructiveButton) {
        [_constructiveButton setFrame:CGRectMake(20, ofs, 280, 48)];
        [_bgView addSubview:_constructiveButton];
        ofs += 48 + padding;
    }
    
    // Cancel button
    ofs += padding;
    [_cancelButton setFrame:CGRectMake(20, ofs, 280, 48)];
    [_bgView addSubview:_cancelButton];
    
    // Store old UINavigationItem button state
    [self hideNavigationItemButtons];
    
    __block UIView *parentView = _viewController.view;
    [UIView animateWithDuration:0.25f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^{
        _bgView.frame = CGRectMake(0, (parentView.frame.size.height + scrollViewOffset.y) - viewHeight, 320, viewHeight);
        [_bgView layoutIfNeeded];
        _fadeView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.4f];
    } completion:^(BOOL isfinished) {
        // ...
    }];
}
                             
- (void) removeViewAndPerformBlock:(void(^)())doneBlock {
    __block UIView *parentView = _viewController.view;

    // Restore UINavigationItem button state.
    [self showNavigationItemButtons];

    [UIView animateWithDuration:0.25f delay:0.0f options:UIViewAnimationOptionCurveEaseIn animations:^{
        _bgView.frame = CGRectMake(0, parentView.frame.size.height, 320, _bgView.frame.size.height);
        _fadeView.backgroundColor = [UIColor clearColor];
    } completion:^(BOOL finished) {
        [_bgView removeFromSuperview];
        _bgView = nil;
        [_fadeView removeFromSuperview];
        _fadeView = nil;
        doneBlock();

        // Check if we should try to re-enable scrolling.
        if (_shouldReenableScroll) {
            UIScrollView *scrollView = (UIScrollView *)_viewController.view;
            [scrollView setScrollEnabled:YES];
        }
        
        // Restore the view's gesture recognizers.
        [[_viewController view] setGestureRecognizers:_oldViewGestureRecognizers];
        [_oldViewGestureRecognizers release];

        // Release the reference we added when
        // showing the MUActionSheet.
        [self release];
    }];
}

@end