Skip to content

Commit

Permalink
Move room badge logic to be built in the view model instead of in the…
Browse files Browse the repository at this point in the history
… view.
  • Loading branch information
pixlwave committed Feb 5, 2024
1 parent 8dc2fa4 commit 1bce84a
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ struct FormattingToolbar_Previews: PreviewProvider, TestablePreview {
static let disabledItems = FormatType.allCases.map { FormatItem(type: $0, state: .disabled) }

static var previews: some View {
VStack {
VStack(spacing: 8) {
FormattingToolbar(formatItems: items) { _ in }
FormattingToolbar(formatItems: disabledItems) { _ in }
}
Expand Down
54 changes: 36 additions & 18 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,44 +157,62 @@ struct HomeScreenRoom: Identifiable, Equatable {

var name = ""

var isMarkedUnread: Bool

var hasUnreadMessages = false

var hasUnreadMentions = false

var hasUnreadNotifications = false
var badges: Badges
struct Badges: Equatable {
let isDotShown: Bool
let isMentionShown: Bool
let isMuteShown: Bool
let isCallShown: Bool
}

var hasOngoingCall = false
let isHighlighted: Bool

var timestamp: String?

var lastMessage: AttributedString?

var avatarURL: URL?

var notificationMode: RoomNotificationModeProxy?

var isPlaceholder = false

var hasNewContent: Bool {
hasUnreadMessages || hasUnreadMentions || hasUnreadNotifications || isMarkedUnread
}

static func placeholder() -> HomeScreenRoom {
HomeScreenRoom(id: UUID().uuidString,
roomId: nil,
name: "Placeholder room name",
isMarkedUnread: false,
hasUnreadMessages: false,
hasUnreadMentions: false,
hasUnreadNotifications: false,
badges: .init(isDotShown: false, isMentionShown: false, isMuteShown: false, isCallShown: false),
isHighlighted: false,
timestamp: "Now",
lastMessage: placeholderLastMessage,
isPlaceholder: true)
}
}

extension HomeScreenRoom {
init(details: RoomSummaryDetails, invalidated: Bool, hideUnreadMessagesBadge: Bool) {
let identifier = invalidated ? "invalidated-" + details.id : details.id

let hasUnreadMessages = hideUnreadMessagesBadge ? false : details.hasUnreadMessages

let isDotShown = hasUnreadMessages || details.hasUnreadMentions || details.hasUnreadNotifications || details.isMarkedUnread
let isMentionShown = details.hasUnreadMentions && !details.isMuted
let isMuteShown = details.isMuted
let isCallShown = details.hasOngoingCall
let isHighlighted = !details.isMuted && (details.hasUnreadNotifications || details.hasUnreadMentions || details.isMarkedUnread)

self.init(id: identifier,
roomId: details.id,
name: details.name,
badges: .init(isDotShown: isDotShown,
isMentionShown: isMentionShown,
isMuteShown: isMuteShown,
isCallShown: isCallShown),
isHighlighted: isHighlighted,
timestamp: details.lastMessageFormattedTimestamp,
lastMessage: details.lastMessage,
avatarURL: details.avatarURL)
}
}

enum RoomListFilter: Int, CaseIterable, Identifiable {
var id: Int {
rawValue
Expand Down
25 changes: 4 additions & 21 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -343,11 +343,11 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
switch summary {
case .empty:
rooms.append(HomeScreenRoom.placeholder())
case .invalidated(let details):
let room = buildRoom(with: details, invalidated: true)
rooms.append(room)
case .filled(let details):
let room = buildRoom(with: details, invalidated: false)
let room = HomeScreenRoom(details: details, invalidated: false, hideUnreadMessagesBadge: appSettings.hideUnreadMessagesBadge)
rooms.append(room)
case .invalidated(let details):
let room = HomeScreenRoom(details: details, invalidated: true, hideUnreadMessagesBadge: appSettings.hideUnreadMessagesBadge)
rooms.append(room)
}
}
Expand All @@ -357,23 +357,6 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
MXLog.verbose("Finished updating rooms")
}

private func buildRoom(with details: RoomSummaryDetails, invalidated: Bool) -> HomeScreenRoom {
let identifier = invalidated ? "invalidated-" + details.id : details.id

return HomeScreenRoom(id: identifier,
roomId: details.id,
name: details.name,
isMarkedUnread: details.isMarkedUnread,
hasUnreadMessages: appSettings.hideUnreadMessagesBadge ? false : details.unreadMessagesCount > 0,
hasUnreadMentions: details.unreadMentionsCount > 0,
hasUnreadNotifications: details.unreadNotificationsCount > 0,
hasOngoingCall: details.hasOngoingCall,
timestamp: details.lastMessageFormattedTimestamp,
lastMessage: details.lastMessage,
avatarURL: details.avatarURL,
notificationMode: details.notificationMode)
}

private func updateVisibleRange(_ range: Range<Int>) {
guard !range.isEmpty else {
return
Expand Down
44 changes: 14 additions & 30 deletions ElementX/Sources/Screens/HomeScreen/View/HomeScreenRoomCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ struct HomeScreenRoomCell: View {

if let timestamp = room.timestamp {
Text(timestamp)
.font(isHighlighted ? .compound.bodySMSemibold : .compound.bodySM)
.foregroundColor(isHighlighted ? .compound.textActionAccent : .compound.textSecondary)
.font(room.isHighlighted ? .compound.bodySMSemibold : .compound.bodySM)
.foregroundColor(room.isHighlighted ? .compound.textActionAccent : .compound.textSecondary)
}
}
}
Expand All @@ -117,38 +117,30 @@ struct HomeScreenRoomCell: View {
Spacer()

HStack(spacing: 8) {
if room.hasOngoingCall {
if room.badges.isCallShown {
CompoundIcon(\.videoCallSolid, size: .xSmall, relativeTo: .compound.bodySM)
.foregroundColor(isHighlighted ? .compound.iconAccentTertiary : .compound.iconQuaternary)
.foregroundColor(room.isHighlighted ? .compound.iconAccentTertiary : .compound.iconQuaternary)
}
if room.notificationMode == .mute {

if room.badges.isMuteShown {
CompoundIcon(\.notificationsOffSolid, size: .custom(15), relativeTo: .compound.bodyMD)
.accessibilityLabel(L10n.a11yNotificationsMuted)
.foregroundColor(.compound.iconQuaternary)
}

if room.hasUnreadMentions, room.notificationMode != .mute {
if room.badges.isMentionShown {
mentionIcon
.foregroundColor(.compound.iconAccentTertiary)
}

if room.hasNewContent {
if room.badges.isDotShown {
Circle()
.frame(width: 12, height: 12)
.foregroundColor(isHighlighted ? .compound.iconAccentTertiary : .compound.iconQuaternary)
.foregroundColor(room.isHighlighted ? .compound.iconAccentTertiary : .compound.iconQuaternary)
}
}
}
}

private var isHighlighted: Bool {
guard !room.isPlaceholder && room.notificationMode != .mute else {
return false
}

return room.hasUnreadNotifications || room.hasUnreadMentions || room.isMarkedUnread
}

private var mentionIcon: some View {
CompoundIcon(\.mention, size: .custom(15), relativeTo: .compound.bodyMD)
Expand Down Expand Up @@ -212,19 +204,11 @@ struct HomeScreenRoomCell_Previews: PreviewProvider, TestablePreview {
static func mockRoom(summary: RoomSummary) -> HomeScreenRoom? {
switch summary {
case .empty:
return nil
case .invalidated(let details), .filled(let details):
return HomeScreenRoom(id: UUID().uuidString,
roomId: details.id,
name: details.name,
isMarkedUnread: details.isMarkedUnread,
hasUnreadMessages: details.unreadMessagesCount > 0,
hasUnreadMentions: details.unreadMentionsCount > 0,
hasUnreadNotifications: details.unreadNotificationsCount > 0,
hasOngoingCall: details.hasOngoingCall,
timestamp: Date(timeIntervalSinceReferenceDate: 0).formattedMinimal(),
lastMessage: details.lastMessage,
notificationMode: details.notificationMode)
nil
case .invalidated(let details):
HomeScreenRoom(details: details, invalidated: true, hideUnreadMessagesBadge: false)
case .filled(let details):
HomeScreenRoom(details: details, invalidated: false, hideUnreadMessagesBadge: false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct HomeScreenRoomList: View {
HomeScreenRoomCell(room: room, context: context, isSelected: isSelected)
.contextMenu {
if context.viewState.markAsUnreadEnabled {
if room.hasNewContent {
if room.badges.isDotShown {
Button {
context.send(viewAction: .markRoomAsRead(roomIdentifier: room.id))
} label: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ struct RoomSummaryDetails {
let canonicalAlias: String?
let inviter: RoomMemberProxyProtocol?
let hasOngoingCall: Bool

var hasUnreadMessages: Bool { unreadMessagesCount > 0 }
var hasUnreadMentions: Bool { unreadMentionsCount > 0 }
var hasUnreadNotifications: Bool { unreadNotificationsCount > 0 }
var isMuted: Bool { notificationMode == .mute }
}

extension RoomSummaryDetails: CustomStringConvertible {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1bce84a

Please sign in to comment.