Refactor app tabs

This commit is contained in:
Thomas Ricouard 2022-12-27 09:25:26 +01:00
parent 94d4db6214
commit a84d3da19a
7 changed files with 71 additions and 33 deletions

View File

@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
9F24EEB829360C330042359D /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 9F24EEB729360C330042359D /* Preview Assets.xcassets */; };
9F295540292B6C3400E0E81B /* Timeline in Frameworks */ = {isa = PBXBuildFile; productRef = 9F29553F292B6C3400E0E81B /* Timeline */; };
9F2B92F6295AE04800DE16D0 /* Tabs.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F2B92F5295AE04800DE16D0 /* Tabs.swift */; };
9F35DB44294F9A7D00B3281A /* Status in Frameworks */ = {isa = PBXBuildFile; productRef = 9F35DB43294F9A7D00B3281A /* Status */; };
9F35DB4729506F6600B3281A /* NotificationTab.swift in Sources */ = {isa = PBXBuildFile; fileRef = 9F35DB4629506F6600B3281A /* NotificationTab.swift */; };
9F35DB4A29506FA100B3281A /* Notifications in Frameworks */ = {isa = PBXBuildFile; productRef = 9F35DB4929506FA100B3281A /* Notifications */; };
@ -34,6 +35,7 @@
9F24EEB729360C330042359D /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = "<group>"; };
9F29553D292B67B600E0E81B /* Network */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Network; path = Packages/Network; sourceTree = "<group>"; };
9F29553E292B6AF600E0E81B /* Timeline */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Timeline; path = Packages/Timeline; sourceTree = "<group>"; };
9F2B92F5295AE04800DE16D0 /* Tabs.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tabs.swift; sourceTree = "<group>"; };
9F35DB42294F9A2900B3281A /* Status */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = Status; path = Packages/Status; sourceTree = "<group>"; };
9F35DB45294FA04C00B3281A /* DesignSystem */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = DesignSystem; path = Packages/DesignSystem; sourceTree = "<group>"; };
9F35DB4629506F6600B3281A /* NotificationTab.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationTab.swift; sourceTree = "<group>"; };
@ -104,6 +106,7 @@
9F35DB4629506F6600B3281A /* NotificationTab.swift */,
9F35DB4B2952005C00B3281A /* AccountTab.swift */,
9F55C68C2955968700F94077 /* ExploreTab.swift */,
9F2B92F5295AE04800DE16D0 /* Tabs.swift */,
);
path = Tabs;
sourceTree = "<group>";
@ -259,6 +262,7 @@
9F35DB4C2952005C00B3281A /* AccountTab.swift in Sources */,
9FAE4ACB293783B000772766 /* SettingsTab.swift in Sources */,
9FAE4AD32937A0C600772766 /* AppAccountsManager.swift in Sources */,
9F2B92F6295AE04800DE16D0 /* Tabs.swift in Sources */,
9F398AB329360A4C00A889F2 /* TimelineTab.swift in Sources */,
9F398AA62935FE8A00A889F2 /* AppRouteur.swift in Sources */,
9FBFE63D292A715500C250E9 /* IceCubesApp.swift in Sources */,

View File

@ -7,10 +7,6 @@ import DesignSystem
@main
struct IceCubesApp: App {
enum Tab: Int {
case timeline, notifications, explore, account, settings, other
}
public static let defaultServer = "mastodon.social"
@Environment(\.scenePhase) private var scenePhase
@ -37,34 +33,14 @@ struct IceCubesApp: App {
}
selectedTab = newTab
})) {
TimelineTab(popToRootTab: $popToRootTab)
.tabItem {
Label("Timeline", systemImage: "rectangle.on.rectangle")
}
.tag(Tab.timeline)
if appAccountsManager.currentClient.isAuth {
NotificationsTab(popToRootTab: $popToRootTab)
ForEach(appAccountsManager.currentClient.isAuth ? Tab.loggedInTabs() : Tab.loggedOutTab()) { tab in
tab.makeContentView(popToRootTab: $popToRootTab)
.tabItem {
Label("Notifications", systemImage: "bell")
tab.label
.badge(tab == .notifications ? watcher.unreadNotificationsCount : 0)
}
.badge(watcher.unreadNotificationsCount)
.tag(Tab.notifications)
ExploreTab(popToRootTab: $popToRootTab)
.tabItem {
Label("Explore", systemImage: "magnifyingglass")
}
.tag(Tab.explore)
AccountTab(popToRootTab: $popToRootTab)
.tabItem {
Label("Profile", systemImage: "person.circle")
}
.tag(Tab.account)
.tag(tab)
}
SettingsTabs()
.tabItem {
Label("Settings", systemImage: "gear")
}
.tag(Tab.settings)
}
.tint(theme.tintColor)
.onChange(of: appAccountsManager.currentClient) { newClient in

View File

@ -9,7 +9,7 @@ struct AccountTab: View {
@EnvironmentObject private var client: Client
@EnvironmentObject private var currentAccount: CurrentAccount
@StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
@Binding var popToRootTab: Tab
var body: some View {
NavigationStack(path: $routeurPath.path) {

View File

@ -9,7 +9,7 @@ import Network
struct ExploreTab: View {
@EnvironmentObject private var client: Client
@StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
@Binding var popToRootTab: Tab
var body: some View {
NavigationStack(path: $routeurPath.path) {

View File

@ -8,7 +8,7 @@ struct NotificationsTab: View {
@EnvironmentObject private var client: Client
@EnvironmentObject private var watcher: StreamWatcher
@StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
@Binding var popToRootTab: Tab
var body: some View {
NavigationStack(path: $routeurPath.path) {

View File

@ -0,0 +1,58 @@
import Foundation
import Status
import Account
import Explore
import SwiftUI
enum Tab: Int, Identifiable {
case timeline, notifications, explore, account, settings, other
var id: Int {
rawValue
}
static func loggedOutTab() -> [Tab] {
[.timeline, .settings]
}
static func loggedInTabs() -> [Tab] {
[.timeline, .notifications, .explore, .account, .settings]
}
@ViewBuilder
func makeContentView(popToRootTab: Binding<Tab>) -> some View {
switch self {
case .timeline:
TimelineTab(popToRootTab: popToRootTab)
case .notifications:
NotificationsTab(popToRootTab: popToRootTab)
case .explore:
ExploreTab(popToRootTab: popToRootTab)
case .account:
AccountTab(popToRootTab: popToRootTab)
case .settings:
SettingsTabs()
case .other:
EmptyView()
}
}
@ViewBuilder
var label: some View {
switch self {
case .timeline:
Label("Timeline", systemImage: "rectangle.on.rectangle")
case .notifications:
Label("Notifications", systemImage: "bell")
case .explore:
Label("Explore", systemImage: "magnifyingglass")
case .account:
Label("Profile", systemImage: "person.circle")
case .settings:
Label("Settings", systemImage: "gear")
case .other:
EmptyView()
}
}
}

View File

@ -7,7 +7,7 @@ import Combine
struct TimelineTab: View {
@EnvironmentObject private var client: Client
@StateObject private var routeurPath = RouterPath()
@Binding var popToRootTab: IceCubesApp.Tab
@Binding var popToRootTab: Tab
@State private var timeline: TimelineFilter = .home
var body: some View {