-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DEMRUM-620: Add exercise of slow frame detection to test app (#236)
* DEMRUM-620: Add exercise of slow frame detection to test app * DEMRUM-620: swiftlint: fix trailing spaces * DEMRUM-620: fix typos * DEMRUM-620: swiftlint: remove weak from IBOutlet * DEMRUM-620: swiftlint: remove needless self references and fix whitespace * DEMRUM-620: swiftlint: sort imports alphabetically * DEMRUM-620: swiftlint: remove weak from IBOutlet
- Loading branch information
1 parent
7795dfe
commit 367d276
Showing
5 changed files
with
209 additions
and
24 deletions.
There are no files selected for viewing
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
84 changes: 84 additions & 0 deletions
84
AgentTestApp/AgentTestApp/Features/SlowFrameBeatingHeartView.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,84 @@ | ||
// | ||
// MRUM SDK, © 2024 CISCO | ||
// | ||
|
||
import UIKit | ||
|
||
class SlowFrameBeatingHeartView: UIView { | ||
private let heartImageView = UIImageView() | ||
|
||
// manual animation -- see below for why | ||
private var timer: Timer? | ||
private var scaleDirection: CGFloat = -1.0 | ||
|
||
override init(frame: CGRect) { | ||
super.init(frame: frame) | ||
setupView() | ||
startAnimation() | ||
} | ||
|
||
required init?(coder: NSCoder) { | ||
super.init(coder: coder) | ||
setupView() | ||
startAnimation() | ||
} | ||
|
||
func dealloc() { | ||
stopAnimation() | ||
} | ||
|
||
override func layoutSubviews() { | ||
super.layoutSubviews() | ||
heartImageView.frame = bounds | ||
} | ||
|
||
private func setupView() { | ||
heartImageView.image = UIImage(systemName: "heart.fill") | ||
heartImageView.tintColor = .red | ||
heartImageView.contentMode = .scaleAspectFit | ||
heartImageView.translatesAutoresizingMaskIntoConstraints = false | ||
addSubview(heartImageView) | ||
} | ||
|
||
func startAnimation() { | ||
// Even with 0.04 it's still not the smoothest but | ||
// it serves the purpose. | ||
timer = Timer.scheduledTimer(withTimeInterval: 0.04, repeats: true) { [weak self] _ in | ||
self?.updateAnimation() | ||
} | ||
} | ||
|
||
func stopAnimation() { | ||
timer?.invalidate() | ||
timer = nil | ||
} | ||
|
||
private func updateAnimation() { | ||
|
||
// Use a manually conducted animation so that we can | ||
// keep it on the main thread and witness the results of | ||
// the main thread stall in the UI. If we instead used a | ||
// built-in animation here, Apple would put it on a | ||
// background worker thread and it would not (reliably) | ||
// pause when the main thread pauses, such as during a | ||
// sleep on the main thread, so the results would not | ||
// be visible or clearly shown in this test app. | ||
|
||
// get the scale component of the transform matrix | ||
let currentScale = heartImageView.transform.a | ||
|
||
let scaleStep: CGFloat = 0.01 | ||
var newScale = currentScale + (scaleStep * scaleDirection) | ||
|
||
if newScale <= 0.5 { | ||
newScale = 0.5 | ||
scaleDirection = 1.0 | ||
} else if newScale >= 1.0 { | ||
newScale = 1.0 | ||
scaleDirection = -1.0 | ||
} | ||
|
||
heartImageView.transform = CGAffineTransform(scaleX: newScale, y: newScale) | ||
} | ||
} | ||
|
35 changes: 35 additions & 0 deletions
35
AgentTestApp/AgentTestApp/Features/SlowFrameDetectorViewController.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,35 @@ | ||
// | ||
// MRUM SDK, © 2024 CISCO | ||
// | ||
|
||
import SwiftUI | ||
import UIKit | ||
|
||
class SlowFrameDetectorViewController: UIViewController { | ||
|
||
@IBOutlet var slowFramesButton: UIButton! | ||
@IBOutlet var frozenFramesButton: UIButton! | ||
@IBOutlet var beatingHeartView: SlowFrameBeatingHeartView! | ||
|
||
override func viewDidLoad() { | ||
super.viewDidLoad() | ||
} | ||
|
||
@IBAction func slowFramesClick(_ sender: UIButton) { | ||
// Sleep for 0.5 seconds on the main thread | ||
print("Sleeping for 0.5 seconds to force slow frames") | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.001) { | ||
Thread.sleep(forTimeInterval: 0.5) | ||
} | ||
} | ||
|
||
@IBAction func frozenFramesClick(_ sender: UIButton) { | ||
// Sleep for 1 second on the main thread | ||
print("Sleeping for 2 seconds to force frozen frames") | ||
DispatchQueue.main.asyncAfter(deadline: .now() + 0.001) { | ||
Thread.sleep(forTimeInterval: 2.0) | ||
} | ||
} | ||
} | ||
|
||
|
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