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
435 changes: 435 additions & 0 deletions RandomizerGroups/RandomizerGroups.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>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// RandomizerController.swift
// RandomizerGroups
//
// Created by Kyle Warren on 8/20/21.
//

import CoreData

class RandomizerController {

static let shared = RandomizerController()

var people: [Person] = []

//MARK: - CRUD

func createPersonWith(name: String){
let newPerson = Person(name: name)
people.append(newPerson)

CoreDataStack.saveContext()
}

func update(person: Person, name: String) {
person.name = name

CoreDataStack.saveContext()
}

func toggleIsGrouped(person: Person) {
person.isGrouped.toggle()

CoreDataStack.saveContext()
}

func deletePerson(person: Person) {
guard let index = people.firstIndex(of: person) else { return }
people.remove(at: index)

CoreDataStack.context.delete(person)
CoreDataStack.saveContext()
}
} // End of Class
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// RandomizerDetailViewController.swift
// RandomizerGroups
//
// Created by Kyle Warren on 8/20/21.
//

import UIKit

class RandomizerDetailViewController: UIViewController {

//MARK: - OUTLETS
@IBOutlet weak var nameTextField: UITextField!


//MARK: - PROPERTIES
var people: Person?

//MARK: - LIFECYCLES

override func viewDidLoad() {
super.viewDidLoad()

// Do any additional setup after loading the view.
}

//MARK: - ACTIONS
@IBAction func saveButtonTapped(_ sender: Any) {
guard let name = nameTextField.text, !name.isEmpty else { return }

if let people = people {
RandomizerController.shared.update(person: people, name: name)
} else {
RandomizerController.shared.createPersonWith(name: name)
}
navigationController?.popViewController(animated: true)
}

func updateViews() {
guard let people = people else { return }
nameTextField.text = people.name
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// RandomizerTableViewController.swift
// RandomizerGroups
//
// Created by Kyle Warren on 8/20/21.
//

import UIKit

class RandomizerTableViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()

}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(true)
tableView.reloadData()
}

// MARK: - Table view data source

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

return RandomizerController.shared.people.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "personCell", for: indexPath)

let person = RandomizerController.shared.people[indexPath.row]
cell.textLabel?.text = person.name

return cell
}


// Override to support editing the table view.
override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
if editingStyle == .delete {
let personToDelete = RandomizerController.shared.people[indexPath.row]
RandomizerController.shared.deletePerson(person: personToDelete)

tableView.deleteRows(at: [indexPath], with: .fade)

}
}

/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}
39 changes: 39 additions & 0 deletions RandomizerGroups/RandomizerGroups/CoreData/CoreDataStack.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// CoreDataStack.swift
// RandomizerGroups
//
// Created by Kyle Warren on 8/20/21.
//

import CoreData

enum CoreDataStack {

static let container: NSPersistentContainer = {

let container = NSPersistentContainer(name: "RandomizerGroups")

container.loadPersistentStores { storeDescription, error in
if let error = error {
fatalError("Error loading persistent stores \(error)")
}
}
return container
}()

static var context: NSManagedObjectContext {
container.viewContext
}

static func saveContext() {
if context.hasChanges {
do{
try context.save()
} catch {
print("Error saving context \(error)")
}
}
}

} // End of enum

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//
// RandomizerGroups+Convenience.swift
// RandomizerGroups
//
// Created by Kyle Warren on 8/20/21.
//

import CoreData

extension Person {

@discardableResult
convenience init(name: String, isGrouped: Bool = false, context: NSManagedObjectContext = CoreDataStack.context) {
self.init(context: context)
self.name = name
self.isGrouped = isGrouped
}
}
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>RandomizerGroups.xcdatamodel</string>
</dict>
</plist>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="18154" systemVersion="20G80" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Person" representedClassName="Person" syncable="YES" codeGenerationType="class">
<attribute name="isGrouped" optional="YES" attributeType="Boolean" usesScalarValueType="YES"/>
<attribute name="name" optional="YES" attributeType="String"/>
</entity>
<elements>
<element name="Person" positionX="-63" positionY="-18" width="128" height="59"/>
</elements>
</model>
81 changes: 81 additions & 0 deletions RandomizerGroups/RandomizerGroups/Resources/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// AppDelegate.swift
// RandomizerGroups
//
// Created by Kyle Warren on 8/20/21.
//

import UIKit
import CoreData

@main
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: "RandomizerGroups")
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,11 @@
{
"colors" : [
{
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Loading