<commit_before>//
//  ViewController.swift
//  MiniTwitter
//
//  Created by kkr on 25/03/2017.
//  Copyright © 2017 allting. All rights reserved.
//

import Cocoa
import Accounts
import Swifter

class Tweet: NSObject {
    var name: String!
    var text: NSAttributedString!
}

class ViewController: NSViewController {
    @IBOutlet weak var collectionView: NSCollectionView!
    
    let useACAccount = true
    var swifter: Swifter!
    
    dynamic var tweets: [Tweet] = []
    
    fileprivate func configureCollectionView(){
//        let flowLayout = NSCollectionViewFlowLayout()
//        flowLayout.itemSize = NSSize(width: 300, height: 40)
//        flowLayout.sectionInset = EdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
//        flowLayout.minimumInteritemSpacing = 20
//        flowLayout.minimumLineSpacing = 10
//        collectionView.collectionViewLayout = flowLayout
        
        collectionView.dataSource = self
        collectionView.delegate = self

        view.wantsLayer = true
        collectionView.backgroundColors = [NSColor.clear]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureCollectionView()

        let failureHandler: (Error) -> Void = { print($0.localizedDescription) }

        if useACAccount {
            let accountStore = ACAccountStore()
            let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
            
            accountStore.requestAccessToAccounts(with: accountType, options: nil) { granted, error in
                guard granted else {
                    print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.")
                    return
                }
                
                guard let twitterAccounts = accountStore.accounts(with: accountType) , !twitterAccounts.isEmpty else {
                    print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.")
                    return
                }
                
                let twitterAccount = twitterAccounts[0] as! ACAccount
                self.swifter = Swifter(account: twitterAccount)
                
                self.swifter.getHomeTimeline(count: 20, success: { statuses in
                    print(statuses)
                    guard let tweets = statuses.array else { return }
                    self.tweets = tweets.map {
                        let tweet = Tweet()
                        tweet.text = self.attributedString($0["text"].string!)
                        tweet.name = $0["user"]["name"].string!
                        return tweet
                    }
                    
                    self.collectionView.reloadData()
                    
                    Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.update), userInfo: nil, repeats: true);
                    
                }, failure: failureHandler)
            }
        } else {
            let swifter = Swifter(consumerKey: "<KEY>", consumerSecret: "SbS0CHk11oJdALARa7NDik0nty4pXvAxdt7aj0R5y1gNzWaNEx")
            swifter.authorize(with: URL(string: "swifter://success")!, success: { _ in
                swifter.getHomeTimeline(count: 100, success: { statuses in
                    guard let tweets = statuses.array else { return }
                    self.tweets = tweets.map {
                        let tweet = Tweet()
                        tweet.text = self.attributedString($0["text"].string!)
                        tweet.name = $0["user"]["name"].string!
                        return tweet
                    }
                }, failure: failureHandler)
            }, failure: failureHandler)
        }
    }

    func update(){
        print("update")
        let failureHandler: (Error) -> Void = { print($0.localizedDescription) }
        self.swifter.getHomeTimeline(count: 20, success: { statuses in
            print(statuses)
            guard let tweets = statuses.array else { return }
            self.tweets = tweets.map {
                let tweet = Tweet()
                tweet.text = self.attributedString($0["text"].string!)
                tweet.name = $0["user"]["name"].string!
                return tweet
            }
            <commit_msg>신규 Tweet merge (sinceID) 추가.<commit_after>//
//  ViewController.swift
//  MiniTwitter
//
//  Created by kkr on 25/03/2017.
//  Copyright © 2017 allting. All rights reserved.
//

import Cocoa
import Accounts
import Swifter

class Tweet: NSObject {
    var name: String!
    var text: NSAttributedString!
    var since: String!
}

class ViewController: NSViewController {
    @IBOutlet weak var collectionView: NSCollectionView!
    
    let useACAccount = true
    var swifter: Swifter!
    
    dynamic var tweets: [Tweet] = []
    
    fileprivate func configureCollectionView(){
//        let flowLayout = NSCollectionViewFlowLayout()
//        flowLayout.itemSize = NSSize(width: 300, height: 40)
//        flowLayout.sectionInset = EdgeInsets(top: 5, left: 10, bottom: 5, right: 10)
//        flowLayout.minimumInteritemSpacing = 20
//        flowLayout.minimumLineSpacing = 10
//        collectionView.collectionViewLayout = flowLayout
        
        collectionView.dataSource = self
        collectionView.delegate = self

        view.wantsLayer = true
        collectionView.backgroundColors = [NSColor.clear]
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        configureCollectionView()

        let failureHandler: (Error) -> Void = { print($0.localizedDescription) }

        if useACAccount {
            let accountStore = ACAccountStore()
            let accountType = accountStore.accountType(withAccountTypeIdentifier: ACAccountTypeIdentifierTwitter)
            
            accountStore.requestAccessToAccounts(with: accountType, options: nil) { granted, error in
                guard granted else {
                    print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.")
                    return
                }
                
                guard let twitterAccounts = accountStore.accounts(with: accountType) , !twitterAccounts.isEmpty else {
                    print("There are no Twitter accounts configured. You can add or create a Twitter account in Settings.")
                    return
                }
                
                let twitterAccount = twitterAccounts[0] as! ACAccount
                self.swifter = Swifter(account: twitterAccount)
                
                self.swifter.getHomeTimeline(count: 20, success: { statuses in
                    print(statuses)
                    guard let tweets = statuses.array else { return }
                    self.tweets = tweets.map {
                        let tweet = Tweet()
                        tweet.text = self.attributedString($0["text"].string!)
                        tweet.name = $0["user"]["name"].string!
                        tweet.since = $0["id_str"].string!
                        return tweet
                    }
                    
                    self.collectionView.reloadData()
                    
                    Timer.scheduledTimer(timeInterval: 30, target: self, selector: #selector(self.update), userInfo: nil, repeats: true);
                    
                }, failure: failureHandler)
            }
        } else {
            let swifter = Swifter(consumerKey: "<KEY>", consumerSecret: "SbS0CHk11oJdALARa7NDik0nty4pXvAxdt7aj0R5y1gNzWaNEx")
            swifter.authorize(with: URL(string: "swifter://success")!, success: { _ in
                swifter.getHomeTimeline(count: 100, success: { statuses in
                    guard let tweets = statuses.array else { return }
                    self.tweets = tweets.map {
                        let tweet = Tweet()
                        tweet.text = self.attributedString($0["text"].string!)
                        tweet.name = $0["user"]["name"].string!
                        return tweet
                    }
                }, failure: failureHandler)
            }, failure: failureHandler)
        }
    }

    func update(){
        let since = self.tweets.first?.since
        print("update - \(since)")
        let failureHandler: (Error) -> Void = { print($0.localizedDescription) }
        
        self.swifter.getHomeTimeline(count: 20, sinceID: since, success: { statuses in
//            print(statuses)
            guard let tweets = statuses.array else { return }
            if tweets.count == 0 {
                return
            }
            
            let temp: [Tweet] = tweets.map {
                let tweet = Tweet()
                tweet.text = self.attributedString($0["text"].string!)
                tweet.name = $0["user"]["name"].string!
                tweet.since = $0["id_str"].string!
                return tweet
            }
            
            self.tweets = temp + self.tweets

{'fraction_non_alphanumeric': 0.09042962311833865, 'fraction_numerical': 0.012416217998022195, 'mean_word_length': 1.6420899854862119, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '6623535', 'n_tokens_mistral': 2530, 'n_tokens_neox': 2267, 'n_words': 556}