Archived
1
0
Fork 0

Move Actions::NewMessageEnter implementation from reducer

This commit is contained in:
Braiden Vasco 2017-07-28 04:42:05 +00:00
parent f4e28de1f6
commit d7d9a0da54
6 changed files with 74 additions and 64 deletions

View file

@ -8,7 +8,7 @@ require 'actions/add_friend_message'
require 'actions/change_friend_name'
require 'actions/change_friend_status'
require 'actions/change_friend_status_message'
require 'actions/new_message_enter'
require 'actions/add_out_friend_message'
require 'actions/new_message_putc'
module Actions

View file

@ -0,0 +1,29 @@
# frozen_string_literal: true
module Actions
class AddOutFriendMessage < Obredux::Action
attr_reader :friend, :text, :error
def initialize(friend, text, error)
self.friend = friend
self.text = text
self.error = error
end
private
def friend=(value)
raise TypeError, "expected friend to be a #{Tox::Friend}" unless value.is_a? Tox::Friend
@friend = value
end
def text=(value)
raise TypeError, "expected text to be a #{String}" unless value.is_a? String
@text = value.frozen? ? value : value.dup.freeze
end
def error=(value)
@error = !!value
end
end
end

View file

@ -1,18 +0,0 @@
# frozen_string_literal: true
module Actions
class NewMessageEnter < Obredux::Action
attr_reader :tox_client
def initialize(tox_client)
self.tox_client = tox_client
end
private
def tox_client=(value)
raise TypeError, "expected Tox client to be a #{Tox::Client}" unless value.is_a? Tox::Client
@tox_client = value
end
end
end

View file

@ -137,7 +137,26 @@ private
end
def on_new_message_enter
store.dispatch Actions::NewMessageEnter.new @tox_client
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
end
def on_new_message_putc(char)

View file

@ -104,8 +104,6 @@ private
menu_up
when Actions::MenuDown
menu_down
when Actions::NewMessageEnter
new_message_enter
when Actions::NewMessagePutc
new_message_putc
when Actions::NewMessageLeft
@ -229,48 +227,6 @@ private
).freeze
end
def new_message_enter
return state if state[:data][:active_friend_index].nil?
friend_number = state[:data][:friends].keys[state[:data][:active_friend_index]]
return state if friend_number.nil?
text = state[:data][:friends][friend_number][:new_message][:text].strip.freeze
return state if text.empty?
error = false
begin
action.tox_client.friend(friend_number).send_message text
rescue
error = true
end
state.merge(
data: state[:data].merge(
friends: state[:data][:friends].merge(
friend_number => state[:data][:friends][friend_number].merge(
new_message: state[:data][:friends][friend_number][:new_message].merge(
text: '',
cursor_pos: 0,
),
history: (state[:data][:friends][friend_number][:history] + [
error: error,
out: true,
received: false,
time: Time.now.utc.freeze,
name: action.tox_client.name.freeze,
text: text,
]).freeze,
).freeze,
).freeze,
).freeze,
).freeze
end
def new_message_putc
return state if state[:data][:active_friend_index].nil?

View file

@ -25,6 +25,8 @@ module Reducers
change_friend_status_message
when Actions::ChangeFriendStatus
change_friend_status
when Actions::AddOutFriendMessage
add_out_friend_message
else
state
end
@ -115,5 +117,27 @@ module Reducers
).freeze,
).freeze
end
def add_out_friend_message
state.merge(
friends: state[:friends].merge(
action.friend.number => state[:friends][action.friend.number].merge(
new_message: state[:friends][action.friend.number][:new_message].merge(
text: '',
cursor_pos: 0,
),
history: (state[:friends][action.friend.number][:history] + [
error: action.error,
out: true,
received: false,
time: Time.now.utc.freeze,
name: action.friend.client.name.freeze,
text: action.text,
]).freeze,
).freeze,
).freeze,
).freeze
end
end
end