Class: Twterm::Tab::UserListManagement

Inherits:
AbstractTab show all
Includes:
Publisher, Dumpable, Loadable, Searchable
Defined in:
lib/twterm/tab/user_list_management.rb

Constant Summary collapse

@@lists =
Concurrent::Array.new

Instance Attribute Summary

Attributes inherited from AbstractTab

#window

Instance Method Summary collapse

Methods included from Scrollable

#scroller, #total_item_count

Methods included from Publisher

#publish

Methods included from Utils

check_type

Methods included from Loadable

#initially_loaded!, #initially_loaded?

Methods included from Dumpable

included

Methods inherited from AbstractTab

#==, #close, #find_or_fetch_list, #find_or_fetch_status, #find_or_fetch_user, #render

Methods included from Subscriber

included, #subscribe, #unsubscribe

Constructor Details

#initialize(app, client, user_id) ⇒ UserListManagement

Returns a new instance of UserListManagement.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/twterm/tab/user_list_management.rb', line 20

def initialize(app, client, user_id)
  super(app, client)

  @user_id = user_id
  @list_ids = Concurrent::Array.new

  client.owned_lists.then do |lists|
    @@lists = lists.sort_by(&:full_name)
    render
  end

  client.memberships(user_id, filter_to_owned_lists: true, count: 1000).then do |lists|
    @list_ids = lists.map(&:id)
    initially_loaded!
  end
end

Instance Method Details

#drawable_item_countObject



37
38
39
# File 'lib/twterm/tab/user_list_management.rb', line 37

def drawable_item_count
  window.maxy / 3
end

#dumpObject



41
42
43
# File 'lib/twterm/tab/user_list_management.rb', line 41

def dump
  user_id
end

#imageObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/twterm/tab/user_list_management.rb', line 45

def image
  return Image.string(initially_loaded? ? 'No lists found' : 'Loading...') if items.empty?

  drawable_items.map.with_index do |list, i|
    curr = scroller.current_index?(i)
    cursor = Image.cursor(2, curr)

    summary = Image.checkbox(@list_ids.include?(list.id)) - Image.whitespace - Image.string(list.full_name).bold(curr)
    description = Image.string('    ') - Image.string(list.description)

    cursor - Image.whitespace - (summary | description)
  end
    .intersperse(Image.blank_line)
    .reduce(Image.empty, :|)
end

#itemsObject



61
62
63
# File 'lib/twterm/tab/user_list_management.rb', line 61

def items
  @@lists
end

#matches?(list, query) ⇒ Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
# File 'lib/twterm/tab/user_list_management.rb', line 65

def matches?(list, query)
  [
    list.description,
    list.full_name,
  ].any? { |x| x.downcase.include?(query.downcase) }
end

#respond_to_key(key) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/twterm/tab/user_list_management.rb', line 72

def respond_to_key(key)
  return true if scroller.respond_to_key(key)

  case key
  when 10 then toggle
  end
end

#titleObject



80
81
82
# File 'lib/twterm/tab/user_list_management.rb', line 80

def title
  (user.nil? ? 'Loading' : "@#{user.screen_name} lists").freeze
end