Skip to content
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

add count of all bookmarks in domain to view model #272

Merged
merged 1 commit into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/duckduckgo/content-scope-scripts",
"state": {
"branch": null,
"revision": "ed10de3485f454ca59259266a0b3942bd21b43db",
"version": "3.4.1"
"revision": "801b7f23476f797c6eaa72b070e6c80abb82801a",
"version": "4.4.4"
}
},
{
Expand Down
21 changes: 19 additions & 2 deletions Sources/Bookmarks/BookmarkListViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,32 @@ public class BookmarkListViewModel: BookmarkListInteracting, ObservableObject {
}

// MARK: - Read


public func countBookmarksForDomain(_ domain: String) -> Int {
let count = countBookmarksForDomain(domain, inFolder: fetchRootBookmarksFolder())
return count
}

private func countBookmarksForDomain(_ domain: String, inFolder folder: BookmarkEntity) -> Int {
var count = 0
folder.childrenArray.forEach { child in
if child.isFolder {
count += countBookmarksForDomain(domain, inFolder: child)
} else if child.urlObject?.isPart(ofDomain: domain) == true {
count += 1
}
}
return count
}

public var totalBookmarksCount: Int {
let countRequest = BookmarkEntity.fetchRequest()
countRequest.predicate = NSPredicate(format: "%K == false", #keyPath(BookmarkEntity.isFolder))

return (try? context.count(for: countRequest)) ?? 0
}

public func fetchRootBookmarksFolder() -> BookmarkEntity {
private func fetchRootBookmarksFolder() -> BookmarkEntity {
return BookmarkUtils.fetchRootFolder(context)!
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/Bookmarks/BookmarksModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public protocol BookmarkListInteracting: BookmarkStoring {
func moveBookmark(_ bookmark: BookmarkEntity,
fromIndex: Int,
toIndex: Int)

func countBookmarksForDomain(_ domain: String) -> Int
}

public protocol FavoritesListInteracting: BookmarkStoring {
Expand Down