From b81064448dd8defa9f1ab32a9241bc1f06d24265 Mon Sep 17 00:00:00 2001 From: Sebastian Staudt Date: Wed, 17 Jun 2015 07:25:56 +0200 Subject: [PATCH] Fix filtering of inventories on iPad --- Suitcase/Classes/SCGamesViewController.m | 47 ++++++++++++++++-------- 1 file changed, 32 insertions(+), 15 deletions(-) diff --git a/Suitcase/Classes/SCGamesViewController.m b/Suitcase/Classes/SCGamesViewController.m index 9bf571a..5d53dc6 100644 --- a/Suitcase/Classes/SCGamesViewController.m +++ b/Suitcase/Classes/SCGamesViewController.m @@ -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; @@ -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 inventory, NSUInteger idx, BOOL *stop) { if (![inventory isLoaded] || [inventory failed]) { @@ -455,7 +454,7 @@ - (void)settingsChanged:(NSNotification *)notification { } if ([inventory.game isSteam]) { - self.steamInventory = inventory; + newSteamInventory = inventory; } else { [newInventories addObject:inventory]; } @@ -463,20 +462,38 @@ - (void)settingsChanged:(NSNotification *)notification { 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];