<commit_before>//
//  SearchMangaViewController.swift
//  Yomu
//
//  Created by <NAME> on 6/5/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import Toaster
import Swiftz

class SearchMangaViewController: UITableViewController {
  var searchMangaHeader: SearchMangaHeader!
  var viewModel = SearchedMangaCollectionViewModel()
  let newManga = PublishSubject<SearchedMangaViewModel>()
  let disposeBag = DisposeBag()

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(R.nib.searchedMangaCell)

    searchMangaHeader = R.nib.searchMangaHeader.firstView(owner: nil)
    tableView.tableHeaderView = searchMangaHeader

    searchMangaHeader
      .searchInput
      .rx.text.orEmpty
      .filter { $0.characters.count > 2 } // At least 3 characters
      .throttle(1.0, latest: true, scheduler: MainScheduler.instance)
      .subscribe(onNext: { [weak self] in
        guard let `self` = self else {
          return
        }

        // Is there a better way to cancel previous requests?
        self.viewModel.disposeBag = DisposeBag()
        self.viewModel
          .search(term: $0)
          .disposed(by: self.viewModel.disposeBag)
      })
      .disposed(by: searchMangaHeader.disposeBag)

    viewModel
      .fetching
      .drive(UIApplication.shared.rx.isNetworkActivityIndicatorVisible)
      .disposed(by: disposeBag)

    viewModel
      .reload
      .drive(onNext: tableView.reloadData)
      .disposed(by: disposeBag)
  }

  // MARK: - Table view data source
  override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return viewModel.count
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(
      withIdentifier: R.nib.searchedMangaCell.identifier,
      for: indexPath
    ) as! SearchedMangaCell

    cell.setup(viewModel: viewModel[indexPath.row])

    return cell
  }

  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let searchedManga = viewModel[indexPath.row]

    guard !searchedManga.existsInDb() else {
      let _disposeBag = DisposeBag()
      ToastCenter.default.currentToast?.cancel()

      searchedManga
        .title
        .drive(onNext: {
          Toast(text: "Whoops, looks like \($0) is already in your collection", delay: 0, duration: Delay.short).show()
        })
        .disposed(by: _disposeBag)

      return
    }

    newManga.on(.next(searchedManga))
  }
}<commit_msg>Resign search input as view responder when user clicks searched manga<commit_after>//
//  SearchMangaViewController.swift
//  Yomu
//
//  Created by <NAME> on 6/5/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import Toaster
import Swiftz

class SearchMangaViewController: UITableViewController {
  var searchMangaHeader: SearchMangaHeader!
  var viewModel = SearchedMangaCollectionViewModel()
  let newManga = PublishSubject<SearchedMangaViewModel>()
  let disposeBag = DisposeBag()

  override func viewDidLoad() {
    super.viewDidLoad()

    tableView.register(R.nib.searchedMangaCell)

    searchMangaHeader = R.nib.searchMangaHeader.firstView(owner: nil)
    tableView.tableHeaderView = searchMangaHeader

    searchMangaHeader
      .searchInput
      .rx.text.orEmpty
      .filter { $0.characters.count > 2 } // At least 3 characters
      .throttle(1.0, latest: true, scheduler: MainScheduler.instance)
      .subscribe(onNext: { [weak self] in
        guard let `self` = self else {
          return
        }

        // Is there a better way to cancel previous requests?
        self.viewModel.disposeBag = DisposeBag()
        self.viewModel
          .search(term: $0)
          .disposed(by: self.viewModel.disposeBag)
      })
      .disposed(by: searchMangaHeader.disposeBag)

    viewModel
      .fetching
      .drive(UIApplication.shared.rx.isNetworkActivityIndicatorVisible)
      .disposed(by: disposeBag)

    viewModel
      .reload
      .drive(onNext: tableView.reloadData)
      .disposed(by: disposeBag)
  }

  // MARK: - Table view data source
  override func numberOfSections(in tableView: UITableView) -> Int {
    return 1
  }

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return viewModel.count
  }

  override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(
      withIdentifier: R.nib.searchedMangaCell.identifier,
      for: indexPath
    ) as! SearchedMangaCell

    cell.setup(viewModel: viewModel[indexPath.row])

    return cell
  }

  override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 80
  }

  override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let searchedManga = viewModel[indexPath.row]

    searchMangaHeader.searchInput.resignFirstResponder()

    guard !searchedManga.existsInDb() else {
      let _disposeBag = DisposeBag()
      ToastCenter.default.currentToast?.cancel()

      searchedManga
        .title
        .drive(onNext: {
          Toast(text: "Whoops, looks like \($0) is already in your collection", delay: 0, duration: Delay.short).show()
        })
        .disposed(by: _disposeBag)

      return
    }

    newManga.on(.next(searchedManga))
  }
}
{'fraction_non_alphanumeric': 0.09284467713787085, 'fraction_numerical': 0.006631762652705061, 'mean_word_length': 3.0732054015636106, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '3311480', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1739, 'n_words': 411}