From b5bda2a26068779bd6d6beb4b75e4565c69fe307 Mon Sep 17 00:00:00 2001 From: fwcd Date: Sat, 3 Aug 2024 23:38:56 +0200 Subject: [PATCH] Migrate to #Preview macro --- .../xcshareddata/swiftpm/Package.resolved | 2 +- DistributedChatApp/View/AttachmentView.swift | 6 ++-- .../View/BubbleMessageView.swift | 23 +++++++-------- .../View/ChannelSnippetView.swift | 19 ++++++------ DistributedChatApp/View/ChannelView.swift | 29 +++++++++---------- DistributedChatApp/View/ChannelsView.swift | 23 +++++++-------- .../View/ClosableStatusBar.swift | 8 ++--- .../View/CompactMessageView.swift | 6 ++-- DistributedChatApp/View/ContentView.swift | 27 +++++++++-------- .../View/FileAttachmentView.swift | 6 ++-- .../View/MessageComposeView.swift | 19 ++++++------ .../View/MessageHistoryView.swift | 27 +++++++++-------- DistributedChatApp/View/MessageView.swift | 27 +++++++++-------- DistributedChatApp/View/NetworkView.swift | 18 +++++------- DistributedChatApp/View/NewChannelView.swift | 10 +++---- .../View/PlainMessageView.swift | 6 ++-- DistributedChatApp/View/PresenceView.swift | 14 ++++----- DistributedChatApp/View/ProfileView.swift | 11 ++++--- .../View/QuickLookAttachmentView.swift | 8 ++--- DistributedChatApp/View/SettingsView.swift | 11 ++++--- .../View/VoiceNoteRecordButton.swift | 6 ++-- 21 files changed, 138 insertions(+), 168 deletions(-) diff --git a/DistributedChat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/DistributedChat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 2bb2ac8..a217fd4 100644 --- a/DistributedChat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/DistributedChat.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "b59e46bd5f55d21597b7891993fdfad5b480f2a164eac75e256504c0971cacb3", + "originHash" : "3fb8bc8365586d0d44419dc7c81707891e304a27cc79313b21fb9f3ab1bd1c6a", "pins" : [ { "identity" : "async-http-client", diff --git a/DistributedChatApp/View/AttachmentView.swift b/DistributedChatApp/View/AttachmentView.swift index d7e7431..0693596 100644 --- a/DistributedChatApp/View/AttachmentView.swift +++ b/DistributedChatApp/View/AttachmentView.swift @@ -26,8 +26,6 @@ struct AttachmentView: View { } } -struct AttachmentView_Previews: PreviewProvider { - static var previews: some View { - AttachmentView(attachment: ChatAttachment(name: "test.txt", content: .url(URL(string: "data:text/plain;base64,dGVzdDEyMwo=")!))) - } +#Preview { + AttachmentView(attachment: ChatAttachment(name: "test.txt", content: .url(URL(string: "data:text/plain;base64,dGVzdDEyMwo=")!))) } diff --git a/DistributedChatApp/View/BubbleMessageView.swift b/DistributedChatApp/View/BubbleMessageView.swift index 2e6cd03..f140cd8 100644 --- a/DistributedChatApp/View/BubbleMessageView.swift +++ b/DistributedChatApp/View/BubbleMessageView.swift @@ -71,21 +71,20 @@ struct BubbleMessageView: View { } } -struct BubbleMessageView_Previews: PreviewProvider { - static let message1 = ChatMessage(author: ChatUser(name: "Alice"), content: "Hi!") - static let message2 = ChatMessage(author: ChatUser(name: "Bob"), content: "This is a long\nmultiline message!", repliedToMessageId: message1.id, wasEncrypted: true) - static let message3 = ChatMessage(author: ChatUser(name: "Charles"), content: .encrypted(ChatCryptoCipherData(sealed: Data(), signature: Data(), ephemeralPublicKey: Data())), repliedToMessageId: message1.id) - @StateObject static var messages = Messages(messages: [ +#Preview { + let message1 = ChatMessage(author: ChatUser(name: "Alice"), content: "Hi!") + let message2 = ChatMessage(author: ChatUser(name: "Bob"), content: "This is a long\nmultiline message!", repliedToMessageId: message1.id, wasEncrypted: true) + let message3 = ChatMessage(author: ChatUser(name: "Charles"), content: .encrypted(ChatCryptoCipherData(sealed: Data(), signature: Data(), ephemeralPublicKey: Data())), repliedToMessageId: message1.id) + let messages = Messages(messages: [ message1, message2, message3 ]) - static var previews: some View { - VStack { - BubbleMessageView(message: message1, isMe: false) - BubbleMessageView(message: message2, isMe: true) - BubbleMessageView(message: message3, isMe: true) - } - .environmentObject(messages) + + return VStack { + BubbleMessageView(message: message1, isMe: false) + BubbleMessageView(message: message2, isMe: true) + BubbleMessageView(message: message3, isMe: true) } + .environmentObject(messages) } diff --git a/DistributedChatApp/View/ChannelSnippetView.swift b/DistributedChatApp/View/ChannelSnippetView.swift index 331ca77..3df31df 100644 --- a/DistributedChatApp/View/ChannelSnippetView.swift +++ b/DistributedChatApp/View/ChannelSnippetView.swift @@ -43,14 +43,13 @@ struct ChannelSnippetView: View { } } -struct ChannelSnippetView_Previews: PreviewProvider { - @StateObject static var messages = Messages() - @StateObject static var settings = Settings() - @StateObject static var network = Network(messages: messages) - static var previews: some View { - ChannelSnippetView(channel: .room("test")) - .environmentObject(messages) - .environmentObject(settings) - .environmentObject(network) - } +#Preview { + let messages = Messages() + let settings = Settings() + let network = Network(messages: messages) + + return ChannelSnippetView(channel: .room("test")) + .environmentObject(messages) + .environmentObject(settings) + .environmentObject(network) } diff --git a/DistributedChatApp/View/ChannelView.swift b/DistributedChatApp/View/ChannelView.swift index fba154c..0f0922b 100644 --- a/DistributedChatApp/View/ChannelView.swift +++ b/DistributedChatApp/View/ChannelView.swift @@ -34,23 +34,22 @@ struct ChannelView: View { } } -struct ChatView_Previews: PreviewProvider { - static let controller = ChatController(transport: MockTransport()) - static let alice = controller.me - static let bob = ChatUser(name: "Bob") - @StateObject static var messages = Messages(messages: [ +#Preview { + let controller = ChatController(transport: MockTransport()) + let alice = controller.me + let bob = ChatUser(name: "Bob") + let messages = Messages(messages: [ ChatMessage(author: alice, content: "Hello!"), ChatMessage(author: bob, content: "Hi!"), ChatMessage(author: bob, content: "This is fancy!"), ]) - @StateObject static var settings = Settings() - @StateObject static var network = Network(myId: controller.me.id, messages: messages) - @StateObject static var navigation = Navigation() - static var previews: some View { - ChannelView(channel: .global, controller: controller) - .environmentObject(messages) - .environmentObject(settings) - .environmentObject(network) - .environmentObject(navigation) - } + let settings = Settings() + let network = Network(myId: controller.me.id, messages: messages) + let navigation = Navigation() + + return ChannelView(channel: .global, controller: controller) + .environmentObject(messages) + .environmentObject(settings) + .environmentObject(network) + .environmentObject(navigation) } diff --git a/DistributedChatApp/View/ChannelsView.swift b/DistributedChatApp/View/ChannelsView.swift index e20f21d..1eb6f5e 100644 --- a/DistributedChatApp/View/ChannelsView.swift +++ b/DistributedChatApp/View/ChannelsView.swift @@ -139,16 +139,15 @@ struct ChannelsView: View { } } -struct ChatsView_Previews: PreviewProvider { - @StateObject static var messages = Messages() - @StateObject static var navigation = Navigation() - @StateObject static var settings = Settings() - @StateObject static var network = Network(messages: messages) - static var previews: some View { - ChannelsView(channels: [], controller: ChatController(transport: MockTransport())) - .environmentObject(messages) - .environmentObject(navigation) - .environmentObject(settings) - .environmentObject(network) - } +#Preview { + let messages = Messages() + let navigation = Navigation() + let settings = Settings() + let network = Network(messages: messages) + + return ChannelsView(channels: [], controller: ChatController(transport: MockTransport())) + .environmentObject(messages) + .environmentObject(navigation) + .environmentObject(settings) + .environmentObject(network) } diff --git a/DistributedChatApp/View/ClosableStatusBar.swift b/DistributedChatApp/View/ClosableStatusBar.swift index b46ec60..16896c4 100644 --- a/DistributedChatApp/View/ClosableStatusBar.swift +++ b/DistributedChatApp/View/ClosableStatusBar.swift @@ -22,10 +22,8 @@ struct ClosableStatusBar: View where V: View { } } -struct ClosableStatusBar_Previews: PreviewProvider { - static var previews: some View { - ClosableStatusBar(onClose: {}) { - Text("Test") - } +#Preview { + ClosableStatusBar(onClose: {}) { + Text("Test") } } diff --git a/DistributedChatApp/View/CompactMessageView.swift b/DistributedChatApp/View/CompactMessageView.swift index 0f8e9c6..0e0e7f2 100644 --- a/DistributedChatApp/View/CompactMessageView.swift +++ b/DistributedChatApp/View/CompactMessageView.swift @@ -30,8 +30,6 @@ struct CompactMessageView: View { } } -struct CompactMessageView_Previews: PreviewProvider { - static var previews: some View { - CompactMessageView(message: ChatMessage(author: ChatUser(name: "Alice"), content: "Test")) - } +#Preview { + CompactMessageView(message: ChatMessage(author: ChatUser(name: "Alice"), content: "Test")) } diff --git a/DistributedChatApp/View/ContentView.swift b/DistributedChatApp/View/ContentView.swift index 24a25b9..b447a42 100644 --- a/DistributedChatApp/View/ContentView.swift +++ b/DistributedChatApp/View/ContentView.swift @@ -49,18 +49,17 @@ struct ContentView: View { } } -struct ContentView_Previews: PreviewProvider { - @StateObject static var settings = Settings() - @StateObject static var messages = Messages() - @StateObject static var navigation = Navigation() - @StateObject static var profile = Profile() - @StateObject static var network = Network(myId: profile.me.id, messages: messages) - static var previews: some View { - ContentView(controller: ChatController(transport: MockTransport())) - .environmentObject(settings) - .environmentObject(messages) - .environmentObject(navigation) - .environmentObject(network) - .environmentObject(profile) - } +#Preview { + let settings = Settings() + let messages = Messages() + let navigation = Navigation() + let profile = Profile() + let network = Network(myId: profile.me.id, messages: messages) + + return ContentView(controller: ChatController(transport: MockTransport())) + .environmentObject(settings) + .environmentObject(messages) + .environmentObject(navigation) + .environmentObject(network) + .environmentObject(profile) } diff --git a/DistributedChatApp/View/FileAttachmentView.swift b/DistributedChatApp/View/FileAttachmentView.swift index ea797f5..cfaaea5 100644 --- a/DistributedChatApp/View/FileAttachmentView.swift +++ b/DistributedChatApp/View/FileAttachmentView.swift @@ -21,8 +21,6 @@ struct FileAttachmentView: View { } } -struct FileAttachmentView_Previews: PreviewProvider { - static var previews: some View { - AttachmentView(attachment: ChatAttachment(name: "test.txt", content: .url(URL(string: "data:text/plain;base64,dGVzdDEyMwo=")!))) - } +#Preview { + AttachmentView(attachment: ChatAttachment(name: "test.txt", content: .url(URL(string: "data:text/plain;base64,dGVzdDEyMwo=")!))) } diff --git a/DistributedChatApp/View/MessageComposeView.swift b/DistributedChatApp/View/MessageComposeView.swift index 6463c56..5b856e7 100644 --- a/DistributedChatApp/View/MessageComposeView.swift +++ b/DistributedChatApp/View/MessageComposeView.swift @@ -211,14 +211,13 @@ struct MessageComposeView: View { } } -struct MessageComposeView_Previews: PreviewProvider { - static let controller = ChatController(transport: MockTransport()) - @StateObject static var messages = Messages() - @StateObject static var network = Network(messages: messages) - @State static var replyingToMessageId: UUID? = nil - static var previews: some View { - MessageComposeView(channel: .global, controller: controller, replyingToMessageId: $replyingToMessageId) - .environmentObject(messages) - .environmentObject(network) - } +#Preview { + let controller = ChatController(transport: MockTransport()) + let messages = Messages() + let network = Network(messages: messages) + let replyingToMessageId: UUID? = nil + + return MessageComposeView(channel: .global, controller: controller, replyingToMessageId: .constant(replyingToMessageId)) + .environmentObject(messages) + .environmentObject(network) } diff --git a/DistributedChatApp/View/MessageHistoryView.swift b/DistributedChatApp/View/MessageHistoryView.swift index d35ab64..ee5b75e 100644 --- a/DistributedChatApp/View/MessageHistoryView.swift +++ b/DistributedChatApp/View/MessageHistoryView.swift @@ -49,22 +49,21 @@ struct MessageHistoryView: View { } } -struct MessageHistoryView_Previews: PreviewProvider { - static let controller = ChatController(transport: MockTransport()) - static let alice = controller.me - static let bob = ChatUser(name: "Bob") - @StateObject static var messages = Messages(messages: [ +#Preview { + let controller = ChatController(transport: MockTransport()) + let alice = controller.me + let bob = ChatUser(name: "Bob") + let messages = Messages(messages: [ ChatMessage(author: alice, content: "Hello!"), ChatMessage(author: bob, content: "Hi!"), ChatMessage(author: bob, content: "This is fancy!"), ]) - @StateObject static var settings = Settings() - @StateObject static var navigation = Navigation() - @State static var replyingToMessageId: UUID? = nil - static var previews: some View { - MessageHistoryView(channel: nil, controller: controller, replyingToMessageId: $replyingToMessageId) - .environmentObject(messages) - .environmentObject(settings) - .environmentObject(navigation) - } + let settings = Settings() + let navigation = Navigation() + let replyingToMessageId: UUID? = nil + + return MessageHistoryView(channel: nil, controller: controller, replyingToMessageId: .constant(replyingToMessageId)) + .environmentObject(messages) + .environmentObject(settings) + .environmentObject(navigation) } diff --git a/DistributedChatApp/View/MessageView.swift b/DistributedChatApp/View/MessageView.swift index 80e9c54..15e9dca 100644 --- a/DistributedChatApp/View/MessageView.swift +++ b/DistributedChatApp/View/MessageView.swift @@ -119,22 +119,21 @@ struct MessageView: View { } } -struct MessageView_Previews: PreviewProvider { - static let controller = ChatController(transport: MockTransport()) - static let alice = controller.me - static let bob = ChatUser(name: "Bob") - @StateObject static var messages = Messages(messages: [ +#Preview { + let controller = ChatController(transport: MockTransport()) + let alice = controller.me + let bob = ChatUser(name: "Bob") + let messages = Messages(messages: [ ChatMessage(author: alice, content: "Hello!"), ChatMessage(author: bob, content: "Hi!"), ChatMessage(author: bob, content: "This is fancy!"), ]) - @StateObject static var settings = Settings() - @StateObject static var navigation = Navigation() - @State static var replyingToMessageId: UUID? = nil - static var previews: some View { - MessageView(message: messages.messages.values.first { $0.content == "Hello!" }!, controller: controller, replyingToMessageId: $replyingToMessageId) - .environmentObject(messages) - .environmentObject(settings) - .environmentObject(navigation) - } + let settings = Settings() + let navigation = Navigation() + let replyingToMessageId: UUID? = nil + + return MessageView(message: messages.messages.values.first { $0.content == "Hello!" }!, controller: controller, replyingToMessageId: .constant(replyingToMessageId)) + .environmentObject(messages) + .environmentObject(settings) + .environmentObject(navigation) } diff --git a/DistributedChatApp/View/NetworkView.swift b/DistributedChatApp/View/NetworkView.swift index 7785168..1626f96 100644 --- a/DistributedChatApp/View/NetworkView.swift +++ b/DistributedChatApp/View/NetworkView.swift @@ -84,21 +84,19 @@ struct NetworkView: View { } } -struct NetworkView_Previews: PreviewProvider { - static let alice = ChatUser(name: "Alice") - static let bob = ChatUser(name: "Bob") - @StateObject static var network = Network(nearbyUsers: [ +#Preview { + let alice = ChatUser(name: "Alice") + let bob = ChatUser(name: "Bob") + let network = Network(nearbyUsers: [ NearbyUser(peripheralIdentifier: UUID(uuidString: "6b61a69b-f4b4-4321-92db-9d61653ddaf6")!, chatUser: alice, rssi: -49), NearbyUser(peripheralIdentifier: UUID(uuidString: "b7b7d248-9640-490d-8187-44fc9ebfa1ff")!, chatUser: bob, rssi: -55), ], presences: [ ChatPresence(user: alice, status: .online), ChatPresence(user: bob, status: .away, info: "At the gym"), ], messages: Messages()) - @StateObject static var navigation = Navigation() + let navigation = Navigation() - static var previews: some View { - NetworkView() - .environmentObject(network) - .environmentObject(navigation) - } + return NetworkView() + .environmentObject(network) + .environmentObject(navigation) } diff --git a/DistributedChatApp/View/NewChannelView.swift b/DistributedChatApp/View/NewChannelView.swift index 4820ea4..181e563 100644 --- a/DistributedChatApp/View/NewChannelView.swift +++ b/DistributedChatApp/View/NewChannelView.swift @@ -45,11 +45,9 @@ struct NewChannelView: View { } } -struct NewChannelView_Previews: PreviewProvider { - @StateObject static var network = Network() +#Preview { + let network = Network() - static var previews: some View { - NewChannelView { _ in } - .environmentObject(network) - } + return NewChannelView { _ in } + .environmentObject(network) } diff --git a/DistributedChatApp/View/PlainMessageView.swift b/DistributedChatApp/View/PlainMessageView.swift index bbe952c..c42ab26 100644 --- a/DistributedChatApp/View/PlainMessageView.swift +++ b/DistributedChatApp/View/PlainMessageView.swift @@ -16,8 +16,6 @@ struct PlainMessageView: View { } } -struct PlainMessageView_Previews: PreviewProvider { - static var previews: some View { - PlainMessageView(message: ChatMessage(author: ChatUser(name: "Alice"), content: "Test")) - } +#Preview { + PlainMessageView(message: ChatMessage(author: ChatUser(name: "Alice"), content: "Test")) } diff --git a/DistributedChatApp/View/PresenceView.swift b/DistributedChatApp/View/PresenceView.swift index 3de1ed0..83c4178 100644 --- a/DistributedChatApp/View/PresenceView.swift +++ b/DistributedChatApp/View/PresenceView.swift @@ -60,13 +60,11 @@ struct PresenceView: View { } } -struct PresenceView_Previews: PreviewProvider { - @StateObject static var network = Network() - @StateObject static var navigation = Navigation() +#Preview { + let network = Network() + let navigation = Navigation() - static var previews: some View { - PresenceView(presence: ChatPresence(user: .init())) - .environmentObject(network) - .environmentObject(navigation) - } + return PresenceView(presence: ChatPresence(user: .init())) + .environmentObject(network) + .environmentObject(navigation) } diff --git a/DistributedChatApp/View/ProfileView.swift b/DistributedChatApp/View/ProfileView.swift index 7db1f3d..1e235b7 100644 --- a/DistributedChatApp/View/ProfileView.swift +++ b/DistributedChatApp/View/ProfileView.swift @@ -38,10 +38,9 @@ struct ProfileView: View { } } -struct ProfileView_Previews: PreviewProvider { - @StateObject static var profile = Profile() - static var previews: some View { - ProfileView() - .environmentObject(profile) - } +#Preview { + let profile = Profile() + + return ProfileView() + .environmentObject(profile) } diff --git a/DistributedChatApp/View/QuickLookAttachmentView.swift b/DistributedChatApp/View/QuickLookAttachmentView.swift index 06bef12..c93cb52 100644 --- a/DistributedChatApp/View/QuickLookAttachmentView.swift +++ b/DistributedChatApp/View/QuickLookAttachmentView.swift @@ -47,10 +47,8 @@ struct QuickLookAttachmentView: View where Content: View { } } -struct QuickLookAttachmentView_Previews: PreviewProvider { - static var previews: some View { - QuickLookAttachmentView(attachment: ChatAttachment(name: "test.txt", content: .url(URL(string: "data:text/plain;base64,dGVzdDEyMwo=")!))) { - Text("Test") - } +#Preview { + QuickLookAttachmentView(attachment: ChatAttachment(name: "test.txt", content: .url(URL(string: "data:text/plain;base64,dGVzdDEyMwo=")!))) { + Text("Test") } } diff --git a/DistributedChatApp/View/SettingsView.swift b/DistributedChatApp/View/SettingsView.swift index 92b72d0..5cca685 100644 --- a/DistributedChatApp/View/SettingsView.swift +++ b/DistributedChatApp/View/SettingsView.swift @@ -54,10 +54,9 @@ struct SettingsView: View { } } -struct SettingsView_Previews: PreviewProvider { - @StateObject static var settings = Settings() - static var previews: some View { - SettingsView() - .environmentObject(settings) - } +#Preview { + let settings = Settings() + + return SettingsView() + .environmentObject(settings) } diff --git a/DistributedChatApp/View/VoiceNoteRecordButton.swift b/DistributedChatApp/View/VoiceNoteRecordButton.swift index 25a9b89..8f6940e 100644 --- a/DistributedChatApp/View/VoiceNoteRecordButton.swift +++ b/DistributedChatApp/View/VoiceNoteRecordButton.swift @@ -37,8 +37,6 @@ struct VoiceNoteRecordButton: View { } } -struct VoiceNoteRecordButton_Previews: PreviewProvider { - static var previews: some View { - VoiceNoteRecordButton { _ in } - } +#Preview { + VoiceNoteRecordButton { _ in } }