Skip to content

Commit

Permalink
Make the last message an enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
pixlwave committed Feb 1, 2023
1 parent a9c24ab commit 87b8144
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"action_confirm" = "Confirm";
"action_match" = "Match";

"new_message" = "New message";
"message" = "Message";

"screenshot_detected_title" = "You took a screenshot";
"screenshot_detected_message" = "Would you like to submit a bug report?";
Expand Down
4 changes: 2 additions & 2 deletions ElementX/Sources/Generated/Strings+Untranslated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ extension ElementL10n {
public static let loginMobileDevice = ElementL10n.tr("Untranslated", "login_mobile_device")
/// Tablet
public static let loginTabletDevice = ElementL10n.tr("Untranslated", "login_tablet_device")
/// New message
public static let newMessage = ElementL10n.tr("Untranslated", "new_message")
/// Message
public static let message = ElementL10n.tr("Untranslated", "message")
/// %1$@ accepted the invite
public static func noticeRoomInviteAccepted(_ p1: Any) -> String {
return ElementL10n.tr("Untranslated", "noticeRoomInviteAccepted", String(describing: p1))
Expand Down
22 changes: 19 additions & 3 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,23 @@ struct HomeScreenViewStateBindings {
}

struct HomeScreenRoom: Identifiable, Equatable {
private static let placeholderLastMessage = AttributedString("Last message")
static let placeholderLastMessage = AttributedString("Hidden last message")

enum LastMessage: Equatable {
case loaded(AttributedString)
case loading
case unknown

init(attributedString: AttributedString?, isLoading: Bool) {
if let message = attributedString, !message.characters.isEmpty {
self = .loaded(message)
} else if isLoading {
self = .loading
} else {
self = .unknown
}
}
}

/// The list item identifier can be a real room identifier, a custom one for invalidated entries
/// or a completely unique one for empty items and skeletons
Expand All @@ -112,7 +128,7 @@ struct HomeScreenRoom: Identifiable, Equatable {

var timestamp: String?

var lastMessage: AttributedString?
var lastMessage: LastMessage

var avatarURL: URL?

Expand All @@ -124,7 +140,7 @@ struct HomeScreenRoom: Identifiable, Equatable {
name: "Placeholder room name",
hasUnreads: false,
timestamp: "Now",
lastMessage: Self.placeholderLastMessage,
lastMessage: .loading,
isPlaceholder: true)
}
}
10 changes: 5 additions & 5 deletions ElementX/Sources/Screens/HomeScreen/HomeScreenViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
case .empty, .invalidated:
guard let allRoomsRoomSummary = allRoomsSummaryProvider?.roomListPublisher.value[safe: index] else {
if case let .invalidated(details) = summary {
rooms.append(buildRoom(with: details, invalidated: true))
rooms.append(buildRoom(with: details, invalidated: true, isLoading: false))
} else {
rooms.append(HomeScreenRoom.placeholder())
}
Expand All @@ -211,10 +211,10 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
case .empty:
rooms.append(HomeScreenRoom.placeholder())
case .filled(let details), .invalidated(let details):
rooms.append(buildRoom(with: details, invalidated: false))
rooms.append(buildRoom(with: details, invalidated: false, isLoading: true))
}
case .filled(let details):
rooms.append(buildRoom(with: details, invalidated: false))
rooms.append(buildRoom(with: details, invalidated: false, isLoading: false))
}
}

Expand All @@ -223,15 +223,15 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
MXLog.info("Finished updating rooms")
}

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

return HomeScreenRoom(id: identifier,
roomId: details.id,
name: details.name,
hasUnreads: details.unreadNotificationCount > 0,
timestamp: details.lastMessageFormattedTimestamp,
lastMessage: details.lastMessage,
lastMessage: .init(attributedString: details.lastMessage, isLoading: isLoading),
avatarURL: details.avatarURL)
}

Expand Down
14 changes: 10 additions & 4 deletions ElementX/Sources/Screens/HomeScreen/View/HomeScreenRoomCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ struct HomeScreenRoomCell: View {
// Hidden text with 2 lines to maintain consistent height, scaling with dynamic text.
Text(" \n ").lastMessageFormatting().hidden()

if let lastMessage = room.lastMessage, !String(lastMessage.characters).isEmpty {
switch room.lastMessage {
case .loaded(let lastMessage):
Text(lastMessage)
.lastMessageFormatting()
} else {
Text(ElementL10n.newMessage)
case .loading:
Text(HomeScreenRoom.placeholderLastMessage)
.lastMessageFormatting()
.redacted(reason: .placeholder)
case .unknown:
Text(ElementL10n.message)
.lastMessageFormatting()
}
}
Expand Down Expand Up @@ -156,7 +161,8 @@ struct HomeScreenRoomCell_Previews: PreviewProvider {
name: details.name,
hasUnreads: details.unreadNotificationCount > 0,
timestamp: Date.now.formattedMinimal(),
lastMessage: details.lastMessage)
lastMessage: .init(attributedString: details.lastMessage,
isLoading: false))
}
}

Expand Down

0 comments on commit 87b8144

Please sign in to comment.