77
88import AppKit
99import CoreSpotlight
10+ import OSLog
1011
1112/// Helper methods for managing the recent projects list and donating list items to CoreSpotlight.
1213///
@@ -15,6 +16,8 @@ import CoreSpotlight
1516/// If a UI element needs to listen to changes in this list, listen for the
1617/// ``RecentProjectsStore/didUpdateNotification`` notification.
1718class RecentProjectsStore {
19+ private let logger = Logger ( subsystem: Bundle . main. bundleIdentifier ?? " " , category: " RecentProjectsStore " )
20+
1821 /// The default projects store, uses the `UserDefaults.standard` storage location.
1922 static let shared = RecentProjectsStore ( )
2023
@@ -56,19 +59,6 @@ class RecentProjectsStore {
5659 /// Save a new paths array to defaults. Automatically limits the list to the most recent `100` items, donates
5760 /// search items to Spotlight, and notifies observers.
5861 private func setPaths( _ paths: [ String ] ) {
59- var paths = paths
60-
61- // Remove duplicates
62- var foundPaths = Set < String > ( )
63- for (idx, path) in paths. enumerated ( ) . reversed ( ) {
64- if foundPaths. contains ( path) {
65- paths. remove ( at: idx)
66- } else {
67- foundPaths. insert ( path)
68- }
69- }
70-
71- // Limit list to to 100 items after de-duplication
7262 defaults. setValue ( Array ( paths. prefix ( 100 ) ) , forKey: Self . projectsdDefaultsKey)
7363 setDocumentControllerRecents ( )
7464 donateSearchableItems ( )
@@ -95,10 +85,11 @@ class RecentProjectsStore {
9585 /// - Parameter paths: The paths to remove.
9686 /// - Returns: The remaining urls in the recent projects list.
9787 func removeRecentProjects( _ paths: Set < URL > ) -> [ URL ] {
98- var recentProjectPaths = recentProjectURLs ( )
88+ let paths = Set ( paths. map { $0. path ( percentEncoded: false ) } )
89+ var recentProjectPaths = recentPaths ( )
9990 recentProjectPaths. removeAll ( where: { paths. contains ( $0) } )
100- setPaths ( recentProjectPaths. map { $0 . path ( percentEncoded : false ) } )
101- return recentProjectURLs ( )
91+ setPaths ( recentProjectPaths)
92+ return recentURLs ( )
10293 }
10394
10495 func clearList( ) {
@@ -109,14 +100,14 @@ class RecentProjectsStore {
109100 /// Syncs AppKit's recent documents list with ours, keeping the dock menu and other lists up-to-date.
110101 private func setDocumentControllerRecents( ) {
111102 CodeEditDocumentController . shared. clearRecentDocuments ( nil )
112- for path in recentProjectURLs ( ) . prefix ( 10 ) {
103+ for path in recentURLs ( ) . prefix ( 10 ) {
113104 CodeEditDocumentController . shared. noteNewRecentDocumentURL ( path)
114105 }
115106 }
116107
117108 /// Donates all recent URLs to Core Search, making them searchable in Spotlight
118109 private func donateSearchableItems( ) {
119- let searchableItems = recentProjectURLs ( ) . map { entity in
110+ let searchableItems = recentURLs ( ) . map { entity in
120111 let attributeSet = CSSearchableItemAttributeSet ( contentType: . content)
121112 attributeSet. title = entity. lastPathComponent
122113 attributeSet. relatedUniqueIdentifier = entity. path ( )
@@ -126,9 +117,9 @@ class RecentProjectsStore {
126117 attributeSet: attributeSet
127118 )
128119 }
129- CSSearchableIndex . default ( ) . indexSearchableItems ( searchableItems) { error in
120+ CSSearchableIndex . default ( ) . indexSearchableItems ( searchableItems) { [ weak self ] error in
130121 if let error = error {
131- print ( error)
122+ self ? . logger . debug ( " Failed to donate recent projects, error: \( error , privacy : . auto ) " )
132123 }
133124 }
134125 }
0 commit comments