Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show dialog to bookmark all tabs #2621

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion DuckDuckGo/Bookmarks/Model/BookmarkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
func move(objectUUIDs: [String], toIndex: Int?, withinParentFolder: ParentFolderType, completion: @escaping (Error?) -> Void)
func moveFavorites(with objectUUIDs: [String], toIndex: Int?, completion: @escaping (Error?) -> Void)
func importBookmarks(_ bookmarks: ImportedBookmarks, source: BookmarkImportSource) -> BookmarksImportSummary

func bookmarkAll(websitesInfo: [WebsiteInfo], withinParentFolder parent: ParentFolderType)
func handleFavoritesAfterDisablingSync()

// Wrapper definition in a protocol is not supported yet
Expand Down Expand Up @@ -348,6 +348,11 @@
return results
}

func bookmarkAll(websitesInfo: [WebsiteInfo], withinParentFolder parent: ParentFolderType) {
// TODO: https://app.asana.com/0/0/1207032959154802/f

Check failure on line 352 in DuckDuckGo/Bookmarks/Model/BookmarkManager.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

TODOs should be resolved (https://app.asana.com/0/0/1207...) (todo)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess technically you can remove this? it does its duty

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put had a TODO because I still have to call sync, reload the bookmarks and write tests so I won’t forget it.

bookmarkStore.bookmarkAll(websitesInfo: websitesInfo, withinParentFolder: parent)
}

// MARK: - Sync

func handleFavoritesAfterDisablingSync() {
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Bookmarks/Model/WebsiteInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import Foundation

struct WebsiteInfo {
struct WebsiteInfo: Equatable {
let url: URL
let title: String?

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Bookmarks/Services/BookmarkStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ protocol BookmarkStore {
func move(objectUUIDs: [String], toIndex: Int?, withinParentFolder: ParentFolderType, completion: @escaping (Error?) -> Void)
func moveFavorites(with objectUUIDs: [String], toIndex: Int?, completion: @escaping (Error?) -> Void)
func importBookmarks(_ bookmarks: ImportedBookmarks, source: BookmarkImportSource) -> BookmarksImportSummary

func bookmarkAll(websitesInfo: [WebsiteInfo], withinParentFolder parent: ParentFolderType)
func handleFavoritesAfterDisablingSync()
}
8 changes: 8 additions & 0 deletions DuckDuckGo/Bookmarks/Services/BookmarkStoreMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ public final class BookmarkStoreMock: BookmarkStore {
return BookmarksImportSummary(successful: 0, duplicates: 0, failed: 0)
}

var bookmarkAllWebsitesInfoCalled = false
var capturedWebsitesInfo: [WebsiteInfo]?
func bookmarkAll(websitesInfo: [WebsiteInfo], withinParentFolder parent: ParentFolderType) {
bookmarkAllWebsitesInfoCalled = true
capturedWebsitesInfo = websitesInfo
capturedParentFolderType = parent
}

var canMoveObjectWithUUIDCalled = false
func canMoveObjectWithUUID(objectUUID uuid: String, to parent: BookmarkFolder) -> Bool {
canMoveObjectWithUUIDCalled = true
Expand Down
4 changes: 4 additions & 0 deletions DuckDuckGo/Bookmarks/Services/LocalBookmarkStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,10 @@

}

func bookmarkAll(websitesInfo: [WebsiteInfo], withinParentFolder parent: ParentFolderType) {
// TODO: https://app.asana.com/0/0/1207032959154802/f

Check failure on line 683 in DuckDuckGo/Bookmarks/Services/LocalBookmarkStore.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

TODOs should be resolved (https://app.asana.com/0/0/1207...) (todo)
}

// MARK: - Import

/// Imports bookmarks into the Core Data store from an `ImportedBookmarks` object.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// UserDefaultsBookmarkFoldersStore.swift
//
// 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

/// A type used to provide the ID of the folder where all tabs were last saved.
protocol BookmarkFoldersStore: AnyObject {
/// The ID of the folder where all bookmarks from the last session were saved.
var lastBookmarkAllTabsFolderIdUsed: String? { get set }
}

final class UserDefaultsBookmarkFoldersStore: BookmarkFoldersStore {

enum Keys {
static let bookmarkAllTabsFolderUsedKey = "bookmarks.all-tabs.last-used-folder"
}

private let userDefaults: UserDefaults

init(userDefaults: UserDefaults = .standard) {
self.userDefaults = userDefaults
}

var lastBookmarkAllTabsFolderIdUsed: String? {
get {
userDefaults.string(forKey: Keys.bookmarkAllTabsFolderUsedKey)
}
set {
userDefaults.set(newValue, forKey: Keys.bookmarkAllTabsFolderUsedKey)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ struct AddBookmarkFolderPopoverView: ModalView {
isDefaultActionDisabled: model.isDefaultActionButtonDisabled,
defaultAction: { _ in model.addFolder() }
)
.padding(.vertical, 16.0)
.font(.system(size: 13))
.frame(width: 320)
}
Expand Down
1 change: 0 additions & 1 deletion DuckDuckGo/Bookmarks/View/AddBookmarkPopoverView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ struct AddBookmarkPopoverView: View {
isDefaultActionDisabled: model.isDefaultActionButtonDisabled,
defaultAction: model.doneButtonAction
)
.padding(.vertical, 16.0)
.font(.system(size: 13))
.frame(width: 320)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct AddEditBookmarkDialogView: ModalView {
isDefaultActionDisabled: viewModel.bookmarkModel.isDefaultActionDisabled,
defaultAction: viewModel.bookmarkModel.addOrSave
)
.frame(width: 448, height: 288)
.frame(width: 448)
}

private var addFolderView: some View {
Expand Down
141 changes: 141 additions & 0 deletions DuckDuckGo/Bookmarks/View/Dialog/BookmarkAllTabsDialogView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//
// BookmarkAllTabsDialogView.swift
//
// 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 SwiftUI
import SwiftUIExtensions

struct BookmarkAllTabsDialogView: ModalView {
@ObservedObject private var viewModel: BookmarkAllTabsDialogCoordinatorViewModel<BookmarkAllTabsDialogViewModel, AddEditBookmarkFolderDialogViewModel>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Used same approach of AddEditBookmarkDialogView.


init(viewModel: BookmarkAllTabsDialogCoordinatorViewModel<BookmarkAllTabsDialogViewModel, AddEditBookmarkFolderDialogViewModel>) {
self.viewModel = viewModel
}

var body: some View {
Group {
switch viewModel.viewState {
case .bookmarkAllTabs:
bookmarkAllTabsView
case .addFolder:
addFolderView
}
}
.font(.system(size: 13))
}

private var bookmarkAllTabsView: some View {
BookmarkDialogContainerView(
title: viewModel.bookmarkModel.title,
middleSection: {
Text(viewModel.bookmarkModel.educationalMessage)
.multilineText()
.multilineTextAlignment(.leading)
.foregroundColor(.secondary)
.font(.system(size: 11))
BookmarkDialogStackedContentView(
.init(
title: UserText.Bookmarks.Dialog.Field.folderName,
content: TextField("", text: $viewModel.bookmarkModel.folderName)
.focusedOnAppear()
.textFieldStyle(RoundedBorderTextFieldStyle())
.font(.system(size: 14))
),
.init(
title: UserText.Bookmarks.Dialog.Field.location,
content: BookmarkDialogFolderManagementView(
folders: viewModel.bookmarkModel.folders,
selectedFolder: $viewModel.bookmarkModel.selectedFolder,
onActionButton: viewModel.addFolderAction
)
)
)
},
bottomSection: {
BookmarkDialogButtonsView(
viewState: .init(.compressed),
otherButtonAction: .init(
title: viewModel.bookmarkModel.cancelActionTitle,
isDisabled: viewModel.bookmarkModel.isOtherActionDisabled,
action: viewModel.bookmarkModel.cancel
),
defaultButtonAction: .init(
title: viewModel.bookmarkModel.defaultActionTitle,
keyboardShortCut: .defaultAction,
isDisabled: viewModel.bookmarkModel.isDefaultActionDisabled,
action: viewModel.bookmarkModel.addOrSave
)
)
}

)
.frame(width: 448)
}

private var addFolderView: some View {
AddEditBookmarkFolderView(
title: viewModel.folderModel.title,
buttonsState: .compressed,
folders: viewModel.folderModel.folders,
folderName: $viewModel.folderModel.folderName,
selectedFolder: $viewModel.folderModel.selectedFolder,
cancelActionTitle: viewModel.folderModel.cancelActionTitle,
isCancelActionDisabled: viewModel.folderModel.isOtherActionDisabled,
cancelAction: { _ in
viewModel.dismissAction()
},
defaultActionTitle: viewModel.folderModel.defaultActionTitle,
isDefaultActionDisabled: viewModel.folderModel.isDefaultActionDisabled,
defaultAction: { _ in
viewModel.folderModel.addOrSave {
viewModel.dismissAction()
}
}
)
.frame(width: 448, height: 210)
}
}

#if DEBUG
#Preview("Bookmark All Tabs - Light") {
let parentFolder = BookmarkFolder(id: "7", title: "DuckDuckGo")
let bookmark = Bookmark(id: "1", url: "www.duckduckgo.com", title: "DuckDuckGo", isFavorite: true, parentFolderUUID: "7")
let bookmarkManager = LocalBookmarkManager(bookmarkStore: BookmarkStoreMock(bookmarks: [bookmark, parentFolder]))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some failure here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happens sometimes, I noticed that closing and re-opening Xcode fixes it 🙃

Screenshot 2024-04-17 at 1 42 07 PM

bookmarkManager.loadBookmarks()
let websitesInfo: [WebsiteInfo] = [
.init(.init(content: .url(URL.duckDuckGo, credential: nil, source: .ui)))!,
.init(.init(content: .url(URL.duckDuckGoEmail, credential: nil, source: .ui)))!,
]

return BookmarksDialogViewFactory.makeBookmarkAllOpenTabsView(websitesInfo: websitesInfo, bookmarkManager: bookmarkManager)
.preferredColorScheme(.light)
}

#Preview("Bookmark All Tabs - Dark") {
let parentFolder = BookmarkFolder(id: "7", title: "DuckDuckGo")
let bookmark = Bookmark(id: "1", url: "www.duckduckgo.com", title: "DuckDuckGo", isFavorite: true, parentFolderUUID: "7")
let bookmarkManager = LocalBookmarkManager(bookmarkStore: BookmarkStoreMock(bookmarks: [bookmark, parentFolder]))
bookmarkManager.loadBookmarks()
let websitesInfo: [WebsiteInfo] = [
.init(.init(content: .url(URL.duckDuckGo, credential: nil, source: .ui)))!,
.init(.init(content: .url(URL.duckDuckGoEmail, credential: nil, source: .ui)))!,
]

return BookmarksDialogViewFactory.makeBookmarkAllOpenTabsView(websitesInfo: websitesInfo, bookmarkManager: bookmarkManager)
.preferredColorScheme(.dark)
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ struct BookmarkDialogContainerView<Content: View, Buttons: View>: View {
Text(title)
.foregroundColor(.primary)
.fontWeight(.semibold)
.padding(.top, 20)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As BookmarkAllTabs dialog can have an educational message on multiple lines for long language I removed the fix height from the bookmark dialogs, moved the padding in here and let the subviews dictate the height. AddEditBookmarks still render properly.

},
center: middleSection,
bottom: bottomSection
bottom: {
bottomSection()
.padding(.bottom, 16.0)
}
)
}
}
12 changes: 12 additions & 0 deletions DuckDuckGo/Bookmarks/View/Dialog/BookmarksDialogViewFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ enum BookmarksDialogViewFactory {
return makeAddEditBookmarkDialogView(viewModel: viewModel, bookmarkManager: bookmarkManager)
}

/// Creates an instance of AddEditBookmarkDialogView for adding Bookmarks for all the open Tabs.
/// - Parameters:
/// - websitesInfo: A list of websites to add as bookmarks.
/// - bookmarkManager: An instance of `BookmarkManager`. This should be used for `#previews` only.
/// - Returns: An instance of BookmarkAllTabsDialogView
static func makeBookmarkAllOpenTabsView(websitesInfo: [WebsiteInfo], bookmarkManager: LocalBookmarkManager = .shared) -> BookmarkAllTabsDialogView {
let addFolderViewModel = AddEditBookmarkFolderDialogViewModel(mode: .add(parentFolder: nil), bookmarkManager: bookmarkManager)
let bookmarkAllTabsViewModel = BookmarkAllTabsDialogViewModel(websites: websitesInfo, foldersStore: UserDefaultsBookmarkFoldersStore(), bookmarkManager: bookmarkManager)
let viewModel = BookmarkAllTabsDialogCoordinatorViewModel(bookmarkModel: bookmarkAllTabsViewModel, folderModel: addFolderViewModel)
return BookmarkAllTabsDialogView(viewModel: viewModel)
}

}

private extension BookmarksDialogViewFactory {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ final class AddEditBookmarkFolderDialogViewModel: BookmarkFolderDialogEditing {

@Published var folderName: String
@Published var selectedFolder: BookmarkFolder?
@Published private(set) var folders: [FolderViewModel]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Soooo, I found an issue when we present the Add folder dialog from AddEditBookmark and BookmarkAllTabs. Unfortunately this was missed during the project to address bookmark feedback (I’m pretty sure tho we would have tested this scenario, so I’m a bit 🙈) .

The issue happens when we add/edit a bookmark or bookmark all tabs, press the icon to add a folder, save and press again the icon to add a folder. The add folder dialog view shows the name of the previously saved folder rather than being empty and the picker selector is not updated.

I’ve fixed this and updated tests. I actually wrote an integration test.


let folders: [FolderViewModel]
private var folderCancellable: AnyCancellable?

var title: String {
mode.title
Expand Down Expand Up @@ -77,14 +78,20 @@ final class AddEditBookmarkFolderDialogViewModel: BookmarkFolderDialogEditing {
folderName = mode.folderName
folders = .init(bookmarkManager.list)
selectedFolder = mode.parentFolder

bind()
}

func cancel(dismiss: () -> Void) {
reset()
dismiss()
}

func addOrSave(dismiss: () -> Void) {
defer { dismiss() }
defer {
reset()
dismiss()
}

guard !folderName.isEmpty else {
assertionFailure("folderName is empty, button should be disabled")
Expand All @@ -110,6 +117,14 @@ final class AddEditBookmarkFolderDialogViewModel: BookmarkFolderDialogEditing {

private extension AddEditBookmarkFolderDialogViewModel {

func bind() {
folderCancellable = bookmarkManager.listPublisher
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] bookmarkList in
self?.folders = .init(bookmarkList)
})
}

func update(folder: BookmarkFolder, originalParent: BookmarkFolder?, newParent: BookmarkFolder?) {
// If the original location of the folder changed move it to the new folder.
if selectedFolder?.id != originalParent?.id {
Expand All @@ -129,6 +144,10 @@ private extension AddEditBookmarkFolderDialogViewModel {
}
}

func reset() {
self.folderName = ""
}

}

// MARK: - AddEditBookmarkFolderDialogViewModel.Mode
Expand Down
Loading
Loading