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
|
|
|
//
|
2023-04-01 12:10:59 +02: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
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Class with default variables.
|
|
|
|
private class Defaults {
|
2023-02-22 14:33:03 +01:00
|
|
|
let statusMaxCharacters = 500
|
|
|
|
let statusMaxMediaAttachments = 4
|
2023-02-22 13:26:35 +01:00
|
|
|
let statusCharactersReservedPerUrl = 23
|
|
|
|
}
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Default variables.
|
|
|
|
private static let defaults = Defaults()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Actual signed in account.
|
|
|
|
@Published private(set) var account: AccountModel?
|
2023-01-31 12:20:49 +01:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// The maximum number of allowed characters per status.
|
|
|
|
@Published private(set) var statusMaxCharacters = defaults.statusMaxCharacters
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// The maximum number of media attachments that can be added to a status.
|
|
|
|
@Published private(set) var statusMaxMediaAttachments = defaults.statusMaxMediaAttachments
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Each URL in a status will be assumed to be exactly this many characters.
|
|
|
|
@Published private(set) var statusCharactersReservedPerUrl = defaults.statusCharactersReservedPerUrl
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Last status seen by the user.
|
2023-01-31 12:20:49 +01:00
|
|
|
@Published var lastSeenStatusId: String?
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Amount of new statuses which are not displayed yet to the user.
|
2023-02-08 20:18:03 +01:00
|
|
|
@Published var amountOfNewStatuses = 0
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Model for newly created comment.
|
2023-02-09 18:58:54 +01:00
|
|
|
@Published var newComment: CommentModel?
|
2023-03-18 18:40:07 +01:00
|
|
|
|
|
|
|
/// Active icon name.
|
|
|
|
@Published var activeIcon = "Default"
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Tint color in whole application.
|
2023-01-12 18:34:48 +01:00
|
|
|
@Published var tintColor = TintColor.accentColor2
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Application theme.
|
2023-01-13 13:37:01 +01:00
|
|
|
@Published var theme = Theme.system
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Avatar shape.
|
2023-01-24 12:22:53 +01:00
|
|
|
@Published var avatarShape = AvatarShape.circle
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
/// Status id for showed interaction row.
|
2023-01-14 08:52:51 +01:00
|
|
|
@Published var showInteractionStatusId = String.empty()
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-05 09:53:06 +01:00
|
|
|
/// Should we fire haptic when user change tabs.
|
|
|
|
@Published var hapticTabSelectionEnabled = true
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-05 09:53:06 +01:00
|
|
|
/// Should we fire haptic when user refresh list.
|
|
|
|
@Published var hapticRefreshEnabled = true
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-05 09:53:06 +01:00
|
|
|
/// Should we fire haptic when user tap button.
|
|
|
|
@Published var hapticButtonPressEnabled = true
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-03-08 17:43:05 +01:00
|
|
|
/// Should photo description for visually impaired be displayed.
|
|
|
|
@Published var showPhotoDescription = false
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-03-11 18:30:33 +01:00
|
|
|
/// Status which should be shown from URL.
|
2023-04-01 12:10:59 +02:00
|
|
|
@Published var showStatusId: String?
|
|
|
|
|
2023-03-25 11:37:02 +01:00
|
|
|
/// Updated user profile.
|
|
|
|
@Published var updatedProfile: Account?
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-04-06 13:19:55 +02:00
|
|
|
/// Information which menu should be shown (top or bottom).
|
|
|
|
@Published var menuPosition = MenuPosition.top
|
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
public func changeApplicationState(accountModel: AccountModel, instance: Instance?, lastSeenStatusId: String?) {
|
|
|
|
self.account = accountModel
|
|
|
|
self.lastSeenStatusId = lastSeenStatusId
|
|
|
|
self.amountOfNewStatuses = 0
|
2023-04-01 12:10:59 +02:00
|
|
|
|
2023-02-22 13:26:35 +01:00
|
|
|
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-04-01 12:10:59 +02:00
|
|
|
|
2023-02-23 08:09:02 +01:00
|
|
|
public func clearApplicationState() {
|
|
|
|
self.account = nil
|
|
|
|
self.lastSeenStatusId = nil
|
|
|
|
self.amountOfNewStatuses = 0
|
2023-04-01 12:10:59 +02:00
|
|
|
|
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
|
|
|
}
|