-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from all commits
a49a6a4
24c6f2f
07fbd4e
05650c8
36a577e
1ff7273
8adbcba
f5f3de8
fbce7ba
fa26092
976979d
0e241ae
2d175c4
9d2cf2e
7bc0262
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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 |
---|---|---|
@@ -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> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Used same approach of |
||
|
||
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])) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Some failure here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
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 |
---|---|---|
|
@@ -42,9 +42,13 @@ struct BookmarkDialogContainerView<Content: View, Buttons: View>: View { | |
Text(title) | ||
.foregroundColor(.primary) | ||
.fontWeight(.semibold) | ||
.padding(.top, 20) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
} | ||
) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,8 +42,9 @@ final class AddEditBookmarkFolderDialogViewModel: BookmarkFolderDialogEditing { | |
|
||
@Published var folderName: String | ||
@Published var selectedFolder: BookmarkFolder? | ||
@Published private(set) var folders: [FolderViewModel] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
@@ -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") | ||
|
@@ -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 { | ||
|
@@ -129,6 +144,10 @@ private extension AddEditBookmarkFolderDialogViewModel { | |
} | ||
} | ||
|
||
func reset() { | ||
self.folderName = "" | ||
} | ||
|
||
} | ||
|
||
// MARK: - AddEditBookmarkFolderDialogViewModel.Mode | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.