Skip to content
This repository has been archived by the owner on Mar 6, 2024. It is now read-only.

Commit

Permalink
Fixes #2453 - "Restore Default Search Engines" doesn't work for remov…
Browse files Browse the repository at this point in the history
…ed default search engines (#2466) (#2468)

Co-authored-by: Bogdan Cernea <bogdan.cernea@softvision.com>
(cherry picked from commit 59ee535)

Co-authored-by: Bogdan Cernea <47420700+bogdancernea28@users.noreply.github.com>
  • Loading branch information
mergify[bot] and bogdancernea28 committed Sep 27, 2021
1 parent d6b68aa commit 146b05a
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions Blockzilla/SearchEngineManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ class SearchEngineManager {
}

func hasDisabledDefaultEngine() -> Bool {
return readCustomEngines().count > 0
return getDisabledDefaultEngineNames().count > 0
}

func restoreDisabledDefaultEngines() {
prefs.removeObject(forKey: SearchEngineManager.prefKeyCustomEngines)
prefs.removeObject(forKey: SearchEngineManager.prefKeyDisabledEngines)
loadEngines()
}

Expand Down Expand Up @@ -145,19 +145,28 @@ class SearchEngineManager {
}

private func readCustomEngines() -> [SearchEngine] {
if let archiveData = prefs.value(forKey: SearchEngineManager.prefKeyCustomEngines) as? NSData {
let archivedCustomEngines = NSKeyedUnarchiver.unarchiveObject(with: archiveData as Data)
let customEngines = archivedCustomEngines as? [SearchEngine] ?? [SearchEngine]()
return customEngines.map { engine in
engine.isCustom = true
return engine
do {
if let archiveData = prefs.value(forKey: SearchEngineManager.prefKeyCustomEngines) as? NSData {
let archivedCustomEngines = try NSKeyedUnarchiver.unarchiveTopLevelObjectWithData(archiveData as Data)
let customEngines = archivedCustomEngines as? [SearchEngine] ?? [SearchEngine]()
return customEngines.map { engine in
engine.isCustom = true
return engine
}
}
}
catch {
print(error)
}
return [SearchEngine]()
}

private func saveCustomEngines(customEngines: [SearchEngine]) {
prefs.set(NSKeyedArchiver.archivedData(withRootObject: customEngines), forKey: SearchEngineManager.prefKeyCustomEngines)
do {
try prefs.set(NSKeyedArchiver.archivedData(withRootObject: customEngines, requiringSecureCoding: false), forKey: SearchEngineManager.prefKeyCustomEngines)
} catch {
print(error)
}
}

private func getDisabledDefaultEngineNames() -> [String] {
Expand Down

0 comments on commit 146b05a

Please sign in to comment.