Skip to content

Commit

Permalink
bug fix on swipe to delete in favorites tableView
Browse files Browse the repository at this point in the history
  • Loading branch information
grenos committed Apr 13, 2020
1 parent 92ea79e commit 4c7ed20
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
4 changes: 4 additions & 0 deletions GHFollowers.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -85,6 +86,7 @@
7DB7351124409BFE00E7B09B /* GFTabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFTabBarController.swift; sourceTree = "<group>"; };
7DB735132443929200E7B09B /* GFDataLoadingVC.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFDataLoadingVC.swift; sourceTree = "<group>"; };
7DB7351524447F5800E7B09B /* UIView+EXt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIView+EXt.swift"; sourceTree = "<group>"; };
7DB73517244489C400E7B09B /* UITableView+EXT.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UITableView+EXT.swift"; sourceTree = "<group>"; };
7DFAC889242A86D200F5780C /* GFAlertContainerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GFAlertContainerView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -173,6 +175,7 @@
7DB735082438AD0F00E7B09B /* Date+Ext.swift */,
7DB7350A2438AD9000E7B09B /* String+Ext.swift */,
7DB7351524447F5800E7B09B /* UIView+EXt.swift */,
7DB73517244489C400E7B09B /* UITableView+EXT.swift */,
);
path = Extensions;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
Binary file not shown.
16 changes: 16 additions & 0 deletions GHFollowers/Extensions/UITableView+EXT.swift
Original file line number Diff line number Diff line change
@@ -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)
}
}
13 changes: 5 additions & 8 deletions GHFollowers/Managers/PersistanceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
22 changes: 13 additions & 9 deletions GHFollowers/Screens/FavoriteListVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit 4c7ed20

Please sign in to comment.