Skip to content

Commit

Permalink
Bug 1315246 - Removed spot reloadSections calls and grouped into sing…
Browse files Browse the repository at this point in the history
…le reloadData call (#2232) r=farhan
  • Loading branch information
Stephan Leroux committed Nov 14, 2016
2 parents 1f93b0a + f63afa8 commit 94eeb3e
Showing 1 changed file with 46 additions and 42 deletions.
88 changes: 46 additions & 42 deletions Client/Frontend/Home/ActivityStreamPanel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,15 @@ class ActivityStreamPanel: UITableViewController, HomePanel {
tableView.estimatedSectionHeaderHeight = 15
tableView.sectionFooterHeight = 0
tableView.sectionHeaderHeight = UITableViewAutomaticDimension

reloadTopSites()
reloadHighlights()
}

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.onyxSession = OnyxTelemetry.sharedClient.beginSession()

all([invalidateTopSites(), invalidateHighlights()]).uponQueue(dispatch_get_main_queue()) { _ in
self.reloadAll()
}
}

override func viewDidDisappear(animated: Bool) {
Expand Down Expand Up @@ -250,55 +251,54 @@ extension ActivityStreamPanel {
func notificationReceived(notification: NSNotification) {
switch notification.name {
case NotificationProfileDidFinishSyncing, NotificationFirefoxAccountChanged, NotificationPrivateDataClearedHistory, NotificationDynamicFontChanged:
self.reloadTopSites()
self.invalidateTopSites().uponQueue(dispatch_get_main_queue()) { _ in
self.reloadAll()
}
default:
log.warning("Received unexpected notification \(notification.name)")
}
}

private func reloadHighlights() {
fetchHighlights().uponQueue(dispatch_get_main_queue()) { result in
self.highlights = result.successValue?.asArray() ?? self.highlights
self.tableView.reloadSections(NSIndexSet(index: Section.Highlights.rawValue), withRowAnimation: .Automatic)
}
}

private func fetchHighlights() -> Deferred<Maybe<Cursor<Site>>> {
return self.profile.recommendations.getHighlights()
private func reloadAll() {
self.tableView.reloadData()
}

private func reloadTopSites() {
invalidateTopSites().uponQueue(dispatch_get_main_queue()) { result in
let defaultSites = self.defaultTopSites()
let mySites = (result.successValue ?? [])

// Merge default topsites with a user's topsites.
let mergedSites = mySites.union(defaultSites, f: { (site) -> String in
return NSURL(string: site.url)?.extractDomainName() ?? ""
})

// Favour topsites from defaultSites as they have better favicons.
let newSites = mergedSites.map { site -> Site in
let domain = NSURL(string: site.url)?.extractDomainName() ?? ""
return defaultSites.find { $0.title.lowercaseString == domain } ?? site
}

self.topSitesManager.currentTraits = self.view.traitCollection
self.topSitesManager.content = newSites.count > ASPanelUX.topSitesCacheSize ? Array(newSites[0..<ASPanelUX.topSitesCacheSize]) : newSites
self.topSitesManager.urlPressedHandler = { [unowned self] url, indexPath in
let event = ASInfo(actionPosition: indexPath.item, source: .topSites)
ASOnyxPing.reportTapEvent(event)
self.showSiteWithURLHandler(url)
}
self.tableView.reloadSections(NSIndexSet(index: Section.TopSites.rawValue), withRowAnimation: .Automatic)
private func invalidateHighlights() -> Success {
return self.profile.recommendations.getHighlights().bindQueue(dispatch_get_main_queue()) { result in
self.highlights = result.successValue?.asArray() ?? self.highlights
return succeed()
}
}

private func invalidateTopSites() -> Deferred<Maybe<[Site]>> {
private func invalidateTopSites() -> Success {
let frecencyLimit = ASPanelUX.topSitesCacheSize
return self.profile.history.updateTopSitesCacheIfInvalidated() >>== { dirty in

// Update our top sites cache if it's been invalidated
return self.profile.history.updateTopSitesCacheIfInvalidated() >>== { _ in
return self.profile.history.getTopSitesWithLimit(frecencyLimit) >>== { topSites in
return deferMaybe(topSites.asArray())
let mySites = topSites.asArray()
let defaultSites = self.defaultTopSites()

// Merge default topsites with a user's topsites.
let mergedSites = mySites.union(defaultSites, f: { (site) -> String in
return NSURL(string: site.url)?.extractDomainName() ?? ""
})

// Favour topsites from defaultSites as they have better favicons.
let newSites = mergedSites.map { site -> Site in
let domain = NSURL(string: site.url)?.extractDomainName() ?? ""
return defaultSites.find { $0.title.lowercaseString == domain } ?? site
}

self.topSitesManager.currentTraits = self.view.traitCollection
self.topSitesManager.content = newSites.count > ASPanelUX.topSitesCacheSize ? Array(newSites[0..<ASPanelUX.topSitesCacheSize]) : newSites
self.topSitesManager.urlPressedHandler = { [unowned self] url, indexPath in
let event = ASInfo(actionPosition: indexPath.item, source: .topSites)
ASOnyxPing.reportTapEvent(event)
self.showSiteWithURLHandler(url)
}

return succeed()
}
}
}
Expand All @@ -313,14 +313,18 @@ extension ActivityStreamPanel {
}
profile.history.removeHostFromTopSites(host).uponQueue(dispatch_get_main_queue()) { result in
guard result.isSuccess else { return }
self.reloadTopSites()
self.invalidateTopSites().uponQueue(dispatch_get_main_queue()) { _ in
self.reloadAll()
}
}
}

func hideFromHighlights(site: Site) {
profile.recommendations.removeHighlightForURL(site.url).uponQueue(dispatch_get_main_queue()) { result in
guard result.isSuccess else { return }
self.reloadHighlights()
self.invalidateHighlights().uponQueue(dispatch_get_main_queue()) { _ in
self.reloadAll()
}
}
}

Expand Down

0 comments on commit 94eeb3e

Please sign in to comment.