Skip to content

Commit

Permalink
fix: safely convert floats to int
Browse files Browse the repository at this point in the history
  • Loading branch information
ioannisj committed Feb 7, 2025
1 parent 35d7b93 commit 7323a04
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 20 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
## Next

- fix: occasional crash when converting to Int in session replay wireframe ([#294](https://github.com/PostHog/posthog-ios/pull/294))

## 3.19.3 - 2025-02-04

- fix: custom hosts with a path ([#290](https://github.com/PostHog/posthog-ios/pull/290))
Expand Down
4 changes: 4 additions & 0 deletions PostHog.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@
DAC699D62CC790D9000D1D6B /* PostHogAutocaptureIntegration.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC699D52CC790D9000D1D6B /* PostHogAutocaptureIntegration.swift */; };
DAC699EC2CCA73E5000D1D6B /* ForwardingPickerViewDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAC699EB2CCA73E5000D1D6B /* ForwardingPickerViewDelegate.swift */; };
DACF6D5D2CD2F5BC00F14133 /* PostHogAutocaptureIntegrationSpec.swift in Sources */ = {isa = PBXBuildFile; fileRef = DACF6D5C2CD2F5BC00F14133 /* PostHogAutocaptureIntegrationSpec.swift */; };
DAD39DF42D560818002A1A69 /* Float+.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD39DEE2D560813002A1A69 /* Float+.swift */; };
DAD5DD0C2CB6DEF30087387B /* PostHogMaskViewModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAD5DD072CB6DEE70087387B /* PostHogMaskViewModifier.swift */; };
DAF79A2A2D1309C00078A3C9 /* TestError.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAF79A242D1309BE0078A3C9 /* TestError.swift */; };
DAF95F512D072F04001E82BB /* UIImage+WebP.swift in Sources */ = {isa = PBXBuildFile; fileRef = DAF95F502D072F04001E82BB /* UIImage+WebP.swift */; };
Expand Down Expand Up @@ -714,6 +715,7 @@
DAC699D52CC790D9000D1D6B /* PostHogAutocaptureIntegration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogAutocaptureIntegration.swift; sourceTree = "<group>"; };
DAC699EB2CCA73E5000D1D6B /* ForwardingPickerViewDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ForwardingPickerViewDelegate.swift; sourceTree = "<group>"; };
DACF6D5C2CD2F5BC00F14133 /* PostHogAutocaptureIntegrationSpec.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogAutocaptureIntegrationSpec.swift; sourceTree = "<group>"; };
DAD39DEE2D560813002A1A69 /* Float+.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Float+.swift"; sourceTree = "<group>"; };
DAD5DD072CB6DEE70087387B /* PostHogMaskViewModifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PostHogMaskViewModifier.swift; sourceTree = "<group>"; };
DAF79A242D1309BE0078A3C9 /* TestError.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestError.swift; sourceTree = "<group>"; };
DAF95F502D072F04001E82BB /* UIImage+WebP.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIImage+WebP.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1053,6 +1055,7 @@
69EE82B82BA9C4DA00EB9542 /* Replay */ = {
isa = PBXGroup;
children = (
DAD39DEE2D560813002A1A69 /* Float+.swift */,
69B7F6062CF7702D00A48BCC /* UIImage+Util.swift */,
69EE82B92BA9C50400EB9542 /* PostHogReplayIntegration.swift */,
69EE82BB2BA9C53000EB9542 /* PostHogSessionReplayConfig.swift */,
Expand Down Expand Up @@ -1779,6 +1782,7 @@
DA9CE3572D3108AD00DFE652 /* yuv_sse2.c in Sources */,
DA9CE3582D3108AD00DFE652 /* frame_enc.c in Sources */,
DA9CE3592D3108AD00DFE652 /* cost_neon.c in Sources */,
DAD39DF42D560818002A1A69 /* Float+.swift in Sources */,
DA9CE35A2D3108AD00DFE652 /* enc_sse41.c in Sources */,
DA9CE35B2D3108AD00DFE652 /* webp_enc.c in Sources */,
DA9CE35C2D3108AD00DFE652 /* upsampling_neon.c in Sources */,
Expand Down
18 changes: 18 additions & 0 deletions PostHog/Replay/Float+.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Float+.swift
// PostHog
//
// Created by Yiannis Josephides on 07/02/2025.
//

extension CGFloat {
func toInt() -> Int {
NSNumber(value: rounded()).intValue
}
}

extension Double {
func toInt() -> Int {
NSNumber(value: rounded()).intValue
}
}
36 changes: 18 additions & 18 deletions PostHog/Replay/PostHogReplayIntegration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@

if !snapshotStatus.sentMetaEvent {
let size = window.bounds.size
let width = Int(size.width)
let height = Int(size.height)
let width = size.width.toInt()
let height = size.height.toInt()

var data: [String: Any] = ["width": width, "height": height]

Expand Down Expand Up @@ -229,10 +229,10 @@
}

private func setPadding(_ insets: UIEdgeInsets, _ style: RRStyle) {
style.paddingTop = Int(insets.top)
style.paddingRight = Int(insets.right)
style.paddingBottom = Int(insets.bottom)
style.paddingLeft = Int(insets.left)
style.paddingTop = insets.top.toInt()
style.paddingRight = insets.right.toInt()
style.paddingBottom = insets.bottom.toInt()
style.paddingLeft = insets.left.toInt()
}

private func createBasicWireframe(_ view: UIView) -> RRWireframe {
Expand All @@ -244,10 +244,10 @@
let frame = view.toAbsoluteRect(view.window)

wireframe.id = view.hash
wireframe.posX = Int(frame.origin.x)
wireframe.posY = Int(frame.origin.y)
wireframe.width = Int(frame.size.width)
wireframe.height = Int(frame.size.height)
wireframe.posX = frame.origin.x.toInt()
wireframe.posY = frame.origin.y.toInt()
wireframe.width = frame.size.width.toInt()
wireframe.height = frame.size.height.toInt()

return wireframe
}
Expand Down Expand Up @@ -528,8 +528,8 @@
wireframe.disabled = !textView.isEditable
style.color = textView.textColor?.toRGBString()
style.fontFamily = textView.font?.familyName
if let fontSize = textView.font?.pointSize {
style.fontSize = Int(fontSize)
if let fontSize = textView.font?.pointSize.toInt() {
style.fontSize = fontSize
}
setAlignment(textView.textAlignment, style)
setPadding(textView.textContainerInset, style)
Expand All @@ -549,8 +549,8 @@
wireframe.disabled = !textField.isEnabled
style.color = textField.textColor?.toRGBString()
style.fontFamily = textField.font?.familyName
if let fontSize = textField.font?.pointSize {
style.fontSize = Int(fontSize)
if let fontSize = textField.font?.pointSize.toInt() {
style.fontSize = fontSize
}
setAlignment(textField.textAlignment, style)
}
Expand Down Expand Up @@ -601,8 +601,8 @@
wireframe.disabled = !label.isEnabled
style.color = label.textColor?.toRGBString()
style.fontFamily = label.font?.familyName
if let fontSize = label.font?.pointSize {
style.fontSize = Int(fontSize)
if let fontSize = label.font?.pointSize.toInt() {
style.fontSize = fontSize
}
setAlignment(label.textAlignment, style)
}
Expand Down Expand Up @@ -631,8 +631,8 @@

style.backgroundColor = view.backgroundColor?.toRGBString()
let layer = view.layer
style.borderWidth = Int(layer.borderWidth)
style.borderRadius = Int(layer.cornerRadius)
style.borderWidth = layer.borderWidth.toInt()
style.borderRadius = layer.cornerRadius.toInt()
style.borderColor = layer.borderColor?.toRGBString()

wireframe.style = style
Expand Down
4 changes: 2 additions & 2 deletions PostHog/Replay/UIApplicationTracker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
continue
}

let posX = Int(touch.location.x)
let posY = Int(touch.location.y)
let posX = touch.location.x.toInt()
let posY = touch.location.y.toInt()

// if the id is 0, BE transformer will set it to the virtual bodyId
let touchData: [String: Any] = ["id": 0, "pointerType": 2, "source": 2, "type": type, "x": posX, "y": posY]
Expand Down

0 comments on commit 7323a04

Please sign in to comment.