|
| 1 | +// |
| 2 | +// DisplaySettings.swift |
| 3 | +// |
| 4 | +// Copyright 2021, 2022 OpenAlloc LLC |
| 5 | +// |
| 6 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | +// file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 9 | +// |
| 10 | + |
| 11 | +import Foundation |
| 12 | + |
| 13 | +import FlowBase |
| 14 | +import AllocData |
| 15 | + |
| 16 | +public enum ReturnsExtent: Int, CaseIterable, Codable { |
| 17 | + case positiveOnly |
| 18 | + case all |
| 19 | + case negativeOnly |
| 20 | + |
| 21 | + public static let _default: ReturnsExtent = .all |
| 22 | +} |
| 23 | + |
| 24 | +public enum ReturnsGrouping: Int, CaseIterable, Codable { |
| 25 | + case assets |
| 26 | + case accounts |
| 27 | + case strategies |
| 28 | + |
| 29 | + public static let _default: ReturnsGrouping = .assets |
| 30 | +} |
| 31 | + |
| 32 | +public enum ReturnsColor: Int, CaseIterable, Codable { |
| 33 | + case color |
| 34 | + case mono |
| 35 | + |
| 36 | + public static let _default: ReturnsColor = .color |
| 37 | +} |
| 38 | + |
| 39 | + |
| 40 | +public enum PeriodSummarySelection: Int, CaseIterable, Codable { |
| 41 | + case deltaMarketValue |
| 42 | + case deltaTotalBasis |
| 43 | + case modifiedDietz |
| 44 | + |
| 45 | + public var isDelta: Bool { self == .deltaMarketValue || self == .deltaTotalBasis } |
| 46 | + public var isDietz: Bool { self == .modifiedDietz } |
| 47 | + |
| 48 | + public static let _default: PeriodSummarySelection = .deltaMarketValue |
| 49 | +} |
| 50 | + |
| 51 | +public enum TabsPrimaryReturns: Int, CaseIterable, Codable { |
| 52 | + case chart |
| 53 | + case assets |
| 54 | + case accounts |
| 55 | + case strategies |
| 56 | + case forecast |
| 57 | + |
| 58 | + public static let defaultTab = TabsPrimaryReturns.chart |
| 59 | + public static let storageKey = "PrimaryReturnsTab" |
| 60 | +} |
| 61 | + |
| 62 | +public enum TabsSecondaryReturns: Int, CaseIterable, Codable { |
| 63 | + case assets |
| 64 | + case accounts |
| 65 | + case delta |
| 66 | + case dietz |
| 67 | + case forecast |
| 68 | + |
| 69 | + public static let defaultTab = TabsSecondaryReturns.assets |
| 70 | + public static let storageKey = "SecondaryReturnsTab" |
| 71 | +} |
| 72 | + |
| 73 | +public enum TabsPositionsBuilder: Int, CaseIterable, Codable { |
| 74 | + case holdings |
| 75 | + case positions |
| 76 | + case previousPositions |
| 77 | + |
| 78 | + public static let defaultTab = TabsPositionsBuilder.positions |
| 79 | + public static let storageKey = "PositionsBuilderTab" |
| 80 | +} |
| 81 | + |
| 82 | +public enum TabsCashflowBuilder: Int, CaseIterable, Codable { |
| 83 | + case transactions |
| 84 | + case nuCashflow |
| 85 | + case prevCashflow |
| 86 | + |
| 87 | + public static let defaultTab = TabsCashflowBuilder.nuCashflow |
| 88 | + public static let storageKey = "CashflowBuilderTab" |
| 89 | +} |
| 90 | + |
| 91 | +public enum TabsSummaryBuilder: Int, CaseIterable, Codable { |
| 92 | + case assets |
| 93 | + case accounts |
| 94 | + case strategies |
| 95 | + |
| 96 | + public static let defaultTab = TabsSummaryBuilder.assets |
| 97 | + public static let storageKey = "SummaryBuilderTab" |
| 98 | +} |
| 99 | + |
| 100 | +// settings not requiring a context-reset |
| 101 | +public struct DisplaySettings: Equatable, Codable { |
| 102 | + public var activeSidebarMenuKey: String? |
| 103 | + public var returnsExpandBottom: Bool |
| 104 | + public var showSecondary: Bool |
| 105 | + public var showChartLegend: Bool |
| 106 | + public var excludedAssetMap: [AssetKey: Bool] |
| 107 | + public var excludedAccountMap: [AccountKey: Bool] |
| 108 | + public var begSnapshotKey: SnapshotKey |
| 109 | + public var endSnapshotKey: SnapshotKey |
| 110 | + public var orderedAssetKeys: [AssetKey] |
| 111 | + public var returnsGrouping: ReturnsGrouping |
| 112 | + public var returnsColor: ReturnsColor |
| 113 | + public var returnsExtent: ReturnsExtent |
| 114 | + public var periodSummarySelection: PeriodSummarySelection |
| 115 | + public var builderCapturedAt: Date |
| 116 | + public var pendingExcludedTxnMap: [TransactionKey: Bool] |
| 117 | + public var snapshotSummaryKey: SnapshotKey |
| 118 | + public var primaryReturnsTab: TabsPrimaryReturns |
| 119 | + public var secondaryReturnsTab: TabsSecondaryReturns |
| 120 | + public var builderPositionsTab: TabsPositionsBuilder |
| 121 | + public var builderCashflowTab: TabsCashflowBuilder |
| 122 | + public var builderSummaryTab: TabsSummaryBuilder |
| 123 | + |
| 124 | + public enum CodingKeys: String, CodingKey, CaseIterable { |
| 125 | + case activeSidebarMenuKey |
| 126 | + case returnsExpandBottom |
| 127 | + case showSecondary |
| 128 | + case showChartLegend |
| 129 | + case excludedAssetMap |
| 130 | + case excludedAccountMap |
| 131 | + case begSnapshotKey |
| 132 | + case endSnapshotKey |
| 133 | + case orderedAssetKeys |
| 134 | + case returnsGrouping |
| 135 | + case returnsColor |
| 136 | + case returnsExtent |
| 137 | + case periodSummarySelection |
| 138 | + case builderCapturedAt |
| 139 | + case pendingExcludedTxnMap |
| 140 | + case snapshotSummaryKey |
| 141 | + case primaryReturnsTab |
| 142 | + case secondaryReturnsTab |
| 143 | + case builderPositionsTab |
| 144 | + case builderCashflowTab |
| 145 | + case builderSummaryTab |
| 146 | + } |
| 147 | + |
| 148 | + public init() { |
| 149 | + activeSidebarMenuKey = "" |
| 150 | + returnsExpandBottom = false |
| 151 | + showSecondary = false |
| 152 | + showChartLegend = false |
| 153 | + excludedAssetMap = [:] |
| 154 | + excludedAccountMap = [:] |
| 155 | + begSnapshotKey = MValuationSnapshot.Key.empty |
| 156 | + endSnapshotKey = MValuationSnapshot.Key.empty |
| 157 | + orderedAssetKeys = [] |
| 158 | + returnsGrouping = ReturnsGrouping._default |
| 159 | + returnsColor = ReturnsColor._default |
| 160 | + returnsExtent = ReturnsExtent._default |
| 161 | + periodSummarySelection = PeriodSummarySelection._default |
| 162 | + builderCapturedAt = Date.init(timeIntervalSinceReferenceDate: 0) |
| 163 | + pendingExcludedTxnMap = [:] |
| 164 | + snapshotSummaryKey = MValuationSnapshot.Key.empty |
| 165 | + primaryReturnsTab = .defaultTab |
| 166 | + secondaryReturnsTab = .defaultTab |
| 167 | + builderPositionsTab = .defaultTab |
| 168 | + builderCashflowTab = .defaultTab |
| 169 | + builderSummaryTab = .defaultTab |
| 170 | + } |
| 171 | + |
| 172 | + public init(from decoder: Decoder) throws { |
| 173 | + let c = try decoder.container(keyedBy: CodingKeys.self) |
| 174 | + activeSidebarMenuKey = try c.decodeIfPresent(String.self, forKey: .activeSidebarMenuKey) ?? "" |
| 175 | + returnsExpandBottom = try c.decodeIfPresent(Bool.self, forKey: .returnsExpandBottom) ?? false |
| 176 | + showSecondary = try c.decodeIfPresent(Bool.self, forKey: .showSecondary) ?? false |
| 177 | + showChartLegend = try c.decodeIfPresent(Bool.self, forKey: .showChartLegend) ?? false |
| 178 | + excludedAssetMap = try c.decodeIfPresent([AssetKey: Bool].self, forKey: .excludedAssetMap) ?? [:] |
| 179 | + excludedAccountMap = try c.decodeIfPresent([AccountKey: Bool].self, forKey: .excludedAccountMap) ?? [:] |
| 180 | + |
| 181 | + begSnapshotKey = try c.decodeIfPresent(SnapshotKey.self, forKey: .begSnapshotKey) ?? MValuationSnapshot.Key.empty |
| 182 | + endSnapshotKey = try c.decodeIfPresent(SnapshotKey.self, forKey: .endSnapshotKey) ?? MValuationSnapshot.Key.empty |
| 183 | + |
| 184 | + orderedAssetKeys = try c.decodeIfPresent([AssetKey].self, forKey: .orderedAssetKeys) ?? [] |
| 185 | + |
| 186 | + returnsGrouping = try c.decodeIfPresent(ReturnsGrouping.self, forKey: .returnsGrouping) ?? ReturnsGrouping._default |
| 187 | + returnsColor = try c.decodeIfPresent(ReturnsColor.self, forKey: .returnsColor) ?? ReturnsColor._default |
| 188 | + returnsExtent = try c.decodeIfPresent(ReturnsExtent.self, forKey: .returnsExtent) ?? ReturnsExtent._default |
| 189 | + periodSummarySelection = try c.decodeIfPresent(PeriodSummarySelection.self, forKey: .periodSummarySelection) ?? PeriodSummarySelection._default |
| 190 | + |
| 191 | + builderCapturedAt = try c.decodeIfPresent(Date.self, forKey: .builderCapturedAt) ?? Date.init(timeIntervalSinceReferenceDate: 0) |
| 192 | + |
| 193 | + pendingExcludedTxnMap = try c.decodeIfPresent([TransactionKey: Bool].self, forKey: .pendingExcludedTxnMap) ?? [:] |
| 194 | + |
| 195 | + snapshotSummaryKey = try c.decodeIfPresent(MValuationSnapshot.Key.self, forKey: .snapshotSummaryKey) ?? MValuationSnapshot.Key.empty |
| 196 | + |
| 197 | + primaryReturnsTab = try c.decodeIfPresent(TabsPrimaryReturns.self, forKey: .primaryReturnsTab) ?? .defaultTab |
| 198 | + secondaryReturnsTab = try c.decodeIfPresent(TabsSecondaryReturns.self, forKey: .secondaryReturnsTab) ?? .defaultTab |
| 199 | + |
| 200 | + builderPositionsTab = try c.decodeIfPresent(TabsPositionsBuilder.self, forKey: .builderPositionsTab) ?? .defaultTab |
| 201 | + builderCashflowTab = try c.decodeIfPresent(TabsCashflowBuilder.self, forKey: .builderCashflowTab) ?? .defaultTab |
| 202 | + builderSummaryTab = try c.decodeIfPresent(TabsSummaryBuilder.self, forKey: .builderSummaryTab) ?? .defaultTab |
| 203 | + } |
| 204 | +} |
0 commit comments