Skip to content

Commit

Permalink
run swiftlint and swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-zethraeus committed Dec 8, 2022
1 parent 3f1f928 commit e2837ac
Show file tree
Hide file tree
Showing 16 changed files with 212 additions and 83 deletions.
81 changes: 81 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# options
--swiftversion 5.6
--self remove # redundantSelf
--importgrouping testable-bottom # sortedImports
--commas always # trailingCommas
--trimwhitespace always # trailingSpace
--indent 4 #indent
--ifdef no-indent #indent
--indentstrings true #indent
--wraparguments before-first # wrapArguments
--wrapparameters before-first # wrapArguments
--wrapcollections before-first # wrapArguments
--wrapconditions before-first # wrapArguments
--wrapreturntype if-multiline #wrapArguments
--closingparen balanced # wrapArguments
--wraptypealiases before-first # wrapArguments
--funcattributes prev-line # wrapAttributes
--typeattributes prev-line # wrapAttributes
--wrapternary before-operators # wrap
--structthreshold 20 # organizeDeclarations
--enumthreshold 20 # organizeDeclarations
--organizetypes class,struct,enum,extension,actor # organizeDeclarations
--markcategories false #organizeDeclarations
--extensionacl on-declarations # extensionAccessControl
--patternlet inline # hoistPatternLet
--redundanttype inferred # redundantType
--typeblanklines preserve # blankLinesAtStartOfScope, blankLinesAtEndOfScope
--emptybraces no-space # emptyBraces
--maxwidth 120 # wrap

# rules
--rules andOperator
--rules anyObjectProtocol
--rules blankLinesAtEndOfScope
--rules blankLinesAtStartOfScope
--rules blankLinesBetweenScopes
--rules blockComments
--rules braces
--rules consecutiveSpaces
--rules duplicateImports
--rules emptyBraces
--rules enumNamespaces
--rules extensionAccessControl
--rules hoistPatternLet
--rules indent
--rules markTypes
--rules organizeDeclarations
--rules redundantClosure
--rules redundantFileprivate
--rules redundantGet
--rules redundantInit
--rules redundantLet
--rules redundantParens
--rules redundantPattern
--rules redundantRawValues
--rules redundantReturn
--rules redundantSelf
--rules redundantType
--rules redundantVoidReturnType
--rules sortDeclarations
--rules sortedImports
--rules spaceAroundBraces
--rules spaceAroundComments
--rules spaceAroundParens
--rules spaceInsideBraces
--rules spaceInsideBrackets
--rules spaceInsideComments
--rules spaceInsideParens
--rules strongifiedSelf
--rules todos
--rules trailingCommas
--rules trailingSpace
--rules typeSugar
--rules unusedArguments
--rules wrap
--rules wrapArguments
--rules wrapAttributes
--rules wrapConditionalBodies
--rules wrapEnumCases
--rules wrapMultilineStatementBraces
--rules wrapSwitchCases
32 changes: 32 additions & 0 deletions .swiftlint
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
only_rules:
- colon
- fatal_error_message
- implicitly_unwrapped_optional
- legacy_cggeometry_functions
- legacy_constant
- legacy_constructor
- legacy_nsgeometry_functions
- operator_usage_whitespace
- return_arrow_whitespace
- trailing_newline
- unused_optional_binding
- vertical_whitespace
- void_return
- custom_rules

excluded:
- Carthage
- Pods
- ./**/.build

colon:
apply_to_dictionaries: false

indentation: 2

custom_rules:
no_objcMembers:
name: "@objcMembers"
regex: "@objcMembers"
message: "Explicitly use @objc on each member you want to expose to Objective-C"
severity: error
9 changes: 6 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ let package = Package(
// Products define the executables and libraries a package produces, and make them visible to other packages.
.library(
name: "Republished",
targets: ["Republished"]),
targets: ["Republished"]
)
],
dependencies: [
// Dependencies declare other packages that this package depends on.
Expand All @@ -21,9 +22,11 @@ let package = Package(
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.target(
name: "Republished",
dependencies: []),
dependencies: []
),
.testTarget(
name: "RepublishedTests",
dependencies: ["Republished"]),
dependencies: ["Republished"]
)
]
)
2 changes: 1 addition & 1 deletion RepublishTestApp.swiftpm/App/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

@main
struct RepublishTestApp: App {

var body: some Scene {
WindowGroup {
ContentView(viewModel: ViewModel(model: DomainModel()))
Expand Down
12 changes: 6 additions & 6 deletions RepublishTestApp.swiftpm/App/DomainModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import SwiftUI

final class DomainModel: ObservableObject {

// A standard ObservableObject.
// A standard ObservableObject.

// Updates to `count` makes the object fire a signal that
// consumers can listen to to know when to read — and SwiftUI
// does this by default.

// However, if you nest this in another ObservableObject, there's
// no inbuilt functionality to make the outer one fire for updates
// in response to this inner one firing.

// (An ObservableObject is a reference type, so an @Published field
// on the outer object containing this object as an inner one
// on the outer object containing this object as an inner one
// isn't actually changing.
@Published private(set) var count: Int = 0

@Published private(set) var count = 0

var isEven: Bool {
count % 2 == 0
Expand Down
37 changes: 19 additions & 18 deletions RepublishTestApp.swiftpm/App/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ import SwiftUI
@MainActor
final class ViewModel: ObservableObject {

// Here the @Republished property wrapper is used to hold
// the nested object *instead* of an @Published property wrapper.
// (There are *no* @Published wrappers in this file.)

// @Republished listens to the inner ObservableObject's
// change notifications and propagates them to the outer one.

// SwiftUI views can use properties derived from the inner object
// normally — just like how they would use an @Published field.
//
// This outer object could also provide @Binding surfaces into
// the inner object's data.

@Republished private var model: DomainModel

init(model: DomainModel) {
_model = .init(wrappedValue: model)
}
Expand All @@ -33,9 +18,9 @@ final class ViewModel: ObservableObject {
model.isMin ? "MININT" : nil,
model.isPrime ? "prime" : nil
]
.compactMap { $0 }
.sorted()
.joined(separator: ", ")
.compactMap { $0 }
.sorted()
.joined(separator: ", ")
}

var countString: String {
Expand All @@ -57,4 +42,20 @@ final class ViewModel: ObservableObject {
func zero() {
model.set(count: 0)
}

// Here the @Republished property wrapper is used to hold
// the nested object *instead* of an @Published property wrapper.
// (There are *no* @Published wrappers in this file.)

// @Republished listens to the inner ObservableObject's
// change notifications and propagates them to the outer one.

// SwiftUI views can use properties derived from the inner object
// normally — just like how they would use an @Published field.
//
// This outer object could also provide @Binding surfaces into
// the inner object's data.

@Republished private var model: DomainModel

}
4 changes: 2 additions & 2 deletions RepublishTestApp.swiftpm/App/Views/CapsuleButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ struct CapsuleButton: View {
let bg: (c: CGFloat, m: CGFloat, y: CGFloat, k: CGFloat)
let fg: CGFloat
let text: String
let action: () -> ()
let action: () -> Void

var body: some View {
Button(text) { action() }
.padding(16)
Expand Down
13 changes: 8 additions & 5 deletions RepublishTestApp.swiftpm/App/Views/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import SwiftUI

// MARK: - ContentView

struct ContentView: View {

// Regular direct use of outer ObservableObject

@StateObject var viewModel: ViewModel

var body: some View {
Expand All @@ -20,28 +22,28 @@ struct ContentView: View {
VStack(alignment: .center, spacing: 24) {
Spacer()
CapsuleButton(
bg: (1,0,0,0),
bg: (1, 0, 0, 0),
fg: 1,
text: "count += 1"
) {
viewModel.increment()
}
CapsuleButton(
bg: (0,1,0,0),
bg: (0, 1, 0, 0),
fg: 1,
text: "count -= 1"
) {
viewModel.decrement()
}
CapsuleButton(
bg: (0,0,1,0),
bg: (0, 0, 1, 0),
fg: 0,
text: "count = rand()"
) {
viewModel.rand()
}
CapsuleButton(
bg: (0,0,0,1),
bg: (0, 0, 0, 1),
fg: 1,
text: "count = 0"
) {
Expand All @@ -56,6 +58,7 @@ struct ContentView: View {
}
}

// MARK: - ContentView_Previews

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
Expand Down
2 changes: 1 addition & 1 deletion RepublishTestApp.swiftpm/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
// This file is automatically generated.
// Do not edit it by hand because the contents will be replaced.

import PackageDescription
import AppleProductTypes
import PackageDescription

let package = Package(
name: "RepublishTestApp",
Expand Down
2 changes: 1 addition & 1 deletion RepublishedExampleApp/RepublishedExampleApp/App.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI

@main
struct RepublishTestApp: App {

var body: some Scene {
WindowGroup {
ContentView(viewModel: ViewModel(model: DomainModel()))
Expand Down
12 changes: 6 additions & 6 deletions RepublishedExampleApp/RepublishedExampleApp/DomainModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import SwiftUI

final class DomainModel: ObservableObject {

// A standard ObservableObject.
// A standard ObservableObject.

// Updates to `count` makes the object fire a signal that
// consumers can listen to to know when to read — and SwiftUI
// does this by default.

// However, if you nest this in another ObservableObject, there's
// no inbuilt functionality to make the outer one fire for updates
// in response to this inner one firing.

// (An ObservableObject is a reference type, so an @Published field
// on the outer object containing this object as an inner one
// on the outer object containing this object as an inner one
// isn't actually changing.
@Published private(set) var count: Int = 0

@Published private(set) var count = 0

var isEven: Bool {
count % 2 == 0
Expand Down
37 changes: 19 additions & 18 deletions RepublishedExampleApp/RepublishedExampleApp/ViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,6 @@ import SwiftUI
@MainActor
final class ViewModel: ObservableObject {

// Here the @Republished property wrapper is used to hold
// the nested object *instead* of an @Published property wrapper.
// (There are *no* @Published wrappers in this file.)

// @Republished listens to the inner ObservableObject's
// change notifications and propagates them to the outer one.

// SwiftUI views can use properties derived from the inner object
// normally — just like how they would use an @Published field.
//
// This outer object could also provide @Binding surfaces into
// the inner object's data.

@Republished private var model: DomainModel

init(model: DomainModel) {
_model = .init(wrappedValue: model)
}
Expand All @@ -33,9 +18,9 @@ final class ViewModel: ObservableObject {
model.isMin ? "MININT" : nil,
model.isPrime ? "prime" : nil
]
.compactMap { $0 }
.sorted()
.joined(separator: ", ")
.compactMap { $0 }
.sorted()
.joined(separator: ", ")
}

var countString: String {
Expand All @@ -57,4 +42,20 @@ final class ViewModel: ObservableObject {
func zero() {
model.set(count: 0)
}

// Here the @Republished property wrapper is used to hold
// the nested object *instead* of an @Published property wrapper.
// (There are *no* @Published wrappers in this file.)

// @Republished listens to the inner ObservableObject's
// change notifications and propagates them to the outer one.

// SwiftUI views can use properties derived from the inner object
// normally — just like how they would use an @Published field.
//
// This outer object could also provide @Binding surfaces into
// the inner object's data.

@Republished private var model: DomainModel

}
Loading

0 comments on commit e2837ac

Please sign in to comment.