Vernissage/EnvironmentKit/Sources/EnvironmentKit/ApplicationState.swift

152 lines
5.2 KiB
Swift
Raw Normal View History

2022-12-30 18:20:54 +01:00
//
// https://mczachurski.dev
2023-04-09 20:51:33 +02:00
// Copyright © 2023 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
2023-04-07 14:20:12 +02:00
import ClientKit
2022-12-30 18:20:54 +01:00
2023-10-19 13:24:02 +02:00
@Observable public class ApplicationState {
2022-12-30 18:20:54 +01:00
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.
2023-10-19 13:24:02 +02:00
public private(set) var account: AccountModel?
/// The maximum number of allowed characters per status.
2023-10-19 13:24:02 +02:00
public private(set) var statusMaxCharacters = defaults.statusMaxCharacters
/// The maximum number of media attachments that can be added to a status.
2023-10-19 13:24:02 +02:00
public private(set) var statusMaxMediaAttachments = defaults.statusMaxMediaAttachments
/// Each URL in a status will be assumed to be exactly this many characters.
2023-10-19 13:24:02 +02:00
public private(set) var statusCharactersReservedPerUrl = defaults.statusCharactersReservedPerUrl
2023-10-22 10:09:02 +02:00
/// Last notification seen by the user.
public var lastSeenNotificationId: String?
/// Information about new notifications.
public var newNotificationsHasBeenAdded = false
/// Last status seen by the user.
2023-10-19 13:24:02 +02:00
public var lastSeenStatusId: String?
2023-10-22 10:09:02 +02:00
/// Amount of new statuses which are not displayed yet to the user.
2023-10-19 13:24:02 +02:00
public var amountOfNewStatuses = 0
/// Model for newly created comment.
2023-10-19 13:24:02 +02:00
public var newComment: CommentModel?
2023-03-18 18:40:07 +01:00
/// Active icon name.
2023-10-19 13:24:02 +02:00
public var activeIcon = "Default"
/// Tint color in whole application.
2023-10-19 13:24:02 +02:00
public var tintColor = TintColor.accentColor2
/// Application theme.
2023-10-19 13:24:02 +02:00
public var theme = Theme.system
/// Avatar shape.
2023-10-19 13:24:02 +02:00
public var avatarShape = AvatarShape.circle
/// Status id for showed interaction row.
2023-10-19 13:24:02 +02:00
public var showInteractionStatusId = ""
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when user change tabs.
2023-10-19 13:24:02 +02:00
public var hapticTabSelectionEnabled = true
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when user refresh list.
2023-10-19 13:24:02 +02:00
public var hapticRefreshEnabled = true
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when user tap button.
2023-10-19 13:24:02 +02:00
public var hapticButtonPressEnabled = true
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when animation is finished.
2023-10-19 13:24:02 +02:00
public var hapticAnimationEnabled = true
2023-03-05 09:53:06 +01:00
/// Should we fire haptic when notification occures.
2023-10-19 13:24:02 +02:00
public var hapticNotificationEnabled = true
2023-03-05 09:53:06 +01:00
/// Should sensitive photos without mask.
2023-10-19 13:24:02 +02:00
public var showSensitive = false
2023-03-08 17:43:05 +01:00
/// Should photo description for visually impaired be displayed.
2023-10-19 13:24:02 +02:00
public var showPhotoDescription = false
2023-03-11 18:30:33 +01:00
/// Status which should be shown from URL.
2023-10-19 13:24:02 +02:00
public var showStatusId: String?
2023-05-01 07:50:24 +02:00
/// Account which should be shown from URL.
2023-10-19 13:24:02 +02:00
public var showAccountId: String?
2023-05-01 07:50:24 +02:00
2023-03-25 11:37:02 +01:00
/// Updated user profile.
2023-10-19 13:24:02 +02:00
public var updatedProfile: Account?
2023-04-06 13:19:55 +02:00
/// Information which menu should be shown (top or bottom).
2023-10-19 13:24:02 +02:00
public var menuPosition = MenuPosition.top
2023-04-06 13:19:55 +02:00
/// Should avatars be visible on timelines.
2023-10-19 13:24:02 +02:00
public var showAvatarsOnTimeline = false
2023-04-14 16:50:47 +02:00
/// Should favourites be visible on timelines.
2023-10-19 13:24:02 +02:00
public var showFavouritesOnTimeline = false
2023-04-14 16:50:47 +02:00
/// Should ALT icon be visible on timelines.
2023-10-19 13:24:02 +02:00
public var showAltIconOnTimeline = false
/// Show warning about missing ALT texts on compose screen.
2023-10-19 13:24:02 +02:00
public var warnAboutMissingAlt = true
/// Show grid of photos on user profile.
2023-10-19 13:24:02 +02:00
public var showGridOnUserProfile = false
/// Show reboosted statuses on home timeline.
2023-10-19 13:24:02 +02:00
public var showReboostedStatuses = false
2023-10-10 13:30:53 +02:00
/// Hide statuses without ALT text.
2023-10-19 13:24:02 +02:00
public var hideStatusesWithoutAlt = false
2023-10-22 10:09:02 +02:00
public func changeApplicationState(accountModel: AccountModel, instance: Instance?, lastSeenStatusId: String?, lastSeenNotificationId: String?) {
self.account = accountModel
2023-10-22 10:09:02 +02:00
self.lastSeenNotificationId = lastSeenNotificationId
self.lastSeenStatusId = lastSeenStatusId
self.amountOfNewStatuses = 0
2023-10-22 10:09:02 +02:00
self.newNotificationsHasBeenAdded = false
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
2023-10-22 10:09:02 +02:00
self.lastSeenNotificationId = nil
2023-02-23 08:09:02 +01:00
self.amountOfNewStatuses = 0
2023-10-22 10:09:02 +02:00
self.newNotificationsHasBeenAdded = false
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
}