Skip to content

Commit

Permalink
Add sample project.
Browse files Browse the repository at this point in the history
  • Loading branch information
laevandus committed Jan 13, 2020
1 parent 7d2192e commit 1022c69
Show file tree
Hide file tree
Showing 22 changed files with 1,363 additions and 1 deletion.
632 changes: 632 additions & 0 deletions CoreDataCombineSwiftUI.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>
34 changes: 34 additions & 0 deletions CoreDataCombineSwiftUI/AppDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// AppDelegate.swift
// CoreDataCombineSwiftUI
//
// Created by Toomas Vahter on 13.01.2020.
// Copyright © 2020 Augmented Code. All rights reserved.
//

import UIKit
import CoreData

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
lazy private(set) var dependencyContainer = DependencyContainer()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
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.
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
{
"images" : [
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "20x20",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "29x29",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "40x40",
"scale" : "3x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "2x"
},
{
"idiom" : "iphone",
"size" : "60x60",
"scale" : "3x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "20x20",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "29x29",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "40x40",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "1x"
},
{
"idiom" : "ipad",
"size" : "76x76",
"scale" : "2x"
},
{
"idiom" : "ipad",
"size" : "83.5x83.5",
"scale" : "2x"
},
{
"idiom" : "ios-marketing",
"size" : "1024x1024",
"scale" : "1x"
}
],
"info" : {
"version" : 1,
"author" : "xcode"
}
}
6 changes: 6 additions & 0 deletions CoreDataCombineSwiftUI/Assets.xcassets/Contents.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"info" : {
"version" : 1,
"author" : "xcode"
}
}
25 changes: 25 additions & 0 deletions CoreDataCombineSwiftUI/Base.lproj/LaunchScreen.storyboard
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" launchScreen="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="01J-lp-oVM">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="EHf-IW-A2E">
<objects>
<viewController id="01J-lp-oVM" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="Ze5-6b-2t3">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" xcode11CocoaTouchSystemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="iYj-Kq-Ea1" userLabel="First Responder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="53" y="375"/>
</scene>
</scenes>
</document>
39 changes: 39 additions & 0 deletions CoreDataCombineSwiftUI/ColorItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
//
// ColorItem.swift
// CoreDataCombineSwiftUI
//
// Created by Toomas Vahter on 13.01.2020.
// Copyright © 2020 Augmented Code. All rights reserved.
//

import CoreData

final class ColorItem: NSManagedObject {
@NSManaged var hex: String
}

extension ColorItem {
static func randomHex() -> String {
let characters = "0123456789ABCDEF"
return "#" + (0..<6).map({ _ in characters.randomElement()! })
}
}

extension ColorItem {
static let entityName = "ColorItem"

convenience init(context: NSManagedObjectContext) {
let entity = NSEntityDescription.entity(forEntityName: Self.entityName, in: context)!
self.init(entity: entity, insertInto: context)
}

static func makeRequest() -> NSFetchRequest<ColorItem> {
return NSFetchRequest<ColorItem>(entityName: entityName)
}

static func resultsController(context: NSManagedObjectContext, sortDescriptors: [NSSortDescriptor] = []) -> NSFetchedResultsController<ColorItem> {
let request = makeRequest()
request.sortDescriptors = sortDescriptors.isEmpty ? nil : sortDescriptors
return NSFetchedResultsController(fetchRequest: request, managedObjectContext: context, sectionNameKeyPath: nil, cacheName: nil)
}
}
58 changes: 58 additions & 0 deletions CoreDataCombineSwiftUI/ContentView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//
// ContentView.swift
// CoreDataCombineSwiftUI
//
// Created by Toomas Vahter on 13.01.2020.
// Copyright © 2020 Augmented Code. All rights reserved.
//

import Combine
import SwiftUI

struct ContentView: View {
@ObservedObject var viewModel: ViewModel

var body: some View {
NavigationView {
VStack {
List(viewModel.colors, id: \.objectID) { (colorItem) in
Cell(colorItem: colorItem).onTapGesture {
self.viewModel.selectedColorItem = colorItem
}
}
if viewModel.selectedColorItem != nil {
SelectedColorView(colorItem: viewModel.selectedColorItem!).frame(minHeight: 50, maxHeight: 50)
}
}.navigationBarTitle("Colors")
.navigationBarItems(trailing: trailingBarItem)
}
}

private var trailingBarItem: some View {
Button(action: viewModel.addRandomColor, label: {
Text("Add")
})
}
}

struct Cell: View {
@ObservedObject var colorItem: ColorItem

var body: some View {
HStack {
Text(verbatim: colorItem.hex)
Spacer()
Rectangle().foregroundColor(Color(colorItem.uiColor)).frame(minWidth: 50, maxWidth: 50)
}
}
}

struct ContentView_Previews: PreviewProvider {
static let dependencyContainer = DependencyContainer()

static var previews: some View {
let context = dependencyContainer.persistentContainer.viewContext
let viewModel = ContentView.ViewModel(managedObjectContext: context)
return ContentView(viewModel: viewModel)
}
}
Loading

0 comments on commit 1022c69

Please sign in to comment.