diff --git a/DuckDuckGo.xcodeproj/project.pbxproj b/DuckDuckGo.xcodeproj/project.pbxproj index bfcd8d6845..a7c05f8715 100644 --- a/DuckDuckGo.xcodeproj/project.pbxproj +++ b/DuckDuckGo.xcodeproj/project.pbxproj @@ -333,7 +333,8 @@ 6FD8E5202C5BA23200345670 /* NewTabPageModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FD8E51F2C5BA23200345670 /* NewTabPageModel.swift */; }; 6FD8E5222C5BA5C400345670 /* NewTabPageIntroMessageSetup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FD8E5212C5BA5C400345670 /* NewTabPageIntroMessageSetup.swift */; }; 6FDA1FB32B59584400AC962A /* AddressDisplayHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDA1FB22B59584400AC962A /* AddressDisplayHelper.swift */; }; - 6FDC63FF2C906DD300DB71B3 /* NewTabPageStorage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDC63FE2C906DD300DB71B3 /* NewTabPageStorage.swift */; }; + 6FDC64012C92F4A300DB71B3 /* NewTabPageIntroDataStoring.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDC64002C92F4A300DB71B3 /* NewTabPageIntroDataStoring.swift */; }; + 6FDC64032C92F4D600DB71B3 /* NewTabPageSettingsPersistentStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FDC64022C92F4D600DB71B3 /* NewTabPageSettingsPersistentStore.swift */; }; 6FE018402C25CB3F001F680D /* FavoritesSectionHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE0183F2C25CB3F001F680D /* FavoritesSectionHeader.swift */; }; 6FE095D82BD90AFB00490FF8 /* UniversalOmniBarState.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE095D72BD90AFB00490FF8 /* UniversalOmniBarState.swift */; }; 6FE127382C20492500EB5724 /* NewTabPage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6FE127372C20492500EB5724 /* NewTabPage.swift */; }; @@ -1591,7 +1592,8 @@ 6FD8E51F2C5BA23200345670 /* NewTabPageModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageModel.swift; sourceTree = ""; }; 6FD8E5212C5BA5C400345670 /* NewTabPageIntroMessageSetup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageIntroMessageSetup.swift; sourceTree = ""; }; 6FDA1FB22B59584400AC962A /* AddressDisplayHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddressDisplayHelper.swift; sourceTree = ""; }; - 6FDC63FE2C906DD300DB71B3 /* NewTabPageStorage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageStorage.swift; sourceTree = ""; }; + 6FDC64002C92F4A300DB71B3 /* NewTabPageIntroDataStoring.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageIntroDataStoring.swift; sourceTree = ""; }; + 6FDC64022C92F4D600DB71B3 /* NewTabPageSettingsPersistentStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPageSettingsPersistentStore.swift; sourceTree = ""; }; 6FE0183F2C25CB3F001F680D /* FavoritesSectionHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FavoritesSectionHeader.swift; sourceTree = ""; }; 6FE095D72BD90AFB00490FF8 /* UniversalOmniBarState.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UniversalOmniBarState.swift; sourceTree = ""; }; 6FE127372C20492500EB5724 /* NewTabPage.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NewTabPage.swift; sourceTree = ""; }; @@ -3803,6 +3805,7 @@ 6F5345AE2C53F2DE00424A43 /* NewTabPageSettingsPersistentStorage.swift */, 6F9FFE252C579BCD00A238BE /* NewTabPageShortcutsSettingsStorage.swift */, 6F9FFE272C579DEA00A238BE /* NewTabPageSectionsSettingsStorage.swift */, + 6FDC64022C92F4D600DB71B3 /* NewTabPageSettingsPersistentStore.swift */, ); name = Storage; sourceTree = ""; @@ -3871,22 +3874,14 @@ children = ( 6FD8E51D2C5B84DE00345670 /* NewTabPageIntroMessageView.swift */, 6FD8E5212C5BA5C400345670 /* NewTabPageIntroMessageSetup.swift */, + 6FDC64002C92F4A300DB71B3 /* NewTabPageIntroDataStoring.swift */, ); name = IntroMessage; sourceTree = ""; }; - 6FDC63FD2C906DBB00DB71B3 /* Storage */ = { - isa = PBXGroup; - children = ( - 6FDC63FE2C906DD300DB71B3 /* NewTabPageStorage.swift */, - ); - name = Storage; - sourceTree = ""; - }; 6FE127362C20436A00EB5724 /* HomeRedesign */ = { isa = PBXGroup; children = ( - 6FDC63FD2C906DBB00DB71B3 /* Storage */, 6FE1273B2C204C0D00EB5724 /* Subviews */, 6F03CAF82C32C3AA004179A8 /* Messages */, 6FE127372C20492500EB5724 /* NewTabPage.swift */, @@ -7241,6 +7236,7 @@ F4E1936625AF722F001D2666 /* HighlightCutOutView.swift in Sources */, 6FB2A67C2C2D9DF0004D20C8 /* FavoritesEmptyStateView.swift in Sources */, 1E162605296840D80004127F /* Triangle.swift in Sources */, + 6FDC64012C92F4A300DB71B3 /* NewTabPageIntroDataStoring.swift in Sources */, B609D5522862EAFF0088CAC2 /* InlineWKDownloadDelegate.swift in Sources */, BDFF03222BA3D8E200F324C9 /* NetworkProtectionFeatureVisibility.swift in Sources */, B652DEFD287BE67400C12A9C /* UserScripts.swift in Sources */, @@ -7322,6 +7318,7 @@ 6FB2A67A2C2C5BAE004D20C8 /* FavoriteEmptyStateItem.swift in Sources */, 6FBF0F8B2BD7C0A900136CF0 /* AllProtectedCell.swift in Sources */, 9F4CC5242C4A4F0D006A96EB /* SwiftUITestUtilities.swift in Sources */, + 6FDC64032C92F4D600DB71B3 /* NewTabPageSettingsPersistentStore.swift in Sources */, 1E4F4A5A297193DE00625985 /* MainViewController+CookiesManaged.swift in Sources */, C12324C32C4697C900FBB26B /* AutofillBreakageReportTableViewCell.swift in Sources */, 8586A10D24CBA7070049720E /* FindInPageActivity.swift in Sources */, @@ -7422,7 +7419,6 @@ 85F2FFCF2211F8E5006BB258 /* TabSwitcherViewController+KeyCommands.swift in Sources */, 3157B43327F497E90042D3D7 /* SaveLoginView.swift in Sources */, F17922E01E71BB59006E3D97 /* AutocompleteViewControllerDelegate.swift in Sources */, - 6FDC63FF2C906DD300DB71B3 /* NewTabPageStorage.swift in Sources */, BDE91CDC2C62AA3A0005CB74 /* DefaultMetadataCollector.swift in Sources */, D664C7C82B289AA200CBFA76 /* SubscriptionFlowView.swift in Sources */, EE458D142ABB652900FC651A /* NetworkProtectionDebugUtilities.swift in Sources */, diff --git a/DuckDuckGo/NewTabPageStorage.swift b/DuckDuckGo/NewTabPageIntroDataStoring.swift similarity index 69% rename from DuckDuckGo/NewTabPageStorage.swift rename to DuckDuckGo/NewTabPageIntroDataStoring.swift index 2db10a3943..af42baa75f 100644 --- a/DuckDuckGo/NewTabPageStorage.swift +++ b/DuckDuckGo/NewTabPageIntroDataStoring.swift @@ -1,5 +1,5 @@ // -// NewTabPageStorage.swift +// NewTabPageIntroDataStoring.swift // DuckDuckGo // // Copyright © 2024 DuckDuckGo. All rights reserved. @@ -25,11 +25,6 @@ protocol NewTabPageIntroDataStoring: AnyObject { var newTabPageIntroMessageSeenCount: Int { get set } } -protocol NewTabPageSettingsDataStoring: AnyObject { - var newTabPageShortcutsSettings: Data? { get set } - var newTabPageSectionsSettings: Data? { get set } -} - final class NewTabPageIntroDataUserDefaultsStorage: NewTabPageIntroDataStoring { @UserDefaultsWrapper(key: .newTabPageIntroMessageEnabled, defaultValue: nil) var newTabPageIntroMessageEnabled: Bool? @@ -37,11 +32,3 @@ final class NewTabPageIntroDataUserDefaultsStorage: NewTabPageIntroDataStoring { @UserDefaultsWrapper(key: .newTabPageIntroMessageSeenCount, defaultValue: 0) var newTabPageIntroMessageSeenCount: Int } - -final class NewTabPageSettingsDataUserDefaultsStorage: NewTabPageSettingsDataStoring { - @UserDefaultsWrapper(key: .newTabPageShortcutsSettings, defaultValue: nil) - var newTabPageShortcutsSettings: Data? - - @UserDefaultsWrapper(key: .newTabPageSectionsSettings, defaultValue: nil) - var newTabPageSectionsSettings: Data? -} diff --git a/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift b/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift index 94d4a42aac..12195cff4b 100644 --- a/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift +++ b/DuckDuckGo/NewTabPageSectionsSettingsStorage.swift @@ -26,10 +26,11 @@ enum NewTabPageSection: String, Codable, CaseIterable { typealias NewTabPageSectionsSettingsStorage = NewTabPageSettingsPersistentStorage + + extension NewTabPageSettingsPersistentStorage { convenience init() { - self.init(persistence: NewTabPageSettingsDataUserDefaultsStorage(), - keyPath: \.newTabPageSectionsSettings, + self.init(persistentStore: NewTabPageSectionsSettingsStore(), defaultOrder: NewTabPageSection.allCases, defaultEnabledItems: NewTabPageSection.allCases) } diff --git a/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift b/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift index 48d249c124..c9b7bb3298 100644 --- a/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift +++ b/DuckDuckGo/NewTabPageSettingsPersistentStorage.swift @@ -24,19 +24,20 @@ private struct NewTabPageItemSettings: Coda let enabledItems: Set } +protocol NewTabPageSettingsPersistentStore: AnyObject { + var data: Data? { get set } +} + final class NewTabPageSettingsPersistentStorage: NewTabPageSettingsStorage { private(set) var itemsOrder: [Item] private var enabledItems: Set - private var persistence: NewTabPageSettingsDataStoring - private let keyPath: WritableKeyPath + private var persistentStore: any NewTabPageSettingsPersistentStore - init(persistence: NewTabPageSettingsDataStoring, - keyPath: WritableKeyPath, + init(persistentStore: NewTabPageSettingsPersistentStore, defaultOrder: [Item], defaultEnabledItems: [Item]) { - self.persistence = persistence - self.keyPath = keyPath + self.persistentStore = persistentStore self.itemsOrder = defaultOrder self.enabledItems = Set(defaultEnabledItems) @@ -62,12 +63,12 @@ final class NewTabPageSettingsPersistentStorage.self, from: settingsData) { itemsOrder = settings.itemsOrder enabledItems = settings.enabledItems diff --git a/DuckDuckGo/NewTabPageSettingsPersistentStore.swift b/DuckDuckGo/NewTabPageSettingsPersistentStore.swift new file mode 100644 index 0000000000..fa3a12540f --- /dev/null +++ b/DuckDuckGo/NewTabPageSettingsPersistentStore.swift @@ -0,0 +1,31 @@ +// +// NewTabPageSettingsPersistentStore.swift +// DuckDuckGo +// +// Copyright © 2024 DuckDuckGo. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +// + +import Foundation +import Core + +final class NewTabPageShorctutsSettingsStore: NewTabPageSettingsPersistentStore { + @UserDefaultsWrapper(key: .newTabPageShortcutsSettings, defaultValue: nil) + var data: Data? +} + +final class NewTabPageSectionsSettingsStore: NewTabPageSettingsPersistentStore { + @UserDefaultsWrapper(key: .newTabPageSectionsSettings, defaultValue: nil) + var data: Data? +} diff --git a/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift b/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift index 7bdcb52710..23379a7eb1 100644 --- a/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift +++ b/DuckDuckGo/NewTabPageShortcutsSettingsStorage.swift @@ -23,8 +23,7 @@ typealias NewTabPageShortcutsSettingsStorage = NewTabPageSettingsPersistentStora extension NewTabPageSettingsPersistentStorage { convenience init() { - self.init(persistence: NewTabPageSettingsDataUserDefaultsStorage(), - keyPath: \.newTabPageShortcutsSettings, + self.init(persistentStore: NewTabPageShorctutsSettingsStore(), defaultOrder: NewTabPageShortcut.allCases, defaultEnabledItems: NewTabPageShortcut.allCases) } diff --git a/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift b/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift index 5837f39c63..bcc4b14177 100644 --- a/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift +++ b/DuckDuckGoTests/NewTabPageSectionsSettingsModelTests.swift @@ -60,8 +60,7 @@ final class NewTabPageSectionsSettingsModelTests: XCTestCase { private func createSUT() -> NewTabPageSectionsSettingsModel { let storage = NewTabPageSectionsSettingsStorage( - persistence: NewTabPageSettingsDataStoringMock(), - keyPath: \.newTabPageSectionsSettings, + persistentStore: NewTabPageSettingsPersistentStoreMock(), defaultOrder: NewTabPageSection.allCases, defaultEnabledItems: NewTabPageSection.allCases ) @@ -70,7 +69,6 @@ final class NewTabPageSectionsSettingsModelTests: XCTestCase { } } -final class NewTabPageSettingsDataStoringMock: NewTabPageSettingsDataStoring { - var newTabPageSectionsSettings: Data? - var newTabPageShortcutsSettings: Data? +final class NewTabPageSettingsPersistentStoreMock: NewTabPageSettingsPersistentStore { + var data: Data? } diff --git a/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift b/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift index 6333f15784..a54ba48fe4 100644 --- a/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift +++ b/DuckDuckGoTests/NewTabPageSettingsPersistentStorageTests.swift @@ -23,7 +23,7 @@ import XCTest final class NewTabPageSettingsPersistentStorageTests: XCTestCase { - private var settingsDataStorage = NewTabPageSettingsDataStoringMock() + private var settingsPersistentStore = NewTabPageSettingsPersistentStoreMock() func testLoadsInitialStateFromDefaults() { let sut = createSUT() @@ -35,7 +35,7 @@ final class NewTabPageSettingsPersistentStorageTests: XCTestCase { func testUsesDefaultsIfDataCorrupted() { let sut = createSUT() - settingsDataStorage[keyPath: Constant.keyPath] = "Random data".data(using: .utf8) + settingsPersistentStore.data = "Random data".data(using: .utf8) XCTAssertEqual(sut.itemsOrder, Constant.defaultItems) XCTAssertEqual(sut.enabledItems, Constant.defaultItems) @@ -90,15 +90,13 @@ final class NewTabPageSettingsPersistentStorageTests: XCTestCase { } private func createSUT(defaultOrder: [StorageItem] = Constant.defaultItems, defaultEnabledItems: [StorageItem] = Constant.defaultItems) -> NewTabPageSettingsPersistentStorage { - NewTabPageSettingsPersistentStorage(persistence: settingsDataStorage, - keyPath: Constant.keyPath, + NewTabPageSettingsPersistentStorage(persistentStore: settingsPersistentStore, defaultOrder: defaultOrder, defaultEnabledItems: defaultEnabledItems) } private enum Constant { static let defaultItems = [StorageItem.one, .two, .three] - static let keyPath = \NewTabPageSettingsDataStoring.newTabPageSectionsSettings } } diff --git a/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift b/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift index 78a775446c..012f2b3b23 100644 --- a/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift +++ b/DuckDuckGoTests/NewTabPageShortcutsSettingsModelTests.swift @@ -54,8 +54,7 @@ final class NewTabPageShortcutsSettingsModelTests: XCTestCase { private func createSUT() -> NewTabPageShortcutsSettingsModel { let storage = NewTabPageShortcutsSettingsStorage( - persistence: NewTabPageSettingsDataStoringMock(), - keyPath: \.newTabPageShortcutsSettings, + persistentStore: NewTabPageSettingsPersistentStoreMock(), defaultOrder: NewTabPageShortcut.allCases, defaultEnabledItems: NewTabPageShortcut.allCases )