Knock Polishing part 4 (#3779)

* added decline and block and inviter redesign

* improved testing

* improved testing

* code improvement

* code improvement

* improved the code
This commit is contained in:
Mauro 2025-02-10 18:31:12 +01:00 committed by GitHub
parent 34d8adcf09
commit 22d0fae423
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
43 changed files with 231 additions and 91 deletions

View File

@ -390,6 +390,10 @@
"screen_join_room_cancel_knock_alert_confirmation" = "Yes, cancel";
"screen_join_room_cancel_knock_alert_description" = "Are you sure that you want to cancel your request to join this room?";
"screen_join_room_cancel_knock_alert_title" = "Cancel request to join";
"screen_join_room_decline_and_block_alert_confirmation" = "Yes, decline & block";
"screen_join_room_decline_and_block_alert_message" = "Are you sure you want to decline the invite to join this room? This will also prevent %1$@ from contacting you or inviting you to rooms.";
"screen_join_room_decline_and_block_alert_title" = "Decline invite & block";
"screen_join_room_decline_and_block_button_title" = "Decline and block";
"screen_join_room_knock_message_description" = "Message (optional)";
"screen_join_room_knock_sent_description" = "You will receive an invite to join the room if your request is accepted.";
"screen_join_room_knock_sent_title" = "Request to join sent";

View File

@ -390,6 +390,10 @@
"screen_join_room_cancel_knock_alert_confirmation" = "Yes, cancel";
"screen_join_room_cancel_knock_alert_description" = "Are you sure that you want to cancel your request to join this room?";
"screen_join_room_cancel_knock_alert_title" = "Cancel request to join";
"screen_join_room_decline_and_block_alert_confirmation" = "Yes, decline & block";
"screen_join_room_decline_and_block_alert_message" = "Are you sure you want to decline the invite to join this room? This will also prevent %1$@ from contacting you or inviting you to rooms.";
"screen_join_room_decline_and_block_alert_title" = "Decline invite & block";
"screen_join_room_decline_and_block_button_title" = "Decline and block";
"screen_join_room_knock_message_description" = "Message (optional)";
"screen_join_room_knock_sent_description" = "You will receive an invite to join the room if your request is accepted.";
"screen_join_room_knock_sent_title" = "Request to join sent";

View File

@ -1322,6 +1322,16 @@ internal enum L10n {
internal static var screenJoinRoomCancelKnockAlertDescription: String { return L10n.tr("Localizable", "screen_join_room_cancel_knock_alert_description") }
/// Cancel request to join
internal static var screenJoinRoomCancelKnockAlertTitle: String { return L10n.tr("Localizable", "screen_join_room_cancel_knock_alert_title") }
/// Yes, decline & block
internal static var screenJoinRoomDeclineAndBlockAlertConfirmation: String { return L10n.tr("Localizable", "screen_join_room_decline_and_block_alert_confirmation") }
/// Are you sure you want to decline the invite to join this room? This will also prevent %1$@ from contacting you or inviting you to rooms.
internal static func screenJoinRoomDeclineAndBlockAlertMessage(_ p1: Any) -> String {
return L10n.tr("Localizable", "screen_join_room_decline_and_block_alert_message", String(describing: p1))
}
/// Decline invite & block
internal static var screenJoinRoomDeclineAndBlockAlertTitle: String { return L10n.tr("Localizable", "screen_join_room_decline_and_block_alert_title") }
/// Decline and block
internal static var screenJoinRoomDeclineAndBlockButtonTitle: String { return L10n.tr("Localizable", "screen_join_room_decline_and_block_button_title") }
/// Joining the room failed.
internal static var screenJoinRoomFailMessage: String { return L10n.tr("Localizable", "screen_join_room_fail_message") }
/// This room is either invite-only or there might be restrictions to access at space level.

View File

@ -90,6 +90,7 @@ struct JoinRoomScreenViewStateBindings {
enum JoinRoomScreenAlertType {
case declineInvite
case declineInviteAndBlock
case cancelKnock
case loadingError
}
@ -100,6 +101,7 @@ enum JoinRoomScreenViewAction {
case join
case acceptInvite
case declineInvite
case declineInviteAndBlock(userID: String)
case forget
case dismiss
}

View File

@ -66,6 +66,8 @@ class JoinRoomScreenViewModel: JoinRoomScreenViewModelType, JoinRoomScreenViewMo
showCancelKnockConfirmationAlert()
case .dismiss:
actionsSubject.send(.dismiss)
case .declineInviteAndBlock(let userID):
showDeclineAndBlockConfirmationAlert(userID: userID)
}
}
@ -302,7 +304,27 @@ class JoinRoomScreenViewModel: JoinRoomScreenViewModelType, JoinRoomScreenViewMo
secondaryButton: .init(title: L10n.screenJoinRoomCancelKnockAlertConfirmation, role: .destructive) { Task { await self.cancelKnock() } })
}
private func declineInvite() async {
private func showDeclineAndBlockConfirmationAlert(userID: String) {
state.bindings.alertInfo = .init(id: .declineInviteAndBlock,
title: L10n.screenJoinRoomDeclineAndBlockAlertTitle,
message: L10n.screenJoinRoomDeclineAndBlockAlertMessage(userID),
primaryButton: .init(title: L10n.actionCancel, role: .cancel, action: nil),
secondaryButton: .init(title: L10n.screenJoinRoomDeclineAndBlockAlertConfirmation, role: .destructive) { Task { await self.declineAndBlock(userID: userID) } })
}
private func declineAndBlock(userID: String) async {
guard await declineInvite() else {
return
}
// The decline alert and the view are already dismissed at this point so we can dispatch this separately as a best effort
// but only if the decline invite was succesfull
Task {
await clientProxy.ignoreUser(userID)
}
}
@discardableResult
private func declineInvite() async -> Bool {
defer {
userIndicatorController.retractIndicatorWithId(roomID)
}
@ -311,16 +333,18 @@ class JoinRoomScreenViewModel: JoinRoomScreenViewModelType, JoinRoomScreenViewMo
guard case let .invited(roomProxy) = room else {
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
return
return false
}
let result = await roomProxy.rejectInvitation()
if case .failure = result {
userIndicatorController.submitIndicator(.init(title: L10n.errorUnknown))
} else {
actionsSubject.send(.dismiss)
return false
}
actionsSubject.send(.dismiss)
return true
}
private func cancelKnock() async {

View File

@ -18,9 +18,16 @@ struct JoinRoomScreen: View {
private enum Focus {
case knockMessage
}
private var topPadding: CGFloat {
if context.viewState.roomDetails?.inviter != nil {
return 32
}
return context.viewState.mode == .knocked ? 151 : 44
}
var body: some View {
FullscreenDialog(topPadding: context.viewState.mode == .knocked ? 151 : 35) {
FullscreenDialog(topPadding: topPadding) {
if context.viewState.mode == .loading {
EmptyView()
} else {
@ -50,6 +57,14 @@ struct JoinRoomScreen: View {
@ViewBuilder
private var defaultView: some View {
VStack(spacing: 16) {
if let inviter = context.viewState.roomDetails?.inviter {
RoomInviterLabel(inviter: inviter, mediaProvider: context.mediaProvider)
.multilineTextAlignment(.center)
.font(.compound.bodyMD)
.foregroundStyle(.compound.textSecondary)
.padding(.bottom, 44)
}
if let avatar = context.viewState.avatar {
RoomAvatarImage(avatar: avatar,
avatarSize: .room(on: .joinRoom),
@ -80,12 +95,6 @@ struct JoinRoomScreen: View {
BadgeLabel(title: "\(memberCount)", icon: \.userProfile, isHighlighted: false)
}
if let inviter = context.viewState.roomDetails?.inviter {
RoomInviterLabel(inviter: inviter, mediaProvider: context.mediaProvider)
.font(.compound.bodyMD)
.foregroundStyle(.compound.textSecondary)
}
if let topic = context.viewState.roomDetails?.topic {
Text(topic)
.font(.compound.bodyMD)
@ -178,8 +187,17 @@ struct JoinRoomScreen: View {
bottomNoticeMessage(L10n.screenJoinRoomInviteRequiredMessage)
case .invited:
ViewThatFits {
HStack(spacing: 8) { inviteButtons }
VStack(spacing: 16) { inviteButtons }
VStack(spacing: 24) {
HStack(spacing: 16) {
inviteButtons
}
declineAndBlockButton
}
VStack(spacing: 16) {
inviteButtons
declineAndBlockButton
}
}
case .banned(let sender, let reason):
VStack(spacing: 24) {
@ -253,6 +271,19 @@ struct JoinRoomScreen: View {
.buttonStyle(.compound(.primary))
}
@ViewBuilder
var declineAndBlockButton: some View {
if let inviter = context.viewState.roomDetails?.inviter {
Button(role: .destructive) {
context.send(viewAction: .declineInviteAndBlock(userID: inviter.id))
} label: {
Text(L10n.screenJoinRoomDeclineAndBlockButtonTitle)
.padding(.vertical, 14)
}
.buttonStyle(.compound(.plain))
}
}
var joinButton: some View {
Button(L10n.screenJoinRoomJoinAction) { context.send(viewAction: .join) }
.buttonStyle(.compound(.super))

View File

@ -11,7 +11,15 @@ import XCTest
@MainActor
class JoinRoomScreenViewModelTests: XCTestCase {
private enum TestMode {
case joined
case knocked
case invited
case banned
}
var viewModel: JoinRoomScreenViewModelProtocol!
var clientProxy: ClientProxyMock!
var context: JoinRoomScreenViewModelType.Context {
viewModel.context
@ -19,6 +27,7 @@ class JoinRoomScreenViewModelTests: XCTestCase {
override func tearDown() {
viewModel = nil
clientProxy = nil
AppSettings.resetAllSettings()
}
@ -39,17 +48,22 @@ class JoinRoomScreenViewModelTests: XCTestCase {
}
func testDeclineInviteInteraction() async throws {
setupViewModel()
setupViewModel(mode: .invited)
try await deferFulfillment(viewModel.context.$viewState) { $0.roomDetails != nil }.fulfill()
context.send(viewAction: .declineInvite)
XCTAssertEqual(viewModel.context.alertInfo?.id, .declineInvite)
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss
}
context.alertInfo?.secondaryButton?.action?()
try await deferred.fulfill()
}
func testKnockedState() async throws {
setupViewModel(knocked: true)
setupViewModel(mode: .knocked)
try await deferFulfillment(viewModel.context.$viewState) { state in
state.mode == .knocked
@ -57,7 +71,7 @@ class JoinRoomScreenViewModelTests: XCTestCase {
}
func testCancelKnock() async throws {
setupViewModel(knocked: true)
setupViewModel(mode: .knocked)
try await deferFulfillment(viewModel.context.$viewState) { state in
state.mode == .knocked
@ -73,14 +87,51 @@ class JoinRoomScreenViewModelTests: XCTestCase {
try await deferred.fulfill()
}
private func setupViewModel(throwing: Bool = false, knocked: Bool = false) {
func testDeclineAndBlockInviteInteraction() async throws {
setupViewModel(mode: .invited)
let expectation = expectation(description: "Wait for the user to be ignored")
clientProxy.ignoreUserClosure = { userID in
defer { expectation.fulfill() }
XCTAssertEqual(userID, "@test:matrix.org")
return .success(())
}
try await deferFulfillment(viewModel.context.$viewState) { $0.roomDetails != nil }.fulfill()
context.send(viewAction: .declineInviteAndBlock(userID: "@test:matrix.org"))
XCTAssertEqual(viewModel.context.alertInfo?.id, .declineInviteAndBlock)
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss
}
context.alertInfo?.secondaryButton?.action?()
try await deferred.fulfill()
await fulfillment(of: [expectation], timeout: 10)
}
func testForgetRoom() async throws {
setupViewModel(mode: .banned)
try await deferFulfillment(viewModel.context.$viewState) { $0.roomDetails != nil }.fulfill()
let deferred = deferFulfillment(viewModel.actionsPublisher) { action in
action == .dismiss
}
context.send(viewAction: .forget)
try await deferred.fulfill()
}
private func setupViewModel(throwing: Bool = false, mode: TestMode = .joined) {
ServiceLocator.shared.settings.knockingEnabled = true
let clientProxy = ClientProxyMock(.init())
clientProxy = ClientProxyMock(.init())
clientProxy.joinRoomViaReturnValue = throwing ? .failure(.sdkError(ClientProxyMockError.generic)) : .success(())
if knocked {
switch mode {
case .knocked:
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.knocked)
clientProxy.roomForIdentifierClosure = { _ in
@ -89,8 +140,22 @@ class JoinRoomScreenViewModelTests: XCTestCase {
roomProxy.cancelKnockUnderlyingReturnValue = .success(())
return .knocked(roomProxy)
}
} else {
case .joined:
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.joinable)
case .invited:
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.invited())
clientProxy.roomForIdentifierClosure = { _ in
let roomProxy = InvitedRoomProxyMock(.init())
roomProxy.rejectInvitationReturnValue = .success(())
return .invited(roomProxy)
}
case .banned:
clientProxy.roomPreviewForIdentifierViaReturnValue = .success(RoomPreviewProxyMock.banned)
clientProxy.roomForIdentifierClosure = { _ in
let roomProxy = BannedRoomProxyMock(.init())
roomProxy.forgetRoomReturnValue = .success(())
return .banned(roomProxy)
}
}
viewModel = JoinRoomScreenViewModel(roomID: "1",