Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement media extension #8

Merged
merged 13 commits into from
Mar 24, 2021
Prev Previous commit
Next Next commit
update MediaState within MediaService
  • Loading branch information
rymorale committed Mar 23, 2021
commit 5ba7c96c7aa3e441817fa65b3b7dec2101a8fed8
16 changes: 6 additions & 10 deletions AEPMedia/Sources/Media.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,17 @@ import AEPServices
public class Media: NSObject, Extension {
private let LOG_TAG = "Media"

// MARK: Extension
public var runtime: ExtensionRuntime
public var name = MediaConstants.EXTENSION_NAME
public var friendlyName = MediaConstants.FRIENDLY_NAME
public static var extensionVersion = MediaConstants.EXTENSION_VERSION
public var metadata: [String: String]?
let dependencies: [String] = [MediaConstants.Configuration.SHARED_STATE_NAME, MediaConstants.Identity.SHARED_STATE_NAME, MediaConstants.Analytics.SHARED_STATE_NAME]
#if DEBUG
var trackers: [String: MediaEventTracker] = [:]
var trackers: [String: MediaEventTracker]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use MediaEventTrackable protocol instead of MediaEventTracker?

var mediaState: MediaState
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove mediaState from the extension and let MediaService maintain it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, i'll update

var mediaService: MediaService?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make MediaService non optional?

#else
private var trackers: [String: MediaCoreTracker] = [:]
private var trackers: [String: MediaEventTracker]
private var mediaState: MediaState
private var mediaService: MediaService?
#endif
Expand All @@ -40,6 +38,7 @@ public class Media: NSObject, Extension {
self.runtime = runtime
self.mediaState = MediaState()
self.mediaService = MediaService(mediaState: mediaState)
self.trackers = [:]
}

/// Invoked when the Media extension has been registered by the `EventHub`
Expand All @@ -65,18 +64,14 @@ public class Media: NSObject, Extension {
/// - Parameter:
/// - event: The configuration response event
private func handleSharedStateUpdate(_ event: Event) {
mediaService?.updateMediaState(event: event)
mediaService?.updateMediaState(event: event, getSharedState: runtime.getSharedState(extensionName:event:barrier:))
}

/// Processes Configuration Response content events to retrieve the configuration data and privacy status settings.
/// - Parameter:
/// - event: The configuration response event
private func handleConfigurationResponseEvent(_ event: Event) {
var sharedStates = [String: [String: Any]?]()
for extensionName in dependencies {
sharedStates[extensionName] = runtime.getSharedState(extensionName: extensionName, event: event, barrier: true)?.value
}
mediaState.update(dataMap: sharedStates)
handleSharedStateUpdate(event)

if mediaState.getPrivacyStatus() == .optedOut {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should directly read privacy status from EventData instead of reading it from Media state

handleOptOut(event: event)
Expand All @@ -99,6 +94,7 @@ public class Media: NSObject, Extension {

let trackerConfig = eventData[MediaConstants.Tracker.EVENT_PARAM] as? [String: Any] ?? [:]

Log.debug(label: LOG_TAG, "\(#function) - Creating tracker with tracker id: \(trackerId).")
trackers[trackerId] = MediaEventTracker(hitProcessor: mediaService!, config: trackerConfig)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should avoid forced unwrapping mediaService.

}

Expand Down
9 changes: 7 additions & 2 deletions AEPMedia/Sources/MediaService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class MediaService : MediaProcessor {

private var mediaSessions: [String: MediaSession] = [:]
private var mediaState: MediaState
private let dependencies = [MediaConstants.Configuration.SHARED_STATE_NAME, MediaConstants.Identity.SHARED_STATE_NAME, MediaConstants.Analytics.SHARED_STATE_NAME]

init(mediaState: MediaState) {
self.mediaState = mediaState
Expand All @@ -37,8 +38,12 @@ class MediaService : MediaProcessor {

}

func updateMediaState(event: Event) {

func updateMediaState(event: Event, getSharedState: (String, Event, Bool) -> SharedStateResult?) {
var sharedStates = [String: [String: Any]?]()
for extensionName in dependencies {
sharedStates[extensionName] = getSharedState(extensionName, event, true)?.value
}
mediaState.update(dataMap: sharedStates)
}

func abort() {
Expand Down
3 changes: 2 additions & 1 deletion AEPMedia/Tests/TestHelpers/FakeMediaService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class FakeMediaService : MediaService {
var updateMediaStateCalled = false
var abortAllSessionsCalled = false

override func updateMediaState(event: Event) {
override func updateMediaState(event: Event, getSharedState: (String, Event, Bool) -> SharedStateResult?) {
super.updateMediaState(event: event, getSharedState: getSharedState)
updateMediaStateCalled = true
}

Expand Down