Skip to content

Instantly share code, notes, and snippets.

@tion-low
Last active September 13, 2019 02:45
Show Gist options
  • Save tion-low/f8a53f6c051eee0fa63b0213d5b58f76 to your computer and use it in GitHub Desktop.
Save tion-low/f8a53f6c051eee0fa63b0213d5b58f76 to your computer and use it in GitHub Desktop.
Tracker
class Tracker {
var trackers: [TrackerProtocol] = []
func track(info: TrackingInfo?) {
guard let info = info else {
assert(false, "TrackingInfo is nil")
}
trackers.forEach { (tracker) in
tracker.track(info: info)
}
}
func add(tracker: TrackerProtocol) {
trackers.append(tracker)
}
}
protocol TrackerProtocol {
func track(info: TrackingInfo)
}
class TrackingInfo {
let event: TrackingEvent
let property: TrackingProperty
init(event: TrackingEvent, property: TrackingProperty) {
self.event = event
self.property = property
}
}
protocol TrackingEvent { // イベント種類の抽象、イベントのカテゴリごとに実装を作れるようにしたい
var name: String { get }
var params: [String: Any] { get }
}
struct BroadcastingEvent: TrackingEvent {
enum `Type`: String {
case start
case pause
case stop
}
var name: String {
return type.rawValue
}
private let type: Type
var params: [String : Any] {
return ["mediaId": mediaId]
}
private let mediaId: String
init(type: Type, mediaId: String) {
self.type = type
self.mediaId = mediaId
}
}
struct LiveEvent: TrackingEvent {
enum `Type`: String {
case start
case finish
}
var name: String {
return type.rawValue
}
private let type: Type
var params: [String : Any] {
return ["mediaid": mediaId]
}
private let mediaId: Int
init(type: Type, mediaId: Int) {
self.type = type
self.mediaId = mediaId
}
}
struct TrackingInfoBuilder {
private var event: TrackingEvent?
private var property: TrackingProperty?
init(event: TrackingEvent? = nil, property: TrackingProperty? = nil) {
self.event = event
self.property = property
}
func build() -> TrackingInfo? {
guard let event = self.event, let property = self.property else { return nil }
return TrackingInfo(event: event, property: property)
}
func setEvent(event: TrackingEvent) -> TrackingInfoBuilder {
return TrackingInfoBuilder(event: event, property: property)
}
func setProperty(property: TrackingProperty) -> TrackingInfoBuilder {
return TrackingInfoBuilder(event: event, property: property)
}
static func `default`() -> TrackingInfoBuilder {
return TrackingInfoBuilder().setProperty(property: TrackingProperty(userId: "hogehoge", gender: 1))
}
}
struct TrackingProperty { // 共通で使う情報
let userId: String
let gender: Int
}
struct HogeTracker: TrackerProtocol {
func track(info: TrackingInfo) {
print("hoge event name: \(info.event.name), property: \(info.property.userId) \(info.property.gender)")
for param in info.event.params {
print("hoge event param: key=\(param.key), value=\(param.value)")
}
}
}
struct FugaTracker: TrackerProtocol {
func track(info: TrackingInfo) {
print("fuga event name: \(info.event.name), propety: \(info.property.userId) \(info.property.gender)")
for param in info.event.params {
if let v = param.value as? String {
print("fuga event param: key=\(param.key), value=\(param.value)")
} else {
let v = "\(param.value)"
print("fuga event param: key=\(param.key), value=\(v)")
}
}
}
}
let tracker = Tracker()
tracker.add(tracker: HogeTracker())
tracker.add(tracker: FugaTracker())
let broadcastingStart = BroadcastingEvent(type: .start, mediaId: "100")
let broadcastingStartInfo = TrackingInfoBuilder.default().setEvent(event: broadcastingStart).build()
tracker.track(info: broadcastingStartInfo)
let liveFinish = LiveEvent(type: .finish, mediaId: 150)
let liveFinishInfo = TrackingInfoBuilder.default().setEvent(event: liveFinish).build()
tracker.track(info: liveFinishInfo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment