<commit_before>//
//  ShortcutItemHandling.swift
//  VindsidenApp
//
//  Created by <NAME> on 23.06.2016.
//  Copyright © 2016 RHC. All rights reserved.
//

import UIKit
import VindsidenKit

enum ShortcutItemType: String {
    case goToStation

    fileprivate static let prefix: String = {
        let bundleIdentifier = Bundle.main.bundleIdentifier!
        return bundleIdentifier + "."
    }()

    init?(prefixedString: String) {
        guard let prefixRange = prefixedString.range(of: ShortcutItemType.prefix) else {
            return nil
        }

        var rawTypeString = prefixedString
        rawTypeString.removeSubrange(prefixRange)
        self.init(rawValue: rawTypeString)
    }

    var prefixedString: String {
        return type(of: self).prefix + self.rawValue
    }
}

struct ShortcutItemUserInfo {
    static let stationIdentifierKey = "stationIdentifier"
    var stationIdentifier: String?

    init(stationIdentifier: String? = nil) {
        self.stationIdentifier = stationIdentifier
    }

    init(dictionaryRepresentation: [String : NSSecureCoding]?) {
        guard let dictionary = dictionaryRepresentation else { return }
        self.stationIdentifier = dictionary[ShortcutItemUserInfo.stationIdentifierKey] as? String
    }

    var dictionaryRepresentation: [String : NSSecureCoding] {
        var dictionary: [String : NSSecureCoding] = [:]
        if let stationIdentifier = stationIdentifier {
            dictionary[ShortcutItemUserInfo.stationIdentifierKey] = stationIdentifier as NSSecureCoding?
        }
        return dictionary
    }
}

struct ShortcutItemHandler {

    static func updateDynamicShortcutItems(for application: UIApplication) {
        var shortcutItems = [UIApplicationShortcutItem]()

        let stations = CDStation.visibleStationsInManagedObjectContext(DataManager.shared.viewContext(), limit: 4)

        for station in stations {
            let type = ShortcutItemType.goToStation
            let title = station.stationName!
            let subtitle = station.city!

            let userInfo = ShortcutItemUserInfo(stationIdentifier: "\(station.stationId!)")
            let shortcutItem = UIApplicationShortcutItem(type: type.prefixedString, localizedTitle: title, localizedSubtitle: subtitle, icon: nil, userInfo:userInfo.dictionaryRepresentation)
            shortcutItems.append(shortcutItem)
        }

        application.shortcutItems = shortcutItems
    }

    static func handle(_ shortcutItem: UIApplicationShortcutItem, with rootViewController: RHCViewController) -> Bool {

        guard let shortcutItemType = ShortcutItemType(prefixedString: shortcutItem.type) else {
            return false
        }
<commit_msg>Don’t force unwrap<commit_after>//
//  ShortcutItemHandling.swift
//  VindsidenApp
//
//  Created by <NAME> on 23.06.2016.
//  Copyright © 2016 RHC. All rights reserved.
//

import UIKit
import VindsidenKit

enum ShortcutItemType: String {
    case goToStation

    fileprivate static let prefix: String = {
        let bundleIdentifier = Bundle.main.bundleIdentifier!
        return bundleIdentifier + "."
    }()

    init?(prefixedString: String) {
        guard let prefixRange = prefixedString.range(of: ShortcutItemType.prefix) else {
            return nil
        }

        var rawTypeString = prefixedString
        rawTypeString.removeSubrange(prefixRange)
        self.init(rawValue: rawTypeString)
    }

    var prefixedString: String {
        return type(of: self).prefix + self.rawValue
    }
}

struct ShortcutItemUserInfo {
    static let stationIdentifierKey = "stationIdentifier"
    var stationIdentifier: String?

    init(stationIdentifier: String? = nil) {
        self.stationIdentifier = stationIdentifier
    }

    init(dictionaryRepresentation: [String : NSSecureCoding]?) {
        guard let dictionary = dictionaryRepresentation else { return }
        self.stationIdentifier = dictionary[ShortcutItemUserInfo.stationIdentifierKey] as? String
    }

    var dictionaryRepresentation: [String : NSSecureCoding] {
        var dictionary: [String : NSSecureCoding] = [:]
        if let stationIdentifier = stationIdentifier {
            dictionary[ShortcutItemUserInfo.stationIdentifierKey] = stationIdentifier as NSSecureCoding?
        }
        return dictionary
    }
}

struct ShortcutItemHandler {

    static func updateDynamicShortcutItems(for application: UIApplication) {
        var shortcutItems = [UIApplicationShortcutItem]()

        let stations = CDStation.visibleStationsInManagedObjectContext(DataManager.shared.viewContext(), limit: 4)

        for station in stations {
            let type = ShortcutItemType.goToStation
            guard let stationId = station.stationId,
                let title = station.stationName,
                let subtitle = station.city else {
                    continue
            }

            let userInfo = ShortcutItemUserInfo(stationIdentifier: "\(stationId)")
            let shortcutItem = UIApplicationShortcutItem(type: type.prefixedString, localizedTitle: title, localizedSubtitle: subtitle, icon: nil, userInfo:userInfo.dictionaryRepresentation)
            shortcutItems.append(shortcutItem)
        }

        application.shortcutItems = shortcutItems
    }

    static func handle(_ shortcutItem: UIApplicationShortcutItem, with rootViewController: RHCViewController) -> Bool {

        guard let shortcutItemType = ShortcutItemType(prefixedString: shortcutItem.type) else {
            return false
        }

{'fraction_non_alphanumeric': 0.0752356780275562, 'fraction_numerical': 0.004713560551124003, 'mean_word_length': 3.01528384279476, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, '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': '7135692', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1402, 'n_words': 338}