2017-07-21 02:48:04 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-07-25 19:06:37 -04:00
|
|
|
require 'tox'
|
|
|
|
|
2017-07-31 00:41:12 -04:00
|
|
|
require 'singleton'
|
2017-07-21 02:48:04 -04:00
|
|
|
|
2017-08-11 22:32:29 -04:00
|
|
|
require 'obredux'
|
|
|
|
require 'obredux/thunk'
|
|
|
|
|
2017-07-27 19:18:28 -04:00
|
|
|
require 'helpers'
|
2017-07-27 20:33:32 -04:00
|
|
|
require 'actions'
|
|
|
|
require 'reducer'
|
2017-07-24 08:48:22 -04:00
|
|
|
require 'screen'
|
2017-07-21 10:00:01 -04:00
|
|
|
|
2017-07-21 02:48:04 -04:00
|
|
|
class Main
|
2017-07-31 00:41:12 -04:00
|
|
|
include Singleton
|
2017-07-21 02:48:04 -04:00
|
|
|
|
2017-07-31 00:41:12 -04:00
|
|
|
SAVEDATA_FILENAME = File.expand_path '../savedata', __dir__
|
2017-07-21 02:48:04 -04:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
call
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-07-27 21:47:18 -04:00
|
|
|
def store
|
2017-08-11 21:58:10 -04:00
|
|
|
@store ||= Obredux::Store.new Reducer, [Obredux::Thunk.new]
|
2017-07-27 21:55:30 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def state
|
|
|
|
store.state.merge(
|
|
|
|
on_window_left: method(:on_window_left),
|
|
|
|
on_window_right: method(:on_window_right),
|
|
|
|
|
|
|
|
on_menu_up: method(:on_menu_up),
|
|
|
|
on_menu_down: method(:on_menu_down),
|
|
|
|
|
|
|
|
on_new_message_enter: method(:on_new_message_enter),
|
|
|
|
|
|
|
|
on_new_message_putc: method(:on_new_message_putc),
|
|
|
|
|
|
|
|
on_new_message_left: method(:on_new_message_left),
|
|
|
|
on_new_message_right: method(:on_new_message_right),
|
|
|
|
on_new_message_home: method(:on_new_message_home),
|
|
|
|
on_new_message_end: method(:on_new_message_end),
|
|
|
|
|
|
|
|
on_new_message_backspace: method(:on_new_message_backspace),
|
|
|
|
on_new_message_delete: method(:on_new_message_delete),
|
|
|
|
).freeze
|
2017-07-27 21:47:18 -04:00
|
|
|
end
|
|
|
|
|
2017-07-21 02:48:04 -04:00
|
|
|
def call
|
2017-07-27 21:55:30 -04:00
|
|
|
@screen = Screen.new
|
|
|
|
Style.default = Style.new
|
|
|
|
|
2017-07-27 22:44:32 -04:00
|
|
|
Reducer.screen_width = Curses.stdscr.maxx
|
|
|
|
Reducer.screen_height = Curses.stdscr.maxy
|
|
|
|
|
2017-07-25 19:13:06 -04:00
|
|
|
tox_options = Tox::Options.new
|
|
|
|
tox_options.savedata = File.binread SAVEDATA_FILENAME if File.exist? SAVEDATA_FILENAME
|
|
|
|
|
|
|
|
@tox_client = Tox::Client.new tox_options
|
2017-07-25 19:06:37 -04:00
|
|
|
|
2017-07-25 19:26:38 -04:00
|
|
|
on_friends_load @tox_client.friends
|
|
|
|
|
2017-07-25 22:19:57 -04:00
|
|
|
@tox_client.on_iteration(&method(:on_iteration))
|
2017-07-25 22:30:40 -04:00
|
|
|
@tox_client.on_friend_request(&method(:on_friend_request))
|
2017-07-25 21:17:18 -04:00
|
|
|
@tox_client.on_friend_message(&method(:on_friend_message))
|
2017-07-25 23:10:47 -04:00
|
|
|
@tox_client.on_friend_name_change(&method(:on_friend_name_change))
|
2017-07-25 23:26:50 -04:00
|
|
|
@tox_client.on_friend_status_message_change(&method(:on_friend_status_message_change))
|
2017-07-25 23:46:15 -04:00
|
|
|
@tox_client.on_friend_status_change(&method(:on_friend_status_change))
|
2017-07-25 21:17:18 -04:00
|
|
|
|
2017-07-25 22:13:11 -04:00
|
|
|
@tox_client.run
|
|
|
|
ensure
|
2017-07-25 19:13:06 -04:00
|
|
|
@screen&.close
|
|
|
|
File.binwrite SAVEDATA_FILENAME, @tox_client.savedata if @tox_client
|
2017-07-21 02:48:04 -04:00
|
|
|
end
|
|
|
|
|
2017-07-25 22:35:43 -04:00
|
|
|
#################
|
|
|
|
# Tox callbacks #
|
|
|
|
#################
|
|
|
|
|
2017-07-25 22:19:57 -04:00
|
|
|
def on_iteration
|
2017-07-24 08:48:22 -04:00
|
|
|
@screen.poll
|
2017-07-27 21:55:30 -04:00
|
|
|
@screen.props = state
|
2017-07-27 20:02:55 -04:00
|
|
|
@screen.draw
|
2017-07-21 08:03:53 -04:00
|
|
|
end
|
2017-07-24 18:38:23 -04:00
|
|
|
|
2017-07-25 19:26:38 -04:00
|
|
|
def on_friends_load(friends)
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::LoadFriends.new friends
|
2017-07-25 19:43:52 -04:00
|
|
|
end
|
|
|
|
|
2017-08-11 22:26:11 -04:00
|
|
|
def on_friend_request(public_key, text)
|
|
|
|
store.dispatch Actions::AddFriend.new @tox_client, public_key, text
|
2017-07-25 19:26:38 -04:00
|
|
|
end
|
|
|
|
|
2017-07-25 21:17:18 -04:00
|
|
|
def on_friend_message(friend, text)
|
2017-07-28 00:05:11 -04:00
|
|
|
store.dispatch Actions::AddFriendMessage.new friend, text
|
2017-07-25 23:10:47 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_friend_name_change(friend, name)
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::ChangeFriendName.new friend, name
|
2017-07-25 23:26:50 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_friend_status_message_change(friend, status_message)
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::ChangeFriendStatusMessage.new friend, status_message
|
2017-07-25 23:46:15 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_friend_status_change(friend, status)
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::ChangeFriendStatus.new friend, status
|
2017-07-25 21:17:18 -04:00
|
|
|
end
|
|
|
|
|
2017-07-25 22:35:43 -04:00
|
|
|
####################
|
|
|
|
# Screen callbacks #
|
|
|
|
####################
|
|
|
|
|
2017-07-25 12:03:32 -04:00
|
|
|
def on_window_left
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::WindowLeft.new
|
2017-07-25 12:03:32 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_window_right
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::WindowRight.new
|
2017-07-25 12:03:32 -04:00
|
|
|
end
|
2017-07-25 12:22:38 -04:00
|
|
|
|
|
|
|
def on_menu_up
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::MenuUp.new
|
2017-07-25 12:22:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_menu_down
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::MenuDown.new
|
2017-07-25 12:22:38 -04:00
|
|
|
end
|
2017-07-25 12:36:19 -04:00
|
|
|
|
2017-07-26 04:07:36 -04:00
|
|
|
def on_new_message_enter
|
2017-07-28 00:42:05 -04:00
|
|
|
return if state[:data][:active_friend_index].nil?
|
|
|
|
|
|
|
|
friend_number = state[:data][:friends].keys[state[:data][:active_friend_index]]
|
|
|
|
|
|
|
|
return if friend_number.nil?
|
|
|
|
|
|
|
|
text = state[:data][:friends][friend_number][:new_message][:text].strip.freeze
|
|
|
|
|
|
|
|
return if text.empty?
|
|
|
|
|
|
|
|
friend = @tox_client.friend friend_number
|
|
|
|
error = false
|
|
|
|
|
|
|
|
begin
|
|
|
|
friend.send_message text
|
|
|
|
rescue
|
|
|
|
error = true
|
|
|
|
end
|
|
|
|
|
|
|
|
store.dispatch Actions::AddOutFriendMessage.new friend, text, error
|
2017-07-26 04:07:36 -04:00
|
|
|
end
|
|
|
|
|
2017-07-25 12:36:19 -04:00
|
|
|
def on_new_message_putc(char)
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::NewMessagePutc.new char
|
2017-07-25 12:36:19 -04:00
|
|
|
end
|
2017-07-25 13:21:15 -04:00
|
|
|
|
2017-07-25 13:36:05 -04:00
|
|
|
def on_new_message_left
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::NewMessageLeft.new
|
2017-07-25 13:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_new_message_right
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::NewMessageRight.new
|
2017-07-25 13:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_new_message_home
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::NewMessageHome.new
|
2017-07-25 13:36:05 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_new_message_end
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::NewMessageEnd.new
|
2017-07-25 13:36:05 -04:00
|
|
|
end
|
|
|
|
|
2017-07-25 13:41:55 -04:00
|
|
|
def on_new_message_backspace
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::NewMessageBackspace.new
|
2017-07-25 13:41:55 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def on_new_message_delete
|
2017-07-27 21:47:18 -04:00
|
|
|
store.dispatch Actions::NewMessageDelete.new
|
2017-07-25 13:41:55 -04:00
|
|
|
end
|
2017-07-21 08:38:11 -04:00
|
|
|
end
|