diff options
Diffstat (limited to 'internal/client/ui/core')
| -rw-r--r-- | internal/client/ui/core/chat/chat.go | 25 | ||||
| -rw-r--r-- | internal/client/ui/core/core.go | 15 | ||||
| -rw-r--r-- | internal/client/ui/core/networklist/networklist.go | 4 | ||||
| -rw-r--r-- | internal/client/ui/core/state/state.go | 16 |
4 files changed, 34 insertions, 26 deletions
diff --git a/internal/client/ui/core/chat/chat.go b/internal/client/ui/core/chat/chat.go index ded47dc..d32d1e5 100644 --- a/internal/client/ui/core/chat/chat.go +++ b/internal/client/ui/core/chat/chat.go @@ -224,7 +224,6 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { // m.keepPreviousLastRead = false // } - log.Println("HIT") state.State.LastReadMessages[frequency.ID] = lastMsg delete(state.State.RemoteNotifications, frequency.ID) } else if m.base == SnapToBottom { @@ -1005,21 +1004,21 @@ func (m *Model) renderMessages(screenHeight int) string { return NoAccess().Width(m.width).Height(screenHeight).String() + "\n" } - var id *snowflake.ID + var chatId *snowflake.ID var btree *btree.BTreeG[data.Message] networkId := state.NetworkId(m.networkIndex) if m.frequencyIndex != -1 && networkId != nil { frequencies := state.State.Frequencies[*networkId] frequencyId := frequencies[m.frequencyIndex].ID - id = &frequencyId + chatId = &frequencyId btree = state.State.Messages[frequencyId] } else if m.receiverIndex != -1 { receiverId := state.Data.Signals[m.receiverIndex] - id = &receiverId + chatId = &receiverId btree = state.State.Messages[receiverId] } - if btree == nil || btree.Len() == 0 || id == nil { + if btree == nil || btree.Len() == 0 || chatId == nil { if m.frequencyIndex != -1 { return NoMessagesFrequency().Width(m.width).Height(screenHeight).String() + "\n" } else if m.receiverIndex != -1 { @@ -1040,7 +1039,7 @@ func (m *Model) renderMessages(screenHeight int) string { renderedGroups := []string{} group := []data.Message{} - lastReadId := state.State.LastReadMessages[*id] + lastReadId := state.State.LastReadMessages[*chatId] if m.keepPreviousLastRead { lastReadId = m.previousLastReadMsg } @@ -1050,12 +1049,12 @@ func (m *Model) renderMessages(screenHeight int) string { last = message.ID // >= is needed so if the message was deleted // Any prior messages will still be separated by "new" - isLastRead := lastReadId != nil && *lastReadId >= message.ID + isThisLastReadMsg := lastReadId != nil && *lastReadId >= message.ID if len(group) == 0 { group = append(group, message) - if isLastRead { + if isThisLastReadMsg { lastReadId = nil } @@ -1065,7 +1064,7 @@ func (m *Model) renderMessages(screenHeight int) string { lastMsg := group[0] sameSender := lastMsg.SenderID == message.SenderID withinTime := lastMsg.ID.Time()-message.ID.Time() <= TimeGap - if sameSender && withinTime && len(group) < MaxViewableMessages && !isLastRead { + if sameSender && withinTime && len(group) < MaxViewableMessages && !isThisLastReadMsg { group = append(group, message) return true } @@ -1074,7 +1073,7 @@ func (m *Model) renderMessages(screenHeight int) string { renderedGroups = append(renderedGroups, renderedGroup) group = []data.Message{message} - if isLastRead { + if isThisLastReadMsg { lineWidth := m.width - lipgloss.Width(NewText) line := strings.Repeat(NewSymbol, lineWidth) @@ -1104,11 +1103,11 @@ func (m *Model) renderMessages(screenHeight int) string { if ok && last == first.ID { m.maxMessagesHeight = height - remainingHeight m.SetIndex(m.index) - if s, ok := state.State.ChatState[*id]; ok { + if s, ok := state.State.ChatState[*chatId]; ok { s.MaxHeight = m.maxMessagesHeight - state.State.ChatState[*id] = s + state.State.ChatState[*chatId] = s } else { - state.State.ChatState[*id] = state.ChatState{ + state.State.ChatState[*chatId] = state.ChatState{ MaxHeight: m.maxMessagesHeight, } } diff --git a/internal/client/ui/core/core.go b/internal/client/ui/core/core.go index 5f5fc3c..fd91026 100644 --- a/internal/client/ui/core/core.go +++ b/internal/client/ui/core/core.go @@ -723,13 +723,13 @@ func (m *Model) HasPopup() bool { } func calculateNotifications() { - for _, signal := range state.Data.Signals { - if _, ok := state.State.Messages[signal]; !ok { + for _, signalId := range state.Data.Signals { + if _, ok := state.State.Messages[signalId]; !ok { continue } - pings := getSignalNotification(signal) - state.State.LocalNotifications[signal] = pings + pings := getSignalNotification(signalId) + state.State.LocalNotifications[signalId] = pings } for networkId := range state.State.Networks { @@ -779,7 +779,8 @@ func getFrequencyNotification(networkId, frequencyId snowflake.ID) (_ int, _ boo pings++ } - return pings <= 10 + // No need to continue if we have 10 pings + return pings < 10 }) return pings, hasNotif @@ -800,7 +801,9 @@ func getSignalNotification(signal snowflake.ID) int { btree.AscendGreaterOrEqual(data.Message{ID: *lastReadMsg + 1}, func(item data.Message) bool { pings++ - return pings <= 10 + + // No need to continue if we have 10 pings + return pings < 10 }) return pings diff --git a/internal/client/ui/core/networklist/networklist.go b/internal/client/ui/core/networklist/networklist.go index 4024668..9b594b2 100644 --- a/internal/client/ui/core/networklist/networklist.go +++ b/internal/client/ui/core/networklist/networklist.go @@ -44,8 +44,8 @@ func (m Model) View() string { var builder strings.Builder pings := 0 - for _, signal := range state.Data.Signals { - p, _ := state.MergedNotification(signal) + for _, signalId := range state.Data.Signals { + p, _ := state.MergedNotification(signalId) pings += p } var signalsIcon lipgloss.Style diff --git a/internal/client/ui/core/state/state.go b/internal/client/ui/core/state/state.go index 1eafaee..fec4695 100644 --- a/internal/client/ui/core/state/state.go +++ b/internal/client/ui/core/state/state.go @@ -269,10 +269,13 @@ func GetLastMessage(id snowflake.ID) *snowflake.ID { } func IsFrequency(id snowflake.ID) bool { - // Note this is very expensive and inefficient + // OPTIMIZE: this is very expensive and inefficient // A map is better but as most of the time frequencies are iterated // over based on a network id, this would add overhead // And this function is only used once in notifications + // Maybe there should be a special bit in the snowflake to determine this? + // This could work if frequencies, user IDs, network IDs etc would all have + // their own "pool" to generate from (using the "machine id" bits) for _, frequencies := range State.Frequencies { for _, frequency := range frequencies { @@ -296,7 +299,10 @@ func UpdateNotifications(info *packet.NotificationsInfo) []snowflake.ID { if ping != nil { State.RemoteNotifications[source] = int(*ping) - if !IsFrequency(source) && !slices.Contains(Data.Signals, source) { + // When someone messages you, and you don't have a signal with him + // already, add a signal with him so you see his messages + // PERF: IsFrequency is expensive so contains is checked first + if !slices.Contains(Data.Signals, source) && !IsFrequency(source) { signals = append(signals, source) } } else { @@ -420,8 +426,8 @@ func UpdateUsersInfo(info *packet.UsersInfo) { } } -func MergedNotification(id snowflake.ID) (pings int, ok bool) { - remotePings, remoteOk := State.RemoteNotifications[id] - localPings, localOk := State.LocalNotifications[id] +func MergedNotification(chatId snowflake.ID) (pings int, ok bool) { + remotePings, remoteOk := State.RemoteNotifications[chatId] + localPings, localOk := State.LocalNotifications[chatId] return remotePings + localPings, remoteOk || localOk } |
