Skip to content

Commit

Permalink
More CloudKit logging stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
insidegui committed May 25, 2017
1 parent 69584e0 commit 81c0bc9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
10 changes: 10 additions & 0 deletions CommunitySupport/CMSCommunityCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extension Notification.Name {
public static let CMSDidFindUserIdentifier = Notification.Name("CMSDidFindUserIdentifierNotificationName")
public static let CMSErrorOccurred = Notification.Name("CMSErrorOccurredNotificationName")
public static let CMSUserProfileDidChange = Notification.Name("CMSUserProfileDidChangeNotificationName")
public static let CMSUserIdentifierDidBecomeAvailable = Notification.Name("CMSUserIdentifierDidBecomeAvailableNotificationName")
}

public final class CMSCommunityCenter: NSObject {
Expand Down Expand Up @@ -92,6 +93,7 @@ public final class CMSCommunityCenter: NSObject {
container.fetchUserRecordID { userRecordID, error in
if let error = retryCloudKitOperationIfPossible(with: error, block: retryBlock) {
DispatchQueue.main.async {
NSLog("[CMSCommunityCenter] Unable to get user record ID from CloudKit: \(error)")
self.sendErrorNotification(with: error, info: "Trying to get user record ID")
completion(.error(error))
}
Expand All @@ -100,20 +102,28 @@ public final class CMSCommunityCenter: NSObject {

guard let userRecordID = userRecordID else {
DispatchQueue.main.async {
NSLog("[CMSCommunityCenter] Nil user record ID")
self.sendErrorNotification(with: nil, info: "Nil user record ID")
completion(.error(CMSCloudKitError.invalidData("No record ID found")))
}
return
}

DispatchQueue.main.async {
NSLog("[CMSCommunityCenter] User record ID: \(userRecordID.recordName)")
NotificationCenter.default.post(name: .CMSUserIdentifierDidBecomeAvailable, object: userRecordID.recordName)
}

self.database.fetch(withRecordID: userRecordID) { userRecord, error in
if let error = retryCloudKitOperationIfPossible(with: error, block: retryBlock) {
NSLog("[CMSCommunityCenter] Unable to get the user record from CloudKit: \(error)")
self.sendErrorNotification(with: error, info: "Trying to get the user record itself")
DispatchQueue.main.async { completion(.error(error)) }
return
}

guard let userRecord = userRecord else {
NSLog("[CMSCommunityCenter] Nil user record")
self.sendErrorNotification(with: nil, info: "Nil user record")
DispatchQueue.main.async { completion(.error(CMSCloudKitError.invalidData("No user record"))) }
return
Expand Down
15 changes: 10 additions & 5 deletions WWDC/AccountPreferencesViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class AccountPreferencesViewController: NSViewController, WWDCImageViewDelegate
f.maximumNumberOfLines = 5
f.translatesAutoresizingMaskIntoConstraints = false
f.alignment = .center
f.isSelectable = false

return f
}()
Expand All @@ -89,6 +90,7 @@ class AccountPreferencesViewController: NSViewController, WWDCImageViewDelegate

let f = WWDCTextField(wrappingLabelWithString: help)

f.isSelectable = false
f.font = NSFont.systemFont(ofSize: 14, weight: NSFontWeightMedium)
f.textColor = .prefsPrimaryText
f.cell?.backgroundStyle = .dark
Expand All @@ -100,19 +102,18 @@ class AccountPreferencesViewController: NSViewController, WWDCImageViewDelegate
}()

private lazy var errorLabel: NSTextField = {
let help = "This feature requires your macOS account to have an iCloud account. Please go to System Preferences and log in to your iCloud account."
let help = "This feature requires your macOS account to have an iCloud account.\nPlease go to System Preferences and log in to your iCloud account."

let f = NSTextField(wrappingLabelWithString: help)

f.font = NSFont.systemFont(ofSize: 14, weight: NSFontWeightRegular)
f.textColor = .errorText
f.cell?.backgroundStyle = .dark
f.isSelectable = true
f.lineBreakMode = .byWordWrapping
f.setContentCompressionResistancePriority(NSLayoutPriorityDefaultLow, for: .horizontal)
f.allowsDefaultTighteningForTruncation = true
f.maximumNumberOfLines = 5
f.translatesAutoresizingMaskIntoConstraints = false
f.alignment = .center
f.isHidden = true
f.isSelectable = false

return f
}()
Expand All @@ -130,6 +131,7 @@ class AccountPreferencesViewController: NSViewController, WWDCImageViewDelegate
view.addSubview(nameLabel)
view.addSubview(avatarImageView)
view.addSubview(infoLabel)
view.addSubview(errorLabel)
view.addSubview(permissionLabel)
view.addSubview(completeButton)

Expand All @@ -142,6 +144,9 @@ class AccountPreferencesViewController: NSViewController, WWDCImageViewDelegate
infoLabel.centerXAnchor.constraint(equalTo: nameLabel.centerXAnchor).isActive = true
infoLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 40).isActive = true

errorLabel.centerXAnchor.constraint(equalTo: nameLabel.centerXAnchor).isActive = true
errorLabel.topAnchor.constraint(equalTo: nameLabel.bottomAnchor, constant: 40).isActive = true

permissionLabel.centerXAnchor.constraint(equalTo: infoLabel.centerXAnchor).isActive = true
permissionLabel.topAnchor.constraint(equalTo: infoLabel.bottomAnchor, constant: 12).isActive = true

Expand Down
10 changes: 8 additions & 2 deletions WWDC/LoggingHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ final class LoggingHelper {

Fabric.with([Crashlytics.self])

observeCommunityCenterErrors()
observeCommunityCenterNotifications()
}

static func registerCloudKitUserIdentifier(_ identifier: String) {
Expand All @@ -41,7 +41,7 @@ final class LoggingHelper {
Answers.logCustomEvent(withName: name, customAttributes: info)
}

static func observeCommunityCenterErrors() {
static func observeCommunityCenterNotifications() {
NotificationCenter.default.addObserver(forName: .CMSErrorOccurred, object: nil, queue: OperationQueue.main) { note in
let error: Error

Expand All @@ -53,6 +53,12 @@ final class LoggingHelper {

LoggingHelper.registerError(error, info: note.userInfo as? [String: Any])
}

NotificationCenter.default.addObserver(forName: .CMSUserIdentifierDidBecomeAvailable, object: nil, queue: OperationQueue.main) { note in
guard let identifier = note.object as? String else { return }

LoggingHelper.registerCloudKitUserIdentifier(identifier)
}
}

}

0 comments on commit 81c0bc9

Please sign in to comment.