Class: Rubio::Radio

Inherits:
Object
  • Object
show all
Includes:
Glimmer
Defined in:
lib/rubio/radio.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, debug: false) ⇒ Radio

Returns a new instance of Radio.



11
12
13
14
15
16
17
18
# File 'lib/rubio/radio.rb', line 11

def initialize(backend, debug: false)
  @stations_all, @table = RadioBrowser.topvote(500)
  @stations = @stations_all.dup
  @station_uuid = nil
  @player = Player.new(backend)

  monitor_thread(debug)
end

Instance Attribute Details

#playerObject (readonly)

Returns the value of attribute player.



9
10
11
# File 'lib/rubio/radio.rb', line 9

def player
  @player
end

#stationsObject (readonly)

Returns the value of attribute stations.



9
10
11
# File 'lib/rubio/radio.rb', line 9

def stations
  @stations
end

Instance Method Details

#launchObject



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
110
111
# File 'lib/rubio/radio.rb', line 63

def launch
  menu('Radio') do
    menu_item('Stop') do
      on_clicked do
        stop_uuid(@station_uuid)
        @station_uuid = nil
      end
    end
    quit_menu_item do
      on_clicked do
        @player.stop_all
      end
    end
  end
  window('Rubio', 400, 200) do
    vertical_box do
      horizontal_box do
        stretchy false
        search_entry do |se|
          on_changed do
            filter_value = se.text
            if filter_value.empty?
              @stations.replace @stations_all.dup
            else
              @stations.replace(@stations_all.filter do |row_data|
                row_data.name.downcase.include?(filter_value.downcase)
              end)
            end
          end
        end
      end
      horizontal_box do
        table do
          button_column('Play') do
            on_clicked do |row|
              selected_station_at(row)
            end
          end
          text_column('name')
          text_column('language')
          cell_rows <= [self, :stations]
        end
      end
    end
    on_closing do
      @player.stop_all
    end
  end.show
end

#monitor_thread(debug) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubio/radio.rb', line 20

def monitor_thread(debug)
  Glimmer::LibUI.timer(1) do
    p @player.history if debug
    next if @station_uuid.nil? || @player.alive?

    message_box("player '#{@player.backend}' stopped!", @player.thr.to_s)
    stop_uuid(@station_uuid)
    @station_uuid = nil
    true
  end
end

#play_uuid(station_uuid) ⇒ Object



44
45
46
47
48
49
50
51
52
53
# File 'lib/rubio/radio.rb', line 44

def play_uuid(station_uuid)
  station = uuid_to_station(station_uuid)
  begin
    @player.play(station.url)
  rescue StandardError => e
    message_box(e.message)
    return false
  end
  station.playing = true
end

#selected_station_at(idx) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/rubio/radio.rb', line 32

def selected_station_at(idx)
  raise unless idx.is_a?(Integer)

  station_uuid = stations[idx].stationuuid
  stop_uuid(@station_uuid)
  if @station_uuid == station_uuid
    @station_uuid = nil
  elsif play_uuid(station_uuid)
    @station_uuid = station_uuid
  end
end

#stop_uuid(station_uuid) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/rubio/radio.rb', line 55

def stop_uuid(station_uuid)
  return if station_uuid.nil?

  station = uuid_to_station(station_uuid)
  @player.stop
  station.playing = false
end