Impressia/Vernissage/Views/MainView.swift

222 lines
7.4 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
2023-01-10 08:04:25 +01:00
import MastodonKit
2022-12-29 08:06:21 +01:00
struct MainView: View {
2023-01-12 18:34:48 +01:00
enum Sheet: String, Identifiable {
case settings, compose
var id: String { rawValue }
}
2022-12-29 08:06:21 +01:00
@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
2023-01-12 18:34:48 +01:00
var onTintChange: ((TintColor) -> Void)?
2023-01-13 13:37:01 +01:00
var onThemeChange: ((Theme) -> Void)?
2023-01-12 18:34:48 +01:00
2023-01-11 13:16:43 +01:00
@State private var showSettings = false
2023-01-12 18:34:48 +01:00
@State private var sheet: Sheet?
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
2023-01-11 13:16:43 +01:00
@FetchRequest(sortDescriptors: [SortDescriptor(\.acct, order: .forward)]) var dbAccounts: FetchedResults<AccountData>
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)
2023-01-12 18:34:48 +01:00
.sheet(item: $sheet, content: { item in
switch item {
case .settings:
SettingsView { color in
self.onTintChange?(color)
2023-01-13 13:37:01 +01:00
} onThemeChange: { theme in
self.onThemeChange?(theme)
2023-01-12 18:34:48 +01:00
}
case .compose:
ComposeView(statusViewModel: .constant(nil))
}
2023-01-11 13:16:43 +01:00
})
2022-12-30 18:20:54 +01:00
.toolbar {
self.getLeadingToolbar()
self.getPrincipalToolbar()
2023-01-12 18:34:48 +01:00
self.getTrailingToolbar()
2022-12-30 18:20:54 +01:00
}
}
@ViewBuilder
private func getMainView() -> some View {
switch self.viewMode {
case .home:
2023-01-14 08:52:51 +01:00
HomeFeedView(accountId: applicationState.accountData?.id ?? String.empty())
.id(applicationState.accountData?.id ?? String.empty())
2022-12-30 18:20:54 +01:00
case .local:
2023-01-21 18:01:17 +01:00
TimelineFeedView(accountId: applicationState.accountData?.id ?? String.empty(), isLocalOnly: true)
2023-01-14 08:52:51 +01:00
.id(applicationState.accountData?.id ?? String.empty())
2022-12-30 18:20:54 +01:00
case .federated:
2023-01-21 18:01:17 +01:00
TimelineFeedView(accountId: applicationState.accountData?.id ?? String.empty(), isLocalOnly: false)
2023-01-14 08:52:51 +01:00
.id(applicationState.accountData?.id ?? String.empty())
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-14 08:52:51 +01:00
.id(applicationState.accountData?.id ?? String.empty())
2023-01-05 21:08:19 +01:00
}
2022-12-30 18:20:54 +01:00
case .notifications:
2023-01-18 18:41:42 +01:00
if let accountData = self.applicationState.accountData {
NotificationsView(accountId: accountData.id)
.id(applicationState.accountData?.id ?? String.empty())
}
2022-12-30 18:20:54 +01:00
}
}
@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 {
2023-01-11 13:16:43 +01:00
ForEach(self.dbAccounts) { account in
Button {
self.applicationState.accountData = account
ApplicationSettingsHandler.shared.setAccountAsDefault(accountData: account)
2023-01-11 13:16:43 +01:00
} label: {
if self.applicationState.accountData?.id == account.id {
Label(account.displayName ?? account.acct, systemImage: "checkmark")
} else {
Text(account.displayName ?? account.acct)
}
2023-01-03 20:42:20 +01:00
}
}
Divider()
Button {
2023-01-12 18:34:48 +01:00
self.sheet = .settings
2023-01-03 20:42:20 +01:00
} label: {
2023-01-11 13:16:43 +01:00
Label("Settings", systemImage: "gear")
2023-01-03 20:42:20 +01:00
}
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
}
}
}
2023-01-12 18:34:48 +01:00
@ToolbarContentBuilder
private func getTrailingToolbar() -> some ToolbarContent {
ToolbarItem(placement: .navigationBarTrailing) {
Button {
self.sheet = .compose
} label: {
2023-01-22 21:44:07 +01:00
Image(systemName: "square.and.pencil")
2023-01-12 18:34:48 +01:00
.tint(.mainTextColor)
}
}
}
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
}
}
}