Skip to content

Instantly share code, notes, and snippets.

Avatar
🎯
Focusing

Artur Rymarz artrmz

🎯
Focusing
View GitHub Profile
View ViewController.swift
private func donate(name: String) {
// 1
let intent = CheckMyGitHubIntent()
// 2
intent.suggestedInvocationPhrase = "Check my GitHub"
intent.name = name
// 3
let interaction = INInteraction(intent: intent, response: nil)
View IntentViewController.swift
class IntentViewController: UIViewController, INUIHostedViewControlling {
@IBOutlet weak var reposLabel: UILabel!
@IBOutlet weak var followersLabel: UILabel!
@IBOutlet weak var activityIndicator: UIActivityIndicatorView!
// MARK: - INUIHostedViewControlling
// Prepare your view controller for the interaction to handle.
func configureView(for parameters: Set<INParameter>, of interaction: INInteraction, interactiveBehavior: INUIInteractiveBehavior, context: INUIHostedViewContext, completion: @escaping (Bool, Set<INParameter>, CGSize) -> Void) {
View GitHubUserIntentHandler.swift
final class CheckMyGitHubIntentHandler: NSObject, CheckMyGitHubIntentHandling {
func handle(intent: CheckMyGitHubIntent, completion: @escaping (CheckMyGitHubIntentResponse) -> Void) {
guard let name = intent.name else {
completion(CheckMyGitHubIntentResponse(code: .failure, userActivity: nil))
return
}
Fetcher.fetch(name: name) { (user, followers) in
guard let user = user else {
completion(CheckMyGitHubIntentResponse(code: .failure, userActivity: nil))
View IntentHandler.swift
class IntentHandler: INExtension {
override func handler(for intent: INIntent) -> Any {
guard intent is CheckMyGitHubIntent else {
fatalError("Unhandled intent type: \(intent)")
}
return CheckMyGitHubIntentHandler()
}
}
View Fetcher.swift
public final class Fetcher: NSObject {
public static func fetch(name: String, completion: @escaping ((user: GitHubUser?, followers: [GitHubFollower])) -> Void) {
guard let url = URL(string: "https://api.github.com/users/\(name)") else {
return
}
let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalAndRemoteCacheData, timeoutInterval: 10.0)
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard
View GitHub.swift
public struct GitHubUser: Codable {
public let name: String
public let location: String
public let repos: Int
private enum CodingKeys: String, CodingKey {
case name
case location
case repos = "public_repos"
}
View CoreData.swift
// 1
@objc(Article)
// 2
final class Article: NSManagedObject {
// 3
@NSManaged var title: String
@NSManaged var content: String
}
View CoreData.swift
private func deleteAllArticles() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
let context = appDelegate.persistentContainer.viewContext
// 1
let articles = loadArticles()
View CoreData.swift
private func loadArticles() -> [Article] {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return []
}
let context = appDelegate.persistentContainer.viewContext
// 1
let request: NSFetchRequest<Article> = Article.fetchRequest()
View CoreData.swift
private func saveSomeArticles() {
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
return
}
// 1
let context = appDelegate.persistentContainer.viewContext
// 2
let mediumArticle = Article(context: context)