Skip to content

Commit

Permalink
updated preview tests and improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Dec 20, 2024
1 parent 218958d commit 349331e
Show file tree
Hide file tree
Showing 24 changed files with 89 additions and 20 deletions.
12 changes: 12 additions & 0 deletions ElementX/Sources/Other/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,15 @@ extension String {
trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
}
}

extension String {
static func makeCanonicalAlias(aliasLocalPart: Self?, serverName: Self?) -> Self? {
guard let aliasLocalPart = aliasLocalPart,
!aliasLocalPart.isEmpty,
let serverName = serverName,
!serverName.isEmpty else {
return nil
}
return "#\(aliasLocalPart):\(serverName)"
}
}
14 changes: 4 additions & 10 deletions ElementX/Sources/Screens/CreateRoom/CreateRoomViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ class CreateRoomViewModel: CreateRoomViewModelType, CreateRoomViewModelProtocol

guard state.isKnockingFeatureEnabled,
!state.bindings.isRoomPrivate,
let canonicalAlias = canonicalAlias(aliasLocalPart: aliasLocalPart) else {
let canonicalAlias = String.makeCanonicalAlias(aliasLocalPart: aliasLocalPart,
serverName: state.serverName) else {
// While is empty or private room we don't change or display the error
return
}
Expand Down Expand Up @@ -209,7 +210,8 @@ class CreateRoomViewModel: CreateRoomViewModelType, CreateRoomViewModelProtocol

// Better to double check the errors also when trying to create the room
if state.isKnockingFeatureEnabled, !createRoomParameters.isRoomPrivate {
guard let canonicalAlias = canonicalAlias(aliasLocalPart: createRoomParameters.aliasLocalPart),
guard let canonicalAlias = String.makeCanonicalAlias(aliasLocalPart: createRoomParameters.aliasLocalPart,
serverName: state.serverName),
isRoomAliasFormatValid(alias: canonicalAlias) else {
state.aliasErrors = [.invalidSymbols]
return
Expand Down Expand Up @@ -271,14 +273,6 @@ class CreateRoomViewModel: CreateRoomViewModelType, CreateRoomViewModelProtocol
}
}

private func canonicalAlias(aliasLocalPart: String?) -> String? {
guard let aliasLocalPart,
!aliasLocalPart.isEmpty else {
return nil
}
return "#\(aliasLocalPart):\(state.serverName)"
}

// MARK: Loading indicator

private static let loadingIndicatorIdentifier = "\(CreateRoomViewModel.self)-Loading"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRo
return
}

guard let canonicalAlias = canonicalAlias(aliasLocalPart: aliasLocalPart) else {
guard let canonicalAlias = String.makeCanonicalAlias(aliasLocalPart: aliasLocalPart,
serverName: state.serverName) else {
// While is empty don't display the errors, since the save button is already disabled
state.aliasErrors.removeAll()
return
Expand All @@ -81,6 +82,12 @@ class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRo

state.aliasErrors.remove(.invalidSymbols)

guard aliasLocalPart != state.currentAliasLocalPart else {
// Doesn't make sense to check the availability and display an error if the alias didn't change, the save button should also be disabled
state.aliasErrors.remove(.alreadyExists)
checkAliasAvailabilityTask = nil
return
}
checkAliasAvailabilityTask = Task { [weak self] in
guard let self else {
return
Expand All @@ -99,8 +106,9 @@ class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRo
}

private func save() async {
guard let canonicalAlias = canonicalAlias(aliasLocalPart: state.bindings.desiredAliasLocalPart),
isRoomAliasFormatValid(alias: canonicalAlias) else {
guard let canonicalAlias = String.makeCanonicalAlias(aliasLocalPart: state.bindings.desiredAliasLocalPart,
serverName: state.serverName),
isRoomAliasFormatValid(alias: canonicalAlias) else {
state.aliasErrors = [.invalidSymbols]
return
}
Expand All @@ -118,11 +126,4 @@ class EditRoomAddressScreenViewModel: EditRoomAddressScreenViewModelType, EditRo

// TODO: API calls to edit/add the alias and maybe also dismiss the view? (check with design)
}

private func canonicalAlias(aliasLocalPart: String) -> String? {
guard !aliasLocalPart.isEmpty else {
return nil
}
return "#\(aliasLocalPart):\(state.serverName)"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,13 @@ struct EditRoomAddressScreen_Previews: PreviewProvider, TestablePreview {
NavigationStack {
EditRoomAddressScreen(context: invalidSymbolsViewModel.context)
}
.snapshotPreferences(delay: 1.2)
.previewDisplayName("Invalid symbols")

NavigationStack {
EditRoomAddressScreen(context: alreadyExistingViewModel.context)
}
.snapshotPreferences(delay: 1.2)
.previewDisplayName("Already existing")
}
}
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 349331e

Please sign in to comment.