Skip to content

Commit

Permalink
Enable favorites reordering on New Tab Page (#3169)
Browse files Browse the repository at this point in the history
Task/Issue URL:
https://app.asana.com/0/1206226850447395/1207932325145653/f
Tech Design URL:
CC:

**Description**:

Adds reordering capability for Favorites on New Tab Page.

**Steps to test this PR**:
1. Reorder Favorites in different ways (to last, to first etc.)
2. Verify if the order is preserved across launches and NTP instances.
3. Check if the order is reflected in the Bookmakrs/Favorites list
  • Loading branch information
dus7 authored Aug 2, 2024
1 parent 06004e9 commit dd24158
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo/Favorite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import Bookmarks
import SwiftUI

struct Favorite: Identifiable, Equatable {
struct Favorite: Identifiable, Equatable, Hashable {
let id: String
let title: String
let domain: String
Expand Down
13 changes: 13 additions & 0 deletions DuckDuckGo/FavoritesDefaultModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ final class FavoritesDefaultModel: FavoritesModel {
onFavoriteEdit?(entity)
}

func moveFavorites(from indexSet: IndexSet, to index: Int) {
guard indexSet.count == 1,
let fromIndex = indexSet.first else { return }

let favorite = allFavorites[fromIndex]
guard let entity = lookupEntity(for: favorite) else { return }

// adjust for different target index handling
let toIndex = index > fromIndex ? index - 1 : index
interactionModel.moveFavorite(entity, fromIndex: fromIndex, toIndex: toIndex)
allFavorites.move(fromOffsets: IndexSet(integer: fromIndex), toOffset: index)
}

private func lookupEntity(for favorite: Favorite) -> BookmarkEntity? {
interactionModel.favorites.first {
$0.uuid == favorite.id
Expand Down
1 change: 1 addition & 0 deletions DuckDuckGo/FavoritesModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ protocol FavoritesModel: AnyObject, ObservableObject {
func favoriteSelected(_ favorite: Favorite)
func editFavorite(_ favorite: Favorite)
func deleteFavorite(_ favorite: Favorite)
func moveFavorites(from indexSet: IndexSet, to index: Int)
}

struct FavoritesSlice {
Expand Down
4 changes: 4 additions & 0 deletions DuckDuckGo/FavoritesPreviewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ final class FavoritesPreviewModel: FavoritesModel {

}

func moveFavorites(from indexSet: IndexSet, to index: Int) {
allFavorites.move(fromOffsets: indexSet, toOffset: index)
}

func loadFavicon(for favorite: Favorite, size: CGFloat) async {

}
Expand Down
28 changes: 27 additions & 1 deletion DuckDuckGo/FavoritesView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import Bookmarks
import SwiftUI
import UniformTypeIdentifiers

struct FavoritesView<Model: FavoritesModel>: View {
@Environment(\.horizontalSizeClass) var horizontalSizeClass
Expand All @@ -35,7 +36,7 @@ struct FavoritesView<Model: FavoritesModel>: View {
let result = model.prefixedFavorites(for: columns)

NewTabPageGridView { _ in
ForEach(result.items) { item in
ReorderableForEach(result.items) { item in
Button(action: {
model.favoriteSelected(item)
selectionFeedback.selectionChanged()
Expand All @@ -52,6 +53,15 @@ struct FavoritesView<Model: FavoritesModel>: View {
.background(.clear)
.frame(width: NewTabPageGrid.Item.edgeSize)
})
.previewShape()
} preview: { favorite in
FavoriteIconView(favorite: favorite, faviconLoading: model.faviconLoader)
.frame(width: NewTabPageGrid.Item.edgeSize)
.previewShape()
} onMove: { from, to in
withAnimation {
model.moveFavorites(from: from, to: to)
}
}
}

Expand All @@ -70,6 +80,22 @@ struct FavoritesView<Model: FavoritesModel>: View {
}
}

private extension View {
func previewShape() -> some View {
contentShape(.dragPreview, RoundedRectangle(cornerRadius: 8))
}
}

extension Favorite: Reorderable {
var dropItemProvider: NSItemProvider {
NSItemProvider(object: (urlObject?.absoluteString ?? "") as NSString)
}

var dropType: UTType {
.plainText
}
}

#Preview {
FavoritesView(model: FavoritesPreviewModel())
}

0 comments on commit dd24158

Please sign in to comment.