Class: Rubio::Model::RadioPresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/rubio/model/radio_presenter.rb

Overview

This is a presenter for the Radio view, which is an advanced controller

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ RadioPresenter

Initializes with view options below: :backend, :initial_width, :initial_height, :radio_station_count, :debug, :show_menu, :show_page_count, :show_bookmarks, :show_margins :gradually_fetch_stations, :table_per_page



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rubio/model/radio_presenter.rb', line 17

def initialize(options = {})
  @options = options
  @options[:radio_station_count] = 1_000_000 if options[:radio_station_count] == -1
  @loaded_station_count = [options[:gradually_fetch_stations] ? 100 : options[:radio_station_count], options[:radio_station_count]].min
  @loaded_station_offset = 0
  @stations = Model::RadioBrowser.topvote(@loaded_station_count, offset: @loaded_station_offset)
  @player = Model::Player.new(options[:backend], show_currently_playing: options[:show_currently_playing])
  @initial_width = (options[:initial_width] || (options[:show_bookmarks] ? 740 : 620)).to_i
  @initial_height = (options[:initial_height] || calculate_initial_height).to_i
  @window_height = @initial_height
  @view = :all

  Glimmer::DataBinding::Observer::Proc.new do
    self.window_height = calculate_initial_height
  end.observe(@player, :currently_playing)
end

Instance Attribute Details

#current_stationObject

Returns the value of attribute current_station.



11
12
13
# File 'lib/rubio/model/radio_presenter.rb', line 11

def current_station
  @current_station
end

#initial_heightObject (readonly)

Returns the value of attribute initial_height.



10
11
12
# File 'lib/rubio/model/radio_presenter.rb', line 10

def initial_height
  @initial_height
end

#initial_widthObject (readonly)

Returns the value of attribute initial_width.



10
11
12
# File 'lib/rubio/model/radio_presenter.rb', line 10

def initial_width
  @initial_width
end

#optionsObject (readonly)

Returns the value of attribute options.



10
11
12
# File 'lib/rubio/model/radio_presenter.rb', line 10

def options
  @options
end

#playerObject (readonly)

Returns the value of attribute player.



10
11
12
# File 'lib/rubio/model/radio_presenter.rb', line 10

def player
  @player
end

#stationsObject

Returns the value of attribute stations.



11
12
13
# File 'lib/rubio/model/radio_presenter.rb', line 11

def stations
  @stations
end

#viewObject

Returns the value of attribute view.



11
12
13
# File 'lib/rubio/model/radio_presenter.rb', line 11

def view
  @view
end

#window_heightObject

Returns the value of attribute window_height.



11
12
13
# File 'lib/rubio/model/radio_presenter.rb', line 11

def window_height
  @window_height
end

Instance Method Details

#fetch_more_stationsObject



71
72
73
74
75
76
77
78
79
# File 'lib/rubio/model/radio_presenter.rb', line 71

def fetch_more_stations
  @loaded_station_offset += @loaded_station_count
  @loaded_station_count *= 2
  new_station_count = [@loaded_station_count, options[:radio_station_count] - @loaded_station_offset].min
  old_station_count = @stations.count
  self.stations += Model::RadioBrowser.topvote(new_station_count, offset: @loaded_station_offset)
  @all_stations_fetched = @stations.count == old_station_count
  self.stations
end

#play_stationObject



51
52
53
54
55
56
57
# File 'lib/rubio/model/radio_presenter.rb', line 51

def play_station
  @player.play(current_station.url, station_name: current_station.name)
  current_station.playing = true
rescue => e
  self.current_station = nil
  raise e
end

#select_station(station) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/rubio/model/radio_presenter.rb', line 34

def select_station(station)
  playing = station.playing?
  stop_station
  self.current_station = station
  if playing
    self.current_station = nil
  else
    play_station
  end
end

#stations_incomplete?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/rubio/model/radio_presenter.rb', line 67

def stations_incomplete?
  !@all_stations_fetched && @stations.count < options[:radio_station_count]
end

#stop_stationObject



59
60
61
62
63
64
65
# File 'lib/rubio/model/radio_presenter.rb', line 59

def stop_station
  return if current_station.nil?

  @player.stop
  current_station.playing = false
  self.current_station = nil
end

#toggle_bookmarked_station(station) ⇒ Object



45
46
47
48
49
# File 'lib/rubio/model/radio_presenter.rb', line 45

def toggle_bookmarked_station(station)
  return unless station

  station.bookmarked = !station.bookmarked?
end