This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView: View { | |
@ObservedObject var viewModel: ViewModel | |
var body: some View { | |
NavigationView { | |
VStack { | |
List(viewModel.colors, id: \.objectID) { (colorItem) in | |
Cell(colorItem: colorItem) | |
} | |
}.navigationBarTitle("Colors") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension ContentView { | |
final class ViewModel: NSObject, NSFetchedResultsControllerDelegate, ObservableObject { | |
private let colorController: NSFetchedResultsController<ColorItem> | |
init(managedObjectContext: NSManagedObjectContext) { | |
let sortDescriptors = [NSSortDescriptor(keyPath: \ColorItem.hex, ascending: true)] | |
colorController = ColorItem.resultsController(context: managedObjectContext, sortDescriptors: sortDescriptors) | |
super.init() | |
colorController.delegate = self | |
try? colorController.performFetch() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class ColorItem: NSManagedObject { | |
@NSManaged var hex: String | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Diffable data source (new in iOS 13) | |
optional func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith snapshot: NSDiffableDataSourceSnapshot) | |
optional func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChangeContentWith diff: CollectionDifference<NSManagedObjectID>) | |
// Manually updating table view and collection view | |
optional func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange anObject: Any, at indexPath: IndexPath?, for type: NSFetchedResultsChangeType, newIndexPath: IndexPath?) | |
optional func controller(_ controller: NSFetchedResultsController<NSFetchRequestResult>, didChange sectionInfo: NSFetchedResultsSectionInfo, atSectionIndex sectionIndex: Int, for type: NSFetchedResultsChangeType) | |
optional func controllerWillChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) | |
optional func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestRes |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct EqualWidthPreferenceKey: PreferenceKey { | |
typealias Value = CGFloat | |
static var defaultValue: CGFloat = 0 | |
static func reduce(value: inout CGFloat, nextValue: () -> CGFloat) { | |
value = nextValue() | |
} | |
} | |
struct EqualWidth: ViewModifier { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct ContentView: View { | |
@State private var textMinWidth: CGFloat? | |
var body: some View { | |
VStack(spacing: 16) { | |
TextBubble(text: "First", minTextWidth: $textMinWidth) | |
TextBubble(text: "Second longer", minTextWidth: $textMinWidth) | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct NumberTextField: View { | |
init(_ title: String, value: Binding<NSNumber>, formatter: NumberFormatter) { | |
self.title = title | |
self.stringTransformer = StringTransformer(value, formatter: formatter) | |
} | |
private let title: String | |
@ObservedObject private var stringTransformer: StringTransformer | |
var body: some View { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Combine | |
import SwiftUI | |
struct FormatterView: View { | |
@State var limits: TemperatureLimits | |
var body: some View { | |
VStack(spacing: 16) { | |
VStack { | |
Text("Low") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
struct TemperatureLimits { | |
var low: Int = 5 { | |
didSet { | |
let high = max(self.high, low + 10) | |
guard self.high != high else { return } | |
self.high = high | |
} | |
} | |
var high: Int = 30 { | |
didSet { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import CoreGraphics | |
extension CGRect { | |
init(center: CGPoint, size: CGSize) { | |
self.init(x: center.x - size.width / 2.0, y: center.y - size.height / 2.0, width: size.width, height: size.height) | |
} | |
var center: CGPoint { return CGPoint(x: midX, y: midY) } | |
} |