Skip to content

Commit

Permalink
Show Add Favorite placeholder as last favorite item on New Tab Page (#…
Browse files Browse the repository at this point in the history
…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
dus7 authored Sep 23, 2024
1 parent 3845520 commit 37d2558
Show file tree
Hide file tree
Showing 24 changed files with 615 additions and 246 deletions.
54 changes: 33 additions & 21 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/duckduckgo/DesignResourcesKit",
"state" : {
"revision" : "fe3c383b5e21e0a419e0f979f7f2cd4e67385b15",
"version" : "3.2.0"
"revision" : "ad133f76501edcb2bfa841e33aebc0da5f92bb5c",
"version" : "3.3.0"
}
},
{
Expand Down
38 changes: 38 additions & 0 deletions DuckDuckGo/AddFavoritePlaceholderItemView.swift
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)
}
8 changes: 4 additions & 4 deletions DuckDuckGo/EditableShortcutsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,12 @@ private extension View {

extension NewTabPageSettingsModel.NTPSetting<NewTabPageShortcut>: Reorderable, Hashable, Equatable {

var dropItemProvider: NSItemProvider {
NSItemProvider(object: item.id as NSString)
var trait: ReorderableTrait {
let itemProvider = NSItemProvider(object: item.id as NSString)
let metadata = MoveMetadata(itemProvider: itemProvider, type: .text)
return .movable(metadata)
}

var dropType: UTType { .text }

static func == (lhs: Self, rhs: Self) -> Bool {
lhs.item == rhs.item
}
Expand Down
95 changes: 95 additions & 0 deletions DuckDuckGo/FavoriteDataSource.swift
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 ?? ""
}

}
50 changes: 50 additions & 0 deletions DuckDuckGo/FavoriteItem.swift
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
}
}
}
Loading

0 comments on commit 37d2558

Please sign in to comment.