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 --- Juick.xcodeproj/project.pbxproj | 4 ++ JuickNext/Helpers/View+ScreenTitle.swift | 39 ++++++++++++++++++ JuickNext/Models.swift | 1 + JuickNext/View/ContentView.swift | 69 +++++++++++++------------------- JuickNext/View/FeedView.swift | 18 +++++---- 5 files changed, 82 insertions(+), 49 deletions(-) create mode 100644 JuickNext/Helpers/View+ScreenTitle.swift diff --git a/Juick.xcodeproj/project.pbxproj b/Juick.xcodeproj/project.pbxproj index 2c548fc..56616f4 100644 --- a/Juick.xcodeproj/project.pbxproj +++ b/Juick.xcodeproj/project.pbxproj @@ -64,6 +64,7 @@ 77AA764C2BF48818007A0FA8 /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77AA76442BF48818007A0FA8 /* Errors.swift */; }; 77AA764D2BF48818007A0FA8 /* ImageFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77AA76452BF48818007A0FA8 /* ImageFetcher.swift */; }; 77AA76502BF488A9007A0FA8 /* Image+Data.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77AA764F2BF488A9007A0FA8 /* Image+Data.swift */; }; + 77AA76522BF4A9DD007A0FA8 /* View+ScreenTitle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 77AA76512BF4A9DD007A0FA8 /* View+ScreenTitle.swift */; }; 77B09994189D0B9900A84F59 /* UIImage+Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = 77B09993189D0B9900A84F59 /* UIImage+Utils.m */; }; 77B099A6189D57F000A84F59 /* User.m in Sources */ = {isa = PBXBuildFile; fileRef = 77B099A5189D57F000A84F59 /* User.m */; }; 77B8B399207A52BD005CB20C /* MessageInputView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 77B8B398207A52BD005CB20C /* MessageInputView.xib */; }; @@ -198,6 +199,7 @@ 77AA76442BF48818007A0FA8 /* Errors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = ""; }; 77AA76452BF48818007A0FA8 /* ImageFetcher.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImageFetcher.swift; sourceTree = ""; }; 77AA764F2BF488A9007A0FA8 /* Image+Data.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Image+Data.swift"; sourceTree = ""; }; + 77AA76512BF4A9DD007A0FA8 /* View+ScreenTitle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "View+ScreenTitle.swift"; sourceTree = ""; }; 77B09992189D0B9900A84F59 /* UIImage+Utils.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIImage+Utils.h"; sourceTree = ""; }; 77B09993189D0B9900A84F59 /* UIImage+Utils.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIImage+Utils.m"; sourceTree = ""; }; 77B099A4189D57F000A84F59 /* User.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = User.h; sourceTree = ""; }; @@ -474,6 +476,7 @@ children = ( 77AA76442BF48818007A0FA8 /* Errors.swift */, 77AA764F2BF488A9007A0FA8 /* Image+Data.swift */, + 77AA76512BF4A9DD007A0FA8 /* View+ScreenTitle.swift */, ); path = Helpers; sourceTree = ""; @@ -709,6 +712,7 @@ 77AA76462BF48818007A0FA8 /* LoadableImageView.swift in Sources */, 77AA764D2BF48818007A0FA8 /* ImageFetcher.swift in Sources */, 77163AB42A104786009BEE0E /* JuickApp.swift in Sources */, + 77AA76522BF4A9DD007A0FA8 /* View+ScreenTitle.swift in Sources */, 77AA76482BF48818007A0FA8 /* Models.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; 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