<commit_before>class StoriesController < UITableViewController
  stylesheet :story_cell

  def viewDidLoad
    super
    @data = []
    loadData
    tableView.addPullToRefreshWithActionHandler(Proc.new { loadData })
    tableView.addInfiniteScrollingWithActionHandler(Proc.new { loadMore })
  end

  def viewDidAppear(animated)
    UIApplication.sharedApplication.delegate.nav_controller.setToolbarHidden(true, animated:false)
  end

  def loadData
    BW::HTTP.get("http://node-hnapi.herokuapp.com/news") do |response|
      if response.ok?
        begin
          BW::JSON.parse response.body.to_str do |json|
            @data = json
            view.reloadData
            @more_loaded = false
          end
        rescue
          App.alert("API error, please try again")
        end
      else
        App.alert(response.error_message)
      end
      tableView.pullToRefreshView.stopAnimating
    end
  end

  def loadMore
    if @more_loaded
      tableView.infiniteScrollingView.stopAnimating
      return
    end

    BW::HTTP.get("http://node-hnapi.herokuapp.com/news2") do |response|
      if response.ok?
        begin
          BW::JSON.parse response.body.to_str do |json|
            @data = @data + json
            view.reloadData
            @more_loaded = true
          end
        rescue
          App.alert("API error, please try again")
        end
      else
        App.alert(response.error_message)
      end
      tableView.infiniteScrollingView.stopAnimating
    end
  end

  def viewWillAppear(animated)
    navigationItem.title = 'HipsterNews'
  end<commit_msg>Added searching of downloaded stories<commit_after>class StoriesController < UITableViewController
  stylesheet :story_cell

  def viewDidLoad
    super
    @data = []
    loadData
    tableView.addPullToRefreshWithActionHandler(Proc.new { loadData })
    tableView.addInfiniteScrollingWithActionHandler(Proc.new { loadMore })
    
    search_bar = UISearchBar.alloc.initWithFrame([[0,0],[320,44]])
    search_bar.delegate = self
    search_bar.setTintColor '#ff6600'.to_color
    view.addSubview(search_bar)
    view.tableHeaderView = search_bar
  end

  def viewDidAppear(animated)
    UIApplication.sharedApplication.delegate.nav_controller.setToolbarHidden(true, animated:false)
    tableView.setContentOffset(CGPoint.new(0,44))
  end

  def loadData
    BW::HTTP.get("http://node-hnapi.herokuapp.com/news") do |response|
      if response.ok?
        begin
          BW::JSON.parse response.body.to_str do |json|
            @data = json
            view.reloadData
            @more_loaded = false
          end
        rescue
          App.alert("API error, please try again")
        end
      else
        App.alert(response.error_message)
      end
      tableView.pullToRefreshView.stopAnimating
      tableView.setContentOffset(CGPoint.new(0,44))
    end
  end

  def loadMore
    if @more_loaded
      tableView.infiniteScrollingView.stopAnimating
      return
    end

    BW::HTTP.get("http://node-hnapi.herokuapp.com/news2") do |response|
      if response.ok?
        begin
          BW::JSON.parse response.body.to_str do |json|
            @data = @data + json
            view.reloadData
            @more_loaded = true
          end
        rescue
          App.alert("API error, please try again")
        end
      else
        App.alert(response.error_message)
      end
      tableView.infiniteScrollingView.stopAnimating
    end
  end
  
  def searchBarShouldBeginEditing(search_bar)
    search_bar.setShowsCancelButton true, animated:true
  end
  
  def searchBarSearchButtonClicked(search_bar)
    search_bar.resignFirstResponder
    navigationItem.title = "Search for '#{search_bar.text}'"
    @original_data = @data
    @data = @data.select{|d| d['title'] =~ /#{search_bar.text}/i } || []
    view.reloadData
    search_bar.subviews[2].setEnabled(true)
  end
  
  def searchBarCancelButtonClicked(search_bar)
    navigationItem.title = "HipsterNews"
    search_bar.resignFirstResponder
    @data = @original_data
    view.reloadData
    search_bar.text = ""
    #tableView.setContentOffset(CGPoint.new(0,44))
  end

  def viewWillAppear(animated)
    navigationItem.title = 'HipsterNews'
  end
{'fraction_non_alphanumeric': 0.08704933586337761, 'fraction_numerical': 0.005455407969639468, 'mean_word_length': 2.601195559350982, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4194508', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1277, 'n_words': 233}