Skip to content

Commit

Permalink
✨ Introduce passive acceptance for asset filters (roundware#23)
Browse files Browse the repository at this point in the history
* 🎉 Introduce AssetPriority.passiveDiscard

* 🚑 Revert changes to tag filters

* Play assets ultimately marked neutral

* Make AnyAssetFilters not neutral
  • Loading branch information
loafofpiecrust authored Jul 17, 2019
1 parent 07098f4 commit 8af92d0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
35 changes: 19 additions & 16 deletions RWFramework/RWFramework/Playlist/AssetFilter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,15 @@ import SwiftyJSON
project-level ordering preferences.
*/
enum AssetPriority: Int {
/// Discard the asset always
case discard = -1
case highest = 0

/// Accept the asset only if not overridden by any other priority
case neutral = 0

case lowest = 1
case normal = 100
case lowest = 999
case highest = 999
}

/// Filter applied to assets as candidates for a specific track
Expand All @@ -37,9 +42,10 @@ struct AnyAssetFilters: AssetFilter {
if filters.isEmpty {
return .lowest
}
return filters.lazy
let ranks = filters.lazy
.map { $0.keep(asset, playlist: playlist, track: track) }
.first { $0 != .discard } ?? .discard
return ranks.first { $0 != .discard && $0 != .neutral }
?? .discard
}
}

Expand All @@ -65,7 +71,8 @@ struct AllAssetFilters: AssetFilter {
return .discard
} else {
// Otherwise, simply use the first returned priority
return ranks.first ?? .normal
// Ideally the first that isn't .neutral
return ranks.first { $0 != .neutral } ?? ranks.first!
}
}
}
Expand Down Expand Up @@ -100,13 +107,10 @@ struct AllTagsFilter: AssetFilter {

struct TrackTagsFilter: AssetFilter {
func keep(_ asset: Asset, playlist: Playlist, track: AudioTrack) -> AssetPriority {
guard let trackTags = track.tags
guard let trackTags = track.tags,
trackTags.count != 0
else { return .lowest }

if (trackTags.count == 0) {
return .lowest
}

let matches = asset.tags.contains { assetTag in
trackTags.contains(assetTag)
}
Expand All @@ -126,7 +130,7 @@ struct DistanceRangesFilter: AssetFilter {
let loc = asset.location,
let minDist = params.minDist,
let maxDist = params.maxDist
else { return .discard }
else { return .neutral }

let dist = params.location.distance(from: loc)
if dist >= minDist && dist <= maxDist {
Expand All @@ -146,7 +150,7 @@ struct DistanceFixedFilter: AssetFilter {
guard playlist.project.geo_listen_enabled,
let params = playlist.currentParams,
let assetLoc = asset.location
else { return .discard }
else { return .neutral }

let listenerLoc = params.location
let maxListenDist = playlist.project.recording_radius
Expand All @@ -166,7 +170,7 @@ struct AssetShapeFilter: AssetFilter {
guard playlist.project.geo_listen_enabled,
let params = playlist.currentParams,
let shape = asset.shape
else { return .discard }
else { return .neutral }

if shape.contains(params.location.toWaypoint()) {
return .normal
Expand All @@ -186,8 +190,7 @@ struct AngleFilter: AssetFilter {
let loc = asset.location,
let heading = opts.heading,
let angularWidth = opts.angularWidth
else { return .discard }

else { return .neutral }

// We can keep any asset if our angular width covers all space.
if angularWidth > 359.0 {
Expand Down Expand Up @@ -290,7 +293,7 @@ struct DynamicTagFilter: AssetFilter {
tagIds.contains(where: { enabledTagIds.contains($0) }) {
return self.filter.keep(asset, playlist: playlist, track: track)
} else {
return .normal
return .neutral
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions RWFramework/RWFramework/Playlist/Playlist.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ extension Playlist {
(asset, self.filters.keep(asset, playlist: self, track: track))
}.filter { (asset, rank) in
rank != .discard
}.sorted { a, b in
a.1.rawValue <= b.1.rawValue
}

let sortedAssets = filteredAssets.sorted { a, b in
a.1.rawValue >= b.1.rawValue
}.sorted { a, b in
// play less played assets first
let dataA = userAssetData[a.0.id]
Expand All @@ -191,19 +193,20 @@ extension Playlist {
}
}.map { (asset, rank) in asset }

print("\(filteredAssets.count) filtered assets")
print("\(sortedAssets.count) filtered assets")

let next = filteredAssets.first
let next = sortedAssets.first
if let next = next {
var playCount = 1
if let prevEntry = userAssetData[next.id] {
playCount += prevEntry.playCount
}
}

userAssetData.updateValue(
UserAssetData(lastListen: Date(), playCount: playCount),
forKey: next.id
)
print("picking asset: \(next)")
print("picking asset: \(next)")
}
return next
}
Expand Down
2 changes: 1 addition & 1 deletion RWFramework/RWFramework/Playlist/TimedAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class TimedAssetFilter: AssetFilter {
]).then { data in
self.timedAssets = data
}
return .discard
return .neutral
} else if timedAssets!.isEmpty {
return .discard
}
Expand Down

0 comments on commit 8af92d0

Please sign in to comment.