From 47b2b4980c83086b11166b9fd76dee25f1cf4cc7 Mon Sep 17 00:00:00 2001 From: kevinzhow Date: Sun, 18 Oct 2015 04:32:41 +0800 Subject: [PATCH] Better performance on skill --- Yep/Info.plist | 2 +- .../Discover/DiscoverViewController.swift | 23 +++++++++++++++++++ .../DiscoverCardUserCell.swift | 13 ++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/Yep/Info.plist b/Yep/Info.plist index 2de6ea5e..56147ea4 100644 --- a/Yep/Info.plist +++ b/Yep/Info.plist @@ -32,7 +32,7 @@ CFBundleVersion - 112 + 113 Fabric APIKey diff --git a/Yep/ViewControllers/Discover/DiscoverViewController.swift b/Yep/ViewControllers/Discover/DiscoverViewController.swift index 4a8599ea..613ed0e3 100644 --- a/Yep/ViewControllers/Discover/DiscoverViewController.swift +++ b/Yep/ViewControllers/Discover/DiscoverViewController.swift @@ -13,6 +13,8 @@ enum DiscoverUserMode: Int { case Card } +var skillSizeCache = [String: CGRect]() + class DiscoverViewController: BaseViewController { @IBOutlet weak var discoverCollectionView: UICollectionView! @@ -60,6 +62,27 @@ class DiscoverViewController: BaseViewController { } }, completion: { discoveredUsers in + + for user in discoveredUsers { + + for skill in user.masterSkills { + + let skillLocalName = skill.localName ?? "" + + let skillID = skill.id + + if let _ = skillSizeCache[skillID] { + + } else { + let rect = skillLocalName.boundingRectWithSize(CGSize(width: CGFloat(FLT_MAX), height: SkillCell.height), options: [.UsesLineFragmentOrigin, .UsesFontLeading], attributes: skillTextAttributes, context: nil) + + skillSizeCache[skillID] = rect + } + + } + + } + dispatch_async(dispatch_get_main_queue()) { [weak self] in self?.discoveredUsers = discoveredUsers self?.activityIndicator.stopAnimating() diff --git a/Yep/Views/Cells/DiscoverCardUser/DiscoverCardUserCell.swift b/Yep/Views/Cells/DiscoverCardUser/DiscoverCardUserCell.swift index 9ce57bb7..ed2e0562 100644 --- a/Yep/Views/Cells/DiscoverCardUser/DiscoverCardUserCell.swift +++ b/Yep/Views/Cells/DiscoverCardUser/DiscoverCardUserCell.swift @@ -8,6 +8,8 @@ import UIKit +let skillTextAttributes = [NSFontAttributeName: UIFont.skillDiscoverTextFont()] + class DiscoverCardUserCell: UICollectionViewCell { @IBOutlet weak var serviceImageView: UIImageView! @@ -22,12 +24,8 @@ class DiscoverCardUserCell: UICollectionViewCell { @IBOutlet weak var userSkillsCollectionView: UICollectionView! - var skillSizeCache = [String: CGRect]() - var discoveredUser: DiscoveredUser? - let skillTextAttributes = [NSFontAttributeName: UIFont.skillDiscoverTextFont()] - let discoverCellHeight: CGFloat = 16 override func awakeFromNib() { @@ -81,15 +79,18 @@ class DiscoverCardUserCell: UICollectionViewCell { // } usernameLabel.text = discoveredUser.nickname - - userSkillsCollectionView.reloadData() + userSkillsCollectionView.reloadData() } } extension DiscoverCardUserCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { + func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int { + return 1 + } + func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { if let discoveredUser = discoveredUser { if discoveredUser.masterSkills.count > 4 {