SwiftUI: Choosing an Application Architecture | by Michael Long | Mar, 2022

It’s exhausting to resolve in case you don’t know what you want.

Picture by Patrick Schneider on Unsplash

Defining the Downside

Efficiency and Compatibility

WWDC’19 “Knowledge Circulate By means of SwiftUI” presentation
WWDC19 “Knowledge Circulate By means of SwiftUI” presentation

Simplicity

View Composition

Testing

struct OrderDetailsRowView: View 
var merchandise: OrderItem
var physique: some View
HStack
if merchandise.amount == 1
Textual content(merchandise.title)
else
Textual content("(merchandise.title) $((merchandise.amount, specifier: "%.2f") @ $(merchandise.worth, specifier: "%.2f")")

Spacer()
Textual content("$((merchandise.whole, specifier: "%.2f")")


Separation of Considerations

struct OrderDetailsView: View   @StateObject var vm = OrderDetailsViewModel()  var physique: some View 
Kind
if vm.message.hasMessage
StatusMessageView(sort: vm.message)
LabelValueRowView(label: "Order", worth: vm.dateValue)

ForEach(vm.gadgets) merchandise in
OrderDetailsRowView(merchandise: merchandise)

if vm.hasDiscount
LabelValueRowView(label: "Subtotal", worth: vm.subtotal)
OrderDetailsDiscountView(worth: vm.low cost)
LabelValueRowView(label: vml.totalLabel, worth: vm.whole) Button("Order Once more")
self.vm.reorder()


.onAppear
vm.load()

Testing Devoted Views

struct OrderDetailsRowView: View     var merchandise: OrderItem    var physique: some View 
HStack
Textual content(itemDescription)
Spacer()
Textual content(itemTotal)

var itemDescription: String
if merchandise.amount == 1
return merchandise.title
else
return "(merchandise.title) ((merchandise.formattedQuantity) @ (merchandise.formattedPrice))"

var itemTotal: String
merchandise.formattedTotal

func testOrderDetailsRowView()     let view1 = OrderDetailsRowView(merchandise: OrderItem.mock1)
XCTAssert(view1.itemDescription == "Gentle Drink")
XCTAssert(view1.itemTotal == "$1.99")
let view2 = OrderDetailsRowView(merchandise: OrderItem.mock2)
XCTAssert(view2.itemDescription == "Cheeseburger (2 @ $4.99)")
XCTAssert(view2.itemTotal == "$9.98")

Standards for a SwiftUI Structure

Completion Block

Need extra fascinating tales?Be at liberty to take a look at the opposite articles in my SwiftUI Series.

More Posts