Skip to content

Commit

Permalink
add database
Browse files Browse the repository at this point in the history
  • Loading branch information
PruthiviRaj27 committed Jan 10, 2024
1 parent 43d664f commit 547cc90
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 3 deletions.
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -28,6 +29,7 @@ let package = Package(
"Alamofire",
"M3U8Parser",
.product(name: "Sentry", package: "sentry-cocoa"),
.product(name: "RealmSwift", package: "realm-swift")
],
path: "Source",
swiftSettings: [
Expand Down
40 changes: 40 additions & 0 deletions Source/Database/Model/OfflineAsset.swift
Original file line number Diff line number Diff line change
@@ -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<OfflineAsset>()
}

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"
}
23 changes: 23 additions & 0 deletions Source/Database/ObjectManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// ObjectManager.swift
// TPStreamsSDK
//
// Created by Prithuvi on 09/01/24.
//

import Foundation
import RealmSwift

public class ObjectManager<T: Object> {
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)
}
}
7 changes: 7 additions & 0 deletions Source/TPStreamsSDK.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import AVFoundation
import Sentry
import RealmSwift

#if SPM
let bundle = Bundle.module
Expand All @@ -28,6 +29,7 @@ public class TPStreamsSDK {
self.provider = provider
self.activateAudioSession()
self.initializeSentry()
self.initializeDatabase()
}

private static func activateAudioSession() {
Expand All @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions TPStreamsSDK.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
53 changes: 51 additions & 2 deletions iOSPlayerSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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 = "<group>"; };
D904B1DD2B3C5BAC00A7E26C /* TPStreamsSDK.xctestplan */ = {isa = PBXFileReference; lastKnownFileType = text; path = TPStreamsSDK.xctestplan; sourceTree = "<group>"; };
D9C1FAA72B4E7EE700B27A3B /* OfflineAsset.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OfflineAsset.swift; sourceTree = "<group>"; };
D9C1FAB12B4E915C00B27A3B /* ObjectManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObjectManager.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -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;
Expand Down Expand Up @@ -360,6 +368,7 @@
8EDE99A62A2643B000E43EA9 /* Source */ = {
isa = PBXGroup;
children = (
D9C1FAAF2B4E90FC00B27A3B /* Database */,
03CE74FE2A79469400B84304 /* Utils */,
0321F3252A2E0D0300E08AEE /* Extensions */,
8E6389E72A278D0400306FA4 /* Managers */,
Expand All @@ -385,6 +394,23 @@
path = Tests;
sourceTree = "<group>";
};
D9C1FAAF2B4E90FC00B27A3B /* Database */ = {
isa = PBXGroup;
children = (
D9C1FAB02B4E910800B27A3B /* Model */,
D9C1FAB12B4E915C00B27A3B /* ObjectManager.swift */,
);
path = Database;
sourceTree = "<group>";
};
D9C1FAB02B4E910800B27A3B /* Model */ = {
isa = PBXGroup;
children = (
D9C1FAA72B4E7EE700B27A3B /* OfflineAsset.swift */,
);
path = Model;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand Down Expand Up @@ -456,6 +482,8 @@
8E6389E42A275B2A00306FA4 /* Alamofire */,
0326BADA2A335911009ABC58 /* Sentry */,
0326BADD2A348690009ABC58 /* M3U8Parser */,
D9C1FA9F2B4E7E2600B27A3B /* Realm */,
D9C1FAA12B4E7E2600B27A3B /* RealmSwift */,
);
productName = iOSPlayerSDK;
productReference = 8EDE99A42A2643B000E43EA9 /* TPStreamsSDK.framework */;
Expand Down Expand Up @@ -517,6 +545,7 @@
8E6389E32A275B2A00306FA4 /* XCRemoteSwiftPackageReference "Alamofire" */,
0326BAD92A335911009ABC58 /* XCRemoteSwiftPackageReference "sentry-cocoa" */,
0326BADC2A348690009ABC58 /* XCRemoteSwiftPackageReference "M3U8Parser" */,
D9C1FA9E2B4E7E2600B27A3B /* XCRemoteSwiftPackageReference "realm-swift" */,
);
productRefGroup = 8EDE99A52A2643B000E43EA9 /* Products */;
projectDirPath = "";
Expand Down Expand Up @@ -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 */,
Expand All @@ -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 */,
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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 */
Expand All @@ -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 */;
Expand Down

0 comments on commit 547cc90

Please sign in to comment.