Invites list (#787)

* Setup invites SS window

* Add invites label in the home screen

* Add empty invtes list

* Setup navigation to invites list

* Inject invitesSummaryProvider

* Show invites

* Add InviteCell

* Refine InviteCell UI

* Push invites

* Amend SS configuration

* Add inviter in RoomProxyProtocol

* Add Invite model type

* Improve InviteCell

* Fix media provider injection

* Refine InviteCell

* Refine invite cell

* Add invites feature flag

* Try different SS config for invites

* Regiester invites view in configureViewsPostInitialSync

* Cleanup

* Start tests

* Cleanup code

* Add changelog.d file

* Add tests

* Handle canonical alias

* Add InvitesListScreen previews

* Add localisations and improve UI tests

* Add reference screenshots

* Cleanup code

* Fix UT build errors

* Refactor InvitesList -> Invites

* Apply pr comments

* Remove reduntant @MainActor

* Naming cleanup

* Fix InvitesScreenCell.title

* Add accept/decline InvitesViewAction

* Revert Package.resolved

* Add warning in InvitesScreenCell

* Improve PlaceholderAvatarImage

* Record snapshots again

* Refactor Invite -> InvitesRoomDeatils

* Rename identifier in PlaceholderAvatarImage
This commit is contained in:
Alfonso Grillo
2023-04-14 12:49:57 +02:00
committed by GitHub
parent 69e696afca
commit 41994baa4f
44 changed files with 910 additions and 35 deletions
@@ -27,6 +27,7 @@ struct DeveloperOptionsScreenViewStateBindings {
var startChatFlowEnabled: Bool
var startChatUserSuggestionsEnabled: Bool
var mediaUploadFlowEnabled: Bool
var invitesFlowEnabled: Bool
}
enum DeveloperOptionsScreenViewAction {
@@ -34,4 +35,5 @@ enum DeveloperOptionsScreenViewAction {
case changedStartChatFlowEnabled
case changedStartChatUserSuggestionsEnabled
case changedMediaUploadFlowEnabled
case changedInvitesFlowEnabled
}
@@ -25,7 +25,8 @@ class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, Deve
let bindings = DeveloperOptionsScreenViewStateBindings(shouldCollapseRoomStateEvents: ServiceLocator.shared.settings.shouldCollapseRoomStateEvents,
startChatFlowEnabled: ServiceLocator.shared.settings.startChatFlowEnabled,
startChatUserSuggestionsEnabled: ServiceLocator.shared.settings.startChatUserSuggestionsEnabled,
mediaUploadFlowEnabled: ServiceLocator.shared.settings.mediaUploadingFlowEnabled)
mediaUploadFlowEnabled: ServiceLocator.shared.settings.mediaUploadingFlowEnabled,
invitesFlowEnabled: ServiceLocator.shared.settings.invitesFlowEnabled)
let state = DeveloperOptionsScreenViewState(bindings: bindings)
super.init(initialViewState: state)
@@ -45,6 +46,8 @@ class DeveloperOptionsScreenViewModel: DeveloperOptionsScreenViewModelType, Deve
ServiceLocator.shared.settings.startChatUserSuggestionsEnabled = state.bindings.startChatUserSuggestionsEnabled
case .changedMediaUploadFlowEnabled:
ServiceLocator.shared.settings.mediaUploadingFlowEnabled = state.bindings.mediaUploadFlowEnabled
case .changedInvitesFlowEnabled:
ServiceLocator.shared.settings.invitesFlowEnabled = state.bindings.invitesFlowEnabled
}
}
}
@@ -50,6 +50,13 @@ struct DeveloperOptionsScreen: View {
.onChange(of: context.mediaUploadFlowEnabled) { _ in
context.send(viewAction: .changedMediaUploadFlowEnabled)
}
Toggle(isOn: $context.invitesFlowEnabled) {
Text("Show Invites flow")
}
.onChange(of: context.invitesFlowEnabled) { _ in
context.send(viewAction: .changedInvitesFlowEnabled)
}
}
Section {
@@ -30,6 +30,7 @@ enum HomeScreenCoordinatorAction {
case presentFeedbackScreen
case presentSessionVerificationScreen
case presentStartChatScreen
case presentInvitesScreen
case signOut
}
@@ -63,6 +64,8 @@ final class HomeScreenCoordinator: CoordinatorProtocol {
self.callback?(.signOut)
case .presentStartChatScreen:
self.callback?(.presentStartChatScreen)
case .presentInvitesScreen:
self.callback?(.presentInvitesScreen)
}
}
}
@@ -24,6 +24,7 @@ enum HomeScreenViewModelAction {
case presentSettingsScreen
case presentFeedbackScreen
case presentStartChatScreen
case presentInvitesScreen
case signOut
}
@@ -40,6 +41,7 @@ enum HomeScreenViewAction {
case verifySession
case skipSessionVerification
case updateVisibleItemRange(range: Range<Int>, isScrolling: Bool)
case selectInvites
}
enum HomeScreenRoomListMode: CustomStringConvertible {
@@ -70,6 +72,8 @@ struct HomeScreenViewState: BindableState {
/// The URL that will be shared when inviting friends to use the app.
let invitePermalink: URL?
var hasPendingInvitations = false
var startChatFlowEnabled: Bool {
ServiceLocator.shared.settings.startChatFlowEnabled
}
@@ -23,6 +23,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
private let userSession: UserSessionProtocol
private let visibleRoomsSummaryProvider: RoomSummaryProviderProtocol?
private let allRoomsSummaryProvider: RoomSummaryProviderProtocol?
private let invitesSummaryProvider: RoomSummaryProviderProtocol?
private let attributedStringBuilder: AttributedStringBuilderProtocol
private var visibleItemRangeObservationToken: AnyCancellable?
@@ -37,6 +38,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
visibleRoomsSummaryProvider = userSession.clientProxy.visibleRoomsSummaryProvider
allRoomsSummaryProvider = userSession.clientProxy.allRoomsSummaryProvider
invitesSummaryProvider = userSession.clientProxy.invitesSummaryProvider
let invitePermalink = try? PermalinkBuilder.permalinkTo(userIdentifier: userSession.userID)
super.init(initialViewState: HomeScreenViewState(userID: userSession.userID, invitePermalink: invitePermalink),
@@ -60,7 +62,7 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
.weakAssign(to: \.state.userAvatarURL, on: self)
.store(in: &cancellables)
guard let visibleRoomsSummaryProvider, let allRoomsSummaryProvider else {
guard let visibleRoomsSummaryProvider, let allRoomsSummaryProvider, let invitesSummaryProvider else {
MXLog.error("Room summary provider unavailable")
return
}
@@ -132,6 +134,12 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
}
.store(in: &cancellables)
invitesSummaryProvider.countPublisher
.map { $0 > 0 }
.receive(on: DispatchQueue.main)
.weakAssign(to: \.state.hasPendingInvitations, on: self)
.store(in: &cancellables)
updateRooms()
}
@@ -158,6 +166,8 @@ class HomeScreenViewModel: HomeScreenViewModelType, HomeScreenViewModelProtocol
visibleItemRangePublisher.send((range, isScrolling))
case .startChat:
callback?(.presentStartChatScreen)
case .selectInvites:
callback?(.presentInvitesScreen)
}
}
@@ -40,6 +40,14 @@ struct HomeScreen: View {
if context.viewState.showSessionVerificationBanner {
sessionVerificationBanner
}
if context.viewState.hasPendingInvitations, ServiceLocator.shared.settings.invitesFlowEnabled {
HomeScreenInvitesButton(title: L10n.actionInvitesList, hasBadge: true) {
context.send(viewAction: .selectInvites)
}
.padding(.trailing, 16)
.frame(maxWidth: .infinity, alignment: .trailing)
}
if context.viewState.roomListMode == .skeletons {
LazyVStack(spacing: 0) {
@@ -240,7 +248,7 @@ struct HomeScreen: View {
struct HomeScreen_Previews: PreviewProvider {
static var previews: some View {
body(.loading)
body(.loaded)
body(.loaded(.mockRooms))
}
static func body(_ state: MockRoomSummaryProviderState) -> some View {
@@ -0,0 +1,61 @@
//
// Copyright 2023 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 HomeScreenInvitesButton: View {
@ScaledMetric private var badgeSize = 12.0
let title: String
let hasBadge: Bool
let action: () -> Void
var body: some View {
Button(action: action) {
HStack(spacing: 8) {
Text(title)
.foregroundColor(.element.primaryContent)
.font(.compound.bodyMD)
if hasBadge {
badge
}
}
}
}
// MARK: - Private
private var badge: some View {
Circle()
.frame(width: badgeSize, height: badgeSize)
.foregroundColor(.element.brand)
}
}
struct InvitesButton_Previews: PreviewProvider {
static var previews: some View {
HomeScreenInvitesButton(title: "Invites", hasBadge: true, action: { })
.previewDisplayName("Badge on")
HomeScreenInvitesButton(title: "Invites", hasBadge: false, action: { })
.previewDisplayName("Badge off")
HomeScreenInvitesButton(title: "Invites", hasBadge: true, action: { })
.previewDisplayName("Badge on (AX1)")
.dynamicTypeSize(.accessibility1)
}
}
@@ -165,7 +165,7 @@ private extension View {
struct HomeScreenRoomCell_Previews: PreviewProvider {
static var previews: some View {
let summaryProvider = MockRoomSummaryProvider(state: .loaded)
let summaryProvider = MockRoomSummaryProvider(state: .loaded(.mockRooms))
let userSession = MockUserSession(clientProxy: MockClientProxy(userID: "John Doe", roomSummaryProvider: summaryProvider),
mediaProvider: MockMediaProvider())
@@ -0,0 +1,51 @@
//
// 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 Combine
import SwiftUI
struct InvitesCoordinatorParameters {
let userSession: UserSessionProtocol
}
enum InvitesCoordinatorAction { }
final class InvitesCoordinator: CoordinatorProtocol {
private let parameters: InvitesCoordinatorParameters
private var viewModel: InvitesViewModelProtocol
private let actionsSubject: PassthroughSubject<InvitesCoordinatorAction, Never> = .init()
private var cancellables: Set<AnyCancellable> = .init()
var actions: AnyPublisher<InvitesCoordinatorAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(parameters: InvitesCoordinatorParameters) {
self.parameters = parameters
viewModel = InvitesViewModel(userSession: parameters.userSession)
}
func start() {
viewModel.actions.sink { [weak self] _ in
guard let self else { return }
}
.store(in: &cancellables)
}
func toPresentable() -> AnyView {
AnyView(InvitesScreen(context: viewModel.context))
}
}
@@ -0,0 +1,35 @@
//
// 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.
//
enum InvitesViewModelAction { }
struct InvitesViewState: BindableState {
var invites: [InvitesRoomDetails]?
}
struct InvitesRoomDetails {
let roomDetails: RoomSummaryDetails
var inviter: RoomMemberProxyProtocol?
var isDirect: Bool {
roomDetails.isDirect
}
}
enum InvitesViewAction {
case accept(InvitesRoomDetails)
case decline(InvitesRoomDetails)
}
@@ -0,0 +1,99 @@
//
// 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 Combine
import SwiftUI
typealias InvitesViewModelType = StateStoreViewModel<InvitesViewState, InvitesViewAction>
class InvitesViewModel: InvitesViewModelType, InvitesViewModelProtocol {
private var actionsSubject: PassthroughSubject<InvitesViewModelAction, Never> = .init()
private let userSession: UserSessionProtocol
var actions: AnyPublisher<InvitesViewModelAction, Never> {
actionsSubject.eraseToAnyPublisher()
}
init(userSession: UserSessionProtocol) {
self.userSession = userSession
super.init(initialViewState: InvitesViewState(), imageProvider: userSession.mediaProvider)
setupSubscriptions()
}
// MARK: - Public
override func process(viewAction: InvitesViewAction) {
switch viewAction {
case .accept:
break
case .decline:
break
}
}
// MARK: - Private
private var invitesSummaryProvider: RoomSummaryProviderProtocol? {
userSession.clientProxy.invitesSummaryProvider
}
private func setupSubscriptions() {
guard let invitesSummaryProvider else {
MXLog.error("Room summary provider unavailable")
return
}
invitesSummaryProvider.roomListPublisher
.sink { [weak self] roomSummaries in
guard let self else { return }
let invites = roomSummaries.invites
self.state.invites = invites
for invite in invites {
self.fetchInviter(for: invite.roomDetails.id)
}
}
.store(in: &cancellables)
}
private func fetchInviter(for roomID: String) {
Task {
guard let room: RoomProxyProtocol = await self.userSession.clientProxy.roomForIdentifier(roomID) else {
return
}
let inviter: RoomMemberProxyProtocol? = await room.inviter()
guard let inviter, let inviteIndex = state.invites?.firstIndex(where: { $0.roomDetails.id == roomID }) else {
return
}
state.invites?[inviteIndex].inviter = inviter
}
}
}
private extension Array where Element == RoomSummary {
var invites: [InvitesRoomDetails] {
compactMap { summary in
guard case .filled(let details) = summary else {
return nil
}
return .init(roomDetails: details)
}
}
}
@@ -0,0 +1,23 @@
//
// 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 Combine
@MainActor
protocol InvitesViewModelProtocol {
var actions: AnyPublisher<InvitesViewModelAction, Never> { get }
var context: InvitesViewModelType.Context { get }
}
@@ -0,0 +1,86 @@
//
// 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 InvitesScreen: View {
@ObservedObject var context: InvitesViewModel.Context
var body: some View {
ScrollView {
if let rooms = context.viewState.invites, !rooms.isEmpty {
LazyVStack {
ForEach(rooms, id: \.roomDetails.id) { invite in
InvitesScreenCell(invite: invite,
imageProvider: context.imageProvider,
acceptAction: { context.send(viewAction: .accept(invite)) },
declineAction: { context.send(viewAction: .decline(invite)) })
}
}
} else {
noInvitesContent
}
}
.navigationTitle(L10n.actionInvitesList)
}
// MARK: - Private
private var noInvitesContent: some View {
Text(L10n.screenInvitesEmptyList)
.font(.element.body)
.foregroundColor(.element.tertiaryContent)
.frame(maxWidth: .infinity)
.listRowBackground(Color.clear)
.accessibilityIdentifier(A11yIdentifiers.invitesScreen.noInvites)
.padding(.top, 80)
}
}
// MARK: - Previews
struct InvitesScreen_Previews: PreviewProvider {
static var previews: some View {
NavigationView {
InvitesScreen(context: InvitesViewModel.noInvites.context)
}
.previewDisplayName("No Invites")
NavigationView {
InvitesScreen(context: InvitesViewModel.someInvite.context)
}
.previewDisplayName("Some Invite")
}
}
private extension InvitesViewModel {
static let noInvites: InvitesViewModel = {
let userSession = MockUserSession(clientProxy: MockClientProxy(userID: "@userid:example.com"),
mediaProvider: MockMediaProvider())
let regularViewModel = InvitesViewModel(userSession: userSession)
return regularViewModel
}()
static let someInvite: InvitesViewModel = {
let clientProxy = MockClientProxy(userID: "@userid:example.com")
clientProxy.invitesSummaryProvider = MockRoomSummaryProvider(state: .loaded(.mockInvites))
clientProxy.visibleRoomsSummaryProvider = MockRoomSummaryProvider(state: .loaded(.mockInvites))
let userSession = MockUserSession(clientProxy: clientProxy,
mediaProvider: MockMediaProvider())
let regularViewModel = InvitesViewModel(userSession: userSession)
return regularViewModel
}()
}
@@ -0,0 +1,177 @@
//
// Copyright 2023 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
@MainActor
struct InvitesScreenCell: View {
let invite: InvitesRoomDetails
let imageProvider: ImageProviderProtocol?
let acceptAction: () -> Void
let declineAction: () -> Void
private let verticalInsets = 16.0
var body: some View {
HStack(alignment: .top, spacing: 16) {
LoadableAvatarImage(url: mainAvatarURL,
name: title,
contentID: invite.roomDetails.id,
avatarSize: .custom(52),
imageProvider: imageProvider)
.accessibilityHidden(true)
mainContent
.frame(maxWidth: .infinity, alignment: .leading)
.padding(.bottom, verticalInsets)
.overlay(alignment: .bottom) {
separator
}
}
.padding(.top, verticalInsets)
.padding(.horizontal, 12)
}
// MARK: - Private
private var mainContent: some View {
VStack(alignment: .leading, spacing: 4) {
Text(title)
.font(.element.headline)
.foregroundColor(.element.primaryContent)
if let subtitle {
Text(subtitle)
.font(.compound.bodyMD)
.foregroundColor(.compound.textPlaceholder)
}
inviterView
buttons
.padding(.top, 10)
}
}
@ViewBuilder
private var inviterView: some View {
if let invitedText = attributedInviteText, let name = invite.inviter?.displayName {
HStack {
LoadableAvatarImage(url: invite.inviter?.avatarURL,
name: name,
contentID: name,
avatarSize: .custom(16),
imageProvider: imageProvider)
Text(invitedText)
}
.padding(.top, 4)
}
}
private var buttons: some View {
HStack(spacing: 12) {
Button(L10n.actionDecline, action: declineAction)
.buttonStyle(.elementCapsule)
Button(L10n.actionAccept, action: acceptAction)
.buttonStyle(.elementCapsuleProminent)
}
}
private var separator: some View {
Rectangle()
.fill(Color.element.quinaryContent)
.frame(height: 1 / UIScreen.main.scale)
}
#warning("Return just `roomDetails.avatarURL` when this logic is implemented in the rust sdk")
private var mainAvatarURL: URL? {
invite.isDirect ? invite.inviter?.avatarURL : invite.roomDetails.avatarURL
}
private var title: String {
invite.roomDetails.name
}
private var subtitle: String? {
invite.isDirect ? invite.inviter?.userID : invite.roomDetails.canonicalAlias
}
private var attributedInviteText: AttributedString? {
guard invite.roomDetails.isDirect == false, let inviterName = invite.inviter?.displayName else {
return nil
}
let text = L10n.screenInvitesInvitedYou(inviterName)
var attributedString = AttributedString(text)
attributedString.font = .compound.bodyMD
attributedString.foregroundColor = .compound.textPlaceholder
if let range = attributedString.range(of: inviterName) {
attributedString[range].foregroundColor = .compound.textPrimary
}
return attributedString
}
}
struct InvitesScreenCell_Previews: PreviewProvider {
static var previews: some View {
InvitesScreenCell(invite: .dm, imageProvider: MockMediaProvider(), acceptAction: { }, declineAction: { })
.previewDisplayName("Direct room")
InvitesScreenCell(invite: .room(alias: nil), imageProvider: MockMediaProvider(), acceptAction: { }, declineAction: { })
.previewDisplayName("Default room")
InvitesScreenCell(invite: .room(alias: "#footest:somewhere.org"), imageProvider: MockMediaProvider(), acceptAction: { }, declineAction: { })
.previewDisplayName("Aliased room")
}
}
@MainActor
private extension InvitesRoomDetails {
static var dm: InvitesRoomDetails {
let dmRoom = RoomSummaryDetails(id: "@someone:somewhere.com",
name: "Some Guy",
isDirect: true,
avatarURL: nil,
lastMessage: nil,
lastMessageFormattedTimestamp: nil,
unreadNotificationCount: 0,
canonicalAlias: "#footest:somewhere.org")
let inviter = RoomMemberProxyMock()
inviter.displayName = "Jack"
inviter.userID = "@jack:somewhere.com"
return .init(roomDetails: dmRoom, inviter: inviter)
}
static func room(alias: String?) -> InvitesRoomDetails {
let dmRoom = RoomSummaryDetails(id: "@someone:somewhere.com",
name: "Awesome Room",
isDirect: false,
avatarURL: nil,
lastMessage: nil,
lastMessageFormattedTimestamp: nil,
unreadNotificationCount: 0,
canonicalAlias: alias)
let inviter = RoomMemberProxyMock()
inviter.displayName = "Luca"
inviter.userID = "@jack:somewhere.com"
inviter.avatarURL = nil
return .init(roomDetails: dmRoom, inviter: inviter)
}
}