-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show Add Favorite placeholder as last favorite item on New Tab Page (#…
…3375) Task/Issue URL: https://app.asana.com/0/72649045549333/1208244619690578/f Tech Design URL: CC: **Description**: Adds a possibility to show an Add button in favorites collection, allowing to create a favorite right from New Tab Page. ([Figma](https://www.figma.com/design/N2GbF5HEvopp5iwmAlMwyD/New-Tab-Page-Customization?node-id=611-147700&m=dev)). Add button is currently navigating to an empty view, which is expected. The view will be implemented in additional PR. In order to reduce the amount of duplicated code and separate additional display elements from pure Favorites data, DataSource for Favorites was defined which is used in common ViewModel logic for Favorites, hence the rename of `NewTabPageModel` -> `NewTabPageViewModel`. There was also a need to keep some of the items immovable, which required a change in `ReorderableForEach`. **Steps to test this PR**: 1. Open NTP without Favorites, + button should be visible as a first element. 2. Add a couple of Favorites, + button should be visible as last item. 3. Try reordering Favorites, + button should be kept stationary and it shouldn't be posible to drop favorite after it. **Definition of Done (Internal Only)**: * [ ] Does this PR satisfy our [Definition of Done](https://app.asana.com/0/1202500774821704/1207634633537039/f)? --- ###### Internal references: [Software Engineering Expectations](https://app.asana.com/0/59792373528535/199064865822552) [Technical Design Template](https://app.asana.com/0/59792373528535/184709971311943)
- Loading branch information
Showing
24 changed files
with
615 additions
and
246 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// | ||
// AddFavoritePlaceholderItemView.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 SwiftUI | ||
import DesignResourcesKit | ||
|
||
struct AddFavoritePlaceholderItemView: View { | ||
var body: some View { | ||
RoundedRectangle(cornerRadius: 8, style: .continuous) | ||
.fill(.clear) | ||
.overlay { | ||
Image(.add24) | ||
.tintIfAvailable(Color(designSystemColor: .icons)) | ||
} | ||
.aspectRatio(1.0, contentMode: .fit) | ||
} | ||
} | ||
|
||
#Preview { | ||
AddFavoritePlaceholderItemView() | ||
.frame(width: 100) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// FavoriteDataSource.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 Combine | ||
import Bookmarks | ||
|
||
final class FavoritesListInteractingAdapter: NewTabPageFavoriteDataSource { | ||
|
||
let favoritesListInteracting: FavoritesListInteracting | ||
|
||
init(favoritesListInteracting: FavoritesListInteracting) { | ||
self.favoritesListInteracting = favoritesListInteracting | ||
} | ||
|
||
var externalUpdates: AnyPublisher<Void, Never> { favoritesListInteracting.externalUpdates } | ||
|
||
var favorites: [Favorite] { | ||
(try? favoritesListInteracting.favorites.map(Favorite.init)) ?? [] | ||
} | ||
|
||
func moveFavorite(_ favorite: Favorite, fromIndex: Int, toIndex: Int) { | ||
guard let entity = bookmarkEntity(for: favorite) else { return } | ||
|
||
// adjust for different target index handling | ||
let toIndex = toIndex > fromIndex ? toIndex - 1 : toIndex | ||
favoritesListInteracting.moveFavorite(entity, fromIndex: fromIndex, toIndex: toIndex) | ||
} | ||
|
||
func bookmarkEntity(for favorite: Favorite) -> BookmarkEntity? { | ||
favoritesListInteracting.favorites.first { | ||
$0.uuid == favorite.id | ||
} | ||
} | ||
|
||
func favorite(at index: Int) throws -> Favorite? { | ||
try favoritesListInteracting.favorite(at: index).map(Favorite.init) | ||
} | ||
|
||
func removeFavorite(_ favorite: Favorite) { | ||
guard let entity = bookmarkEntity(for: favorite) else { return } | ||
|
||
favoritesListInteracting.removeFavorite(entity) | ||
} | ||
} | ||
|
||
private extension Favorite { | ||
init(_ bookmark: BookmarkEntity) throws { | ||
guard let uuid = bookmark.uuid else { | ||
throw FavoriteMappingError.missingUUID | ||
} | ||
|
||
self.id = uuid | ||
self.title = bookmark.displayTitle | ||
self.domain = bookmark.host | ||
self.urlObject = bookmark.urlObject | ||
} | ||
} | ||
|
||
private extension BookmarkEntity { | ||
|
||
var displayTitle: String { | ||
if let title = title?.trimmingWhitespace(), !title.isEmpty { | ||
return title | ||
} | ||
|
||
if let host = urlObject?.host?.droppingWwwPrefix() { | ||
return host | ||
} | ||
|
||
assertionFailure("Unable to create display title") | ||
return "" | ||
} | ||
|
||
var host: String { | ||
return urlObject?.host ?? "" | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// | ||
// FavoriteItem.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 UniformTypeIdentifiers | ||
|
||
enum FavoriteItem { | ||
case favorite(Favorite) | ||
case addFavorite | ||
} | ||
|
||
extension FavoriteItem: Identifiable { | ||
var id: String { | ||
switch self { | ||
case .favorite(let favorite): | ||
return favorite.id | ||
case .addFavorite: | ||
return "addFavorite" | ||
} | ||
} | ||
} | ||
|
||
extension FavoriteItem: Reorderable { | ||
var trait: ReorderableTrait { | ||
switch self { | ||
case .favorite(let favorite): | ||
let itemProvider = NSItemProvider(object: (favorite.urlObject?.absoluteString ?? "") as NSString) | ||
let metadata = MoveMetadata(itemProvider: itemProvider, type: .plainText) | ||
return .movable(metadata) | ||
case .addFavorite: | ||
return .stationary | ||
} | ||
} | ||
} |
Oops, something went wrong.