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
4 changes: 3 additions & 1 deletion coredata-device.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
isa = PBXProject;
attributes = {
LastSwiftUpdateCheck = 1030;
LastUpgradeCheck = 1030;
LastUpgradeCheck = 1220;
ORGANIZATIONNAME = "Cascadia College";
TargetAttributes = {
D14BD0112353FD18008357E2 = {
Expand Down Expand Up @@ -192,6 +192,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down Expand Up @@ -253,6 +254,7 @@
CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES;
CLANG_WARN_OBJC_LITERAL_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES;
CLANG_WARN_RANGE_LOOP_ANALYSIS = YES;
CLANG_WARN_STRICT_PROTOTYPES = YES;
CLANG_WARN_SUSPICIOUS_MOVE = YES;
Expand Down
10 changes: 4 additions & 6 deletions coredata-device/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="LQX-Wn-s9V">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="17506" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="LQX-Wn-s9V">
<device id="retina6_1" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="17505"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
Expand All @@ -26,14 +24,14 @@
</connections>
</tableView>
</subviews>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="Htd-Ll-Q8D" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" id="7cJ-05-jQH"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="Htd-Ll-Q8D" secondAttribute="trailing" id="EDy-mU-mwh"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="bottom" secondItem="Htd-Ll-Q8D" secondAttribute="bottom" id="ZOg-Wy-E0a"/>
<constraint firstItem="Htd-Ll-Q8D" firstAttribute="centerY" secondItem="8bC-Xf-vdC" secondAttribute="centerY" id="uxA-8G-2AA"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<navigationItem key="navigationItem" id="H5j-EI-Isz">
<barButtonItem key="rightBarButtonItem" systemItem="add" id="b6w-Rw-pNU">
Expand Down
75 changes: 60 additions & 15 deletions coredata-device/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,51 @@
//

import UIKit
import CoreData

class SubtitleTableViewCell: UITableViewCell {

override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: .subtitle, reuseIdentifier: "Cell")
}

required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}

class ViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
//TODO: refactor in-app storage to use NSManagedObject array
var serialNumbers:[String] = []
var devices:[NSManagedObject] = []

let managedContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
lazy var entity = NSEntityDescription.entity(forEntityName: "Device", in: managedContext)!

override func viewDidLoad() {
super.viewDidLoad()

title = "Devices"
tableView.register(UITableViewCell.self, forCellReuseIdentifier: "Cell")
tableView.register(SubtitleTableViewCell.self, forCellReuseIdentifier: "Cell")
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reload()

}

@IBAction func addDevice(_ sender: UIBarButtonItem) {
let alert = UIAlertController(title: "New Device", message: "Enter Device Serial Number", preferredStyle: .alert)
let alert = UIAlertController(title: "New Device", message: "Enter Device Serial Number and Type", preferredStyle: .alert)
alert.addTextField(configurationHandler: nil)

let saveAction = UIAlertAction(title: "Save", style: .default, handler: {
action in
guard let textField = alert.textFields?.first,
let serialNumber = textField.text else
{
return
}
let saveAction = UIAlertAction(title: "Save", style: .default, handler: { action in

self.serialNumbers.append(serialNumber)
self.tableView.reloadData()
guard let serialNumberTextField = alert.textFields?.first, let serialNumber = serialNumberTextField.text else { return }
guard let deviceTypeTextField = alert.textFields?[1], let deviceType = deviceTypeTextField.text else { return }
self.save(with: serialNumber, with: deviceType)
self.reload()
})

let cancelAction = UIAlertAction(title: "Cancel", style: .cancel)
Expand All @@ -44,8 +62,29 @@ class ViewController: UIViewController {
present(alert,animated: true)
}

func save(with serialNumber:String){
func save(with serialNumber: String, with deviceType: String){
//TODO:Use the MOC with the Device entity to create a newDevice object, update it's property and save it to persistent storage
let newDevice = NSManagedObject(entity: entity, insertInto: managedContext)
let id = UUID()
newDevice.setValue(serialNumber, forKey: "serialNumber")
newDevice.setValue(deviceType, forKey: "type")
newDevice.setValue(id, forKey: "id")
do {
try managedContext.save()
// print("device with serial number \(serialNumber), type \(deviceType), and id \(id) saved")
} catch {
print("error saving context")
}
}

func reload(){
let request = Device.fetchRequest() as NSFetchRequest<Device>
let predicate = NSPredicate(format: "type == %@", "iPad")
request.predicate = predicate
if let fetchedDevices = try? managedContext.fetch(request) as [Device] {
devices = fetchedDevices
}
tableView.reloadData()
}
}

Expand All @@ -55,12 +94,18 @@ extension ViewController:UITableViewDataSource{
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
//TODO: refactor to get the device object and use it's value(forKeyPath: ) method to pull the serialNumber text
cell.textLabel?.text = serialNumbers[indexPath.row]
let device = devices[indexPath.row]
let serialNumber = device.value(forKeyPath: "serialNumber") as? String ?? ""
let deviceType = device.value(forKeyPath: "type") as? String ?? ""
let id = (device.value(forKey: "id") as? UUID)?.uuidString ?? ""

cell.textLabel?.text = serialNumber
cell.detailTextLabel?.text = deviceType + " - " + id
return cell
}


func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return serialNumbers.count
return devices.count
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="1" systemVersion="11A491" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<elements/>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17511" systemVersion="20C69" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Device" representedClassName="Device" syncable="YES" codeGenerationType="class">
<attribute name="id" optional="YES" attributeType="UUID" usesScalarValueType="NO"/>
<attribute name="serialNumber" optional="YES" attributeType="String"/>
<attribute name="type" optional="YES" attributeType="String"/>
</entity>
<elements>
<element name="Device" positionX="-63" positionY="-18" width="128" height="74"/>
</elements>
</model>