diff --git a/Package.swift b/Package.swift index 555dbcc..f1d5114 100644 --- a/Package.swift +++ b/Package.swift @@ -16,7 +16,8 @@ let package = Package( dependencies: [ .package(url: "https://github.com/Alamofire/Alamofire.git", .upToNextMajor(from: "5.0.0")), .package(url: "https://github.com/getsentry/sentry-cocoa.git", .upToNextMajor(from: "8.0.0")), - .package(url: "https://github.com/M3U8Kit/M3U8Parser", .upToNextMajor(from: "1.0.0")) + .package(url: "https://github.com/M3U8Kit/M3U8Parser", .upToNextMajor(from: "1.0.0")), + .package(url: "https://github.com/realm/realm-swift", .upToNextMajor(from: "10.45.0")) ], targets: [ @@ -28,6 +29,7 @@ let package = Package( "Alamofire", "M3U8Parser", .product(name: "Sentry", package: "sentry-cocoa"), + .product(name: "RealmSwift", package: "realm-swift") ], path: "Source", swiftSettings: [ diff --git a/Source/Database/Model/OfflineAsset.swift b/Source/Database/Model/OfflineAsset.swift new file mode 100644 index 0000000..94af622 --- /dev/null +++ b/Source/Database/Model/OfflineAsset.swift @@ -0,0 +1,40 @@ +// +// OfflineAsset.swift +// TPStreamsSDK +// +// Created by Prithuvi on 08/01/24. +// + +import Foundation +import RealmSwift + +public class OfflineAsset: Object { + @Persisted(primaryKey: true) var assetId: String = "" + @Persisted var createdAt = Date() + @Persisted var srcURL: String = "" + @Persisted var downloadedPath: String = "" + @Persisted var downloadedAt = Date() + @Persisted var status:String = Status.notStarted.rawValue + @Persisted var percentageCompleted: Float = 0.0 + + public static var manager = ObjectManager() +} + +extension OfflineAsset { + internal func update(_ attributes: [String: Any]) { + let realm = try! Realm() + try! realm.write { + for (key, value) in attributes { + self[key] = value + } + } + } +} + +enum Status: String { + case notStarted = "notStarted" + case inProgress = "inProgress" + case paused = "paused" + case finished = "finished" + case failed = "failed" +} diff --git a/Source/Database/ObjectManager.swift b/Source/Database/ObjectManager.swift new file mode 100644 index 0000000..43f997f --- /dev/null +++ b/Source/Database/ObjectManager.swift @@ -0,0 +1,23 @@ +// +// ObjectManager.swift +// TPStreamsSDK +// +// Created by Prithuvi on 09/01/24. +// + +import Foundation +import RealmSwift + +public class ObjectManager { + let realm = try! Realm() + + func add(object: T) { + try! realm.write { + realm.add(object) + } + } + + func get(assetId: Any) -> T? { + return realm.object(ofType: T.self, forPrimaryKey: assetId) + } +} diff --git a/Source/TPStreamsSDK.swift b/Source/TPStreamsSDK.swift index 3ff1e94..298c624 100644 --- a/Source/TPStreamsSDK.swift +++ b/Source/TPStreamsSDK.swift @@ -8,6 +8,7 @@ import Foundation import AVFoundation import Sentry +import RealmSwift #if SPM let bundle = Bundle.module @@ -28,6 +29,7 @@ public class TPStreamsSDK { self.provider = provider self.activateAudioSession() self.initializeSentry() + self.initializeDatabase() } private static func activateAudioSession() { @@ -50,6 +52,11 @@ public class TPStreamsSDK { options.attachViewHierarchy = true } } + + private static func initializeDatabase() { + let config = Realm.Configuration(schemaVersion: 1) + Realm.Configuration.defaultConfiguration = config + } } public enum Provider { diff --git a/TPStreamsSDK.podspec b/TPStreamsSDK.podspec index 02c0062..80c6cd1 100644 --- a/TPStreamsSDK.podspec +++ b/TPStreamsSDK.podspec @@ -14,6 +14,7 @@ Pod::Spec.new do |spec| spec.dependency 'Sentry', '~> 8.0.0' spec.dependency 'Alamofire', '~> 5.0.0' spec.dependency 'M3U8Kit', '~> 1.0.0' + spec.dependency 'RealmSwift', '~>10' spec.pod_target_xcconfig = { 'OTHER_SWIFT_FLAGS[config=Debug]' => '-DCocoaPods', 'OTHER_SWIFT_FLAGS[config=Release]' => '-DCocoaPods' diff --git a/iOSPlayerSDK.xcodeproj/project.pbxproj b/iOSPlayerSDK.xcodeproj/project.pbxproj index d4fc3fb..bc652c4 100644 --- a/iOSPlayerSDK.xcodeproj/project.pbxproj +++ b/iOSPlayerSDK.xcodeproj/project.pbxproj @@ -56,6 +56,10 @@ 8EDE99AF2A2643B000E43EA9 /* TPStreamsSDK.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 8EDE99A42A2643B000E43EA9 /* TPStreamsSDK.framework */; }; 8EDE99B42A2643B000E43EA9 /* iOSPlayerSDKTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8EDE99B32A2643B000E43EA9 /* iOSPlayerSDKTests.swift */; }; 8EDE99B52A2643B000E43EA9 /* iOSPlayerSDK.h in Headers */ = {isa = PBXBuildFile; fileRef = 8EDE99A72A2643B000E43EA9 /* iOSPlayerSDK.h */; settings = {ATTRIBUTES = (Public, ); }; }; + D9C1FAA02B4E7E2600B27A3B /* Realm in Frameworks */ = {isa = PBXBuildFile; productRef = D9C1FA9F2B4E7E2600B27A3B /* Realm */; }; + D9C1FAA22B4E7E2600B27A3B /* RealmSwift in Frameworks */ = {isa = PBXBuildFile; productRef = D9C1FAA12B4E7E2600B27A3B /* RealmSwift */; }; + D9C1FAA82B4E7EE700B27A3B /* OfflineAsset.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9C1FAA72B4E7EE700B27A3B /* OfflineAsset.swift */; }; + D9C1FAB22B4E915C00B27A3B /* ObjectManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9C1FAB12B4E915C00B27A3B /* ObjectManager.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -164,6 +168,8 @@ 8EDE99AE2A2643B000E43EA9 /* iOSPlayerSDKTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = iOSPlayerSDKTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 8EDE99B32A2643B000E43EA9 /* iOSPlayerSDKTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iOSPlayerSDKTests.swift; sourceTree = ""; }; D904B1DD2B3C5BAC00A7E26C /* TPStreamsSDK.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = TPStreamsSDK.xctestplan; sourceTree = ""; }; + D9C1FAA72B4E7EE700B27A3B /* OfflineAsset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OfflineAsset.swift; sourceTree = ""; }; + D9C1FAB12B4E915C00B27A3B /* ObjectManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectManager.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -189,6 +195,8 @@ files = ( 0326BADE2A348690009ABC58 /* M3U8Parser in Frameworks */, 8E6389E52A275B2A00306FA4 /* Alamofire in Frameworks */, + D9C1FAA02B4E7E2600B27A3B /* Realm in Frameworks */, + D9C1FAA22B4E7E2600B27A3B /* RealmSwift in Frameworks */, 0326BADB2A335911009ABC58 /* Sentry in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; @@ -360,6 +368,7 @@ 8EDE99A62A2643B000E43EA9 /* Source */ = { isa = PBXGroup; children = ( + D9C1FAAF2B4E90FC00B27A3B /* Database */, 03CE74FE2A79469400B84304 /* Utils */, 0321F3252A2E0D0300E08AEE /* Extensions */, 8E6389E72A278D0400306FA4 /* Managers */, @@ -385,6 +394,23 @@ path = Tests; sourceTree = ""; }; + D9C1FAAF2B4E90FC00B27A3B /* Database */ = { + isa = PBXGroup; + children = ( + D9C1FAB02B4E910800B27A3B /* Model */, + D9C1FAB12B4E915C00B27A3B /* ObjectManager.swift */, + ); + path = Database; + sourceTree = ""; + }; + D9C1FAB02B4E910800B27A3B /* Model */ = { + isa = PBXGroup; + children = ( + D9C1FAA72B4E7EE700B27A3B /* OfflineAsset.swift */, + ); + path = Model; + sourceTree = ""; + }; /* End PBXGroup section */ /* Begin PBXHeadersBuildPhase section */ @@ -456,6 +482,8 @@ 8E6389E42A275B2A00306FA4 /* Alamofire */, 0326BADA2A335911009ABC58 /* Sentry */, 0326BADD2A348690009ABC58 /* M3U8Parser */, + D9C1FA9F2B4E7E2600B27A3B /* Realm */, + D9C1FAA12B4E7E2600B27A3B /* RealmSwift */, ); productName = iOSPlayerSDK; productReference = 8EDE99A42A2643B000E43EA9 /* TPStreamsSDK.framework */; @@ -517,6 +545,7 @@ 8E6389E32A275B2A00306FA4 /* XCRemoteSwiftPackageReference "Alamofire" */, 0326BAD92A335911009ABC58 /* XCRemoteSwiftPackageReference "sentry-cocoa" */, 0326BADC2A348690009ABC58 /* XCRemoteSwiftPackageReference "M3U8Parser" */, + D9C1FA9E2B4E7E2600B27A3B /* XCRemoteSwiftPackageReference "realm-swift" */, ); productRefGroup = 8EDE99A52A2643B000E43EA9 /* Products */; projectDirPath = ""; @@ -601,6 +630,7 @@ 03913C482A850BF9002E7E0C /* ProgressBar.swift in Sources */, 8E6C5CB52A28BD9A003EC948 /* TPStreamsSDK.swift in Sources */, 0377C4132A2B272C00F7E58F /* TestpressAPI.swift in Sources */, + D9C1FAB22B4E915C00B27A3B /* ObjectManager.swift in Sources */, 0377C4112A2B1F0700F7E58F /* Asset.swift in Sources */, 8E6389E22A275AA800306FA4 /* StreamsAPI.swift in Sources */, 03BF8C522B173ED10006CBC1 /* TPStreamPlayerConfiguration.swift in Sources */, @@ -609,6 +639,7 @@ 03CE75022A7A337B00B84304 /* UIView.swift in Sources */, 03CA2D372A30A8E500532549 /* PlayerProgressBar.swift in Sources */, 0377C40E2A2B1C7300F7E58F /* BaseAPI.swift in Sources */, + D9C1FAA82B4E7EE700B27A3B /* OfflineAsset.swift in Sources */, 03CA2D312A2F757D00532549 /* TimeIndicatorView.swift in Sources */, 035351A22A2F49E3001E38F3 /* MediaControlsView.swift in Sources */, 03CC86682AE142FF002F5D28 /* ResourceLoaderDelegate.swift in Sources */, @@ -924,7 +955,7 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_MODULE_VERIFIER = YES; + ENABLE_MODULE_VERIFIER = NO; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_NSHumanReadableCopyright = ""; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; @@ -959,7 +990,7 @@ DYLIB_COMPATIBILITY_VERSION = 1; DYLIB_CURRENT_VERSION = 1; DYLIB_INSTALL_NAME_BASE = "@rpath"; - ENABLE_MODULE_VERIFIER = YES; + ENABLE_MODULE_VERIFIER = NO; GENERATE_INFOPLIST_FILE = YES; INFOPLIST_KEY_NSHumanReadableCopyright = ""; INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; @@ -1090,6 +1121,14 @@ minimumVersion = 5.0.0; }; }; + D9C1FA9E2B4E7E2600B27A3B /* XCRemoteSwiftPackageReference "realm-swift" */ = { + isa = XCRemoteSwiftPackageReference; + repositoryURL = "https://github.com/realm/realm-swift"; + requirement = { + kind = upToNextMajorVersion; + minimumVersion = 10.45.0; + }; + }; /* End XCRemoteSwiftPackageReference section */ /* Begin XCSwiftPackageProductDependency section */ @@ -1108,6 +1147,16 @@ package = 8E6389E32A275B2A00306FA4 /* XCRemoteSwiftPackageReference "Alamofire" */; productName = Alamofire; }; + D9C1FA9F2B4E7E2600B27A3B /* Realm */ = { + isa = XCSwiftPackageProductDependency; + package = D9C1FA9E2B4E7E2600B27A3B /* XCRemoteSwiftPackageReference "realm-swift" */; + productName = Realm; + }; + D9C1FAA12B4E7E2600B27A3B /* RealmSwift */ = { + isa = XCSwiftPackageProductDependency; + package = D9C1FA9E2B4E7E2600B27A3B /* XCRemoteSwiftPackageReference "realm-swift" */; + productName = RealmSwift; + }; /* End XCSwiftPackageProductDependency section */ }; rootObject = 8EDE999B2A2643B000E43EA9 /* Project object */;