Impressia/Vernissage/Views/MainView.swift

184 lines
5.6 KiB
Swift
Raw Normal View History

2022-12-29 08:06:21 +01:00
//
// https://mczachurski.dev
// Copyright © 2022 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import SwiftUI
import UIKit
import CoreData
import MastodonSwift
struct MainView: View {
@Environment(\.managedObjectContext) private var viewContext
2022-12-30 18:20:54 +01:00
@EnvironmentObject var applicationState: ApplicationState
2022-12-29 08:06:21 +01:00
2022-12-30 18:20:54 +01:00
@State private var navBarTitle: String = "Home"
@State private var viewMode: ViewMode = .home {
didSet {
2022-12-31 16:31:05 +01:00
self.navBarTitle = self.getViewTitle(viewMode: viewMode)
2022-12-30 18:20:54 +01:00
}
}
2022-12-29 17:27:15 +01:00
2022-12-30 18:20:54 +01:00
private enum ViewMode {
2023-01-05 21:08:19 +01:00
case home, local, federated, profile, notifications
2022-12-30 18:20:54 +01:00
}
2022-12-29 08:06:21 +01:00
var body: some View {
2022-12-30 18:20:54 +01:00
self.getMainView()
.navigationBarTitle(navBarTitle)
.navigationBarTitleDisplayMode(.inline)
.toolbar {
self.getLeadingToolbar()
self.getPrincipalToolbar()
}
}
@ViewBuilder
private func getMainView() -> some View {
switch self.viewMode {
case .home:
HomeFeedView()
case .local:
LocalFeedView()
case .federated:
FederatedFeedView()
2023-01-05 21:08:19 +01:00
case .profile:
if let accountData = self.applicationState.accountData {
UserProfileView(accountId: accountData.id,
accountDisplayName: accountData.displayName,
2023-01-06 13:05:21 +01:00
accountUserName: accountData.acct)
2023-01-05 21:08:19 +01:00
}
2022-12-30 18:20:54 +01:00
case .notifications:
NotificationsView()
}
}
@ToolbarContentBuilder
private func getPrincipalToolbar() -> some ToolbarContent {
ToolbarItem(placement: .principal) {
Menu {
Button {
viewMode = .home
} label: {
HStack {
2022-12-31 16:31:05 +01:00
Text(self.getViewTitle(viewMode: .home))
2022-12-30 18:20:54 +01:00
Image(systemName: "house")
2022-12-29 17:27:15 +01:00
}
2022-12-30 18:20:54 +01:00
}
Button {
viewMode = .local
} label: {
HStack {
2022-12-31 16:31:05 +01:00
Text(self.getViewTitle(viewMode: .local))
2022-12-30 18:20:54 +01:00
Image(systemName: "text.redaction")
2022-12-29 17:27:15 +01:00
}
2022-12-30 18:20:54 +01:00
}
2022-12-29 17:27:15 +01:00
2022-12-30 18:20:54 +01:00
Button {
viewMode = .federated
2022-12-29 17:27:15 +01:00
} label: {
2022-12-30 18:20:54 +01:00
HStack {
2022-12-31 16:31:05 +01:00
Text(self.getViewTitle(viewMode: .federated))
2022-12-30 18:20:54 +01:00
Image(systemName: "globe.europe.africa")
}
2022-12-29 17:27:15 +01:00
}
2022-12-30 18:20:54 +01:00
2023-01-03 20:42:20 +01:00
Divider()
2023-01-05 21:08:19 +01:00
Button {
viewMode = .profile
} label: {
HStack {
Text(self.getViewTitle(viewMode: .profile))
Image(systemName: "person")
}
}
2023-01-03 20:42:20 +01:00
2022-12-29 17:27:15 +01:00
Button {
2022-12-30 18:20:54 +01:00
viewMode = .notifications
2022-12-29 17:27:15 +01:00
} label: {
2022-12-30 18:20:54 +01:00
HStack {
2022-12-31 16:31:05 +01:00
Text(self.getViewTitle(viewMode: .notifications))
2022-12-30 18:20:54 +01:00
Image(systemName: "bell.badge")
2022-12-29 17:27:15 +01:00
}
}
2022-12-30 18:20:54 +01:00
} label: {
HStack {
Text(navBarTitle)
.font(.headline)
Image(systemName: "chevron.down")
.font(.subheadline)
}
.frame(width: 150)
2023-01-06 13:05:21 +01:00
.foregroundColor(.mainTextColor)
2022-12-29 08:06:21 +01:00
}
}
2022-12-30 18:20:54 +01:00
}
@ToolbarContentBuilder
private func getLeadingToolbar() -> some ToolbarContent {
ToolbarItem(placement: .navigationBarLeading) {
2023-01-03 20:42:20 +01:00
Menu {
Button {
2023-01-05 11:55:20 +01:00
// TODO: Switch accounts.
2023-01-03 20:42:20 +01:00
} label: {
HStack {
2023-01-06 13:05:21 +01:00
Text(self.applicationState.accountData?.displayName ?? self.applicationState.accountData?.acct ?? "")
2023-01-03 20:42:20 +01:00
Image(systemName: "person.circle.fill")
.resizable()
2023-01-06 13:05:21 +01:00
.foregroundColor(.mainTextColor)
2023-01-03 20:42:20 +01:00
}
}
Divider()
Button {
2023-01-05 11:55:20 +01:00
// TODO: Open settings.
2023-01-03 20:42:20 +01:00
} label: {
HStack {
Text("Settings")
Image(systemName: "gear")
}
}
2022-12-30 18:20:54 +01:00
} label: {
if let avatarData = self.applicationState.accountData?.avatarData, let uiImage = UIImage(data: avatarData) {
Image(uiImage: uiImage)
.resizable()
.clipShape(Circle())
2022-12-31 16:31:05 +01:00
.frame(width: 32.0, height: 32.0)
2022-12-30 18:20:54 +01:00
} else {
Image(systemName: "person.circle")
2022-12-31 16:31:05 +01:00
.resizable()
.frame(width: 32.0, height: 32.0)
2023-01-06 13:05:21 +01:00
.foregroundColor(.mainTextColor)
2022-12-30 18:20:54 +01:00
}
2022-12-29 08:06:21 +01:00
}
}
}
2022-12-31 16:31:05 +01:00
private func getViewTitle(viewMode: ViewMode) -> String {
switch viewMode {
case .home:
return "Home"
case .local:
return "Local"
case .federated:
return "Federated"
2023-01-05 21:08:19 +01:00
case .profile:
return "Profile"
2022-12-31 16:31:05 +01:00
case .notifications:
return "Notifications"
2022-12-29 08:06:21 +01:00
}
}
}
struct MainView_Previews: PreviewProvider {
static var previews: some View {
2022-12-31 16:31:05 +01:00
MainView().environment(\.managedObjectContext, CoreDataHandler.preview.container.viewContext)
2022-12-29 08:06:21 +01:00
}
}