Skip to content

Commit

Permalink
Fix filtering of inventories on iPad
Browse files Browse the repository at this point in the history
  • Loading branch information
koraktor committed Jun 17, 2015
1 parent 6ca51d2 commit b810644
Showing 1 changed file with 32 additions and 15 deletions.
47 changes: 32 additions & 15 deletions Suitcase/Classes/SCGamesViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -429,10 +429,6 @@ - (void)settingsViewControllerDidEnd:(IASKAppSettingsViewController *)sender
}

- (void)settingsChanged:(NSNotification *)notification {
NSNumber *steamId64 = [[NSUserDefaults standardUserDefaults] valueForKey:@"SteamID64"];
NSArray *inventories = [[SCAbstractInventory inventoriesForUser:steamId64] allValues];
NSMutableArray *newInventories = [NSMutableArray arrayWithCapacity:inventories.count];

if (![notification.userInfo.allKeys containsObject:@"skip_empty_inventories"] &&
![notification.userInfo.allKeys containsObject:@"skip_failed_inventories"]) {
return;
Expand All @@ -441,7 +437,10 @@ - (void)settingsChanged:(NSNotification *)notification {
BOOL skipEmptyInventories = [[[NSUserDefaults standardUserDefaults] valueForKey:@"skip_empty_inventories"] boolValue];
BOOL skipFailedInventories = [[[NSUserDefaults standardUserDefaults] valueForKey:@"skip_failed_inventories"] boolValue];

self.steamInventory = nil;
NSNumber *steamId64 = [[NSUserDefaults standardUserDefaults] valueForKey:@"SteamID64"];
NSArray *inventories = [[SCAbstractInventory inventoriesForUser:steamId64] allValues];
NSMutableArray *newInventories = [NSMutableArray arrayWithCapacity:inventories.count];
__block SCCommunityInventory *newSteamInventory = nil;

[inventories enumerateObjectsUsingBlock:^(id <SCInventory> inventory, NSUInteger idx, BOOL *stop) {
if (![inventory isLoaded] || [inventory failed]) {
Expand All @@ -455,28 +454,46 @@ - (void)settingsChanged:(NSNotification *)notification {
}

if ([inventory.game isSteam]) {
self.steamInventory = inventory;
newSteamInventory = inventory;
} else {
[newInventories addObject:inventory];
}
}];

if (UIDevice.currentDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
[self.tableView beginUpdates];
BOOL inventoriesEmptyBefore = [self.inventories count] == 0;

BOOL steamInventoryEmptyBefore = self.steamInventory == nil;
self.steamInventory = newSteamInventory;
BOOL steamInventoryEmptyAfter = self.steamInventory == nil;
if (steamInventoryEmptyBefore != steamInventoryEmptyAfter) {
NSArray *steamIndexPath = @[[NSIndexPath indexPathForRow:0 inSection:SCInventorySectionSteam]];
if (steamInventoryEmptyBefore) {
[self.tableView insertRowsAtIndexPaths:steamIndexPath withRowAnimation:UITableViewRowAnimationFade];
} else {
[self.tableView deleteRowsAtIndexPaths:steamIndexPath withRowAnimation:UITableViewRowAnimationFade];
}
}

NSUInteger inventoryCountBefore = [self.inventories count];
self.inventories = [newInventories sortedArrayUsingSelector:@selector(compare:)];
BOOL inventoriesEmptyAfter = [self.inventories count] == 0;
[self.tableView reloadSections:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0, 2)]
withRowAnimation:UITableViewRowAnimationFade];
NSUInteger inventoryCountAfter = [self.inventories count];
NSIndexSet *gamesSection = [NSIndexSet indexSetWithIndex:SCInventorySectionGames];
if (inventoriesEmptyBefore != inventoriesEmptyAfter) {
if (inventoriesEmptyAfter) {
if (inventoryCountBefore != inventoryCountAfter) {
if (inventoryCountBefore == 0) {
[self.tableView insertSections:gamesSection withRowAnimation:UITableViewRowAnimationFade];
} else {
} else if (inventoryCountAfter == 0) {
[self.tableView deleteSections:gamesSection withRowAnimation:UITableViewRowAnimationFade];
} else {
[self.tableView reloadSections:gamesSection withRowAnimation:UITableViewRowAnimationFade];
}
} else {
[self.tableView reloadSections:gamesSection withRowAnimation:UITableViewRowAnimationFade];
}

NSArray *noInventoriesPath = @[[NSIndexPath indexPathForRow:0 inSection:SCInventorySectionNoInventories]];
if (steamInventoryEmptyBefore && inventoryCountBefore == 0 && !(steamInventoryEmptyAfter && inventoryCountAfter == 0)) {
[self.tableView deleteRowsAtIndexPaths:noInventoriesPath withRowAnimation:UITableViewRowAnimationFade];
} else if (!(steamInventoryEmptyBefore && inventoryCountBefore == 0) && steamInventoryEmptyAfter && inventoryCountAfter == 0) {
[self.tableView insertRowsAtIndexPaths:noInventoriesPath withRowAnimation:UITableViewRowAnimationFade];
}

[self.tableView endUpdates];
Expand Down

0 comments on commit b810644

Please sign in to comment.