Skip to content

Commit

Permalink
#8 Create AuthManager mock, inject into classes
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrorseth committed Aug 26, 2018
1 parent ef7acb9 commit 2b65918
Show file tree
Hide file tree
Showing 14 changed files with 1,514 additions and 1,467 deletions.
22 changes: 18 additions & 4 deletions AtMe.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
5D1C70431E58C64A00FEDEC3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 5D1C70421E58C64A00FEDEC3 /* Assets.xcassets */; };
5D1C70461E58C64A00FEDEC3 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5D1C70441E58C64A00FEDEC3 /* LaunchScreen.storyboard */; };
5D28129C2132401200BD616E /* AuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D28129B2132401200BD616E /* AuthManager.swift */; };
5D28129F21324DE500BD616E /* MockAuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D28129E21324DE500BD616E /* MockAuthManager.swift */; };
5D2812A021324E2F00BD616E /* MockAuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D28129E21324DE500BD616E /* MockAuthManager.swift */; };
5D2B62601EC909A300B8363E /* PromptViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D2B625F1EC909A300B8363E /* PromptViewController.swift */; };
5D2B62621EC909CC00B8363E /* PromptView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D2B62611EC909CC00B8363E /* PromptView.swift */; };
5D323B721F107C3E00853EDE /* EmptyChatListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5D323B711F107C3E00853EDE /* EmptyChatListView.swift */; };
Expand Down Expand Up @@ -49,7 +51,7 @@
5DAF28B71E5BBCBC00288F31 /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DAF28B61E5BBCBC00288F31 /* Constants.swift */; };
5DAFF1621E72FD02008DE303 /* SignUpViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DAFF1611E72FD02008DE303 /* SignUpViewController.swift */; };
5DD231841F2B65BE0058DD54 /* ChatToolbarView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DD231831F2B65BE0058DD54 /* ChatToolbarView.swift */; };
5DE764EE1F0046E700E483C6 /* AuthController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DE764ED1F0046E700E483C6 /* AuthController.swift */; };
5DE764EE1F0046E700E483C6 /* FirebaseAuthManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DE764ED1F0046E700E483C6 /* FirebaseAuthManager.swift */; };
5DE764F01F00814900E483C6 /* UserSetupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DE764EF1F00814900E483C6 /* UserSetupViewController.swift */; };
5DF36ED91EB3D35400346868 /* Message.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DF36ED81EB3D35400346868 /* Message.swift */; };
5DFC7F3D1E7A27CF003C6594 /* ConversationCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5DFC7F3C1E7A27CF003C6594 /* ConversationCell.swift */; };
Expand Down Expand Up @@ -108,6 +110,7 @@
5D22766F20C9B6C8002E6958 /* AtMeTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AtMeTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
5D22767320C9B6C8002E6958 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
5D28129B2132401200BD616E /* AuthManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AuthManager.swift; sourceTree = "<group>"; };
5D28129E21324DE500BD616E /* MockAuthManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockAuthManager.swift; sourceTree = "<group>"; };
5D2B625F1EC909A300B8363E /* PromptViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PromptViewController.swift; sourceTree = "<group>"; };
5D2B62611EC909CC00B8363E /* PromptView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PromptView.swift; sourceTree = "<group>"; };
5D323B711F107C3E00853EDE /* EmptyChatListView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EmptyChatListView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -137,7 +140,7 @@
5DAF28B61E5BBCBC00288F31 /* Constants.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
5DAFF1611E72FD02008DE303 /* SignUpViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SignUpViewController.swift; sourceTree = "<group>"; };
5DD231831F2B65BE0058DD54 /* ChatToolbarView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ChatToolbarView.swift; sourceTree = "<group>"; };
5DE764ED1F0046E700E483C6 /* AuthController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = AuthController.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
5DE764ED1F0046E700E483C6 /* FirebaseAuthManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = FirebaseAuthManager.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
5DE764EF1F00814900E483C6 /* UserSetupViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserSetupViewController.swift; sourceTree = "<group>"; };
5DF36ED81EB3D35400346868 /* Message.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Message.swift; sourceTree = "<group>"; };
5DFC7F3C1E7A27CF003C6594 /* ConversationCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConversationCell.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -242,7 +245,7 @@
5DF36ED81EB3D35400346868 /* Message.swift */,
5D5D67751ED1E90200FF8F2A /* DatabaseController.swift */,
5D65EBF51EFA1798009F512E /* NotificationsController.swift */,
5DE764ED1F0046E700E483C6 /* AuthController.swift */,
5DE764ED1F0046E700E483C6 /* FirebaseAuthManager.swift */,
);
name = Models;
sourceTree = "<group>";
Expand Down Expand Up @@ -310,6 +313,7 @@
5D22767020C9B6C8002E6958 /* AtMeTests */ = {
isa = PBXGroup;
children = (
5D28129D21324C8F00BD616E /* Mocks */,
5D22766A20C9B68B002E6958 /* Models */,
5D22766920C9B682002E6958 /* Controllers */,
5D22766820C9B672002E6958 /* Cells */,
Expand All @@ -327,6 +331,14 @@
name = Protocols;
sourceTree = "<group>";
};
5D28129D21324C8F00BD616E /* Mocks */ = {
isa = PBXGroup;
children = (
5D28129E21324DE500BD616E /* MockAuthManager.swift */,
);
path = Mocks;
sourceTree = "<group>";
};
5D5EDF831EC3D01900F7FC44 /* Views */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -628,6 +640,7 @@
5D04E98C1E58E24E006D8CD5 /* ConvoViewController.swift in Sources */,
5D3DDA381E62469000B6FBB7 /* UserInfoCell.swift in Sources */,
5D65EBF61EFA1798009F512E /* NotificationsController.swift in Sources */,
5D2812A021324E2F00BD616E /* MockAuthManager.swift in Sources */,
5DA1C37C1F33FCA8002E2773 /* ReportUserViewController.swift in Sources */,
5DA9E0DD1E5B6E0900F38C96 /* MessageCell.swift in Sources */,
5D04E98A1E58DD0C006D8CD5 /* ChatListViewController.swift in Sources */,
Expand Down Expand Up @@ -658,7 +671,7 @@
5D00D1061F33A72E00130C59 /* BlockedUserCell.swift in Sources */,
5D2B62601EC909A300B8363E /* PromptViewController.swift in Sources */,
5D6394F11F314F800095C1C5 /* LegalViewController.swift in Sources */,
5DE764EE1F0046E700E483C6 /* AuthController.swift in Sources */,
5DE764EE1F0046E700E483C6 /* FirebaseAuthManager.swift in Sources */,
5D9D443A1EAEE81500CF25F7 /* UserState.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand All @@ -668,6 +681,7 @@
buildActionMask = 2147483647;
files = (
5D7549C820CB24B9000683D9 /* AuthControllerTests.swift in Sources */,
5D28129F21324DE500BD616E /* MockAuthManager.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
8 changes: 8 additions & 0 deletions AtMe.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
4 changes: 2 additions & 2 deletions AtMe/AuthManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
// Copyright © 2018 Joel Rorseth. All rights reserved.
//

import Foundation

protocol AuthManager {

var authenticationDelegate: AuthenticationDelegate? { get set }

func createAccount(email: String, firstName: String, lastName: String, password: String,
completion: @escaping ((Error?, String?) -> ()))

Expand Down
232 changes: 116 additions & 116 deletions AtMe/BlockedUsersViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,130 +9,130 @@
import UIKit

class BlockedUsersViewController: UITableViewController {

lazy var databaseManager = DatabaseController()
lazy var authManager = AuthController()

lazy var databaseManager = DatabaseController()
var authManager: AuthManager = FirebaseAuthManager()

var blockedUsers: [UserProfile] = []


/**
Called when the view is done loading in the view controller.
*/
override func viewDidLoad() {
super.viewDidLoad()

var blockedUsers: [UserProfile] = []
// Immediately ask AuthController for blocked users to populate
// Insert these into table view directly since it will return asynchronously

authManager.findCurrentUserBlockedUsers(completion: { user in
self.blockedUsers.append(user)
self.tableView.insertRows(at: [IndexPath(row: self.blockedUsers.count - 1, section: 0)], with: .automatic)
})

/**
Called when the view is done loading in the view controller.
*/
override func viewDidLoad() {
super.viewDidLoad()

// Immediately ask AuthController for blocked users to populate
// Insert these into table view directly since it will return asynchronously

authManager.findCurrentUserBlockedUsers(completion: { user in
self.blockedUsers.append(user)
self.tableView.insertRows(at: [IndexPath(row: self.blockedUsers.count - 1, section: 0)], with: .automatic)
})

self.title = "Blocked"
self.tableView.tableFooterView = UIView()
}
self.title = "Blocked"
self.tableView.tableFooterView = UIView()
}


// MARK: - Table view
/**
Determine the number of sections in the table view.
*/
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}


/**
Determine the number of rows in a given section of the table view.
*/
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {


// MARK: - Table view
/**
Determine the number of sections in the table view.
*/
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
if blockedUsers.count == 0 {

// Create a custom label / view to display when empty
let label = UILabel(frame: tableView.frame)
label.font = Constants.Fonts.emptyViewMessageFont
label.backgroundColor = UIColor.groupTableViewBackground
label.textColor = UIColor.darkGray
label.text = Constants.Messages.noBlockedUsersMessage
label.textAlignment = NSTextAlignment.center
label.translatesAutoresizingMaskIntoConstraints = false
label.sizeToFit()
tableView.backgroundView = label
tableView.separatorStyle = UITableViewCellSeparatorStyle.none

} else {

// Restore to normal look when content is available
tableView.backgroundView = nil
tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
}


/**
Determine the number of rows in a given section of the table view.
*/
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

if blockedUsers.count == 0 {

// Create a custom label / view to display when empty
let label = UILabel(frame: tableView.frame)
label.font = Constants.Fonts.emptyViewMessageFont
label.backgroundColor = UIColor.groupTableViewBackground
label.textColor = UIColor.darkGray
label.text = Constants.Messages.noBlockedUsersMessage
label.textAlignment = NSTextAlignment.center
label.translatesAutoresizingMaskIntoConstraints = false
label.sizeToFit()
tableView.backgroundView = label
tableView.separatorStyle = UITableViewCellSeparatorStyle.none

} else {

// Restore to normal look when content is available
tableView.backgroundView = nil
tableView.separatorStyle = UITableViewCellSeparatorStyle.singleLine
}

return blockedUsers.count
}

return blockedUsers.count
}


/**
Determine the contents of a table view cell at a given index path.
*/
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.CellIdentifiers.blockedUserCell, for: indexPath) as! BlockedUserCell

/**
Determine the contents of a table view cell at a given index path.
*/
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: Constants.CellIdentifiers.blockedUserCell, for: indexPath) as! BlockedUserCell

// Important: Set delegate for button so this view controller knows about taps
cell.blockUserCellDelegate = self
cell.unblockButton.layer.cornerRadius = Constants.Radius.regularRadius

// Extract user info from relevant data source record
cell.nameLabel.text = blockedUsers[indexPath.row].name
cell.usernameLabel.text = "@" + blockedUsers[indexPath.row].username

let uid = blockedUsers[indexPath.row].uid
let path = "displayPictures/\(uid)/\(uid).JPG"


// Also important: Set uid and username for use in the cell delegate
cell.uid = uid
cell.username = blockedUsers[indexPath.row].username


// Download the image into the display picture image view
databaseManager.downloadImage(into: cell.displayPictureImageView, from: path, completion: { _ in
cell.displayPictureImageView.layer.masksToBounds = true
cell.displayPictureImageView.layer.cornerRadius = cell.displayPictureImageView.frame.size.width / 2
})

return cell
}
// Important: Set delegate for button so this view controller knows about taps
cell.blockUserCellDelegate = self
cell.unblockButton.layer.cornerRadius = Constants.Radius.regularRadius

// Extract user info from relevant data source record
cell.nameLabel.text = blockedUsers[indexPath.row].name
cell.usernameLabel.text = "@" + blockedUsers[indexPath.row].username

let uid = blockedUsers[indexPath.row].uid
let path = "displayPictures/\(uid)/\(uid).JPG"


// Also important: Set uid and username for use in the cell delegate
cell.uid = uid
cell.username = blockedUsers[indexPath.row].username


// Download the image into the display picture image view
databaseManager.downloadImage(into: cell.displayPictureImageView, from: path, completion: { _ in
cell.displayPictureImageView.layer.masksToBounds = true
cell.displayPictureImageView.layer.cornerRadius = cell.displayPictureImageView.frame.size.width / 2
})

return cell
}
}

extension BlockedUsersViewController: BlockedUserCellDelegate {

/**
Handle unblocking the selected user and removing them from the table.
- parameters:
- button: A reference to the button which was selected to trigger this change.
- uid: The uid of the user selected to be unblocked.
- username: The username of the user selected to be unblocked.
*/
func didTapUnblock(button: UIButton, uid: String, username: String) {
UIView.animate(withDuration: 0.2, animations: {
button.backgroundColor = UIColor.groupTableViewBackground
button.alpha = 0.0
}, completion: { _ in

// Unblock the user and remove that row from table and data source
self.authManager.unblockUser(uid: uid, username: username)
for (index, user) in self.blockedUsers.enumerated() {
if (uid == user.uid) {
self.blockedUsers.remove(at: index)
self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
}
})
}
/**
Handle unblocking the selected user and removing them from the table.
- parameters:
- button: A reference to the button which was selected to trigger this change.
- uid: The uid of the user selected to be unblocked.
- username: The username of the user selected to be unblocked.
*/
func didTapUnblock(button: UIButton, uid: String, username: String) {

UIView.animate(withDuration: 0.2, animations: {
button.backgroundColor = UIColor.groupTableViewBackground
button.alpha = 0.0

}, completion: { _ in
// Unblock the user and remove that row from table and data source
self.authManager.unblockUser(uid: uid, username: username)

for (index, user) in self.blockedUsers.enumerated() {
if (uid == user.uid) {
self.blockedUsers.remove(at: index)
self.tableView.deleteRows(at: [IndexPath(row: index, section: 0)], with: .automatic)
}
}
})
}
}
Loading

0 comments on commit 2b65918

Please sign in to comment.