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

Commit

Permalink
Refactor SearchHistoryUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
sblatz committed Feb 14, 2019
1 parent 5578130 commit 0bb9b05
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 60 deletions.
1 change: 1 addition & 0 deletions Blockzilla/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate, ModalDelegate, AppSplashC
// We clear the Caches directory after each Erase, but WebKit apparently maintains
// localStorage in-memory (bug 1319208), so we just disable it altogether.
UserDefaults.standard.set(false, forKey: "WebKitLocalStorageEnabledPreferenceKey")
UserDefaults.standard.removeObject(forKey: "searchedHistory")

// Re-register the blocking lists at startup in case they've changed.
Utils.reloadSafariContentBlocker()
Expand Down
2 changes: 0 additions & 2 deletions Blockzilla/BrowserViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,8 +418,6 @@ class BrowserViewController: UIViewController {

func resetBrowser(hidePreviousSession: Bool = false) {

UserDefaults.standard.set(nil, forKey: "searchedHistory")

// Used when biometrics fail and the previous session should be obscured
if hidePreviousSession {
clearBrowser()
Expand Down
82 changes: 24 additions & 58 deletions Blockzilla/SearchHistoryUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,55 +17,37 @@ struct textSearched {
guard let text = dictionary["text"], let isCurrentSearch = dictionary["isCurrentSearch"] else { return nil }
self.init(text: text as! String, isCurrentSearch: isCurrentSearch as! Bool)
}

var propertyListRepresentation: [String: Any] {
return ["text": text, "isCurrentSearch": isCurrentSearch]
}

}

class SearchHistoryUtils {

static var isFromURLBar = false
static var isNavigating = false
static var isReload = false
private static var currentStack = [textSearched]()

static func pushSearchToStack(with searchedText: String) {
var currentStack = [textSearched]()

if let propertylistSearchesRead = UserDefaults.standard.object(forKey: "searchedHistory") as? [[String: Any]] {
currentStack = propertylistSearchesRead.compactMap { textSearched(dictionary: $0) }

// Check whether the lastSearch is the current search. If not, remove subsequent searches
if let lastSearch = currentStack.last, !lastSearch.isCurrentSearch {
for index in 0..<currentStack.count {
if currentStack[index].isCurrentSearch {
currentStack.removeSubrange(index+1..<currentStack.count)
break
}
}
}

// Check whether the lastSearch is the current search. If not, remove subsequent searches
if let lastSearch = currentStack.last, !lastSearch.isCurrentSearch {
for index in 0..<currentStack.count {
currentStack[index].isCurrentSearch = false
if currentStack[index].isCurrentSearch {
currentStack.removeSubrange(index+1..<currentStack.count)
break
}
}
}

currentStack.append(textSearched(text: searchedText, isCurrentSearch: true))
for index in 0..<currentStack.count {
currentStack[index].isCurrentSearch = false
}

let propertylistSearchesWrite = currentStack.map { $0.propertyListRepresentation }
UserDefaults.standard.set(propertylistSearchesWrite, forKey: "searchedHistory")
currentStack.append(textSearched(text: searchedText, isCurrentSearch: true))
}

static func pullSearchFromStack() -> String? {
var currentStack = [textSearched]()
if let propertylistSearchesRead = UserDefaults.standard.object(forKey: "searchedHistory") as? [[String: Any]] {

currentStack = propertylistSearchesRead.compactMap { textSearched(dictionary: $0) }
for search in currentStack {
if search.isCurrentSearch {
return search.text
}
for search in currentStack {
if search.isCurrentSearch {
return search.text
}
}

Expand All @@ -74,39 +56,23 @@ class SearchHistoryUtils {

static func goForward() {
isNavigating = true
var currentStack = [textSearched]()
if let propertylistSearchesRead = UserDefaults.standard.object(forKey: "searchedHistory") as? [[String: Any]] {

currentStack = propertylistSearchesRead.compactMap { textSearched(dictionary: $0) }

for index in 0..<currentStack.count {
if currentStack[index].isCurrentSearch && index + 1 < currentStack.count {
currentStack[index + 1].isCurrentSearch = true
currentStack[index].isCurrentSearch = false
break
}
for index in 0..<currentStack.count {
if currentStack[index].isCurrentSearch && index + 1 < currentStack.count {
currentStack[index + 1].isCurrentSearch = true
currentStack[index].isCurrentSearch = false
break
}
let propertylistSearchesWrite = currentStack.map { $0.propertyListRepresentation }
UserDefaults.standard.set(propertylistSearchesWrite, forKey: "searchedHistory")
}
}

static func goBack() {
isNavigating = true
var currentStack = [textSearched]()
if let propertylistSearchesRead = UserDefaults.standard.object(forKey: "searchedHistory") as? [[String: Any]] {

currentStack = propertylistSearchesRead.compactMap { textSearched(dictionary: $0) }

for index in 0..<currentStack.count {
if currentStack[index].isCurrentSearch && index - 1 >= 0 {
currentStack[index - 1].isCurrentSearch = true
currentStack[index].isCurrentSearch = false
break
}
for index in 0..<currentStack.count {
if currentStack[index].isCurrentSearch && index - 1 >= 0 {
currentStack[index - 1].isCurrentSearch = true
currentStack[index].isCurrentSearch = false
break
}
let propertylistSearchesWrite = currentStack.map { $0.propertyListRepresentation }
UserDefaults.standard.set(propertylistSearchesWrite, forKey: "searchedHistory")
}
}
}

0 comments on commit 0bb9b05

Please sign in to comment.