<commit_before>//
//  CalendarManager.swift
//  12306ForMac
//
//  Created by fancymax on 2016/12/5.
//  Copyright © 2016年 fancy. All rights reserved.
//

import Foundation
import EventKit

class CalendarManager:NSObject {
    private let eventStore:EKEventStore
    private var isAccessToEventStoreGranted:Bool
    
    fileprivate static let sharedManager = CalendarManager()
    class var sharedInstance: CalendarManager {
        return sharedManager
    }
    
    fileprivate override init () {
        isAccessToEventStoreGranted = false
        eventStore = EKEventStore()
    }
    
    private func createCalendar() {
        if !isAccessToEventStoreGranted {
            return
        }
        var shouldCreateCalendar = false
        if let calendarId = UserDefaults.standard.string(forKey: "calendarId") {
            let calendar = eventStore.calendar(withIdentifier: calendarId)
            
            if calendar == nil {
                shouldCreateCalendar = true
            }
        }
        else {
            shouldCreateCalendar = true
        }
        if !shouldCreateCalendar {
            return
        }
        
        let calendar = EKCalendar(for: .event, eventStore: eventStore)
        calendar.title = "12306ForMac"
        calendar.color = NSColor.gray
        calendar.source = eventStore.defaultCalendarForNewEvents.source
        do {
            try self.eventStore.saveCalendar(calendar, commit: true)
            logger.info("calendarId = \(calendar.calendarIdentifier)")
            UserDefaults.standard.set(calendar.calendarIdentifier, forKey: "calendarId")
        }
        catch {
            logger.error(error)
        }
    }
    
    func updateAuthorizationStatus()  {
        switch EKEventStore.authorizationStatus(for: .event) {
        case .authorized:
            self.isAccessToEventStoreGranted = true
            self.createCalendar()
        case .notDetermined:
            self.eventStore.requestAccess(to: .event, completion: {[unowned self] granted,error in
                DispatchQueue.main.async {
                    self.isAccessToEventStoreGranted = granted
                    self.createCalendar()
                }
                })
        case .restricted, .denied:
            isAccessToEventStoreGranted = false
        }
    }
    
    func createEvent(title:String, startDate:Date, endDate:Date)->Bool {
        if !isAccessToEventStoreGranted {
            return false
        }
        
        if let calendarId = UserDefaults.standard.string(forKey: "calendarId") {
            if let calendar = eventStore.calendar(withIdentifier: calendarId) {
                let event = EKEvent(eventStore: self.eventStore)
                event.title = title
                event.startDate = startDate
                event.endDate = endDate
                event.calendar = calendar
                do {
                    try self.eventStore.save(event, span: .thisEvent, commit: true)
                } catch {
                    logger.error(error)
                }
            }
            else {
                self.createCalendar()
                logger.error("Calendar = nil,create")
            }
        }
        return true
    }
    
}

<commit_msg>fix: create calendar<commit_after>//
//  CalendarManager.swift
//  12306ForMac
//
//  Created by fancymax on 2016/12/5.
//  Copyright © 2016年 fancy. All rights reserved.
//

import Foundation
import EventKit

class CalendarManager:NSObject {
    private let eventStore:EKEventStore
    private var isAccessToEventStoreGranted:Bool
    
    fileprivate static let sharedManager = CalendarManager()
    class var sharedInstance: CalendarManager {
        return sharedManager
    }
    
    fileprivate override init () {
        isAccessToEventStoreGranted = false
        eventStore = EKEventStore()
    }
    
    private func createCalendar() {
        if !isAccessToEventStoreGranted {
            return
        }
        var shouldCreateCalendar = false
        if let calendarId = UserDefaults.standard.string(forKey: "calendarId") {
            let calendar = eventStore.calendar(withIdentifier: calendarId)
            
            if calendar == nil {
                shouldCreateCalendar = true
            }
        }
        else {
            shouldCreateCalendar = true
        }
        if !shouldCreateCalendar {
            return
        }
        
        let calendar = EKCalendar(for: .event, eventStore: eventStore)
        calendar.title = "12306ForMac"
        calendar.color = NSColor.gray
        calendar.source = eventStore.defaultCalendarForNewEvents.source
        do {
            try self.eventStore.saveCalendar(calendar, commit: true)
            logger.info("calendarId = \(calendar.calendarIdentifier)")
            UserDefaults.standard.set(calendar.calendarIdentifier, forKey: "calendarId")
        }
        catch {
            logger.error(error)
        }
    }
    
    func updateAuthorizationStatus()  {
        switch EKEventStore.authorizationStatus(for: .event) {
        case .authorized:
            self.isAccessToEventStoreGranted = true
            self.createCalendar()
        case .notDetermined:
            self.eventStore.requestAccess(to: .event, completion: {[unowned self] granted,error in
                DispatchQueue.main.async {
                    self.isAccessToEventStoreGranted = granted
                    self.createCalendar()
                }
                })
        case .restricted, .denied:
            isAccessToEventStoreGranted = false
        }
    }
    
    func createEvent(title:String, startDate:Date, endDate:Date)->Bool {
        if !isAccessToEventStoreGranted {
            return false
        }
        
        if let calendarId = UserDefaults.standard.string(forKey: "calendarId") {
            if let calendar = eventStore.calendar(withIdentifier: calendarId) {
                let event = EKEvent(eventStore: self.eventStore)
                event.title = title
                event.startDate = startDate
                event.endDate = endDate
                event.calendar = calendar
                do {
                    try self.eventStore.save(event, span: .thisEvent, commit: true)
                } catch {
                    logger.error(error)
                }
            }
            else {
                logger.error("Calendar = nil,create")
                self.createCalendar()
                
                if let calendarId = UserDefaults.standard.string(forKey: "calendarId") {
                    if let calendar = eventStore.calendar(withIdentifier: calendarId) {
                        let event = EKEvent(eventStore: self.eventStore)
                        event.title = title
                        event.startDate = startDate
                        event.endDate = endDate
                        event.calendar = calendar
                        do {
                            try self.eventStore.save(event, span: .thisEvent, commit: true)
                        } catch {
                            logger.error(error)
                        }
                    }
                    else{
                        logger.error("Calendar = nil,again")
                    }
                }
            }
        }
        return true
    }
    
}


{'fraction_non_alphanumeric': 0.0745578231292517, 'fraction_numerical': 0.005714285714285714, 'mean_word_length': 1.5488904299583912, 'pattern_counts': {'":': 0, '<': 3, '<?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': '5617955', 'n_tokens_mistral': 1878, 'n_tokens_neox': 1758, 'n_words': 378}