root/apps/iphone/superbook/trunk/ShareKit/UI/SHKShareMenu.m
@
893
| Revision 893, 9.0 kB (checked in by henri, 21 months ago) |
|---|
| Line | |
|---|---|
| 1 | // |
| 2 | // SHKShareMenu.m |
| 3 | // ShareKit |
| 4 | // |
| 5 | // Created by Nathan Weiner on 6/18/10. |
| 6 | |
| 7 | // |
| 8 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | // of this software and associated documentation files (the "Software"), to deal |
| 10 | // in the Software without restriction, including without limitation the rights |
| 11 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | // copies of the Software, and to permit persons to whom the Software is |
| 13 | // furnished to do so, subject to the following conditions: |
| 14 | // |
| 15 | // The above copyright notice and this permission notice shall be included in |
| 16 | // all copies or substantial portions of the Software. |
| 17 | // |
| 18 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | // THE SOFTWARE. |
| 25 | // |
| 26 | // |
| 27 | |
| 28 | #import "SHKShareMenu.h" |
| 29 | #import "SHK.h" |
| 30 | #import "SHKSharer.h" |
| 31 | #import "SHKCustomShareMenuCell.h" |
| 32 | |
| 33 | @implementation SHKShareMenu |
| 34 | |
| 35 | @dynamic item; |
| 36 | @synthesize tableData; |
| 37 | @synthesize exclusions; |
| 38 | |
| 39 | #pragma mark - |
| 40 | #pragma mark Initialization |
| 41 | |
| 42 | - (void)dealloc |
| 43 | { |
| 44 | [item release]; |
| 45 | [tableData release]; |
| 46 | [exclusions release]; |
| 47 | [super dealloc]; |
| 48 | } |
| 49 | |
| 50 | |
| 51 | - (id)initWithStyle:(UITableViewStyle)style |
| 52 | { |
| 53 | if (self = [super initWithStyle:style]) |
| 54 | { |
| 55 | self.title = SHKLocalizedString(@"Share"); |
| 56 | |
| 57 | self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel |
| 58 | target:self |
| 59 | action:@selector(cancel)]; |
| 60 | |
| 61 | self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:SHKLocalizedString(@"Edit") |
| 62 | style:UIBarButtonItemStyleBordered |
| 63 | target:self |
| 64 | action:@selector(edit)]; |
| 65 | |
| 66 | } |
| 67 | return self; |
| 68 | } |
| 69 | |
| 70 | - (void)viewDidDisappear:(BOOL)animated |
| 71 | { |
| 72 | [super viewDidDisappear:animated]; |
| 73 | |
| 74 | // Remove the SHK view wrapper from the window |
| 75 | [[SHK currentHelper] viewWasDismissed]; |
| 76 | } |
| 77 | |
| 78 | - (SHKItem *)item { |
| 79 | return item; |
| 80 | } |
| 81 | |
| 82 | - (void)setItem:(SHKItem *)i |
| 83 | { |
| 84 | @synchronized(item) { |
| 85 | [item release]; |
| 86 | item = [[i retain] autorelease]; |
| 87 | [self rebuildTableDataAnimated:NO]; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | - (void)rebuildTableDataAnimated:(BOOL)animated |
| 92 | { |
| 93 | self.tableView.allowsSelectionDuringEditing = YES; |
| 94 | self.tableData = [NSMutableArray arrayWithCapacity:0]; |
| 95 | [tableData addObject:[self section:@"actions"]]; |
| 96 | [tableData addObject:[self section:@"services"]]; |
| 97 | |
| 98 | // Handling Excluded items |
| 99 | // If in editing mode, show them |
| 100 | // If not editing, hide them |
| 101 | self.exclusions = [[[[NSUserDefaults standardUserDefaults] objectForKey:@"SHKExcluded"] mutableCopy] autorelease]; |
| 102 | |
| 103 | if (exclusions == nil) |
| 104 | self.exclusions = [NSMutableDictionary dictionaryWithCapacity:0]; |
| 105 | |
| 106 | NSMutableArray *excluded = [NSMutableArray arrayWithCapacity:0]; |
| 107 | |
| 108 | if (!self.tableView.editing || animated) |
| 109 | { |
| 110 | int s = 0; |
| 111 | int r = 0; |
| 112 | |
| 113 | // Use temp objects so we can mutate as we are enumerating |
| 114 | NSMutableArray *sectionCopy; |
| 115 | NSMutableDictionary *tableDataCopy = [[tableData mutableCopy] autorelease]; |
| 116 | NSMutableIndexSet *indexes = [[NSMutableIndexSet alloc] init]; |
| 117 | |
| 118 | for(NSMutableArray *section in tableDataCopy) |
| 119 | { |
| 120 | r = 0; |
| 121 | [indexes removeAllIndexes]; |
| 122 | |
| 123 | sectionCopy = [[section mutableCopy] autorelease]; |
| 124 | |
| 125 | for (NSMutableDictionary *row in section) |
| 126 | { |
| 127 | if ([exclusions objectForKey:[row objectForKey:@"className"]]) |
| 128 | { |
| 129 | [excluded addObject:[NSIndexPath indexPathForRow:r inSection:s]]; |
| 130 | |
| 131 | if (!self.tableView.editing) |
| 132 | [indexes addIndex:r]; |
| 133 | } |
| 134 | |
| 135 | r++; |
| 136 | } |
| 137 | |
| 138 | if (!self.tableView.editing) |
| 139 | { |
| 140 | [sectionCopy removeObjectsAtIndexes:indexes]; |
| 141 | [tableData replaceObjectAtIndex:s withObject:sectionCopy]; |
| 142 | } |
| 143 | |
| 144 | s++; |
| 145 | } |
| 146 | |
| 147 | [indexes release]; |
| 148 | |
| 149 | if (animated) |
| 150 | { |
| 151 | [self.tableView beginUpdates]; |
| 152 | |
| 153 | if (!self.tableView.editing) |
| 154 | [self.tableView deleteRowsAtIndexPaths:excluded withRowAnimation:UITableViewRowAnimationFade]; |
| 155 | else |
| 156 | [self.tableView insertRowsAtIndexPaths:excluded withRowAnimation:UITableViewRowAnimationFade]; |
| 157 | |
| 158 | [self.tableView endUpdates]; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | } |
| 163 | |
| 164 | - (NSMutableArray *)section:(NSString *)section |
| 165 | { |
| 166 | id class; |
| 167 | NSMutableArray *sectionData = [NSMutableArray arrayWithCapacity:0]; |
| 168 | NSArray *source = [[SHK sharersDictionary] objectForKey:section]; |
| 169 | |
| 170 | for( NSString *sharerClassName in source) |
| 171 | { |
| 172 | class = NSClassFromString(sharerClassName); |
| 173 | if ( [class canShare] && [class canShareType:item.shareType] ) |
| 174 | [sectionData addObject:[NSDictionary dictionaryWithObjectsAndKeys:sharerClassName,@"className",[class sharerTitle],@"name",nil]]; |
| 175 | } |
| 176 | |
| 177 | if (sectionData.count && SHKShareMenuAlphabeticalOrder) |
| 178 | [sectionData sortUsingDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease]]]; |
| 179 | |
| 180 | return sectionData; |
| 181 | } |
| 182 | |
| 183 | #pragma mark - |
| 184 | #pragma mark View lifecycle |
| 185 | |
| 186 | - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation |
| 187 | { |
| 188 | return YES; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | #pragma mark - |
| 193 | #pragma mark Table view data source |
| 194 | |
| 195 | - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView |
| 196 | { |
| 197 | return tableData.count; |
| 198 | } |
| 199 | |
| 200 | |
| 201 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section |
| 202 | { |
| 203 | return [[tableData objectAtIndex:section] count]; |
| 204 | } |
| 205 | |
| 206 | |
| 207 | - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath |
| 208 | { |
| 209 | static NSString *CellIdentifier = @"Cell"; |
| 210 | |
| 211 | SHKCustomShareMenuCell *cell = (SHKCustomShareMenuCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; |
| 212 | if (cell == nil) |
| 213 | { |
| 214 | cell = [[[SHKCustomShareMenuCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease]; |
| 215 | cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; |
| 216 | } |
| 217 | |
| 218 | NSDictionary *rowData = [self rowDataAtIndexPath:indexPath]; |
| 219 | cell.textLabel.text = [rowData objectForKey:@"name"]; |
| 220 | |
| 221 | if (cell.editingAccessoryView == nil) |
| 222 | { |
| 223 | UISwitch *toggle = [[UISwitch alloc] initWithFrame:CGRectZero]; |
| 224 | toggle.userInteractionEnabled = NO; |
| 225 | cell.editingAccessoryView = toggle; |
| 226 | [toggle release]; |
| 227 | } |
| 228 | |
| 229 | [(UISwitch *)cell.editingAccessoryView setOn:[exclusions objectForKey:[rowData objectForKey:@"className"]] == nil]; |
| 230 | |
| 231 | return cell; |
| 232 | } |
| 233 | |
| 234 | - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath |
| 235 | { |
| 236 | return UITableViewCellEditingStyleNone; |
| 237 | } |
| 238 | |
| 239 | - (BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath |
| 240 | { |
| 241 | return NO; |
| 242 | } |
| 243 | |
| 244 | |
| 245 | #pragma mark - |
| 246 | #pragma mark Table view delegate |
| 247 | |
| 248 | - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath |
| 249 | { |
| 250 | NSDictionary *rowData = [self rowDataAtIndexPath:indexPath]; |
| 251 | |
| 252 | if (tableView.editing) |
| 253 | { |
| 254 | UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath]; |
| 255 | |
| 256 | UISwitch *toggle = (UISwitch *)[cell editingAccessoryView]; |
| 257 | [toggle setOn:!toggle.on animated:YES]; |
| 258 | |
| 259 | if (toggle.on) |
| 260 | [exclusions removeObjectForKey:[rowData objectForKey:@"className"]]; |
| 261 | |
| 262 | else |
| 263 | [exclusions setObject:@"1" forKey:[rowData objectForKey:@"className"]]; |
| 264 | |
| 265 | [self.tableView deselectRowAtIndexPath:indexPath animated:NO]; |
| 266 | } |
| 267 | |
| 268 | else |
| 269 | { |
| 270 | [NSClassFromString([rowData objectForKey:@"className"]) shareItem:item]; |
| 271 | |
| 272 | [[SHK currentHelper] hideCurrentViewControllerAnimated:YES]; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | - (NSDictionary *)rowDataAtIndexPath:(NSIndexPath *)indexPath |
| 277 | { |
| 278 | return [[tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row]; |
| 279 | } |
| 280 | |
| 281 | - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section |
| 282 | { |
| 283 | if ([[tableData objectAtIndex:section] count]) |
| 284 | { |
| 285 | if (section == 0) |
| 286 | return SHKLocalizedString(@"Actions"); |
| 287 | |
| 288 | else if (section == 1) |
| 289 | return SHKLocalizedString(@"Services"); |
| 290 | } |
| 291 | |
| 292 | return nil; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | #pragma mark - |
| 297 | #pragma mark Toolbar Buttons |
| 298 | |
| 299 | - (void)cancel |
| 300 | { |
| 301 | [[SHK currentHelper] hideCurrentViewControllerAnimated:YES]; |
| 302 | } |
| 303 | |
| 304 | - (void)edit |
| 305 | { |
| 306 | [self.tableView setEditing:YES animated:YES]; |
| 307 | [self rebuildTableDataAnimated:YES]; |
| 308 | |
| 309 | [self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone |
| 310 | target:self |
| 311 | action:@selector(save)] autorelease] animated:YES]; |
| 312 | } |
| 313 | |
| 314 | - (void)save |
| 315 | { |
| 316 | [[NSUserDefaults standardUserDefaults] setObject:exclusions forKey:@"SHKExcluded"]; |
| 317 | |
| 318 | [self.tableView setEditing:NO animated:YES]; |
| 319 | [self rebuildTableDataAnimated:YES]; |
| 320 | |
| 321 | [self.navigationItem setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithTitle:SHKLocalizedString(@"Edit") |
| 322 | style:UIBarButtonItemStyleBordered |
| 323 | target:self |
| 324 | action:@selector(edit)] autorelease] animated:YES]; |
| 325 | |
| 326 | } |
| 327 | |
| 328 | @end |
| 329 |
Note: See TracBrowser
for help on using the browser.








