Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added PairGenrator/.DS_Store
Binary file not shown.
416 changes: 416 additions & 0 deletions PairGenrator/PairGenrator.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>
43 changes: 43 additions & 0 deletions PairGenrator/PairGenrator/Model/CoreData.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//
// CoreData.swift
// PairGenrator
//
// Created by Hin Wong on 4/10/20.
// Copyright © 2020 Hin Wong. All rights reserved.
//

import Foundation
import CoreData

enum CoreDataStack {

//MARK: - Core data stack
static let container: NSPersistentContainer = {
let container = NSPersistentContainer(name: "PairGenrator")
container.loadPersistentStores { (_, error) in
if let error = error {
fatalError("Failed to load persistet stores \(error)")
}
}
return container
} ()

static var context: NSManagedObjectContext {
return container.viewContext
}

//MARK: - Core data saving

static func saveContext () {
let context = CoreDataStack.context
if context.hasChanges {
do {
try context.save()
} catch {
let nserror = error as NSError
fatalError("Unresolved error saving Core Data context:\n\(nserror), \(nserror.userInfo)")
}
}
}
}

18 changes: 18 additions & 0 deletions PairGenrator/PairGenrator/Model/Person+Convenience.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// Person+Convenience.swift
// PairGenrator
//
// Created by Hin Wong on 4/10/20.
// Copyright © 2020 Hin Wong. All rights reserved.
//

import Foundation
import CoreData

extension Person {
@discardableResult
convenience init(name: String, context: NSManagedObjectContext = CoreDataStack.context) {
self.init(context: context)
self.name = name
}
}
67 changes: 67 additions & 0 deletions PairGenrator/PairGenrator/ModelController/PersonController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// PersonController.swift
// PairGenrator
//
// Created by Hin Wong on 4/10/20.
// Copyright © 2020 Hin Wong. All rights reserved.
//

import Foundation
import CoreData

class PersonController {

//MARK: - Properties
static let shared = PersonController()

var persons: [Person] {
let fetchRequest: NSFetchRequest<Person> = Person.fetchRequest()
return (try? CoreDataStack.context.fetch(fetchRequest)) ?? []
}

//MARK: - CRUD Functions

func createPerson(name: String) {
Person(name: name)
CoreDataStack.saveContext()
}

func updatePerson(person: Person, name: String) {
person.name = name
CoreDataStack.saveContext()
}

func deletePerson(person: Person) {
CoreDataStack.context.delete(person)
CoreDataStack.saveContext()
}

//MARK: - Helper Functions

//TODO: - Potential error

func pairingUp() -> [[Person]]{
let randomPairs = persons.shuffled()

var pairs = [[Person]]()
var pair = [Person]()

for person in randomPairs {
if pair.count == 0 {
pair.append(person)
} else {
pair.append(person)
pairs.append(pair)
pair = [Person]()
}

}

if pair.count != 0 {
pairs.append(pair)
}

return pairs
}

}//End of class
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>_XCCurrentVersionName</key>
<string>PairGenrator.xcdatamodel</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="16119" systemVersion="19E266" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Person" representedClassName="Person" syncable="YES" codeGenerationType="class">
<attribute name="name" optional="YES" attributeType="String"/>
</entity>
<elements>
<element name="Person" positionX="-54" positionY="-9" width="128" height="58"/>
</elements>
</model>
82 changes: 82 additions & 0 deletions PairGenrator/PairGenrator/Resource/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
//
// AppDelegate.swift
// PairGenrator
//
// Created by Hin Wong on 4/10/20.
// Copyright © 2020 Hin Wong. All rights reserved.
//

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {



func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
return true
}

// MARK: UISceneSession Lifecycle

func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
// Called when a new scene session is being created.
// Use this method to select a configuration to create the new scene with.
return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
}

func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
// Called when the user discards a scene session.
// If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
// Use this method to release any resources that were specific to the discarded scenes, as they will not return.
}

// MARK: - Core Data stack

lazy var persistentContainer: NSPersistentContainer = {
/*
The persistent container for the application. This implementation
creates and returns a container, having loaded the store for the
application to it. This property is optional since there are legitimate
error conditions that could cause the creation of the store to fail.
*/
let container = NSPersistentContainer(name: "PairGenrator")
container.loadPersistentStores(completionHandler: { (storeDescription, error) in
if let error = error as NSError? {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.

/*
Typical reasons for an error here include:
* The parent directory does not exist, cannot be created, or disallows writing.
* The persistent store is not accessible, due to permissions or data protection when the device is locked.
* The device is out of space.
* The store could not be migrated to the current model version.
Check the error message to determine what the actual problem was.
*/
fatalError("Unresolved error \(error), \(error.userInfo)")
}
})
return container
}()

// MARK: - Core Data Saving support

func saveContext () {
let context = persistentContainer.viewContext
if context.hasChanges {
do {
try context.save()
} catch {
// Replace this implementation with code to handle the error appropriately.
// fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
let nserror = error as NSError
fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
}
}
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "20x20"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "29x29"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "40x40"
},
{
"idiom" : "iphone",
"scale" : "2x",
"size" : "60x60"
},
{
"idiom" : "iphone",
"scale" : "3x",
"size" : "60x60"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "20x20"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "29x29"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "40x40"
},
{
"idiom" : "ipad",
"scale" : "1x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "76x76"
},
{
"idiom" : "ipad",
"scale" : "2x",
"size" : "83.5x83.5"
},
{
"idiom" : "ios-marketing",
"scale" : "1x",
"size" : "1024x1024"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading