-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce ViewControllerInstanceObserver (#19)
* Revert using `after` in swizzler, as in this case we miss some time in viewDidAppear * Introduce ViewControllerInstanceObserver Separating view controllers by 2 types: - ViewControllerObserver. This is a single observer per app. It consumes events from all view controllers - ViewControllerInstanceObserver. Those are instance per view controller. They receive identifier in initializer and do not need view controller reference at all. Instance observers: - TTIObserver - RenderingObserver - LoggingObserver Other observers: - ViewControllerLeaksObserver - LastOpenedScreenObserver Renamed AppStateObserver to AppStateListener not to confuse with other observers. Reorganized sources folder structure a bit. * Fixing swiftlint issues --------- Co-authored-by: Gleb Tarasov <[email protected]>
- Loading branch information
Showing
42 changed files
with
504 additions
and
475 deletions.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
PerformanceSuite/Sources/InstanceObservers/LoggingObserver.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// | ||
// LoggingObserver.swift | ||
// PerformanceSuite | ||
// | ||
// Created by Gleb Tarasov on 23/09/2022. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
import SwiftUI | ||
|
||
/// Use this protocol for light-weight operations like logging only, | ||
/// it is not intended to be used for some business-logic. | ||
/// | ||
/// If you execute something heavy, offload it to some other background thread. | ||
public protocol ViewControllerLoggingReceiver: ScreenMetricsReceiver { | ||
|
||
/// Method is called during view controller's initialization | ||
func onInit(screen: ScreenIdentifier) | ||
|
||
/// Method is called during view controller's `viewDidLoad` | ||
func onViewDidLoad(screen: ScreenIdentifier) | ||
|
||
/// Method is called during view controller's `viewWillAppear` | ||
func onViewWillAppear(screen: ScreenIdentifier) | ||
|
||
/// Method is called during view controller's `viewDidAppear` | ||
func onViewDidAppear(screen: ScreenIdentifier) | ||
|
||
/// Method is called during view controller's `viewWillDisappear` | ||
func onViewWillDisappear(screen: ScreenIdentifier) | ||
} | ||
|
||
|
||
/// Observer which forward all delegate methods to its receiver for logging purposes | ||
final class LoggingObserver<V: ViewControllerLoggingReceiver>: ViewControllerInstanceObserver { | ||
|
||
init(screen: V.ScreenIdentifier, receiver: V) { | ||
self.screen = screen | ||
self.receiver = receiver | ||
} | ||
|
||
private let screen: V.ScreenIdentifier | ||
private let receiver: V | ||
|
||
func beforeInit() { | ||
PerformanceMonitoring.consumerQueue.async { | ||
self.receiver.onInit(screen: self.screen) | ||
} | ||
} | ||
|
||
func beforeViewDidLoad() { | ||
PerformanceMonitoring.consumerQueue.async { | ||
self.receiver.onViewDidLoad(screen: self.screen) | ||
} | ||
} | ||
|
||
func afterViewWillAppear() { | ||
PerformanceMonitoring.consumerQueue.async { | ||
self.receiver.onViewWillAppear(screen: self.screen) | ||
} | ||
} | ||
|
||
func afterViewDidAppear() { | ||
PerformanceMonitoring.consumerQueue.async { | ||
self.receiver.onViewDidAppear(screen: self.screen) | ||
} | ||
} | ||
|
||
func beforeViewWillDisappear() { | ||
PerformanceMonitoring.consumerQueue.async { | ||
self.receiver.onViewWillDisappear(screen: self.screen) | ||
} | ||
} | ||
|
||
static var identifier: AnyObject { | ||
return loggingObserverIdentifier | ||
} | ||
} | ||
|
||
private let loggingObserverIdentifier = NSObject() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.