Skip to content

Commit

Permalink
Merge branch 'release/0.18.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mallexxx committed Jan 17, 2022
2 parents c030847 + 32dbb4e commit a9e6d3a
Show file tree
Hide file tree
Showing 106 changed files with 3,455 additions and 890 deletions.
258 changes: 208 additions & 50 deletions DuckDuckGo.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
"package": "Lottie",
"repositoryURL": "https://github.com/airbnb/lottie-ios",
"state": {
"branch": "lottie/macos-spm",
"revision": "f4d701070f9743b9ef7b9ac42d386d1ef98b842f",
"version": null
"branch": null,
"revision": "4a6058cbbdfe4f74aeae92c8bd51ad3b0de2a1ee",
"version": "3.3.0"
}
},
{
Expand Down
18 changes: 4 additions & 14 deletions DuckDuckGo/API/APIRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,7 @@ import os.log
typealias APIRequestCompletion = (APIRequest.Response?, Error?) -> Void

enum APIRequest {

private static var defaultCallbackQueue: OperationQueue = {
let queue = OperationQueue()
queue.name = "APIRequest default callback queue"
queue.qualityOfService = .utility
queue.maxConcurrentOperationCount = 1
return queue
}()

private static let defaultSession = URLSession(configuration: .default, delegate: nil, delegateQueue: defaultCallbackQueue)
private static let mainThreadCallbackSession = URLSession(configuration: .default, delegate: nil, delegateQueue: OperationQueue.main)


struct Response {

var data: Data?
Expand Down Expand Up @@ -66,10 +55,10 @@ enum APIRequest {

let urlRequest = urlRequestFor(url: url, method: method, parameters: parameters, headers: headers, timeoutInterval: timeoutInterval)

let session = callBackOnMainThread ? mainThreadCallbackSession : defaultSession
let session: URLSession = callBackOnMainThread ? .mainThreadCallbackSession : .default

let task = session.dataTask(with: urlRequest) { (data, response, error) in

let httpResponse = response as? HTTPURLResponse

if let error = error {
Expand Down Expand Up @@ -100,6 +89,7 @@ enum APIRequest {
urlRequest.timeoutInterval = timeoutInterval
return urlRequest
}

}

extension HTTPURLResponse {
Expand Down
1 change: 1 addition & 0 deletions DuckDuckGo/AppDelegate/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ final class AppDelegate: NSObject, NSApplicationDelegate {

HTTPSUpgrade.shared.loadDataAsync()
LocalBookmarkManager.shared.loadBookmarks()
FaviconManager.shared.loadFavicons()
_=ConfigurationManager.shared
_=DownloadListCoordinator.shared

Expand Down
20 changes: 14 additions & 6 deletions DuckDuckGo/Bookmarks/Model/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ internal class BaseBookmarkEntity {
return Bookmark(id: id,
url: url,
title: title,
favicon: managedObject.faviconEncrypted as? NSImage,
oldFavicon: managedObject.faviconEncrypted as? NSImage,
isFavorite: managedObject.isFavorite,
parentFolderUUID: parentFolderUUID)
}
Expand Down Expand Up @@ -134,21 +134,29 @@ final class Bookmark: BaseBookmarkEntity {
}

let url: URL
var favicon: NSImage?
var isFavorite: Bool
var parentFolderUUID: UUID?

// Property oldFavicon can be removed in future updates when favicon cache is built
var oldFavicon: NSImage?
let faviconManagement: FaviconManagement
func favicon(_ sizeCategory: Favicon.SizeCategory) -> NSImage? {
return faviconManagement.getCachedFavicon(for: url, sizeCategory: sizeCategory)?.image ?? oldFavicon
}

init(id: UUID,
url: URL,
title: String,
favicon: NSImage? = nil,
oldFavicon: NSImage? = nil,
isFavorite: Bool,
parentFolderUUID: UUID? = nil) {
parentFolderUUID: UUID? = nil,
faviconManagement: FaviconManagement = FaviconManager.shared) {

self.url = url
self.favicon = favicon
self.oldFavicon = oldFavicon
self.isFavorite = isFavorite
self.parentFolderUUID = parentFolderUUID
self.faviconManagement = faviconManagement

super.init(id: id, title: title, isFolder: false)
}
Expand All @@ -157,7 +165,7 @@ final class Bookmark: BaseBookmarkEntity {
self.init(id: bookmark.id,
url: newUrl,
title: bookmark.title,
favicon: nil,
oldFavicon: nil,
isFavorite: bookmark.isFavorite)
}

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Bookmarks/Model/BookmarkList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct BookmarkList {

var topLevelEntities: [BaseBookmarkEntity] = []

private var allBookmarkURLsOrdered: [URL]
private(set) var allBookmarkURLsOrdered: [URL]
private var favoriteBookmarkURLsOrdered: [URL]
private var itemsDict: [URL: Bookmark]

Expand Down
46 changes: 13 additions & 33 deletions DuckDuckGo/Bookmarks/Model/BookmarkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Combine
protocol BookmarkManager: AnyObject {

func isUrlBookmarked(url: URL) -> Bool
func isHostInBookmarks(host: String) -> Bool
func getBookmark(for url: URL) -> Bookmark?
@discardableResult func makeBookmark(for url: URL, title: String, isFavorite: Bool) -> Bookmark?
@discardableResult func makeFolder(for title: String, parent: BookmarkFolder?) -> BookmarkFolder
Expand All @@ -45,22 +46,18 @@ final class LocalBookmarkManager: BookmarkManager {

static let shared = LocalBookmarkManager()

private init() {
subscribeToCachedFavicons()
}
private init() {}

init(bookmarkStore: BookmarkStore, faviconService: FaviconService) {
init(bookmarkStore: BookmarkStore, faviconManagement: FaviconManagement) {
self.bookmarkStore = bookmarkStore
self.faviconService = faviconService

subscribeToCachedFavicons()
self.faviconManagement = faviconManagement
}

@Published private(set) var list: BookmarkList?
var listPublisher: Published<BookmarkList?>.Publisher { $list }

private lazy var bookmarkStore: BookmarkStore = LocalBookmarkStore()
private lazy var faviconService: FaviconService = LocalFaviconService.shared
private lazy var faviconManagement: FaviconManagement = FaviconManager.shared

// MARK: - Bookmarks

Expand All @@ -86,6 +83,12 @@ final class LocalBookmarkManager: BookmarkManager {
return list?[url] != nil
}

func isHostInBookmarks(host: String) -> Bool {
return list?.allBookmarkURLsOrdered.contains(where: { url in
url.host == host
}) ?? false
}

func getBookmark(for url: URL) -> Bookmark? {
return list?[url]
}
Expand All @@ -99,7 +102,7 @@ final class LocalBookmarkManager: BookmarkManager {
}

let id = UUID()
let bookmark = Bookmark(id: id, url: url, title: title, favicon: favicon(for: url.host), isFavorite: isFavorite)
let bookmark = Bookmark(id: id, url: url, title: title, isFavorite: isFavorite)

list?.insert(bookmark)
bookmarkStore.save(bookmark: bookmark, parent: nil) { [weak self] success, _ in
Expand Down Expand Up @@ -201,32 +204,9 @@ final class LocalBookmarkManager: BookmarkManager {

// MARK: - Favicons

private var faviconCancellable: AnyCancellable?

private func subscribeToCachedFavicons() {
faviconCancellable = faviconService.cachedFaviconsPublisher
.sink(receiveValue: { [weak self] (host, favicon) in
self?.update(favicon: favicon, for: host)
})
}

private func update(favicon: NSImage, for host: String) {
guard let bookmarks = list?.bookmarks() else { return }

bookmarks
.filter { $0.url.host == host &&
$0.favicon?.size.isSmaller(than: favicon.size) ?? true
}
.forEach {
let bookmark = $0
bookmark.favicon = favicon
update(bookmark: bookmark)
}
}

private func favicon(for host: String?) -> NSImage? {
if let host = host {
return faviconService.getCachedFavicon(for: host, mustBeFromUserScript: false)
return faviconManagement.getCachedFavicon(for: host, sizeCategory: .small)?.image
}

return nil
Expand Down
2 changes: 0 additions & 2 deletions DuckDuckGo/Bookmarks/Services/BookmarkStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ final class LocalBookmarkStore: BookmarkStore {
bookmarkMO.id = bookmark.id
bookmarkMO.urlEncrypted = bookmark.url as NSURL?
bookmarkMO.titleEncrypted = bookmark.title as NSString
bookmarkMO.faviconEncrypted = bookmark.favicon
bookmarkMO.isFavorite = bookmark.isFavorite
bookmarkMO.isFolder = bookmark.isFolder
bookmarkMO.dateAdded = NSDate.now
Expand Down Expand Up @@ -485,7 +484,6 @@ fileprivate extension BookmarkManagedObject {
id = bookmark.id
urlEncrypted = bookmark.url as NSURL?
titleEncrypted = bookmark.title as NSString
faviconEncrypted = bookmark.favicon
isFavorite = bookmark.isFavorite
isFolder = false
}
Expand Down
6 changes: 6 additions & 0 deletions DuckDuckGo/Bookmarks/View/BookmarkListViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ final class BookmarkListViewController: NSViewController {
}.store(in: &cancellables)
}

override func viewWillAppear() {
super.viewWillAppear()

reloadData()
}

private func reloadData() {
let selectedNodes = self.selectedNodes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ final class BookmarkManagementSidebarViewController: NSViewController {

override func viewWillAppear() {
super.viewWillAppear()
reloadData()

tabSwitcherButton.select(tabType: .bookmarks)
}

Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Bookmarks/View/BookmarkOutlineViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class BookmarkOutlineViewCell: NSTableCellView {
private func commonInit() {}

func update(from bookmark: Bookmark) {
faviconImageView.image = bookmark.favicon ?? Self.defaultBookmarkFavicon
faviconImageView.image = bookmark.favicon(.small) ?? Self.defaultBookmarkFavicon
titleLabel.stringValue = bookmark.title
countLabel.stringValue = ""
}
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Bookmarks/View/BookmarkTableCellView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ final class BookmarkTableCellView: NSTableCellView, NibLoadable {
func update(from bookmark: Bookmark) {
self.entity = bookmark

faviconImageView.image = bookmark.favicon ?? Self.defaultBookmarkFavicon
faviconImageView.image = bookmark.favicon(.small)
accessoryImageView.image = bookmark.isFavorite ? Self.favoriteAccessoryViewImage : nil
favoriteButton.image = bookmark.isFavorite ? Self.favoriteFilledAccessoryViewImage : Self.favoriteAccessoryViewImage
primaryTitleLabelValue = bookmark.title
Expand Down
2 changes: 1 addition & 1 deletion DuckDuckGo/Bookmarks/ViewModel/BookmarkViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ struct BookmarkViewModel {
// bookmark.isFavorite ? bookmark.favicon?.makeFavoriteOverlay() : bookmark.favicon

if let bookmark = entity as? Bookmark {
let favicon = bookmark.favicon?.copy() as? NSImage
let favicon = bookmark.favicon(.small)?.copy() as? NSImage
favicon?.size = NSSize.faviconSize
return favicon
} else if entity is BookmarkFolder {
Expand Down
Loading

0 comments on commit a9e6d3a

Please sign in to comment.