Class: Soundcloud9000::Controllers::TrackController

Inherits:
Controller
  • Object
show all
Defined in:
lib/soundcloud9000/controllers/track_controller.rb

Overview

Handles the navigation thru the current track list

Instance Attribute Summary

Attributes inherited from Controller

#events

Instance Method Summary collapse

Methods inherited from Controller

#render

Constructor Details

#initialize(view, client) ⇒ TrackController

Returns a new instance of TrackController.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/soundcloud9000/controllers/track_controller.rb', line 12

def initialize(view, client)
  super(view)

  @client = client

  events.on(:key) do |key|
    case key
    when :enter
      @view.select
      events.trigger(:select, current_track)
    when :up, :k
      @view.up
    when :down, :j
      @view.down
      @tracks.load_more if @view.bottom?
    when :u
      user = fetch_user_with_message('Change to soundcloud user: ')
      unless user.nil?
        @client.current_user = user
        @tracks.collection_to_load = :user
        @tracks.clear_and_replace
      end
    when :f
      @client.current_user = fetch_user_with_message('Change to SoundCloud user\'s favourites: ') if @client.current_user.nil?
      unless @client.current_user.nil?
        @tracks.collection_to_load = :favorites
        @tracks.clear_and_replace
      end
    when :s
      @view.clear
      @client.current_user = fetch_user_with_message('Change to SoundCloud user: ') if @client.current_user.nil?
      unless @client.current_user.nil?
        set = UI::Input.getstr('Change to SoundCloud playlist: ')
        set_request = @client.resolve(@client.current_user.permalink + '/sets/' + set)
        if set_request.nil?
          UI::Input.error("No such set/playlist '#{set}' for #{@client.current_user.username}")
          @client.current_user = nil
        else
          @tracks.playlist = Models::Playlist.new(set_request)
          @tracks.collection_to_load = :playlist
          @tracks.clear_and_replace
        end
      end
    # when :im grown up, im gonna go to brown and be smarter than u, sumanth <333 (Rahel Selemon, April 29, 2019)
    when :m
      @tracks.shuffle = !@tracks.shuffle
      UI::Input.message("Shuffle #{@tracks.shuffle ? 'enabled' : 'disabled'}.")
    when :h
      @tracks.help = !@tracks.help
      if @tracks.help
        height = 40
        width  = 80
        top    = (Curses.lines - height) / 2
        left   = (Curses.cols - width) / 2
        win    = Curses::Window.new(height, width, top, left)
        win.attrset(Curses.color_pair(4) | Curses::A_REVERSE | Curses::A_BOLD)
        win.setpos(2, 3)
        help = Application.get_help
        win.addstr(help)
        win.setpos(help.lines.count, 0)
        win.addstr('-' * width)
        win.setpos(help.lines.count + 1, 3)
        shortcuts = %(
Shortcuts:
[enter]/[ctrl-J]    play selected track from beginning
[down]/j            select track below currently selected track
[up]/k              select track above currently selected track
[space]             play or pause the current track
[right]/[left]      move backward or forward in current track
1                   jump to the time at 1/10 of the current track
2                   jump to the time at 2/10 of the current track
3                   jump to the time at 3/10 of the current track
4                   jump to the time at 4/10 of the current track
5                   jump to the time at 5/10 of the current track
6                   jump to the time at 6/10 of the current track
7                   jump to the time at 7/10 of the current track
8                   jump to the time at 8/10 of the current track
9                   jump to the time at 9/10 of the current track
u                   play tracks of different users
f                   play favorites from a user
s                   play sets/playlists from a user
m                   play songs in random order
h                   toggle this help screen
o                   change order of tracks
        )
        win.addstr(shortcuts)
        win.box('|', '-')
        win.refresh
        win.getch
        win.close
      else
        @tracks.clear_and_replace
      end
    when :o
      p 'order'
    end
  end
end

Instance Method Details

#bind_to(tracks) ⇒ Object



126
127
128
129
# File 'lib/soundcloud9000/controllers/track_controller.rb', line 126

def bind_to(tracks)
  @tracks = tracks
  @view.bind_to(tracks)
end

#current_trackObject



122
123
124
# File 'lib/soundcloud9000/controllers/track_controller.rb', line 122

def current_track
  @tracks[@view.current]
end

#fetch_user_with_message(message_to_display) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'lib/soundcloud9000/controllers/track_controller.rb', line 111

def fetch_user_with_message(message_to_display)
  permalink = UI::Input.getstr(message_to_display)
  user_hash = @client.resolve(permalink)
  if user_hash
    Models::User.new(user_hash)
  else
    UI::Input.error("No such user '#{permalink}'. Use u to try again.")
    nil
  end
end

#loadObject



131
132
133
# File 'lib/soundcloud9000/controllers/track_controller.rb', line 131

def load
  @tracks.load
end

#next_trackObject



135
136
137
138
139
140
141
142
143
# File 'lib/soundcloud9000/controllers/track_controller.rb', line 135

def next_track
  if @tracks.shuffle
    @view.random
  else
    @view.down
  end
  @view.select
  events.trigger(:select, current_track)
end