<commit_before>import UIKit

protocol SavedViewControllerDelegate: NSObjectProtocol {
    func savedWillShowSortAlert(_ saved: SavedViewController, from button: UIButton)
    func saved(_ saved: SavedViewController, searchBar: UISearchBar, textDidChange searchText: String)
    func saved(_ saved: SavedViewController, searchBarSearchButtonClicked searchBar: UISearchBar)
    func saved(_ saved: SavedViewController, searchBarTextDidBeginEditing searchBar: UISearchBar)
    func saved(_ saved: SavedViewController, searchBarTextDidEndEditing searchBar: UISearchBar)
}

@objc(WMFSavedViewController)
class SavedViewController: ViewController {

    private var savedArticlesViewController: SavedArticlesViewController!
    
    private lazy var readingListsViewController: ReadingListsViewController? = {
        guard let dataStore = dataStore else {
            assertionFailure("dataStore is nil")
            return nil
        }
        let readingListsCollectionViewController = ReadingListsViewController(with: dataStore)
        return readingListsCollectionViewController
    }()

    @IBOutlet weak var containerView: UIView!
    @IBOutlet var extendedNavBarView: UIView!
    @IBOutlet var underBarView: UIView!
    @IBOutlet var allArticlesButton: UIButton!
    @IBOutlet var readingListsButton: UIButton!
    @IBOutlet var searchBar: UISearchBar!
    @IBOutlet weak var actionButton: UIButton!
    @IBOutlet weak var separatorView: UIView!
    @IBOutlet var toggleButtons: [UIButton]!
    @IBOutlet weak var progressContainerView: UIView!

    lazy var addReadingListBarButtonItem: UIBarButtonItem = {
        return UIBarButtonItem(barButtonSystemItem: .add, target: readingListsViewController.self, action: #selector(readingListsViewController?.presentCreateReadingListViewController))
    }()
    
    fileprivate lazy var savedProgressViewController: SavedProgressViewController? = SavedProgressViewController.wmf_initialViewControllerFromClassStoryboard()

    public weak var savedDelegate: SavedViewControllerDelegate?
    
    // MARK: - Initalization and setup
    
    @objc public var dataStore: MWKDataStore? {
        didSet {
            guard let newValue = dataStore else {
                assertionFailure("cannot set dataStore to nil")
                return
            }
            title = CommonStrings.savedTabTitle
            savedArticlesViewController = SavedArticlesViewController(with: newValue)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // MARK: - Toggling views
    
    private enum View: Int {
        case savedArticles, readingLists
    }
    
    @IBAction func toggleButtonPressed(_ sender: UIButton) {
        toggleButtons.first { $0.tag != sender.tag }?.isSelected = false
        sender.isSelected = true
        currentView = View(rawValue: sender.tag) ?? .savedArticles
    }
    
    private var activeEditableCollection: EditableCollection?
    
    private var currentView: View = .savedArticles {
        didSet {
            searchBar.resignFirstResponder()
            switch currentView {
            case .savedArticles:
                removeChild(readingListsViewController)
                addChild(savedArticlesViewController)
                savedArticlesViewController.editController.navigationDelegate = self
                readingListsViewController?.editController.navigationDelegate = nil
                savedDelegate = savedArticlesViewController
                leftButtonType = .none
                isSearchBarHidden = isSavedArticlesEmpty
                scrollView = savedArticlesViewController.collectionView
                activeEditableCollection = savedArticlesViewController
            case .readingLists :
                readingListsViewController?.editController.navigationDelegate = self
                savedArticlesViewController.editController.navigationDelegate = nil
                removeChild(savedArticlesViewController)
                addChild(readingListsViewController)
                leftButtonType = .add
                scrollView = readingListsViewController?.collectionView
                isSearchBarHidden = true
                activeEditableCollection = readingListsViewController
            }
        }
    }
    
    private var isSavedArticlesEmpty: Bool {
        return savedArticlesViewController.editController.isCollectionViewEmpty
    }
    
    private enum LeftButtonType {
        case add
        case none
    }
    
    private var leftButtonType: LeftButtonType = .none {
        didSet {
            guard oldValue != leftButtonType else {
                return
            }
            switch leftButtonType {
            case .add:
                navigationItem.leftBarButtonItems = [addReadingListBarButtonItem]
            default:
                navigationItem.leftBarButtonItems = []
            }
        }
    }

    private var isSearchBarHidden: Bool = false {
        didSet {
            if isSearchBarHidden {
                navigationBar.removeExtendedNavigationBarView()
            } else {
                navigationBar.addExtendedNavigationBarView(extendedNavBarView)
            }
        }
    }
    
    private func addChild(_ vc: UIViewController?) {
        guard let vc = vc else {
            return
        }
        addChildViewController(vc)
        containerView.wmf_addSubviewWithConstraintsToEdges(vc.view)
        vc.didMove(toParentViewController: self)
    }
    
    private func removeChild(_ vc: UIViewController?) {
        guard let vc = vc else {
            return
        }
        vc.view.removeFromSuperview()
        vc.willMove(toParentViewController: nil)
        vc.removeFromParentViewController()
    }
    
    // MARK: - View lifecycle
    
    override func viewDidLoad() {
        navigationBar.addExtendedNavigationBarView(extendedNavBarView)
        navigationBar.addUnderNavigationBarView(underBarView)
        navigationBar.isBackVisible = false

        wmf_add(childController:savedProgressViewController, andConstrainToEdgesOfContainerView: progressContainerView)

        currentView = .savedArticles
        
        let allArticlesButtonTitle = WMFLocalizedString("saved-all-articles-title", value: "All articles", comment: "Title of the all articles button on Saved screen")
        allArticlesButton.setTitle(allArticlesButtonTitle, for: .normal)
        let readingListsButtonTitle = WMFLocalizedString("saved-reading-lists-title", value: "Reading lists", comment: "Title of the reading lists button on Saved screen")
        readingListsButton.setTitle(readingListsButtonTitle, for: .normal)

        searchBar.delegate = self
        searchBar.returnKeyType = .search
        searchBar.placeholder = WMFLocalizedString("saved-search-default-text", value:"Search", comment:"Placeholder text for the search bar in Saved")
        
        extendedLayoutIncludesOpaqueBars = true
        edgesForExtendedLayout = .all
        
        super.viewDidLoad()
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        actionButton.titleLabel?.setFont(with: .system, style: .body, traitCollection: traitCollection)
    }
    
    // MARK: - Sorting and searching
    
    private enum ActionButtonType {
        case sort
        case cancel
    }
    
    private var actionButtonType: ActionButtonType = .sort {
        didSet {
            guard oldValue != actionButtonType else {
                return
            }
            switch actionButtonType {
            case .sort:
                actionButton.setTitle(CommonStrings.sortActionTitle, for: .normal)
            case .cancel:
                actionButton.setTitle(CommonStrings.cancelActionTitle, for: .normal)
            }<commit_msg>set actionButtonType to .sort, remove guard so that we always set title<commit_after>import UIKit

protocol SavedViewControllerDelegate: NSObjectProtocol {
    func savedWillShowSortAlert(_ saved: SavedViewController, from button: UIButton)
    func saved(_ saved: SavedViewController, searchBar: UISearchBar, textDidChange searchText: String)
    func saved(_ saved: SavedViewController, searchBarSearchButtonClicked searchBar: UISearchBar)
    func saved(_ saved: SavedViewController, searchBarTextDidBeginEditing searchBar: UISearchBar)
    func saved(_ saved: SavedViewController, searchBarTextDidEndEditing searchBar: UISearchBar)
}

@objc(WMFSavedViewController)
class SavedViewController: ViewController {

    private var savedArticlesViewController: SavedArticlesViewController!
    
    private lazy var readingListsViewController: ReadingListsViewController? = {
        guard let dataStore = dataStore else {
            assertionFailure("dataStore is nil")
            return nil
        }
        let readingListsCollectionViewController = ReadingListsViewController(with: dataStore)
        return readingListsCollectionViewController
    }()

    @IBOutlet weak var containerView: UIView!
    @IBOutlet var extendedNavBarView: UIView!
    @IBOutlet var underBarView: UIView!
    @IBOutlet var allArticlesButton: UIButton!
    @IBOutlet var readingListsButton: UIButton!
    @IBOutlet var searchBar: UISearchBar!
    @IBOutlet weak var actionButton: UIButton!
    @IBOutlet weak var separatorView: UIView!
    @IBOutlet var toggleButtons: [UIButton]!
    @IBOutlet weak var progressContainerView: UIView!

    lazy var addReadingListBarButtonItem: UIBarButtonItem = {
        return UIBarButtonItem(barButtonSystemItem: .add, target: readingListsViewController.self, action: #selector(readingListsViewController?.presentCreateReadingListViewController))
    }()
    
    fileprivate lazy var savedProgressViewController: SavedProgressViewController? = SavedProgressViewController.wmf_initialViewControllerFromClassStoryboard()

    public weak var savedDelegate: SavedViewControllerDelegate?
    
    // MARK: - Initalization and setup
    
    @objc public var dataStore: MWKDataStore? {
        didSet {
            guard let newValue = dataStore else {
                assertionFailure("cannot set dataStore to nil")
                return
            }
            title = CommonStrings.savedTabTitle
            savedArticlesViewController = SavedArticlesViewController(with: newValue)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    // MARK: - Toggling views
    
    private enum View: Int {
        case savedArticles, readingLists
    }
    
    @IBAction func toggleButtonPressed(_ sender: UIButton) {
        toggleButtons.first { $0.tag != sender.tag }?.isSelected = false
        sender.isSelected = true
        currentView = View(rawValue: sender.tag) ?? .savedArticles
    }
    
    private var activeEditableCollection: EditableCollection?
    
    private var currentView: View = .savedArticles {
        didSet {
            searchBar.resignFirstResponder()
            switch currentView {
            case .savedArticles:
                removeChild(readingListsViewController)
                addChild(savedArticlesViewController)
                savedArticlesViewController.editController.navigationDelegate = self
                readingListsViewController?.editController.navigationDelegate = nil
                savedDelegate = savedArticlesViewController
                leftButtonType = .none
                isSearchBarHidden = isSavedArticlesEmpty
                scrollView = savedArticlesViewController.collectionView
                activeEditableCollection = savedArticlesViewController
            case .readingLists :
                readingListsViewController?.editController.navigationDelegate = self
                savedArticlesViewController.editController.navigationDelegate = nil
                removeChild(savedArticlesViewController)
                addChild(readingListsViewController)
                leftButtonType = .add
                scrollView = readingListsViewController?.collectionView
                isSearchBarHidden = true
                activeEditableCollection = readingListsViewController
            }
        }
    }
    
    private var isSavedArticlesEmpty: Bool {
        return savedArticlesViewController.editController.isCollectionViewEmpty
    }
    
    private enum LeftButtonType {
        case add
        case none
    }
    
    private var leftButtonType: LeftButtonType = .none {
        didSet {
            guard oldValue != leftButtonType else {
                return
            }
            switch leftButtonType {
            case .add:
                navigationItem.leftBarButtonItems = [addReadingListBarButtonItem]
            default:
                navigationItem.leftBarButtonItems = []
            }
        }
    }

    private var isSearchBarHidden: Bool = false {
        didSet {
            if isSearchBarHidden {
                navigationBar.removeExtendedNavigationBarView()
            } else {
                navigationBar.addExtendedNavigationBarView(extendedNavBarView)
            }
        }
    }
    
    private func addChild(_ vc: UIViewController?) {
        guard let vc = vc else {
            return
        }
        addChildViewController(vc)
        containerView.wmf_addSubviewWithConstraintsToEdges(vc.view)
        vc.didMove(toParentViewController: self)
    }
    
    private func removeChild(_ vc: UIViewController?) {
        guard let vc = vc else {
            return
        }
        vc.view.removeFromSuperview()
        vc.willMove(toParentViewController: nil)
        vc.removeFromParentViewController()
    }
    
    // MARK: - View lifecycle
    
    override func viewDidLoad() {
        navigationBar.addExtendedNavigationBarView(extendedNavBarView)
        navigationBar.addUnderNavigationBarView(underBarView)
        navigationBar.isBackVisible = false

        wmf_add(childController:savedProgressViewController, andConstrainToEdgesOfContainerView: progressContainerView)

        currentView = .savedArticles
        
        let allArticlesButtonTitle = WMFLocalizedString("saved-all-articles-title", value: "All articles", comment: "Title of the all articles button on Saved screen")
        allArticlesButton.setTitle(allArticlesButtonTitle, for: .normal)
        let readingListsButtonTitle = WMFLocalizedString("saved-reading-lists-title", value: "Reading lists", comment: "Title of the reading lists button on Saved screen")
        readingListsButton.setTitle(readingListsButtonTitle, for: .normal)

        searchBar.delegate = self
        searchBar.returnKeyType = .search
        searchBar.placeholder = WMFLocalizedString("saved-search-default-text", value:"Search", comment:"Placeholder text for the search bar in Saved")
        
        extendedLayoutIncludesOpaqueBars = true
        edgesForExtendedLayout = .all
        
        actionButtonType = .sort
        
        super.viewDidLoad()
    }
    
    override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
        super.traitCollectionDidChange(previousTraitCollection)
        actionButton.titleLabel?.setFont(with: .system, style: .body, traitCollection: traitCollection)
    }
    
    // MARK: - Sorting and searching
    
    private enum ActionButtonType {
        case sort
        case cancel
    }
    
    private var actionButtonType: ActionButtonType = .sort {
        didSet {
            switch actionButtonType {
            case .sort:
                actionButton.setTitle(CommonStrings.sortActionTitle, for: .normal)
            case .cancel:
                actionButton.setTitle(CommonStrings.cancelActionTitle, for: .normal)
            }
{'fraction_non_alphanumeric': 0.06052548274770497, 'fraction_numerical': 0.00012662234884457108, 'mean_word_length': 2.6163003663003663, 'pattern_counts': {'":': 0, '<': 3, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5987864', 'n_tokens_mistral': 4052, 'n_tokens_neox': 3789, 'n_words': 876}