Impressia/Vernissage/EnvironmentObjects/ApplicationState.swift

117 lines
4.1 KiB
Swift
Raw Normal View History

2022-12-30 18:20:54 +01:00
//
// https://mczachurski.dev
// Copyright © 2022 Marcin Czachurski and the repository contributors.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2022-12-30 18:20:54 +01:00
//
2022-12-30 18:20:54 +01:00
import Foundation
2023-01-12 18:34:48 +01:00
import SwiftUI
2023-02-19 10:32:38 +01:00
import PixelfedKit
2022-12-30 18:20:54 +01:00
public class ApplicationState: ObservableObject {
public static let shared = ApplicationState()
2023-01-06 13:05:21 +01:00
private init() { }
2023-01-10 20:38:02 +01:00
/// Class with default variables.
private class Defaults {
2023-02-22 14:33:03 +01:00
let statusMaxCharacters = 500
let statusMaxMediaAttachments = 4
let statusCharactersReservedPerUrl = 23
}
/// Default variables.
private static let defaults = Defaults()
/// Actual signed in account.
@Published private(set) var account: AccountModel?
/// The maximum number of allowed characters per status.
@Published private(set) var statusMaxCharacters = defaults.statusMaxCharacters
/// The maximum number of media attachments that can be added to a status.
@Published private(set) var statusMaxMediaAttachments = defaults.statusMaxMediaAttachments
/// Each URL in a status will be assumed to be exactly this many characters.
@Published private(set) var statusCharactersReservedPerUrl = defaults.statusCharactersReservedPerUrl
/// Last status seen by the user.
@Published var lastSeenStatusId: String?
/// Amount of new statuses which are not displayed yet to the user.
@Published var amountOfNewStatuses = 0
/// Model for newly created comment.
@Published var newComment: CommentModel?
2023-03-18 18:40:07 +01:00
/// Active icon name.
@Published var activeIcon = "Default"
/// Tint color in whole application.
2023-01-12 18:34:48 +01:00
@Published var tintColor = TintColor.accentColor2
/// Application theme.
2023-01-13 13:37:01 +01:00
@Published var theme = Theme.system
/// Avatar shape.
2023-01-24 12:22:53 +01:00
@Published var avatarShape = AvatarShape.circle
/// Status id for showed interaction row.
2023-01-14 08:52:51 +01:00
@Published var showInteractionStatusId = String.empty()
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when user change tabs.
@Published var hapticTabSelectionEnabled = true
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when user refresh list.
@Published var hapticRefreshEnabled = true
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when user tap button.
@Published var hapticButtonPressEnabled = true
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when animation is finished.
@Published var hapticAnimationEnabled = true
/// Should we fire haptic when notification occures.
@Published var hapticNotificationEnabled = true
/// Should sensitive photos without mask.
@Published var showSensitive = false
2023-03-08 17:43:05 +01:00
/// Should photo description for visually impaired be displayed.
@Published var showPhotoDescription = false
2023-03-11 18:30:33 +01:00
/// Status which should be shown from URL.
@Published var showStatusId: String?
2023-03-25 11:37:02 +01:00
/// Updated user profile.
@Published var updatedProfile: Account?
2023-04-06 13:19:55 +02:00
/// Information which menu should be shown (top or bottom).
@Published var menuPosition = MenuPosition.top
public func changeApplicationState(accountModel: AccountModel, instance: Instance?, lastSeenStatusId: String?) {
self.account = accountModel
self.lastSeenStatusId = lastSeenStatusId
self.amountOfNewStatuses = 0
if let statusesConfiguration = instance?.configuration?.statuses {
self.statusMaxCharacters = statusesConfiguration.maxCharacters
self.statusMaxMediaAttachments = statusesConfiguration.maxMediaAttachments
self.statusCharactersReservedPerUrl = statusesConfiguration.charactersReservedPerUrl
} else {
self.statusMaxCharacters = ApplicationState.defaults.statusMaxCharacters
self.statusMaxMediaAttachments = ApplicationState.defaults.statusMaxMediaAttachments
self.statusCharactersReservedPerUrl = ApplicationState.defaults.statusCharactersReservedPerUrl
}
}
2023-02-23 08:09:02 +01:00
public func clearApplicationState() {
self.account = nil
self.lastSeenStatusId = nil
self.amountOfNewStatuses = 0
2023-02-23 08:09:02 +01:00
self.statusMaxCharacters = ApplicationState.defaults.statusMaxCharacters
self.statusMaxMediaAttachments = ApplicationState.defaults.statusMaxMediaAttachments
self.statusCharactersReservedPerUrl = ApplicationState.defaults.statusCharactersReservedPerUrl
}
2022-12-30 18:20:54 +01:00
}