<commit_before>import UIKit
import MapKit
import WMF
import TUSafariActivity

enum PlaceSearchType {
    case text
    case location
    case top
    case saved
}

struct PlaceSearch {
    let type: PlaceSearchType
    let string: String?
    let region: MKCoordinateRegion?
    let localizedDescription: String?
    let searchCompletion: MKLocalSearchCompletion?
}

protocol PlaceSearchSuggestionControllerDelegate: NSObjectProtocol {
    func placeSearchSuggestionController(_ controller: PlaceSearchSuggestionController, didSelectSearch search: PlaceSearch)
}

extension MKCoordinateRegion {
    var width: CLLocationDistance {
        get {
            let halfLongitudeDelta = span.longitudeDelta * 0.5
            let left =  CLLocation(latitude: center.latitude, longitude: center.longitude - halfLongitudeDelta)
            let right =  CLLocation(latitude: center.latitude, longitude: center.longitude + halfLongitudeDelta)
            let width = right.distance(from: left)
            return width
        }
    }
    
    var height: CLLocationDistance {
        get {
            let halfLatitudeDelta = span.latitudeDelta * 0.5
            let top = CLLocation(latitude: center.latitude + halfLatitudeDelta, longitude: center.longitude)
            let bottom = CLLocation(latitude: center.latitude - halfLatitudeDelta, longitude: center.longitude)
            let height = top.distance(from: bottom)
            return height
        }
    }
}

class PlaceSearchSuggestionController: NSObject, UITableViewDataSource, UITableViewDelegate {
    static let cellReuseIdentifier = "org.wikimedia.places"
    
    var tableView: UITableView = UITableView() {
        didSet {
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: PlaceSearchSuggestionController.cellReuseIdentifier)
            tableView.dataSource = self
            tableView.delegate = self
            tableView.reloadData()
        }
    }
    
    var searches: [[PlaceSearch]] = [[],[],[],[]]{
        didSet {
            tableView.reloadData()
        }
    }
    
    weak var delegate: PlaceSearchSuggestionControllerDelegate?
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return searches.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searches[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier:  PlaceSearchSuggestionController.cellReuseIdentifier, for: indexPath)
        let search = searches[indexPath.section][indexPath.row]
        cell.textLabel?.text = search.localizedDescription
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let search = searches[indexPath.section][indexPath.row]
        delegate?.placeSearchSuggestionController(self, didSelectSearch: search)
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        guard searches[section].count > 0 else {
            return nil
        }
        switch section {
        case 0:
            return localizedStringForKeyFallingBackOnEnglish("places-search-suggested-searches-header")
        case 1:
            return localizedStringForKeyFallingBackOnEnglish("places-search-recently-searched-header")
        default:
            return nil
        }
    }
}


class PlacesViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate, ArticlePopoverViewControllerDelegate, UITableViewDataSource, UITableViewDelegate, MKLocalSearchCompleterDelegate, PlaceSearchSuggestionControllerDelegate, WMFLocationManagerDelegate {
    
    @IBOutlet weak var redoSearchButton: UIButton!
    let nearbyFetcher = WMFLocationSearchFetcher()
    
    var localSearch: MKLocalSearch?
    let localCompleter = MKLocalSearchCompleter()
    let globalCompleter = MKLocalSearchCompleter()
    
    let locationManager = WMFLocationManager.coarse()
    
    let animationDuration = 0.6
    
    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var listView: UITableView!
    @IBOutlet weak var searchSuggestionView: UITableView!
    
    var searchSuggestionController: PlaceSearchSuggestionController!
    
    var searchBar: UISearchBar!
    var siteURL: URL = NSURL.wmf_URLWithDefaultSiteAndCurrentLocale()!
    var articles: [WMFArticle] = []
    var articleStore: WMFArticleDataStore!
    var dataStore: MWKDataStore!
    var segmentedControl: UISegmentedControl!
    
    var currentGroupingPrecision: QuadKeyPrecision = 1
    
    var currentSearch: PlaceSearch? {
        didSet {
            if let search = currentSearch {
                searchBar.text = search.localizedDescription
                performSearch(search)
            }
        }
    }
    
    var mapRegion: MKCoordinateRegion? {
        didSet {
            guard let region = mapRegion else {
                return
            }
            
            regroupArticlesIfNecessary(forVisibleRegion: region)
            showRedoSearchButtonIfNecessary(forVisibleRegion: region)
            localCompleter.region = region
            
            UIView.animate(withDuration: animationDuration, animations: {
                self.mapView.region = region
            }) { (finished) in
                
            }
        }
    }
    
    var currentSearchRegion: MKCoordinateRegion?

    override func viewDidLoad() {
        super.viewDidLoad()<commit_msg>use regionThatFits<commit_after>import UIKit
import MapKit
import WMF
import TUSafariActivity

enum PlaceSearchType {
    case text
    case location
    case top
    case saved
}

struct PlaceSearch {
    let type: PlaceSearchType
    let string: String?
    let region: MKCoordinateRegion?
    let localizedDescription: String?
    let searchCompletion: MKLocalSearchCompletion?
}

protocol PlaceSearchSuggestionControllerDelegate: NSObjectProtocol {
    func placeSearchSuggestionController(_ controller: PlaceSearchSuggestionController, didSelectSearch search: PlaceSearch)
}

extension MKCoordinateRegion {
    var width: CLLocationDistance {
        get {
            let halfLongitudeDelta = span.longitudeDelta * 0.5
            let left =  CLLocation(latitude: center.latitude, longitude: center.longitude - halfLongitudeDelta)
            let right =  CLLocation(latitude: center.latitude, longitude: center.longitude + halfLongitudeDelta)
            let width = right.distance(from: left)
            return width
        }
    }
    
    var height: CLLocationDistance {
        get {
            let halfLatitudeDelta = span.latitudeDelta * 0.5
            let top = CLLocation(latitude: center.latitude + halfLatitudeDelta, longitude: center.longitude)
            let bottom = CLLocation(latitude: center.latitude - halfLatitudeDelta, longitude: center.longitude)
            let height = top.distance(from: bottom)
            return height
        }
    }
}

class PlaceSearchSuggestionController: NSObject, UITableViewDataSource, UITableViewDelegate {
    static let cellReuseIdentifier = "org.wikimedia.places"
    
    var tableView: UITableView = UITableView() {
        didSet {
            tableView.register(UITableViewCell.self, forCellReuseIdentifier: PlaceSearchSuggestionController.cellReuseIdentifier)
            tableView.dataSource = self
            tableView.delegate = self
            tableView.reloadData()
        }
    }
    
    var searches: [[PlaceSearch]] = [[],[],[],[]]{
        didSet {
            tableView.reloadData()
        }
    }
    
    weak var delegate: PlaceSearchSuggestionControllerDelegate?
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return searches.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return searches[section].count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier:  PlaceSearchSuggestionController.cellReuseIdentifier, for: indexPath)
        let search = searches[indexPath.section][indexPath.row]
        cell.textLabel?.text = search.localizedDescription
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let search = searches[indexPath.section][indexPath.row]
        delegate?.placeSearchSuggestionController(self, didSelectSearch: search)
    }
    
    func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        guard searches[section].count > 0 else {
            return nil
        }
        switch section {
        case 0:
            return localizedStringForKeyFallingBackOnEnglish("places-search-suggested-searches-header")
        case 1:
            return localizedStringForKeyFallingBackOnEnglish("places-search-recently-searched-header")
        default:
            return nil
        }
    }
}


class PlacesViewController: UIViewController, MKMapViewDelegate, UISearchBarDelegate, UIPopoverPresentationControllerDelegate, ArticlePopoverViewControllerDelegate, UITableViewDataSource, UITableViewDelegate, MKLocalSearchCompleterDelegate, PlaceSearchSuggestionControllerDelegate, WMFLocationManagerDelegate {
    
    @IBOutlet weak var redoSearchButton: UIButton!
    let nearbyFetcher = WMFLocationSearchFetcher()
    
    var localSearch: MKLocalSearch?
    let localCompleter = MKLocalSearchCompleter()
    let globalCompleter = MKLocalSearchCompleter()
    
    let locationManager = WMFLocationManager.coarse()
    
    let animationDuration = 0.6
    
    @IBOutlet weak var mapView: MKMapView!
    @IBOutlet weak var listView: UITableView!
    @IBOutlet weak var searchSuggestionView: UITableView!
    
    var searchSuggestionController: PlaceSearchSuggestionController!
    
    var searchBar: UISearchBar!
    var siteURL: URL = NSURL.wmf_URLWithDefaultSiteAndCurrentLocale()!
    var articles: [WMFArticle] = []
    var articleStore: WMFArticleDataStore!
    var dataStore: MWKDataStore!
    var segmentedControl: UISegmentedControl!
    
    var currentGroupingPrecision: QuadKeyPrecision = 1
    
    var currentSearch: PlaceSearch? {
        didSet {
            if let search = currentSearch {
                searchBar.text = search.localizedDescription
                performSearch(search)
            }
        }
    }
    
    var mapRegion: MKCoordinateRegion? {
        didSet {
            guard let region = mapRegion else {
                return
            }
            
            let regionThatFits = mapView.regionThatFits(region)
            regroupArticlesIfNecessary(forVisibleRegion: regionThatFits)
            showRedoSearchButtonIfNecessary(forVisibleRegion: regionThatFits)
            localCompleter.region = regionThatFits
            
            UIView.animate(withDuration: animationDuration, animations: {
                self.mapView.region = regionThatFits
            }) { (finished) in
                
            }
        }
    }
    
    var currentSearchRegion: MKCoordinateRegion?

    override func viewDidLoad() {
        super.viewDidLoad()
{'fraction_non_alphanumeric': 0.06613314572157242, 'fraction_numerical': 0.001758860258552458, 'mean_word_length': 2.781842367808447, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3507374', 'n_tokens_mistral': 3131, 'n_tokens_neox': 2948, 'n_words': 661}