From f2e7593d04ab2ccacd1560dcef290491cd63882e Mon Sep 17 00:00:00 2001 From: nixzhu Date: Thu, 21 Jan 2016 10:30:16 +0800 Subject: [PATCH] only update if profileUserIsMe --- .../Profile/ProfileViewController.swift | 9 +-- .../ProfileFooter/ProfileFooterCell.swift | 62 +++++++++++++++---- 2 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Yep/ViewControllers/Profile/ProfileViewController.swift b/Yep/ViewControllers/Profile/ProfileViewController.swift index 1b1d1a68..3b114952 100644 --- a/Yep/ViewControllers/Profile/ProfileViewController.swift +++ b/Yep/ViewControllers/Profile/ProfileViewController.swift @@ -1444,15 +1444,8 @@ extension ProfileViewController: UICollectionViewDataSource, UICollectionViewDel case ProfileSection.Footer.rawValue: let cell = collectionView.dequeueReusableCellWithReuseIdentifier(footerCellIdentifier, forIndexPath: indexPath) as! ProfileFooterCell - cell.configureWithNickname(profileUser?.nickname ?? "", username: profileUser?.username, introduction: introductionText) - if let profileUser = profileUser { - switch profileUser { - case .DiscoveredUserType(let discoveredUser): - cell.location = CLLocation(latitude: discoveredUser.latitude, longitude: discoveredUser.longitude) - case .UserType(let user): - cell.location = CLLocation(latitude: user.latitude, longitude: user.longitude) - } + cell.configureWithProfileUser(profileUser, introduction: introductionText) } return cell diff --git a/Yep/Views/Cells/ProfileFooter/ProfileFooterCell.swift b/Yep/Views/Cells/ProfileFooter/ProfileFooterCell.swift index 2b9579f1..76def121 100644 --- a/Yep/Views/Cells/ProfileFooter/ProfileFooterCell.swift +++ b/Yep/Views/Cells/ProfileFooter/ProfileFooterCell.swift @@ -36,6 +36,38 @@ class ProfileFooterCell: UICollectionViewCell { YepUserDefaults.userLocationName.removeListenerWithName(listener.userLocationName) } + private func updateUIWithUserLocationName(userLocationName: String?) { + + if let userLocationName = userLocationName { + locationContainerView.hidden = false + locationLabel.text = userLocationName + + } else { + locationContainerView.hidden = true + } + } + + var profileUserIsMe = false { + didSet { + if profileUserIsMe { + YepUserDefaults.userLocationName.bindAndFireListener(listener.userLocationName, action: { [weak self] userLocationName in + self?.updateUIWithUserLocationName(userLocationName) + }) + } + } + } + + var newUserLocationName: String? { + didSet { + if profileUserIsMe { + YepUserDefaults.userLocationName.value = newUserLocationName + + } else { + updateUIWithUserLocationName(newUserLocationName) + } + } + } + override func awakeFromNib() { super.awakeFromNib() @@ -45,19 +77,24 @@ class ProfileFooterCell: UICollectionViewCell { introductionLabel.font = YepConfig.Profile.introductionLabelFont introductionLabel.textColor = UIColor.yepGrayColor() - YepUserDefaults.userLocationName.bindAndFireListener(listener.userLocationName, action: { [weak self] userLocationName in - - if let userLocationName = userLocationName { - self?.locationContainerView.hidden = false - self?.locationLabel.text = userLocationName - - } else { - self?.locationContainerView.hidden = true - } - }) + newUserLocationName = nil } - func configureWithNickname(nickname: String, username: String?, introduction: String) { + func configureWithProfileUser(profileUser: ProfileUser, introduction: String) { + + profileUserIsMe = profileUser.isMe + + configureWithNickname(profileUser.nickname ?? "", username: profileUser.username, introduction: introduction) + + switch profileUser { + case .DiscoveredUserType(let discoveredUser): + location = CLLocation(latitude: discoveredUser.latitude, longitude: discoveredUser.longitude) + case .UserType(let user): + location = CLLocation(latitude: user.latitude, longitude: user.longitude) + } + } + + private func configureWithNickname(nickname: String, username: String?, introduction: String) { nicknameLabel.text = nickname @@ -92,8 +129,7 @@ class ProfileFooterCell: UICollectionViewCell { if let placemarks = placemarks { if let firstPlacemark = placemarks.first { - let name = firstPlacemark.locality ?? (firstPlacemark.name ?? firstPlacemark.country) - YepUserDefaults.userLocationName.value = name + self?.newUserLocationName = firstPlacemark.locality ?? (firstPlacemark.name ?? firstPlacemark.country) } } }