<commit_before>//
//  TopicListViewController.swift
//  Stage1st
//
//  Created by <NAME> on 4/9/16.
//  Copyright © 2016 Renaissance. All rights reserved.
//

import Ainoaibo
import ReactiveSwift
import DeviceKit

final class TopicListViewController: UIViewController {
    let viewModel: TopicListViewModel

    /// Sometimes we can not get what we need by delegation.
    var containerViewController: ContainerViewController! = nil {
        didSet {
            containerViewController.topicListSelection <~ viewModel.tabBarSelection
        }
    }

    let naviItem = UINavigationItem()
    let navigationBar = UINavigationBar(frame: .zero)

    let tableView = UITableView(frame: .zero, style: .plain)

    lazy var refreshControl: ODRefreshControl = {
        return ODRefreshControl(in: tableView)
    }()

    let searchBar = UISearchBar(frame: .zero)
    let searchBarWrapperView = UIView(frame: .zero)
    let footerView = MessageFooterView()
    let refreshHUD = S1HUD(frame: .zero)

    private var observations = [NSKeyValueObservation]()

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        viewModel = TopicListViewModel(dataCenter: AppEnvironment.current.dataCenter)

        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        // Navigation Bar
        naviItem.title = "Stage1st"
        naviItem.leftBarButtonItem = UIBarButtonItem(
            image: UIImage(named: "Settings"),
            style: .plain,
            target: self,
            action: #selector(settings)
        )

        AppEnvironment.current.dataCenter.noticeCount
            .map { ($0?.myPost ?? 0) == 0 ? UIImage(named: "Notice") : UIImage(named: "Notice2") }
            .producer.startWithValues { [weak self] (image) in
                guard let strongSelf = self else { return }
                strongSelf.naviItem.rightBarButtonItems = [
                    UIBarButtonItem(
                        image: UIImage(named: "Archive"),
                        style: .plain,
                        target: strongSelf,
                        action: #selector(TopicListViewController.archive)
                    ),
                    UIBarButtonItem(
                        image: image,
                        style: .plain,
                        target: strongSelf,
                        action: #selector(TopicListViewController.notification)
                    )
                ]
            }

        navigationBar.delegate = self
        navigationBar.pushItem(naviItem, animated: false)

        // Search Bar
        if #available(iOS 11.0, *) {
            searchBar.frame = CGRect(x: 0.0, y: 0.0, width: 0.0, height: 50.0)
        } else {<commit_msg>Fix a background UI operation issue which may leads to crash.<commit_after>//
//  TopicListViewController.swift
//  Stage1st
//
//  Created by <NAME> on 4/9/16.
//  Copyright © 2016 Renaissance. All rights reserved.
//

import Ainoaibo
import ReactiveSwift
import DeviceKit

final class TopicListViewController: UIViewController {
    let viewModel: TopicListViewModel

    /// Sometimes we can not get what we need by delegation.
    var containerViewController: ContainerViewController! = nil {
        didSet {
            containerViewController.topicListSelection <~ viewModel.tabBarSelection
        }
    }

    let naviItem = UINavigationItem()
    let navigationBar = UINavigationBar(frame: .zero)

    let tableView = UITableView(frame: .zero, style: .plain)

    lazy var refreshControl: ODRefreshControl = {
        return ODRefreshControl(in: tableView)
    }()

    let searchBar = UISearchBar(frame: .zero)
    let searchBarWrapperView = UIView(frame: .zero)
    let footerView = MessageFooterView()
    let refreshHUD = S1HUD(frame: .zero)

    private var observations = [NSKeyValueObservation]()

    public override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        viewModel = TopicListViewModel(dataCenter: AppEnvironment.current.dataCenter)

        super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)

        // Navigation Bar
        naviItem.title = "Stage1st"
        naviItem.leftBarButtonItem = UIBarButtonItem(
            image: UIImage(named: "Settings"),
            style: .plain,
            target: self,
            action: #selector(settings)
        )

        AppEnvironment.current.dataCenter.noticeCount
            .map { ($0?.myPost ?? 0) == 0 ? UIImage(named: "Notice") : UIImage(named: "Notice2") }
            .producer
            .start(on: UIScheduler())
            .observe(on: UIScheduler())
            .startWithValues { [weak self] (image) in
                guard let strongSelf = self else { return }
                strongSelf.naviItem.rightBarButtonItems = [
                    UIBarButtonItem(
                        image: UIImage(named: "Archive"),
                        style: .plain,
                        target: strongSelf,
                        action: #selector(TopicListViewController.archive)
                    ),
                    UIBarButtonItem(
                        image: image,
                        style: .plain,
                        target: strongSelf,
                        action: #selector(TopicListViewController.notification)
                    )
                ]
            }

        navigationBar.delegate = self
        navigationBar.pushItem(naviItem, animated: false)

        // Search Bar
        if #available(iOS 11.0, *) {
            searchBar.frame = CGRect(x: 0.0, y: 0.0, width: 0.0, height: 50.0)
        } else {
{'fraction_non_alphanumeric': 0.09077938291421438, 'fraction_numerical': 0.009630818619582664, 'mean_word_length': 1.9719130895601484, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, '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': '2364443', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1465, 'n_words': 363}