-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Adwin Ronald Ross
committed
Jan 29, 2025
1 parent
750cf4e
commit da367aa
Showing
23 changed files
with
483 additions
and
23 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// CrashScreenshotSaver.swift | ||
// MeasureSDK | ||
// | ||
// Created by Adwin Ross on 28/01/25. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol CrashScreenshotSaver { | ||
func onViewDidAppearCalled() | ||
func enable() | ||
} | ||
|
||
final class BaseCrashScreenshotSaver: CrashScreenshotSaver { | ||
private let logger: Logger | ||
private var lastUpdateTime: Date? | ||
private let screenshotGenerator: ScreenshotGenerator | ||
private let systemFileManager: SystemFileManager | ||
private var isEnabled = false | ||
|
||
init(logger: Logger, screenshotGenerator: ScreenshotGenerator, systemFileManager: SystemFileManager) { | ||
self.logger = logger | ||
self.screenshotGenerator = screenshotGenerator | ||
self.systemFileManager = systemFileManager | ||
} | ||
|
||
func enable() { | ||
isEnabled = true | ||
} | ||
|
||
func onViewDidAppearCalled() { | ||
guard isEnabled else { return } | ||
|
||
let now = Date() | ||
|
||
if let lastUpdateTime = self.lastUpdateTime, now.timeIntervalSince(lastUpdateTime) < 1.0 { | ||
return | ||
} | ||
|
||
self.lastUpdateTime = now | ||
|
||
guard let screenshot = screenshotGenerator.generate(), let screenshotData = screenshot.pngData() else { | ||
return | ||
} | ||
|
||
if let filepath = systemFileManager.saveFile(data: screenshotData, name: crashScreenshotName, folderName: crashScreenshotDirectoryName, directory: .documentDirectory) { | ||
logger.log(level: .debug, message: "Screenshot saved at \(filepath).", error: nil, data: nil) | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// | ||
// AttachmentProcessor.swift | ||
// MeasureSDK | ||
// | ||
// Created by Adwin Ross on 17/01/25. | ||
// | ||
|
||
import Foundation | ||
|
||
enum AttachmentStorageType { | ||
case data | ||
case fileStorage | ||
} | ||
|
||
protocol AttachmentProcessor { | ||
func getAttachmentObject(for image: UIImage, name: String, storageType: AttachmentStorageType, attachmentType: AttachmentType, compressionRatio: CGFloat) -> Attachment? | ||
} | ||
|
||
final class BaseAttachmentProcessor: AttachmentProcessor { | ||
private let fileManager: SystemFileManager | ||
private let logger: Logger | ||
private let idProvider: IdProvider | ||
|
||
init(logger: Logger, fileManager: SystemFileManager, idProvider: IdProvider) { | ||
self.fileManager = fileManager | ||
self.logger = logger | ||
self.idProvider = idProvider | ||
} | ||
|
||
func getAttachmentObject(for image: UIImage, | ||
name: String, | ||
storageType: AttachmentStorageType, | ||
attachmentType: AttachmentType, | ||
compressionRatio: CGFloat) -> Attachment? { | ||
guard compressionRatio >= 0, compressionRatio <= 1 else { | ||
logger.internalLog(level: .error, message: "Invalid compression ratio: \(compressionRatio). Must be between 0 and 1.", error: nil, data: nil) | ||
return nil | ||
} | ||
|
||
guard let imageData = image.jpegData(compressionQuality: compressionRatio) else { | ||
logger.internalLog(level: .error, message: "Failed to compress image with ratio: \(compressionRatio).", error: nil, data: nil) | ||
return nil | ||
} | ||
|
||
let uuid = idProvider.createId() | ||
switch storageType { | ||
case .data: | ||
return Attachment(name: name, type: attachmentType, size: Int64(imageData.count), id: uuid, bytes: imageData, path: nil) | ||
case .fileStorage: | ||
guard let fileURL = fileManager.saveFile(data: imageData, name: name, folderName: "attachments", directory: .documentDirectory) else { | ||
logger.internalLog(level: .error, message: "Failed to save compressed image to file storage.", error: nil, data: nil) | ||
return nil | ||
} | ||
return Attachment(name: name, type: attachmentType, size: Int64(imageData.count), id: uuid, bytes: nil, path: fileURL.path) | ||
} | ||
} | ||
} |
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 |
---|---|---|
|
@@ -9,4 +9,5 @@ import Foundation | |
|
||
enum AttachmentType: String, Codable { | ||
case screenshot | ||
case layoutSnapshot | ||
} |
Oops, something went wrong.