Skip to content

Commit

Permalink
Merge pull request #13 from PhilippeBoisney/dev
Browse files Browse the repository at this point in the history
Handle Memory Warning
  • Loading branch information
PhilippeBoisney committed Mar 15, 2017
2 parents fb9c30a + 277731c commit 13e572d
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion Pod/Classes/ModernSearchBar.swift
Expand Up @@ -75,12 +75,12 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
setup()
}


private func setup(){
self.delegate = self
self.isSuggestionsViewOpened = false
self.interceptOrientationChange()
self.interceptKeyboardChange()
self.interceptMemoryWarning()
}

private func configureViews(){
Expand Down Expand Up @@ -331,6 +331,7 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
self.addViewToParent(view: self.suggestionsShadow)
self.addViewToParent(view: self.suggestionsView)
self.isSuggestionsViewOpened = true
self.suggestionsView.reloadData()
}
}
}
Expand Down Expand Up @@ -429,6 +430,18 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
// UTILS
// --------------------------------

private func clearCacheOfList(){
///Clearing cache
for suggestionItem in self.suggestionListWithUrl {
suggestionItem.imgCache = nil
}
///Clearing cache
for suggestionItem in self.suggestionListWithUrlFiltred {
suggestionItem.imgCache = nil
}
self.suggestionsView.reloadData()
}

private func addViewToParent(view: UIView){
if let topController = getTopViewController() {
let superView: UIView = topController.view
Expand Down Expand Up @@ -479,6 +492,8 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
}
}

// ---------------

private func interceptKeyboardChange(){
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
Expand All @@ -498,6 +513,16 @@ public class ModernSearchBar: UISearchBar, UISearchBarDelegate, UITableViewDataS
self.updateSizeSuggestionsView()
}

// ---------------

private func interceptMemoryWarning(){
NotificationCenter.default.addObserver(self, selector: #selector(didReceiveMemoryWarning(notification:)), name: NSNotification.Name.UIApplicationDidReceiveMemoryWarning, object: nil)
}

@objc private func didReceiveMemoryWarning(notification: NSNotification) {
self.clearCacheOfList()
}

// --------------------------------
// PUBLIC ACCESS
// --------------------------------
Expand Down

0 comments on commit 13e572d

Please sign in to comment.