Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into graeme/subscription-k…
Browse files Browse the repository at this point in the history
…eychain-sharing
  • Loading branch information
graeme committed Mar 1, 2024
2 parents 5c0c481 + 444cf51 commit cb11e54
Show file tree
Hide file tree
Showing 36 changed files with 1,171 additions and 49 deletions.
12 changes: 12 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ let package = Package(
.library(name: "NetworkProtectionTestUtils", targets: ["NetworkProtectionTestUtils"]),
.library(name: "SecureStorage", targets: ["SecureStorage"]),
.library(name: "Subscription", targets: ["Subscription"]),
.library(name: "History", targets: ["History"]),
],
dependencies: [
.package(url: "https://github.com/duckduckgo/duckduckgo-autofill.git", exact: "10.1.0"),
Expand Down Expand Up @@ -97,6 +98,17 @@ let package = Package(
],
plugins: [swiftlintPlugin]
),
.target(
name: "History",
dependencies: [
"Persistence",
"Common"
],
swiftSettings: [
.define("DEBUG", .when(configuration: .debug))
],
plugins: [swiftlintPlugin]
),
.executableTarget(
name: "BookmarksTestDBBuilder",
dependencies: [
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DuckDuckGo is growing fast and we continue to expand our fully distributed team.

## What is it?

`BrowserServicesKit` is a narrowly scoped package that contains modules shared between DuckDuckGo projects.
`BrowserServicesKit` is a package that contains modules shared between DuckDuckGo projects.

## Building

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// HistoryEntry.swift
// HistorySuggestion.swift
//
// Copyright © 2021 DuckDuckGo. All rights reserved.
//
Expand All @@ -18,7 +18,7 @@

import Foundation

public protocol HistoryEntry {
public protocol HistorySuggestion {

var identifier: UUID { get }
var url: URL { get }
Expand Down
2 changes: 1 addition & 1 deletion Sources/BrowserServicesKit/Suggestions/Score.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ extension Score {
self.init(title: bookmark.title, url: urlObject, visitCount: 0, query: query, queryTokens: queryTokens)
}

init(historyEntry: HistoryEntry, query: Query, queryTokens: [Query]? = nil) {
init(historyEntry: HistorySuggestion, query: Query, queryTokens: [Query]? = nil) {
self.init(title: historyEntry.title ?? "",
url: historyEntry.url,
visitCount: historyEntry.numberOfVisits,
Expand Down
2 changes: 1 addition & 1 deletion Sources/BrowserServicesKit/Suggestions/Suggestion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ extension Suggestion {
allowedInTopHits: bookmark.isFavorite)
}

init(historyEntry: HistoryEntry) {
init(historyEntry: HistorySuggestion) {
let areVisitsLow = historyEntry.numberOfVisits < 4
let allowedInTopHits = !(historyEntry.failedToLoad ||
(areVisitsLow && !historyEntry.url.isRoot))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public protocol SuggestionLoadingDataSource: AnyObject {

func bookmarks(for suggestionLoading: SuggestionLoading) -> [Bookmark]

func history(for suggestionLoading: SuggestionLoading) -> [HistoryEntry]
func history(for suggestionLoading: SuggestionLoading) -> [HistorySuggestion]

func suggestionLoading(_ suggestionLoading: SuggestionLoading,
suggestionDataFromUrl url: URL,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class SuggestionProcessing {
}

func result(for query: Query,
from history: [HistoryEntry],
from history: [HistorySuggestion],
bookmarks: [Bookmark],
apiResult: APIResult?) -> SuggestionResult? {
let query = query.lowercased()
Expand Down Expand Up @@ -87,7 +87,7 @@ final class SuggestionProcessing {

// MARK: - History and Bookmarks

private func historyAndBookmarkSuggestions(from history: [HistoryEntry], bookmarks: [Bookmark], query: Query) -> [Suggestion] {
private func historyAndBookmarkSuggestions(from history: [HistorySuggestion], bookmarks: [Bookmark], query: Query) -> [Suggestion] {
let historyAndBookmarks: [Any] = bookmarks + history
let queryTokens = Score.tokens(from: query)

Expand All @@ -98,7 +98,7 @@ final class SuggestionProcessing {
switch item {
case let bookmark as Bookmark:
score = Score(bookmark: bookmark, query: query, queryTokens: queryTokens)
case let historyEntry as HistoryEntry:
case let historyEntry as HistorySuggestion:
score = Score(historyEntry: historyEntry, query: query, queryTokens: queryTokens)
default:
score = 0
Expand All @@ -115,7 +115,7 @@ final class SuggestionProcessing {
switch $0.item {
case let bookmark as Bookmark:
return Suggestion(bookmark: bookmark)
case let historyEntry as HistoryEntry:
case let historyEntry as HistorySuggestion:
return Suggestion(historyEntry: historyEntry)
default:
return nil
Expand Down
116 changes: 116 additions & 0 deletions Sources/Common/Extensions/DateExtension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// DateExtension.swift
//
// Copyright © 2021 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

public extension Date {

struct IndexedMonth: Hashable {
public let name: String
public let index: Int
}

var components: DateComponents {
return Calendar.current.dateComponents([.day, .year, .month], from: self)
}

static var weekAgo: Date {
return Calendar.current.date(byAdding: .weekOfMonth, value: -1, to: Date())!
}

static var monthAgo: Date! {
return Calendar.current.date(byAdding: .month, value: -1, to: Date())!
}

static func daysAgo(_ days: Int) -> Date! {
return Calendar.current.date(byAdding: .day, value: -days, to: Date())!
}

static var startOfDayTomorrow: Date {
let tomorrow = Calendar.current.date(byAdding: .day, value: 1, to: Date())!
return Calendar.current.startOfDay(for: tomorrow)
}

static var startOfDayToday: Date {
return Calendar.current.startOfDay(for: Date())
}

var startOfDay: Date {
return Calendar.current.startOfDay(for: self)
}

static var startOfMinuteNow: Date {
let date = Calendar.current.date(bySetting: .second, value: 0, of: Date())!
let start = Calendar.current.date(byAdding: .minute, value: -1, to: date)!
return start
}

static var monthsWithIndex: [IndexedMonth] {
let months = Calendar.current.monthSymbols

return months.enumerated().map { index, month in
return IndexedMonth(name: month, index: index + 1)
}
}

static var daysInMonth: [Int] = {
return Array(1...31)
}()

static var nextTenYears: [Int] = {
let offsetComponents = DateComponents(year: 1)

var years = [Int]()
var currentDate = Date()

for _ in 0...10 {
let currentYear = Calendar.current.component(.year, from: currentDate)
years.append(currentYear)

currentDate = Calendar.current.date(byAdding: offsetComponents, to: currentDate)!
}

return years
}()

static var lastHundredYears: [Int] = {
let offsetComponents = DateComponents(year: -1)

var years = [Int]()
var currentDate = Date()

for _ in 0...100 {
let currentYear = Calendar.current.component(.year, from: currentDate)
years.append(currentYear)

currentDate = Calendar.current.date(byAdding: offsetComponents, to: currentDate)!
}

return years
}()

var daySinceReferenceDate: Int {
Int(self.timeIntervalSinceReferenceDate / TimeInterval.day)
}

@inlinable
func adding(_ timeInterval: TimeInterval) -> Date {
addingTimeInterval(timeInterval)
}

}
2 changes: 2 additions & 0 deletions Sources/Common/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ extension OSLog {
case passwordManager = "Password Manager"
case remoteMessaging = "Remote Messaging"
case subscription = "Subscription"
case history = "History"
}

#if DEBUG
Expand All @@ -52,6 +53,7 @@ extension OSLog {
@OSLogWrapper(.passwordManager) public static var passwordManager
@OSLogWrapper(.remoteMessaging) public static var remoteMessaging
@OSLogWrapper(.subscription) public static var subscription
@OSLogWrapper(.history) public static var history

public static var enabledLoggingCategories = Set<String>()

Expand Down
Loading

0 comments on commit cb11e54

Please sign in to comment.