-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
208 lines (173 loc) · 5.41 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"errors"
"github.com/k2on/koms/types"
)
type AppState struct {
cache AppCache
conversations map[int]ConversationState
pos int
focusInput bool
jumpBy int
quit bool
search SearchQuery
}
type SearchQuery struct {
open bool
opened bool
filters []SearchQueryFilter
filterPos int
focusInput bool
result SearchResult
}
type SearchResult = AppCache
type SearchQueryFilter struct {
name string
}
type ConversationState struct {
draft string
messagePos int
provider string
selected []string
carouselImageSelections map[int]int
}
type AppCache struct {
conversations []types.Conversation
messages map[int][]types.Message
}
func MakeEmptyState() AppState {
return AppState{
cache: AppCache{
conversations: []types.Conversation{},
messages: make(map[int][]types.Message),
},
conversations: make(map[int]ConversationState),
pos: 0,
jumpBy: -1,
}
}
func GetStateConversations(state AppState) []types.Conversation {
isFiltered := len(state.search.filters) > 0
if isFiltered { return state.search.result.conversations }
return state.cache.conversations
}
func GetStateConversationsLen(state AppState) int {
return len(GetStateConversations(state))
}
func GetStateConversation(state AppState) ConversationState {
return state.conversations[state.pos]
}
func GetCacheConversation(state AppState) types.Conversation {
return state.cache.conversations[state.pos]
}
func GetCacheMessages(state AppState) ([]types.Message, bool) {
messages, exists := state.cache.messages[state.pos]
return messages, exists
}
func GetStateMessagePos(state AppState) int {
return state.conversations[state.pos].messagePos
}
func GetStateDraft(state AppState) string {
return state.conversations[state.pos].draft
}
func GetStateProvider(state AppState) string {
return state.conversations[state.pos].provider
}
func GetStateMessage(state AppState) (types.Message, error) {
msgs, exists := GetCacheMessages(state)
if !exists { return types.Message{}, errors.New("no cached messages for convo") }
if len(msgs) == 0 { return types.Message{}, errors.New("no messages in convo") }
messagePos := GetStateMessagePos(state)
return msgs[messagePos], nil
}
func UpdateStateConversationState(state AppState, fn func(ConversationState) ConversationState) AppState {
conversation := GetStateConversation(state)
state.conversations[state.pos] = fn(conversation)
return state
}
func UpdateStateConversationPos(state AppState, pos int) AppState {
state.pos = pos
return state
}
func UpdateStateDraft(state AppState, draft string) AppState {
return UpdateStateConversationState(state, func(convo ConversationState) ConversationState {
convo.draft = draft
return convo
})
}
func UpdateStateMessagePos(state AppState, pos int) AppState {
return UpdateStateConversationState(state, func(convo ConversationState) ConversationState {
convo.messagePos = pos
return convo
})
}
func UpdateStateMessagePosFn(state AppState, fn func(int) int) AppState {
return UpdateStateConversationState(state, func(convo ConversationState) ConversationState {
convo.messagePos = fn(convo.messagePos)
return convo
})
}
func UpdateStateCarouselSelectedImage(state AppState, fn func(int) int) AppState {
return UpdateStateConversationState(state, func(convo ConversationState) ConversationState {
if convo.carouselImageSelections == nil { convo.carouselImageSelections = make(map[int]int) }
imageSelection := convo.carouselImageSelections[convo.messagePos]
convo.carouselImageSelections[convo.messagePos] = fn(imageSelection)
return convo
})
}
func GetStateCarouselSelectedImage(state AppState) int {
convo := GetStateConversation(state)
return convo.carouselImageSelections[convo.messagePos]
}
func UpdateStateProvider(state AppState, provider string) AppState {
return UpdateStateConversationState(state, func(convo ConversationState) ConversationState {
convo.provider = provider
return convo
})
}
func UpdateStateSelected(state AppState, fn func([]string) []string) AppState {
return UpdateStateConversationState(state, func(convo ConversationState) ConversationState {
convo.selected = fn(convo.selected)
return convo
})
}
func UpdateStateSelectedToggle(state AppState, toggledId string) AppState {
return UpdateStateSelected(state, func(ids []string) []string {
result := []string{}
removed := false
for _, id := range ids {
if id == toggledId { removed = true; continue }
result = append(result, id)
}
if !removed { result = append(result, toggledId) }
return result
})
}
func UpdateStateSearchFilterPos(state AppState, pos int) AppState {
state.search.filterPos = pos
return state
}
func UpdateStateSearchFilterPosFn(state AppState, fn IntMod) AppState {
return UpdateStateSearchFilterPos(state, fn(state.search.filterPos))
}
func UpdateStateSearchOpen(state AppState) AppState {
state.search.open = true
return UpdateStateSearchFocus(state)
}
func UpdateStateSearchClose(state AppState) AppState {
state.search.open = false
state.search.opened = false
state.search.focusInput = false
return state
}
func UpdateStateSearchFocus(state AppState) AppState {
state.search.focusInput = true
return state
}
func UpdateStateSearchFilters(state AppState, filters []SearchQueryFilter) AppState {
state.search.filters = filters
return state
}
func UpdateStateSearchFiltersClear(state AppState) AppState {
return UpdateStateSearchFilters(state, []SearchQueryFilter{})
}