From 7fe780a549f71bb95e4df0a52a1e701c5f8169dd Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Wed, 15 May 2024 11:52:54 +0300 Subject: Fix both layouts --- JuickNext/Helpers/View+ScreenTitle.swift | 39 ++++++++++++++++++ JuickNext/Models.swift | 1 + JuickNext/View/ContentView.swift | 69 +++++++++++++------------------- JuickNext/View/FeedView.swift | 18 +++++---- 4 files changed, 78 insertions(+), 49 deletions(-) create mode 100644 JuickNext/Helpers/View+ScreenTitle.swift (limited to 'JuickNext') diff --git a/JuickNext/Helpers/View+ScreenTitle.swift b/JuickNext/Helpers/View+ScreenTitle.swift new file mode 100644 index 0000000..192b3bc --- /dev/null +++ b/JuickNext/Helpers/View+ScreenTitle.swift @@ -0,0 +1,39 @@ +// +// View+ScreenTitle.swift +// JuickNext +// +// Created by Vitaly Takmazov on 15.05.2024. +// Copyright © 2024 com.juick. All rights reserved. +// + +import Foundation +import SwiftUI + +public struct ScreenTitleModifier: ViewModifier { + + let screenTitle: String + + init(title: String) { + self.screenTitle = title + } + + + public func body(content: Content) -> some View { + if #available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *) { + content.navigationTitle(self.screenTitle) + } else { +#if os(iOS) + content.navigationBarTitle(self.screenTitle) +#else + content +#endif + } + } +} + +extension View { + @ViewBuilder + func screenTitle(title: String) -> some View { + modifier(ScreenTitleModifier(title: title)) + } +} diff --git a/JuickNext/Models.swift b/JuickNext/Models.swift index f0d8109..368c8df 100644 --- a/JuickNext/Models.swift +++ b/JuickNext/Models.swift @@ -49,4 +49,5 @@ struct Message : Model { struct Feed { var title: String var url: String + var imageName: String } diff --git a/JuickNext/View/ContentView.swift b/JuickNext/View/ContentView.swift index 3912c59..7f5db89 100644 --- a/JuickNext/View/ContentView.swift +++ b/JuickNext/View/ContentView.swift @@ -11,58 +11,43 @@ import SwiftUI struct ContentView: View { @Environment(\.horizontalSizeClass) private var size - let today = Feed(title: "Today", url: "https://api.juick.com/messages?popular=1") - let discussions = Feed(title: "Discussions", url: "https://api.juick.com/messages/discussions") - let discover = Feed(title: "Discover", url: "https://api.juick.com/messages") + let today = Feed(title: "Today", url: "https://api.juick.com/messages?popular=1", imageName: "ei-clock") + let discussions = Feed(title: "Discussions", url: "https://api.juick.com/messages/discussions", imageName: "ei-bell") + let discover = Feed(title: "Discover", url: "https://api.juick.com/messages", imageName: "Discover") @State private var showFeed : Bool = false @State private var selectedFeed: Feed? = nil + var body: some View { + let tabs = [today, discussions, discover] let view = (size == .compact) ? - AnyView(TabView { - FeedView(today).tabItem { - Image("ei-clock") - } - FeedView(discussions).tabItem { - Image("ei-bell") - } - FeedView(discover).tabItem { - Image("Discover") - } - }): AnyView(NavigationView { - let tabs = [today, discussions, discover] - VStack { - List { - ForEach(tabs, id: \.title) { - tab in - Button(action: { - selectedFeed = tab - showFeed.toggle() - }, label: { - Text(tab.title) - }) + AnyView( + TabView { + ForEach(tabs, id: \.title) { + tab in + FeedView(tab).tabItem { + Image(tab.imageName) } - }.background { - if let feed = selectedFeed { - NavigationLink( - destination: FeedView(feed), - isActive: $showFeed, - label: { - VStack { - Text(feed.title) - } + } + } + ): AnyView( + NavigationView { + VStack { + List { + ForEach(tabs, id: \.title) { + tab in + NavigationLink(destination: FeedView(tab), label: { + HStack { + Image(tab.imageName) + Text(tab.title) + }.padding() }) - } else { - EmptyView() + } } - } - }.toolbar { - ToolbarItem(placement: .principal) { - Text("Juick") - } + }.screenTitle(title: "Juick") } - }) + ) view } } diff --git a/JuickNext/View/FeedView.swift b/JuickNext/View/FeedView.swift index cd0569f..783d8c0 100644 --- a/JuickNext/View/FeedView.swift +++ b/JuickNext/View/FeedView.swift @@ -9,6 +9,7 @@ import SwiftUI struct FeedView: View { + @Environment(\.horizontalSizeClass) private var size @ObservedObject var messageFetcher : MessageFetcher let feed: Feed @@ -38,18 +39,21 @@ struct FeedView: View { } var body: some View { - VStack { + let content = VStack { stateContent - }.toolbar { - ToolbarItem(placement: .principal) { - Text(feed.title) - } - } + }.screenTitle(title: feed.title) + + let view = (size == .compact) ? + AnyView( + NavigationView { + content + }) : AnyView(content) + view } } struct FeedView_Previews: PreviewProvider { static var previews: some View { - FeedView(Feed(title:"Discover", url: "https://api.juick.com/messages")) + FeedView(Feed(title:"Discover", url: "https://api.juick.com/messages", imageName: "Discover")) } } -- cgit v1.2.3