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

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

37 changes: 19 additions & 18 deletions Examples/CaseStudies/CaseStudies/CaseStudiesApp.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import DependenciesAdditions
import SwiftUI

#if canImport(UIKit)
import UIKit
import UIKit
#endif

@main
Expand All @@ -12,23 +13,23 @@ struct Case_StudiesApp: App {
model: withDependencies {
$0.persistentContainer = .default(inMemory: true).withInitialData()
#if targetEnvironment(simulator)
// Note: This will emit warnings, as using these `$` accessors should only
// be used for previews and testing, but we do so here because the following
// values are undefined when running on the simulator.
// We are furthermore forced to perform a `context` danse to avoid emitting
// runtime warning, as one shouldn't access `$` device properties
// in a `live` context. So we temporarily switch to a `preview` context
// to avoid the warning when we access the values two lines below.
var dependencies = $0
withDependencies { inner in
inner.context = .preview
} operation: {
dependencies.device.$batteryLevel = 0.72
dependencies.device.$batteryState = UIDevice.BatteryState.charging
}
$0 = dependencies
$0.device.$isBatteryMonitoringEnabled = .constant(true)
$0.context = .live
// Note: This will emit warnings, as using these `$` accessors should only
// be used for previews and testing, but we do so here because the following
// values are undefined when running on the simulator.
// We are furthermore forced to perform a `context` danse to avoid emitting
// runtime warning, as one shouldn't access `$` device properties
// in a `live` context. So we temporarily switch to a `preview` context
// to avoid the warning when we access the values two lines below.
var dependencies = $0
withDependencies { inner in
inner.context = .preview
} operation: {
dependencies.device.$batteryLevel = 0.72
dependencies.device.$batteryState = UIDevice.BatteryState.charging
}
$0 = dependencies
$0.device.$isBatteryMonitoringEnabled = .constant(true)
$0.context = .live
#endif
} operation: {
.init()
Expand Down
20 changes: 10 additions & 10 deletions Examples/CaseStudies/CaseStudies/Studies/CompressionStudy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct CompressionStudyView: View {
} header: {
Text("Text")
} footer: {
Gauge(value: max(1 - model.compressionRatio, 0), in: 0 ... 1) {
Gauge(value: max(1 - model.compressionRatio, 0), in: 0...1) {
Text("Compression Ratio")
} currentValueLabel: {
Text(model.compressionRatio.formatted(.percent.precision(.fractionLength(0))))
Expand Down Expand Up @@ -103,7 +103,7 @@ struct CompressionStudyView: View {
#if os(iOS)
.listStyle(.grouped)
#endif
.navigationTitle("Codable & Compression")
.navigationTitle("Codable & Compression")
}
}

Expand All @@ -112,14 +112,14 @@ struct CompressionStudyView_Previews: PreviewProvider {
NavigationStack {
CompressionStudyView(
model:
withDependencies {
let encoder = JSONEncoder()
encoder.outputFormatting.insert(.prettyPrinted)
encoder.outputFormatting.insert(.sortedKeys)
$0.encode = DataEncoder(encoder)
} operation: {
.init()
}
withDependencies {
let encoder = JSONEncoder()
encoder.outputFormatting.insert(.prettyPrinted)
encoder.outputFormatting.insert(.sortedKeys)
$0.encode = DataEncoder(encoder)
} operation: {
.init()
}
)
}
}
Expand Down
63 changes: 33 additions & 30 deletions Examples/CaseStudies/CaseStudies/Studies/CoreDataStudy.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import _CoreDataDependency
import _SwiftUIDependency
import Dependencies
import SwiftUI
import SwiftUINavigation
import _CoreDataDependency
import _SwiftUIDependency

@MainActor
final class CoreDataStudy: ObservableObject {
enum Destination {
Expand Down Expand Up @@ -159,7 +160,9 @@ struct CoreDataStudyView: View {
}
}
} header: {
Text("^[\(songsByYear.count) \("song")](inflect: true) from \(songsByYear.id.formatted(.number.grouping(.never)))")
Text(
"^[\(songsByYear.count) \("song")](inflect: true) from \(songsByYear.id.formatted(.number.grouping(.never)))"
)
}
}
}
Expand Down Expand Up @@ -234,7 +237,7 @@ struct AddSongView: View {
var body: some View {
Form {
TextField("Name", text: $model.song.editor.name.emptyIfNil())
Stepper(value: $model.song.editor.year, in: 1960 ... 1970) {
Stepper(value: $model.song.editor.year, in: 1960...1970) {
LabeledContent("Year", value: "\(model.song.year)")
}
}
Expand All @@ -260,46 +263,46 @@ struct CoreDataStudyView_Previews: PreviewProvider {
NavigationStack {
CoreDataStudyView(
model:
withDependencies {
$0.persistentContainer = .default(inMemory: true).withInitialData()
} operation: {
CoreDataStudy()
}
withDependencies {
$0.persistentContainer = .default(inMemory: true).withInitialData()
} operation: {
CoreDataStudy()
}
)
}

// TODO: Having two previews creates navigation glitches.
// NavigationStack {
// AddSongView(
// model:
// withDependencies {
// $0.persistentContainer = .canonical(inMemory: true).withInitialData()
// $0.uuid = .incrementing
// } operation: {
// @Dependency(\.persistentContainer) var persistentContainer
// @Dependency(\.uuid) var uuid
//
// return AddSongModel(song: try! persistentContainer.insert(Song.self) {
// $0.identifier = uuid()
// $0.year = 1970
// $0.name = "Let it be"
// })
// }
// )
// }
// NavigationStack {
// AddSongView(
// model:
// withDependencies {
// $0.persistentContainer = .canonical(inMemory: true).withInitialData()
// $0.uuid = .incrementing
// } operation: {
// @Dependency(\.persistentContainer) var persistentContainer
// @Dependency(\.uuid) var uuid
//
// return AddSongModel(song: try! persistentContainer.insert(Song.self) {
// $0.identifier = uuid()
// $0.year = 1970
// $0.name = "Let it be"
// })
// }
// )
// }
}
}

public extension Binding {
func nilIfEmpty() -> Binding<Value?> where Value: RangeReplaceableCollection {
extension Binding {
public func nilIfEmpty() -> Binding<Value?> where Value: RangeReplaceableCollection {
Binding<Value?> {
self.wrappedValue.isEmpty ? nil : self.wrappedValue
} set: { newValue, _ in
self.transaction(transaction).wrappedValue = newValue ?? .init()
}
}

func emptyIfNil<T>() -> Binding<T> where Value == T?, T: RangeReplaceableCollection {
public func emptyIfNil<T>() -> Binding<T> where Value == T?, T: RangeReplaceableCollection {
Binding<T> {
self.wrappedValue ?? .init()
} set: { newValue, _ in
Expand Down
Loading
Loading