Skip to content

Commit

Permalink
Allow dynamic tag filters (roundware#19)
Browse files Browse the repository at this point in the history
* 🎉 Implement dynamic tag filters

* 🚑 Handle no filter field in tag

* ⚡ Make dynamic tag filter list static and faster

* 🎨 Make DynamicTagFilter a struct
  • Loading branch information
loafofpiecrust authored May 4, 2019
1 parent 99d4994 commit b2771ba
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
49 changes: 49 additions & 0 deletions RWFramework/RWFramework/Playlist/AssetFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import Foundation
import Promises
import GEOSwift
import SwiftyJSON

/**
The priority to place on an asset, or to discard it from use.
Expand Down Expand Up @@ -250,3 +251,51 @@ struct TimedRepeatFilter: AssetFilter {
}
}
}

struct DynamicTagFilter: AssetFilter {
/// Mapping of dynamic filter name to tag id
private static let tags = try! JSON(
data: UserDefaults.standard.data(forKey: "tags")!
).array!.reduce(into: [String: [Int]]()) { acc, t in
let key = t["filter"].string!
acc[key] = (acc[key] ?? []) + [t["id"].int!]
}

private let key: String
private let filter: AssetFilter

init(_ key: String, _ filter: AssetFilter) {
self.key = key
self.filter = filter
}

func keep(_ asset: Asset, playlist: Playlist, track: AudioTrack) -> AssetPriority {
// see if there are any tags using this filter
if let tagIds = DynamicTagFilter.tags[self.key],
// grab the list of enabled tags
let enabledTagIds = RWFramework.sharedInstance.getSubmittableListenTagIDsSet(),
// if any filter tags are enabled, apply the filter
tagIds.contains(where: { enabledTagIds.contains($0) }) {
return self.filter.keep(asset, playlist: playlist, track: track)
} else {
return .normal
}
}
}

struct MostRecentFilter: AssetFilter {
private let maxAge: TimeInterval

init(days: Int) {
self.maxAge = TimeInterval(days * 24 * 60 * 60)
}

func keep(_ asset: Asset, playlist: Playlist, track: AudioTrack) -> AssetPriority {
let timeSinceCreated = Date().timeIntervalSince(asset.createdDate)
if timeSinceCreated > maxAge {
return .discard
} else {
return .normal
}
}
}
1 change: 1 addition & 0 deletions RWFramework/RWFramework/RWFramework.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ private lazy var __once: () = { () -> Void in
// or a user-specified distance range
AllAssetFilters([DistanceRangesFilter(), AngleFilter()]),
]),
DynamicTagFilter("_ten_most_recent_days", MostRecentFilter(days: 10))
], sortBy: [
SortRandomly(),
])
Expand Down

0 comments on commit b2771ba

Please sign in to comment.