diff options
author | Eric Wong <e@80x24.org> | 2015-12-07 02:34:28 +0000 |
---|---|---|
committer | Eric Wong <e@80x24.org> | 2015-12-14 02:04:44 +0000 |
commit | 6c8504238ddbd96deea577df58c043b95cb1068d (patch) | |
tree | 3ddcebc0f388d6935b3d4df4dda23851a41aa73d /lib/dtas/tracklist.rb | |
parent | 71cc32bcac667c699b89ef5f0b5147cf1b3670a5 (diff) | |
download | dtas-6c8504238ddbd96deea577df58c043b95cb1068d.tar.gz |
This (from the point of the client) will atomically swap two tracks. This should allow easily implementing of a tracklist editor.
Diffstat (limited to 'lib/dtas/tracklist.rb')
-rw-r--r-- | lib/dtas/tracklist.rb | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/dtas/tracklist.rb b/lib/dtas/tracklist.rb index b9abe55..7366799 100644 --- a/lib/dtas/tracklist.rb +++ b/lib/dtas/tracklist.rb @@ -220,4 +220,25 @@ class DTAS::Tracklist # :nodoc: end @goto_pos = prev_idx end + + def swap(a_id, b_id) + ok = { a_id => a_idx = [], b_id => b_idx = [] } + @list.each_with_index do |t,i| + ary = ok.delete(t.track_id) or next + ary[0] = i + break if ok.empty? + end + a_idx = a_idx[0] or return + b_idx = b_idx[0] or return + @list[a_idx], @list[b_idx] = @list[b_idx], @list[a_idx] + unless @shuffle + [ :@goto_pos, :@pos ].each do |v| + case instance_variable_get(v) + when a_idx then instance_variable_set(v, b_idx) + when b_idx then instance_variable_set(v, a_idx) + end + end + end + true + end end |