Skip to content

Commit

Permalink
Clean up AppCoordinator and have a look at performance
Browse files Browse the repository at this point in the history
  • Loading branch information
allenhumphreys committed Jun 30, 2023
1 parent b9a5585 commit 4b5dca0
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 197 deletions.
35 changes: 35 additions & 0 deletions Packages/ConfCore/ConfCore/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,38 @@ public extension Logging {
public func makeLogger(subsystem: String, category: String) -> Logger {
Logger(subsystem: subsystem, category: category)
}

public protocol Signposting: Logging {
static var signposter: OSSignposter { get }
var signposter: OSSignposter { get }
}

public extension Signposting {
static func makeSignposter() -> OSSignposter { OSSignposter(logger: log) }
var signposter: OSSignposter { Self.signposter }
}

public extension OSSignposter {
/// Convenient but several caveats because OSLogMessage is stupid
func withEscapingOneShotIntervalSignpost<T>(
_ name: StaticString,
_ message: String? = nil,
around task: (@escaping () -> Void) throws -> T
) rethrows -> T {
var state: OSSignpostIntervalState?
if let message {
state = beginInterval(name, id: makeSignpostID(), "\(message)")
} else {
state = beginInterval(name, id: makeSignpostID())
}

let end = {
if let innerState = state {
state = nil
endInterval(name, innerState)
}
}

return try task(end)
}
}
29 changes: 24 additions & 5 deletions Packages/ConfCore/ConfCore/Session.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ public class Session: Object, Decodable {
mediaDuration = other.mediaDuration

// merge assets
let assets = other.assets.filter { otherAsset in
return !self.assets.contains(where: { $0.identifier == otherAsset.identifier })
// Pulling the identifiers into Swift Arrays is an optimization for realm
let currentAssetIds = Array(self.assets.map(\.identifier))
other.assets.forEach { otherAsset in
guard !currentAssetIds.contains(otherAsset.identifier) else { return }
self.assets.append(otherAsset)
}
self.assets.append(objectsIn: assets)

let currentRelatedIds = Array(related.map(\.identifier))
other.related.forEach { newRelated in
let effectiveRelated: RelatedResource

Expand All @@ -131,10 +134,11 @@ public class Session: Object, Decodable {
effectiveRelated = newRelated
}

guard !related.contains(where: { $0.identifier == effectiveRelated.identifier }) else { return }
guard !currentRelatedIds.contains(effectiveRelated.identifier) else { return }
related.append(effectiveRelated)
}

let currentFocusIds = Array(focuses.map(\.name))
other.focuses.forEach { newFocus in
let effectiveFocus: Focus

Expand All @@ -144,7 +148,7 @@ public class Session: Object, Decodable {
effectiveFocus = newFocus
}

guard !focuses.contains(where: { $0.name == effectiveFocus.name }) else { return }
guard !currentFocusIds.contains(effectiveFocus.name) else { return }

focuses.append(effectiveFocus)
}
Expand Down Expand Up @@ -311,6 +315,21 @@ extension Session {
return asset
}

public func thumbImageAsset() -> SessionAsset? {
guard let baseURL = event.first.flatMap({ URL(string: $0.imagesPath) }) else { return nil }

let filename = "\(staticContentId)_wide_162x91_2x.jpg"

let url = baseURL.appendingPathComponent("\(staticContentId)/\(filename)")

let asset = SessionAsset()

asset.assetType = .image
asset.remoteURL = url.absoluteString

return asset
}

public func assets(matching types: [SessionAssetType]) -> Results<SessionAsset> {
assert(!types.contains(.image), "This method does not support finding image assets")

Expand Down
11 changes: 9 additions & 2 deletions Packages/ConfCore/ConfCore/Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import Foundation
import RealmSwift
import OSLog

public final class Storage: Logging {
public final class Storage: Logging, Signposting {

public let realmConfig: Realm.Configuration
public let realm: Realm

private var disposeBag: Set<AnyCancellable> = []
public static let log = makeLogger()
public static var signposter = makeSignposter()

public init(_ realm: Realm) {
self.realmConfig = realm.configuration
Expand Down Expand Up @@ -77,6 +78,7 @@ public final class Storage: Logging {
}

func store(contentResult: Result<ContentsResponse, APIError>, completion: @escaping (Error?) -> Void) {
let state = signposter.beginInterval("store content result", id: signposter.makeSignpostID(), "begin")
let contentsResponse: ContentsResponse
do {
contentsResponse = try contentResult.get()
Expand All @@ -86,7 +88,12 @@ public final class Storage: Logging {
return
}

performSerializedBackgroundWrite(disableAutorefresh: true, completionBlock: completion) { backgroundRealm in
performSerializedBackgroundWrite(
disableAutorefresh: true
) { [weak self] in
self?.signposter.endInterval("store content result", state, "end")
completion($0)
} writeBlock: { backgroundRealm in
// Save everything
backgroundRealm.add(contentsResponse.rooms, update: .all)
backgroundRealm.add(contentsResponse.tracks, update: .all)
Expand Down
1 change: 1 addition & 0 deletions PlayerUI/Views/PUIPlayerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,7 @@ extension PUIPlayerView: AVPictureInPictureControllerDelegate {
}

// Called 2nd, not called when the exit button is pressed
// TODO: The restore button doesn't attempt to do a restoration if the source view is no longer in a window
public func pictureInPictureController(
_ pictureInPictureController: AVPictureInPictureController,
restoreUserInterfaceForPictureInPictureStopWithCompletionHandler completionHandler: @escaping (Bool) -> Void
Expand Down
6 changes: 3 additions & 3 deletions WWDC/AppCoordinator+SessionActions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,18 @@ extension AppCoordinator {
alert.window.center()

enum Choice: NSApplication.ModalResponse.RawValue {
case removeCalender = 1000
case removeCalendar = 1000
case cancel = 1001
}

guard let choice = Choice(rawValue: alert.runModal().rawValue) else { return }

switch choice {
case .removeCalender:
case .removeCalendar:
do {
try eventStore.remove(storedEvent, span: .thisEvent, commit: true)
} catch let error as NSError {
log.error("Failed to remove event from calender: \(String(describing: error), privacy: .public)")
log.error("Failed to remove event from calendar: \(String(describing: error), privacy: .public)")
}
default:
break
Expand Down
Loading

0 comments on commit 4b5dca0

Please sign in to comment.