From a47a13e9c9a2ea2d9e43568605e61d51317ab64d Mon Sep 17 00:00:00 2001 From: Helloyunho Date: Mon, 19 Jun 2023 22:29:33 +0900 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20feat:=20text=20glow=20when=20the?= =?UTF-8?q?re's=20unread=20message?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Swiftcord/Views/Server/ChannelButton.swift | 9 +++++++-- Swiftcord/Views/Server/ChannelList.swift | 12 ++++++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/Swiftcord/Views/Server/ChannelButton.swift b/Swiftcord/Views/Server/ChannelButton.swift index fd304b95..8dbc70cd 100644 --- a/Swiftcord/Views/Server/ChannelButton.swift +++ b/Swiftcord/Views/Server/ChannelButton.swift @@ -16,15 +16,16 @@ struct ChannelButton: View, Equatable { } let channel: Channel + var unread: Bool @Binding var selectedCh: Channel? var body: some View { if channel.type == .dm || channel.type == .groupDM { - DMButton(dm: channel, selectedCh: $selectedCh) + DMButton(dm: channel, unread: unread, selectedCh: $selectedCh) .buttonStyle(DiscordChannelButton(isSelected: selectedCh?.id == channel.id)) .controlSize(.large) } else { - GuildChButton(channel: channel, selectedCh: $selectedCh) + GuildChButton(channel: channel, unread: unread, selectedCh: $selectedCh) .buttonStyle(DiscordChannelButton(isSelected: selectedCh?.id == channel.id)) } } @@ -32,6 +33,7 @@ struct ChannelButton: View, Equatable { struct GuildChButton: View { let channel: Channel + var unread: Bool @Binding var selectedCh: Channel? @EnvironmentObject var serverCtx: ServerContext @@ -48,12 +50,14 @@ struct GuildChButton: View { .padding(.vertical, 5) .padding(.horizontal, 4) .frame(maxWidth: .infinity, alignment: .leading) + .foregroundColor(unread ? Color(nsColor: .labelColor) : .gray) } } } struct DMButton: View { let dm: Channel // swiftlint:disable:this identifier_name + var unread: Bool @Binding var selectedCh: Channel? @EnvironmentObject var gateway: DiscordGateway @@ -82,6 +86,7 @@ struct DMButton: View { .font(.caption) } } + .foregroundColor(unread ? Color(nsColor: .labelColor) : .gray) Spacer() } .padding(.horizontal, 6) diff --git a/Swiftcord/Views/Server/ChannelList.swift b/Swiftcord/Views/Server/ChannelList.swift index b16497b4..fdc99b9f 100644 --- a/Swiftcord/Views/Server/ChannelList.swift +++ b/Swiftcord/Views/Server/ChannelList.swift @@ -17,15 +17,23 @@ struct ChannelList: View { @AppStorage("nsfwShown") var nsfwShown: Bool = true @EnvironmentObject var serverCtx: ServerContext @EnvironmentObject var gateway: DiscordGateway + + private func hasUnreadMsg(for channel: Channel) -> Bool { + if let lastID = gateway.readState[channel.id]?.last_message_id, let _chLastID = channel.last_message_id, let chLastID = Int(_chLastID), lastID.intValue < chLastID { + return true + } else { + return false + } + } @_transparent @_optimize(speed) @ViewBuilder private func item(for channel: Channel) -> some View { - ChannelButton(channel: channel, selectedCh: $selCh) + ChannelButton(channel: channel, unread: hasUnreadMsg(for: channel), selectedCh: $selCh) .equatable() .listRowInsets(.init(top: 1, leading: 0, bottom: 1, trailing: 0)) .listRowBackground(Spacer().overlay(alignment: .leading) { // Check if we should show unread indicator - if let lastID = gateway.readState[channel.id]?.last_message_id, let _chLastID = channel.last_message_id, let chLastID = Int(_chLastID), lastID.intValue < chLastID { + if hasUnreadMsg(for: channel) { Circle().fill(.primary).frame(width: 8, height: 8).offset(x: 2) } })