<commit_before>//
//  NotificationsViewController.swift
//  Freetime
//
//  Created by <NAME> on 5/13/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import IGListKit
import SnapKit
import FlatCache

class NotificationsViewController: BaseListViewController<NSNumber>,
//    NotificationClientListener,
ForegroundHandlerDelegate,
RatingSectionControllerDelegate,
PrimaryViewController,
TabNavRootViewControllerType,
BaseListViewControllerDataSource,
FlatCacheListener {

    enum InboxType {
        case unread
        case repo(NotificationClient.NotificationRepository)
        case all
    }

    private let client: NotificationClient
    private let foreground = ForegroundHandler(threshold: 5 * 60)
    private let inboxType: InboxType
    private var notificationIDs = [String]()

    // set to nil and update to dismiss the rating control
    private var ratingToken: RatingToken? = RatingController.inFeedToken()

    init(client: NotificationClient, inboxType: InboxType) {
        self.client = client
        self.inboxType = inboxType

        super.init(
            emptyErrorMessage: NSLocalizedString("Cannot load your inbox.", comment: ""),
            dataSource: self
        )

        self.foreground.delegate = self

        switch inboxType {
        case .all:
            title = NSLocalizedString("Archived", comment: "")
        case .unread:
            title = NSLocalizedString("Inbox", comment: "")
        case .repo(let repo):
            title = repo.name
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        resetRightBarItem()

        switch inboxType {
        case .unread:
            let item = UIBarButtonItem(
                image: UIImage(named: "bullets-hollow"),
                style: .plain,
                target: self,
                action: #selector(NotificationsViewController.onMore)
            )
            item.accessibilityLabel = NSLocalizedString("More options", comment: "")
            navigationItem.leftBarButtonItem = item
        case .repo, .all: break
        }

        navigationController?.tabBarItem.badgeColor = Styles.Colors.Red.medium.color
    }

    // MARK: Private API

    @objc func onMore() {
        let alert = UIAlertController.configured(preferredStyle: .actionSheet)

        alert.add(action: UIAlertAction(
            title: NSLocalizedString("View Archived", comment: ""),
            style: .default,
            handler: { [weak self] _ in
                self?.onViewAll()
        }))

        let cache = client.githubClient.cache
        var repoNames = Set<String>()
        for id in notificationIDs {
            guard let notification = cache.get(id: id) as NotificationViewModel?,
                !repoNames.contains(notification.repo)
                else { continue }
            repoNames.insert(notification.repo)
            alert.add(action: UIAlertAction(title: notification.repo, style: .default, handler: { [weak self] _ in
                self?.pushRepoNotifications(owner: notification.owner, repo: notification.repo)
            }))
        }

        alert.add(action: AlertAction.cancel())

        present(alert, animated: true)
    }

    func pushRepoNotifications(owner: String, repo: String) {
        let model = NotificationClient.NotificationRepository(owner: owner, name: repo)
        let controller = NotificationsViewController(client: client, inboxType: .repo(model))
        navigationController?.pushViewController(controller, animated: true)
    }

    func onViewAll() {
        let controller = NotificationsViewController(client: client, inboxType: .all)
        navigationController?.pushViewController(controller, animated: trueUnlessReduceMotionEnabled)<commit_msg>notification alert ipad crash (#1351)<commit_after>//
//  NotificationsViewController.swift
//  Freetime
//
//  Created by <NAME> on 5/13/17.
//  Copyright © 2017 <NAME>. All rights reserved.
//

import UIKit
import IGListKit
import SnapKit
import FlatCache

class NotificationsViewController: BaseListViewController<NSNumber>,
//    NotificationClientListener,
ForegroundHandlerDelegate,
RatingSectionControllerDelegate,
PrimaryViewController,
TabNavRootViewControllerType,
BaseListViewControllerDataSource,
FlatCacheListener {

    enum InboxType {
        case unread
        case repo(NotificationClient.NotificationRepository)
        case all
    }

    private let client: NotificationClient
    private let foreground = ForegroundHandler(threshold: 5 * 60)
    private let inboxType: InboxType
    private var notificationIDs = [String]()

    // set to nil and update to dismiss the rating control
    private var ratingToken: RatingToken? = RatingController.inFeedToken()

    init(client: NotificationClient, inboxType: InboxType) {
        self.client = client
        self.inboxType = inboxType

        super.init(
            emptyErrorMessage: NSLocalizedString("Cannot load your inbox.", comment: ""),
            dataSource: self
        )

        self.foreground.delegate = self

        switch inboxType {
        case .all:
            title = NSLocalizedString("Archived", comment: "")
        case .unread:
            title = NSLocalizedString("Inbox", comment: "")
        case .repo(let repo):
            title = repo.name
        }
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        resetRightBarItem()

        switch inboxType {
        case .unread:
            let item = UIBarButtonItem(
                image: UIImage(named: "bullets-hollow"),
                style: .plain,
                target: self,
                action: #selector(NotificationsViewController.onMore(sender:))
            )
            item.accessibilityLabel = NSLocalizedString("More options", comment: "")
            navigationItem.leftBarButtonItem = item
        case .repo, .all: break
        }

        navigationController?.tabBarItem.badgeColor = Styles.Colors.Red.medium.color
    }

    // MARK: Private API

    @objc func onMore(sender: UIBarButtonItem) {
        let alert = UIAlertController.configured(preferredStyle: .actionSheet)

        alert.add(action: UIAlertAction(
            title: NSLocalizedString("View Archived", comment: ""),
            style: .default,
            handler: { [weak self] _ in
                self?.onViewAll()
        }))

        let cache = client.githubClient.cache
        var repoNames = Set<String>()
        for id in notificationIDs {
            guard let notification = cache.get(id: id) as NotificationViewModel?,
                !repoNames.contains(notification.repo)
                else { continue }
            repoNames.insert(notification.repo)
            alert.add(action: UIAlertAction(title: notification.repo, style: .default, handler: { [weak self] _ in
                self?.pushRepoNotifications(owner: notification.owner, repo: notification.repo)
            }))
        }

        alert.add(action: AlertAction.cancel())

        alert.popoverPresentationController?.barButtonItem = sender

        present(alert, animated: true)
    }

    func pushRepoNotifications(owner: String, repo: String) {
        let model = NotificationClient.NotificationRepository(owner: owner, name: repo)
        let controller = NotificationsViewController(client: client, inboxType: .repo(model))
        navigationController?.pushViewController(controller, animated: true)
    }

    func onViewAll() {
        let controller = NotificationsViewController(client: client, inboxType: .all)
        navigationController?.pushViewController(controller, animated: trueUnlessReduceMotionEnabled)
{'fraction_non_alphanumeric': 0.08968666751236838, 'fraction_numerical': 0.0035519472282126096, 'mean_word_length': 2.5641952983725136, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3628556', 'n_tokens_mistral': 2100, 'n_tokens_neox': 1954, 'n_words': 496}