Skip to content

Commit

Permalink
tests done!
Browse files Browse the repository at this point in the history
  • Loading branch information
Velin92 committed Sep 26, 2023
1 parent cccb022 commit 07b4fd5
Show file tree
Hide file tree
Showing 23 changed files with 55 additions and 35 deletions.
4 changes: 4 additions & 0 deletions ElementX/Sources/Application/AppSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ final class AppSettings {
case hasShownWelcomeScreen
case swiftUITimelineEnabled
case voiceMessageEnabled
case mentionsEnabled
}

private static var suiteName: String = InfoPlistReader.main.appGroupIdentifier
Expand Down Expand Up @@ -248,4 +249,7 @@ final class AppSettings {

@UserPreference(key: UserDefaultsKeys.voiceMessageEnabled, defaultValue: false, storageType: .userDefaults(store))
var voiceMessageEnabled

@UserPreference(key: UserDefaultsKeys.mentionsEnabled, defaultValue: false, storageType: .userDefaults(store))
var mentionsEnabled
}
5 changes: 5 additions & 0 deletions ElementX/Sources/Other/Pills/MentionBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import UIKit

extension AttributedStringBuilder: MentionBuilder {
func handleUserMention(for attributedString: NSMutableAttributedString, in range: NSRange, url: URL, userID: String) {
guard ServiceLocator.shared.settings.mentionsEnabled else {
attributedString.addAttributes([.MatrixUserID: userID], range: range)
return
}

let font = attributedString.attribute(.font, at: 0, longestEffectiveRange: nil, in: range) as? UIFont ?? UIFont.preferredFont(forTextStyle: .body)
let attachmentData = PillTextAttachmentData(type: .user(userId: userID), font: font)
guard let attachment = PillTextAttachment(attachmentData: attachmentData) else {
Expand Down
9 changes: 3 additions & 6 deletions ElementX/Sources/Other/Pills/MessageText.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ final class MessageTextView: UITextView {
}

// Required to setup the first rendering of the pill view
override func willMove(toWindow newWindow: UIWindow?) {
override func layoutSubviews() {
invalidateTextAttachmentsDisplay(update: false)
super.willMove(toWindow: newWindow)
super.layoutSubviews()
}
}

Expand Down Expand Up @@ -132,7 +132,7 @@ struct MessageText_Previews: PreviewProvider, TestablePreview {
return AttributedString()
}

var attributedString = "Hello test test test" + AttributedString(NSAttributedString(attachment: attachment)) + " World!"
var attributedString = "Hello test test test " + AttributedString(NSAttributedString(attachment: attachment)) + " World!"
attributedString
.mergeAttributes(defaultFontContainer)
return attributedString
Expand Down Expand Up @@ -166,9 +166,6 @@ struct MessageText_Previews: PreviewProvider, TestablePreview {
.previewDisplayName("SwiftUI Default Text")
attachmentPreview
.previewDisplayName("Custom Attachment")
attachmentPreview
.previewDisplayName("Custom Attachment 2")
.snapshot(delay: 5)
if let attributedString = attributedStringBuilder.fromHTML(htmlStringWithQuote) {
MessageText(attributedString: attributedString)
.border(Color.purple)
Expand Down
7 changes: 5 additions & 2 deletions ElementX/Sources/Other/Pills/PillAttachmentViewProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ final class PillAttachmentViewProvider: NSTextAttachmentViewProvider {
}

let viewModel: PillViewModel
if isXcodePreview {
let imageProvider: ImageProviderProtocol?
if isXcodePreview || Tests.isRunningTests {
viewModel = PillViewModel.mockViewModel(type: .loadUser)
imageProvider = MockMediaProvider()
} else if let roomContext = messageTextView?.roomContext {
viewModel = PillViewModel(roomContext: roomContext, data: textAttachmentData)
imageProvider = Self.currentSession?.mediaProvider
} else {
MXLog.failure("[PillAttachmentViewProvider]: missing room context")
return
}

let view = PillView(imageProvider: Self.currentSession?.mediaProvider, viewModel: viewModel) { [weak self] in
let view = PillView(imageProvider: imageProvider, viewModel: viewModel) { [weak self] in
self?.messageTextView?.invalidateTextAttachmentsDisplay(update: true)
}
let controller = UIHostingController(rootView: view)
Expand Down
3 changes: 0 additions & 3 deletions ElementX/Sources/Other/Pills/PillView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,6 @@ struct PillView_Previews: PreviewProvider, TestablePreview {
static var previews: some View {
loading
.previewDisplayName("Loading")
loading
.previewDisplayName("Loading 2")
.snapshot(delay: 5)
PillView(imageProvider: mockMediaProvider,
viewModel: PillViewModel.mockViewModel(type: .loadedUser)) { }
.previewDisplayName("Loaded Long")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ private struct PreviewBubbleModifier: ViewModifier {
.padding(timelineStyle == .bubbles ? 8 : 0)
.background(timelineStyle == .bubbles ? Color.compound._bgBubbleOutgoing : nil)
.cornerRadius(timelineStyle == .bubbles ? 12 : 0)
.environmentObject(RoomScreenViewModel.mock.context)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ protocol DeveloperOptionsProtocol: AnyObject {
var readReceiptsEnabled: Bool { get set }
var swiftUITimelineEnabled: Bool { get set }
var voiceMessageEnabled: Bool { get set }
var mentionsEnabled: Bool { get set }
}

extension AppSettings: DeveloperOptionsProtocol { }
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ struct DeveloperOptionsScreen: View {
Text("SwiftUI Timeline")
Text("Resets on reboot")
}

Toggle(isOn: $context.mentionsEnabled) {
Text("Show user mentions")
Text("Requires app reboot")
}
}

Section("Room creation") {
Expand Down
1 change: 1 addition & 0 deletions ElementX/Sources/UnitTests/UnitTestsAppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class UnitTestsAppCoordinator: AppCoordinatorProtocol {
ServiceLocator.shared.register(analytics: AnalyticsService(client: AnalyticsClientMock(),
appSettings: ServiceLocator.shared.settings,
bugReportService: ServiceLocator.shared.bugReportService))
NSTextAttachment.registerViewProviderClass(PillAttachmentViewProvider.self, forFileType: InfoPlistReader.main.pillsUTType)
}

func start() { }
Expand Down
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.
4 changes: 2 additions & 2 deletions UnitTests/__Snapshots__/PreviewTests/test_roomScreen.1.png
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.
4 changes: 2 additions & 2 deletions UnitTests/__Snapshots__/PreviewTests/test_timelineView.1.png
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 07b4fd5

Please sign in to comment.