import SwiftUI enum Selector: Hashable { case overview case room(String) case direct(String) } struct Room: Identifiable { var id: String { name } let name: String } struct Direct: Identifiable { var id: String { name } let name: String } struct ContentView: View { let rooms = ["Core Dev", "Rust", "Ads"].map { Room(name: $0) } let directs = ["Jeroen", "Ilnur"].map { Direct(name: $0) } @State private var selected: Selector = .overview; var body: some View { NavigationSplitView { List(selection: $selected) { Text("Overview").tag(Selector.overview) Section("Rooms") { ForEach(rooms) { it in Text(it.name).tag(Selector.room(it.id)) } } Section("Directs") { ForEach(directs) { it in Text(it.name).tag(Selector.direct(it.id)) } } } } detail: { switch selected { case .overview: Overview() case .room(_): TopicListView() case .direct(_): MessageListView() } } } } struct Overview: View { var body: some View { Text("welcome") } } struct MessageListView: View { @State var newMessage: String = "" @ObservedObject var viewModel: TopicViewModel = TopicViewModel(messages: [ Message(author: "Jeroen", body: "Hello everyone!"), Message(author: "Jeroen", body: "Hoe gaat het?"), Message(author: "Ilnur", body: "Normalno"), Message(author: "Jeroen", body: "Zbs!"), ]) var body: some View { ScrollView { Text("Start of the conversation") VStack(alignment: .leading) { ForEach(viewModel.messages) { it in Divider() MessageBatchView( author: it.author, messages: [it.body] ) } } } .contentMargins(5) .defaultScrollAnchor(.bottom) .background(.white) HStack { TextField(text: $newMessage) { Text("New message") }.textFieldStyle(.roundedBorder) Button(action: {}) { Text("Send") } }.padding(5) } } struct MessageBatchView: View { let author: String let messages: [String] var body: some View { Text("\(author):").bold() ForEach(messages, id: \.hash) { it in Text(it) } } } struct TopicListView: View { @State var topicTitle: String = "" var body: some View { ScrollView { VStack(alignment: .leading) { TextField(text: $topicTitle) { Text("A new topic!") } Divider() TopicSlugView( title: "How do I patch KDE3 for FreeBSD?", numberOfReplies: 5, openedWhen: "Opened 5 days ago" ) TopicSlugView( title: "Our coffee machine is broken, pls fix", numberOfReplies: 133, openedWhen: "Opened yesterday" ) } .padding(5) } } } struct TopicSlugView: View { let title: String let numberOfReplies: Int let openedWhen: String var body: some View { Button { } label: { VStack(alignment: .leading) { Text(title).bold() HStack { Text("\(numberOfReplies) replies") Spacer() Text(openedWhen) } } } } } class TopicViewModel: ObservableObject { @Published var messages: [Message] init(messages: [Message]) { self.messages = messages } } struct Message: Identifiable { let id: UUID = UUID() var author: String var body: String } #Preview { ContentView() }