diff --git a/GHFollowers.xcodeproj/project.pbxproj b/GHFollowers.xcodeproj/project.pbxproj index b7d3fe4..82c6a95 100644 --- a/GHFollowers.xcodeproj/project.pbxproj +++ b/GHFollowers.xcodeproj/project.pbxproj @@ -43,6 +43,7 @@ 7DB7351224409BFE00E7B09B /* GFTabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7351124409BFE00E7B09B /* GFTabBarController.swift */; }; 7DB735142443929200E7B09B /* GFDataLoadingVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB735132443929200E7B09B /* GFDataLoadingVC.swift */; }; 7DB7351624447F5800E7B09B /* UIView+EXt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB7351524447F5800E7B09B /* UIView+EXt.swift */; }; + 7DB73518244489C400E7B09B /* UITableView+EXT.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DB73517244489C400E7B09B /* UITableView+EXT.swift */; }; 7DFAC88A242A86D200F5780C /* GFAlertContainerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */; }; /* End PBXBuildFile section */ @@ -85,6 +86,7 @@ 7DB7351124409BFE00E7B09B /* GFTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFTabBarController.swift; sourceTree = ""; }; 7DB735132443929200E7B09B /* GFDataLoadingVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFDataLoadingVC.swift; sourceTree = ""; }; 7DB7351524447F5800E7B09B /* UIView+EXt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+EXt.swift"; sourceTree = ""; }; + 7DB73517244489C400E7B09B /* UITableView+EXT.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView+EXT.swift"; sourceTree = ""; }; 7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFAlertContainerView.swift; sourceTree = ""; }; /* End PBXFileReference section */ @@ -173,6 +175,7 @@ 7DB735082438AD0F00E7B09B /* Date+Ext.swift */, 7DB7350A2438AD9000E7B09B /* String+Ext.swift */, 7DB7351524447F5800E7B09B /* UIView+EXt.swift */, + 7DB73517244489C400E7B09B /* UITableView+EXT.swift */, ); path = Extensions; sourceTree = ""; @@ -379,6 +382,7 @@ 7D98BB4C242A7327006C53E9 /* UIViewController+EXT.swift in Sources */, 7DA45F812433DE1300AB426F /* GFTitleLabel-Secondary.swift in Sources */, 7D98BB44242A29B4006C53E9 /* FollowerListVC.swift in Sources */, + 7DB73518244489C400E7B09B /* UITableView+EXT.swift in Sources */, 7DB7350D243F523700E7B09B /* PersistanceManager.swift in Sources */, 7DB7350B2438AD9000E7B09B /* String+Ext.swift in Sources */, 7DA45F6C242BAEEC00AB426F /* ErrorMessage.swift in Sources */, diff --git a/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate b/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate index 423a350..a42c737 100644 Binary files a/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate and b/GHFollowers.xcodeproj/project.xcworkspace/xcuserdata/vasilis.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/GHFollowers/Extensions/UITableView+EXT.swift b/GHFollowers/Extensions/UITableView+EXT.swift new file mode 100644 index 0000000..b8b3bd6 --- /dev/null +++ b/GHFollowers/Extensions/UITableView+EXT.swift @@ -0,0 +1,16 @@ +// +// UITableView+EXT.swift +// GHFollowers +// +// Created by Vasileios Gkreen on 13/04/2020. +// Copyright © 2020 Vasileios Gkreen. All rights reserved. +// + +import UIKit + + +extension UITableView { + func removeUnusedCells() { + tableFooterView = UIView(frame: .zero) + } +} diff --git a/GHFollowers/Managers/PersistanceManager.swift b/GHFollowers/Managers/PersistanceManager.swift index 06592ce..0a80324 100644 --- a/GHFollowers/Managers/PersistanceManager.swift +++ b/GHFollowers/Managers/PersistanceManager.swift @@ -32,30 +32,27 @@ enum PersistanceManager { // first we need to get the favorites array from User Defaults retrieveFavorites { result in switch result { - case .success(let favorites): - - // create a new array from the favorites array (because now it is imutable) - var retrievedFavorites = favorites + case .success(var favorites): // see if we want to add or remove a follower switch actionType { case .add: // see if follwer is alreay added to array - guard !retrievedFavorites.contains(favorite) else { + guard !favorites.contains(favorite) else { completed(.alreadyInFavorites) return } // if user isn't in array of favorites - retrievedFavorites.append(favorite) + favorites.append(favorite) case .remove: // if we want to remove, remove all items with same login with passed user - retrievedFavorites.removeAll { $0.login == favorite.login } + favorites.removeAll { $0.login == favorite.login } } // call function to save the updated favorites Array - completed(save(favorites: retrievedFavorites)) + completed(save(favorites: favorites)) case .failure(let error): diff --git a/GHFollowers/Screens/FavoriteListVC.swift b/GHFollowers/Screens/FavoriteListVC.swift index 3896960..c5096cc 100644 --- a/GHFollowers/Screens/FavoriteListVC.swift +++ b/GHFollowers/Screens/FavoriteListVC.swift @@ -41,6 +41,7 @@ class FavoriteListVC: GFDataLoadingVC { tableView.rowHeight = 80 tableView.delegate = self tableView.dataSource = self + tableView.removeUnusedCells() tableView.register(FavoriteCell.self, forCellReuseIdentifier: FavoriteCell.reuseID) } @@ -107,20 +108,23 @@ extension FavoriteListVC: UITableViewDelegate, UITableViewDataSource { // make sure we delete guard editingStyle == .delete else { return } - // get selected user to delete + // get selected user let favorite = favorites[indexPath.row] - // delete user from array that we use to populate the tableview - favorites.remove(at: indexPath.row) - // delete user from the tableView - tableView.deleteRows(at: [indexPath], with: .left) - - + // delete user from persistnace PersistanceManager.updateWith(favorite: favorite, actionType: PersistanceActionType.remove) { [weak self] (error) in guard let self = self else { return } - // if we dont have error dont do nothing and return - guard let error = error else { return } + // if we dont have error update UI + guard let error = error else { + + // delete user from array that we use to populate the tableview + self.favorites.remove(at: indexPath.row) + // delete user from the tableView + tableView.deleteRows(at: [indexPath], with: .left) + return + + } // if we have an error self.presentGFAlertOnMainThread(title: "Oooops. Unable to remove user 😩", message: error.rawValue, buttonTitle: "Ok")