Skip to content

Commit

Permalink
Fix crash when delete resources
Browse files Browse the repository at this point in the history
  • Loading branch information
tinymind committed Dec 15, 2015
1 parent 775c65b commit 655f26f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 29 deletions.
2 changes: 1 addition & 1 deletion LSUnusedResources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<string>1.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down
10 changes: 5 additions & 5 deletions LSUnusedResources/Recourse/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -1000,7 +1000,7 @@
<font key="titleFont" metaFont="system"/>
</box>
<button verticalHuggingPriority="750" id="d2Z-MZ-XC6">
<rect key="frame" x="3" y="3" width="96" height="32"/>
<rect key="frame" x="4" y="3" width="94" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<animations/>
<buttonCell key="cell" type="push" title="Search" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="cUV-ux-EjC">
Expand All @@ -1024,8 +1024,8 @@
</connections>
</button>
<button hidden="YES" verticalHuggingPriority="750" id="q84-1O-TmH">
<rect key="frame" x="364" y="3" width="82" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
<rect key="frame" x="350" y="3" width="94" height="32"/>
<autoresizingMask key="autoresizingMask" flexibleMinX="YES" flexibleMaxY="YES"/>
<buttonCell key="cell" type="push" title="Delete" bezelStyle="rounded" alignment="center" borderStyle="border" imageScaling="proportionallyDown" inset="2" id="Cgc-bZ-CHo">
<behavior key="behavior" pushIn="YES" lightByBackground="YES" lightByGray="YES"/>
<font key="font" metaFont="system"/>
Expand All @@ -1035,12 +1035,12 @@
</connections>
</button>
<progressIndicator hidden="YES" horizontalHuggingPriority="750" verticalHuggingPriority="750" maxValue="100" bezeled="NO" indeterminate="YES" controlSize="small" style="spinning" id="N6r-4n-Mu1">
<rect key="frame" x="104" y="12" width="16" height="16"/>
<rect key="frame" x="94" y="12" width="16" height="16"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<animations/>
</progressIndicator>
<textField verticalHuggingPriority="750" id="T1v-qi-uUW">
<rect key="frame" x="126" y="12" width="238" height="17"/>
<rect key="frame" x="110" y="12" width="238" height="17"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMaxY="YES"/>
<animations/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" id="emm-3c-l2S">
Expand Down
49 changes: 26 additions & 23 deletions LSUnusedResources/ViewController/MainViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,24 @@ - (IBAction)onExportButtonClicked:(id)sender {

- (IBAction)onDeleteButtonClicked:(id)sender {
if (self.resultsTableView.numberOfSelectedRows > 0) {
NSLog(@"selected rows count:%ld", self.resultsTableView.numberOfSelectedRows);

__block NSError *error;

__block NSArray *results = [self.unusedResults copy];
[self.resultsTableView.selectedRowIndexes enumerateIndexesUsingBlock:^(NSUInteger idx, BOOL * _Nonnull stop) {
ResourceFileInfo *info = [results objectAtIndex:idx];
[[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:info.path] error:&error];
if (error) {
NSLog(@"File deletion error:%@", [error description]);
*stop = YES;
}else{
[self.resultsTableView beginUpdates];
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:idx];
[self.resultsTableView removeRowsAtIndexes:indexSet withAnimation:NSTableViewAnimationSlideUp];
[self.unusedResults removeObject:info];
[self.resultsTableView endUpdates];
NSArray *results = [self.unusedResults copy];
NSIndexSet *selectedIndexSet = self.resultsTableView.selectedRowIndexes;
NSUInteger index = [selectedIndexSet firstIndex];
while (index != NSNotFound) {
if (index < results.count) {
ResourceFileInfo *info = [results objectAtIndex:index];
[[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:info.path] error:nil];
}
}];
index = [selectedIndexSet indexGreaterThanIndex:index];
}

[self.unusedResults removeObjectsAtIndexes:selectedIndexSet];
[self.resultsTableView reloadData];
[self updateUnusedResultsCount];
} else {
NSAlert *alert = [[NSAlert alloc] init];
[alert setMessageText:@"Please select first."];
[alert runModal];
}
}

Expand Down Expand Up @@ -278,7 +277,7 @@ - (NSArray *)includeFileSuffixs {
[suffixs addObject:@"plist"];
}
if ([self.cssCheckbox state]) {
[suffixs addObject:@"plist"];
[suffixs addObject:@"css"];
}
if ([self.xibCheckbox state]) {
[suffixs addObject:@"xib"];
Expand All @@ -296,10 +295,7 @@ - (NSArray *)includeFileSuffixs {
- (void)setUIEnabled:(BOOL)state {
// Individual
if (state) {
[self.processIndicator stopAnimation:self];
NSUInteger count = self.unusedResults.count;
NSString *tips = count > 2 ? @"resources" : @"resource";
self.statusLabel.stringValue = [NSString stringWithFormat:@"%d unsued %@.", (int)count, tips];
[self updateUnusedResultsCount];
} else {
[self.processIndicator startAnimation:self];
self.statusLabel.stringValue = @"Searching...";
Expand Down Expand Up @@ -332,6 +328,13 @@ - (void)setUIEnabled:(BOOL)state {
[_processIndicator setHidden:state];
}

- (void)updateUnusedResultsCount {
[self.processIndicator stopAnimation:self];
NSUInteger count = self.unusedResults.count;
NSString *tips = count > 2 ? @"resources" : @"resource";
self.statusLabel.stringValue = [NSString stringWithFormat:@"%d unsued %@.", (int)count, tips];
}

- (void)searchUnusedResources {
if (self.isFileDone && self.isStringDone) {
NSArray *resNames = [[[ResourceFileSearcher sharedObject].resNameInfoDict allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Expand Down
Binary file modified LSUnusedResourcesExample.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Release/LSUnusedResources.app.zip
Binary file not shown.

0 comments on commit 655f26f

Please sign in to comment.