From 4e7fb31e978040ee96d9ca1f91c449a5a8392f2d Mon Sep 17 00:00:00 2001 From: Alexey Martemyanov Date: Sun, 17 Dec 2023 15:52:53 +0600 Subject: [PATCH] minor fixes --- .../DataImport/Model/DataImportViewModel.swift | 8 +++++--- .../DataImport/View/DataImportSourcePicker.swift | 7 ++++--- DuckDuckGo/DataImport/View/DataImportView.swift | 13 +++++++------ 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/DuckDuckGo/DataImport/Model/DataImportViewModel.swift b/DuckDuckGo/DataImport/Model/DataImportViewModel.swift index bd0da1a6b4..34c5dcc9cb 100644 --- a/DuckDuckGo/DataImport/Model/DataImportViewModel.swift +++ b/DuckDuckGo/DataImport/Model/DataImportViewModel.swift @@ -32,6 +32,7 @@ struct DataImportViewModel { @UserDefaultsWrapper(key: .homePageContinueSetUpImport, defaultValue: nil) var successfulImportHappened: Bool? + let availableImportSources: [DataImport.Source] /// Browser to import data from let importSource: Source /// BrowserProfileList loader (factory method) - used @@ -128,7 +129,7 @@ struct DataImportViewModel { init(importSource: Source? = nil, screen: Screen? = nil, - availableImportSources: @autoclosure () -> Set = Set(ThirdPartyBrowser.installedBrowsers.map(\.importSource)), + availableImportSources: [DataImport.Source] = ThirdPartyBrowser.installedBrowsers.map(\.importSource), preferredImportSources: [Source] = [.chrome, .firefox, .safari], summary: [DataTypeImportResult] = [], loadProfiles: @escaping (ThirdPartyBrowser) -> BrowserProfileList = { $0.browserProfiles() }, @@ -137,7 +138,7 @@ struct DataImportViewModel { openPanelCallback: @escaping @MainActor (DataType) -> URL? = Self.openPanelCallback, reportSenderFactory: @escaping ReportSenderFactory = { FeedbackSender().sendDataImportReport }) { - lazy var availableImportSources = availableImportSources() + self.availableImportSources = availableImportSources let importSource = importSource ?? preferredImportSources.first(where: { availableImportSources.contains($0) }) ?? .csv self.importSource = importSource @@ -459,7 +460,7 @@ extension DataImportViewModel { for dataType in (currentDataType.map { DataType.dataTypes(after: $0) } ?? DataType.allCases[0...]) where selectedDataTypes.contains(dataType) { // if some of selected data types failed to import or not imported yet switch summary.last(where: { $0.dataType == dataType })?.result { - case .success(let summary) where summary.successful == 0 && summary.duplicate == 0 && summary.failed == 0: + case .success(let summary) where summary.isEmpty: return .fileImport(dataType: dataType) case .failure(let error) where error.errorType == .noData: return .fileImport(dataType: dataType) @@ -680,6 +681,7 @@ extension DataImportViewModel { } } + log("dismiss") dismiss() if case .xcPreviews = NSApp.runType { self.update(with: importSource) // reset diff --git a/DuckDuckGo/DataImport/View/DataImportSourcePicker.swift b/DuckDuckGo/DataImport/View/DataImportSourcePicker.swift index b2c2c03c08..3f94ffe2a0 100644 --- a/DuckDuckGo/DataImport/View/DataImportSourcePicker.swift +++ b/DuckDuckGo/DataImport/View/DataImportSourcePicker.swift @@ -29,9 +29,10 @@ struct DataImportSourcePicker: View { viewModel.importSources } - init(selectedSource: DataImport.Source, + init(importSources: [DataImport.Source], + selectedSource: DataImport.Source, onSelectedSourceChanged: @escaping (DataImport.Source) -> Void) { - self.viewModel = DataImportSourceViewModel(selectedSource: selectedSource) + self.viewModel = DataImportSourceViewModel(importSources: importSources, selectedSource: selectedSource) self.onSelectedSourceChanged = onSelectedSourceChanged } @@ -61,7 +62,7 @@ struct DataImportSourcePicker: View { } #Preview { - DataImportSourcePicker(selectedSource: .csv) { + DataImportSourcePicker(importSources: DataImport.Source.allCases, selectedSource: .csv) { print("selection:", $0) } .padding() diff --git a/DuckDuckGo/DataImport/View/DataImportView.swift b/DuckDuckGo/DataImport/View/DataImportView.swift index 45682a2981..9480a224c4 100644 --- a/DuckDuckGo/DataImport/View/DataImportView.swift +++ b/DuckDuckGo/DataImport/View/DataImportView.swift @@ -197,15 +197,16 @@ struct DataImportView: View { HStack(spacing: 8) { Spacer() - ForEach(model.buttons, id: \.self) { button in + ForEach(model.buttons.indices, id: \.self) { idx in Button { - model.performAction(for: button, dismiss: dismiss.callAsFunction) + model.performAction(for: model.buttons[idx], + dismiss: dismiss.callAsFunction) } label: { - Text(button.title(dataType: model.screen.fileImportDataType)) + Text(model.buttons[idx].title(dataType: model.screen.fileImportDataType)) .frame(minWidth: 80 - 16 - 1) } - .keyboardShortcut(button.shortcut) - .disabled(button.isDisabled) + .keyboardShortcut(model.buttons[idx].shortcut) + .disabled(model.buttons[idx].isDisabled) } } } @@ -500,7 +501,7 @@ extension DataImportViewModel.ButtonType { } } - let viewModel = DataImportViewModel(importSource: .bookmarksHTML) { browser in + let viewModel = DataImportViewModel(importSource: .bookmarksHTML, availableImportSources: DataImport.Source.allCases) { browser in guard case .chrome = browser else { print("empty profiles") return .init(browser: browser, profiles: [])