mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-11-06 12:28:29 +00:00
201 lines
7.1 KiB
Swift
201 lines
7.1 KiB
Swift
//
|
|
// Copyright 2022 New Vector Ltd
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
//
|
|
|
|
import SwiftUI
|
|
|
|
struct HomeScreenRoomCell: View {
|
|
@Environment(\.redactionReasons) private var redactionReasons
|
|
|
|
let room: HomeScreenRoom
|
|
let context: HomeScreenViewModel.Context
|
|
|
|
private let verticalInsets = 12.0
|
|
private let horizontalInsets = 16.0
|
|
|
|
var body: some View {
|
|
Button {
|
|
if let roomId = room.roomId {
|
|
context.send(viewAction: .selectRoom(roomIdentifier: roomId))
|
|
}
|
|
} label: {
|
|
HStack(spacing: 16.0) {
|
|
avatar
|
|
|
|
content
|
|
.padding(.vertical, verticalInsets)
|
|
.overlay(alignment: .bottom) {
|
|
Rectangle()
|
|
.fill(Color.element.quinaryContent)
|
|
.frame(height: 1 / UIScreen.main.scale)
|
|
.padding(.trailing, -horizontalInsets)
|
|
}
|
|
}
|
|
.padding(.horizontal, horizontalInsets)
|
|
.accessibilityElement(children: .combine)
|
|
}
|
|
.buttonStyle(HomeScreenRoomCellButtonStyle(isSelected: false))
|
|
.accessibilityIdentifier(A11yIdentifiers.homeScreen.roomName(room.name))
|
|
}
|
|
|
|
@ViewBuilder
|
|
var avatar: some View {
|
|
LoadableAvatarImage(url: room.avatarURL,
|
|
name: room.name,
|
|
contentID: room.roomId,
|
|
avatarSize: .room(on: .home),
|
|
imageProvider: context.imageProvider)
|
|
.accessibilityHidden(true)
|
|
}
|
|
|
|
var content: some View {
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
header
|
|
footer
|
|
}
|
|
// Hide the normal content for Skeletons and overlay centre aligned placeholders.
|
|
.opacity(redactionReasons.contains(.placeholder) ? 0 : 1)
|
|
.overlay {
|
|
if redactionReasons.contains(.placeholder) {
|
|
VStack(alignment: .leading, spacing: 2) {
|
|
header
|
|
lastMessage
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
var header: some View {
|
|
HStack(alignment: .top, spacing: 16) {
|
|
Text(room.name)
|
|
.font(.element.headline)
|
|
.foregroundColor(.element.primaryContent)
|
|
.lineLimit(1)
|
|
.frame(maxWidth: .infinity, alignment: .leading)
|
|
|
|
if let timestamp = room.timestamp {
|
|
Text(timestamp)
|
|
.font(.element.footnote)
|
|
.foregroundColor(room.hasUnreads ? .element.brand : .element.tertiaryContent)
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
var footer: some View {
|
|
HStack(alignment: .firstTextBaseline) {
|
|
ZStack(alignment: .topLeading) {
|
|
// Hidden text with 2 lines to maintain consistent height, scaling with dynamic text.
|
|
Text(" \n ")
|
|
.lastMessageFormatting()
|
|
.hidden()
|
|
.environment(\.redactionReasons, []) // Always maintain consistent height
|
|
|
|
lastMessage
|
|
}
|
|
|
|
Spacer()
|
|
|
|
if room.hasUnreads {
|
|
Circle()
|
|
.frame(width: 12, height: 12)
|
|
.foregroundColor(.element.brand)
|
|
.padding(.leading, 12)
|
|
} else {
|
|
// Force extra padding between last message text and the right border of the screen if there is no unread dot
|
|
Circle()
|
|
.frame(width: 12, height: 12)
|
|
.hidden()
|
|
}
|
|
}
|
|
}
|
|
|
|
@ViewBuilder
|
|
var lastMessage: some View {
|
|
switch room.lastMessage {
|
|
case .loaded(let lastMessage):
|
|
Text(lastMessage)
|
|
.lastMessageFormatting()
|
|
case .loading:
|
|
Text(HomeScreenRoom.placeholderLastMessage)
|
|
.lastMessageFormatting()
|
|
.redacted(reason: .placeholder)
|
|
case .unknown:
|
|
Text(ElementL10n.message)
|
|
.lastMessageFormatting()
|
|
}
|
|
}
|
|
}
|
|
|
|
struct HomeScreenRoomCellButtonStyle: ButtonStyle {
|
|
let isSelected: Bool
|
|
|
|
func makeBody(configuration: Configuration) -> some View {
|
|
configuration.label
|
|
.background(configuration.isPressed || isSelected ? Color.element.system : .clear)
|
|
.contentShape(Rectangle())
|
|
}
|
|
}
|
|
|
|
private extension View {
|
|
func lastMessageFormatting() -> some View {
|
|
font(.element.subheadline)
|
|
.foregroundColor(.element.tertiaryContent)
|
|
.lineLimit(2)
|
|
.multilineTextAlignment(.leading)
|
|
}
|
|
}
|
|
|
|
struct HomeScreenRoomCell_Previews: PreviewProvider {
|
|
static var previews: some View {
|
|
let summaryProvider = MockRoomSummaryProvider(state: .loaded)
|
|
|
|
let userSession = MockUserSession(clientProxy: MockClientProxy(userID: "John Doe", roomSummaryProvider: summaryProvider),
|
|
mediaProvider: MockMediaProvider())
|
|
|
|
let viewModel = HomeScreenViewModel(userSession: userSession,
|
|
attributedStringBuilder: AttributedStringBuilder())
|
|
|
|
let rooms: [HomeScreenRoom] = summaryProvider.roomListPublisher.value.compactMap { summary -> HomeScreenRoom? in
|
|
switch summary {
|
|
case .empty:
|
|
return nil
|
|
case .filled(let details), .invalidated(let details):
|
|
return HomeScreenRoom(id: UUID().uuidString,
|
|
roomId: details.id,
|
|
name: details.name,
|
|
hasUnreads: details.unreadNotificationCount > 0,
|
|
timestamp: Date.now.formattedMinimal(),
|
|
lastMessage: .init(attributedString: details.lastMessage,
|
|
isLoading: false))
|
|
}
|
|
}
|
|
|
|
return VStack(spacing: 0) {
|
|
ForEach(rooms) { room in
|
|
HomeScreenRoomCell(room: room, context: viewModel.context)
|
|
}
|
|
|
|
HomeScreenRoomCell(room: .placeholder(), context: viewModel.context)
|
|
.redacted(reason: .placeholder)
|
|
HomeScreenRoomCell(room: .placeholder(), context: viewModel.context)
|
|
.redacted(reason: .placeholder)
|
|
HomeScreenRoomCell(room: .placeholder(), context: viewModel.context)
|
|
.redacted(reason: .placeholder)
|
|
}
|
|
}
|
|
}
|