Hello togehter,
i do have a pretty complex struct view with a couple of properties and methods. Due to the fact, that I want to have a different design of this particular view in dependence of portrait and landscape orientation of a device, I distinguish between a Portrait and Landscape view. Functionality of both subviews should be the same (same properties and same methods), the design and arrangement of single view elements will be different. I don’t want to bother you with the details, therefore I simplified the code snippets to a minimum.
import Foundation
import SwiftUI
struct ScoreboardView: View {
@Environment(\.horizontalSizeClass) var hSizeClass
@Environment(\.verticalSizeClass) var vSizeClass
// Properties
let minDURATION = 1.0 // global minimum duration time for onLongPressGesture
@State var strStatus: String = "Start Game"
@State var intBalls = 0
@State var intStrikes = 0
@State var intOuts = 0
@State var runsHome = 0
@State var runsGuest = 0
@State var halfInning = inningHalfs.top
@State var intInning = 1
@State var statBase = [false,false,false]
// View
var body: some View {
// device orientation: portrait
if hSizeClass == .compact && vSizeClass == .regular {
ScoreboardViewPortrait()
}
//device orientation: landscape
else {
ScoreboardViewLandscape()
}
}
// Methods
func resetBaseStatus() {
for index in 0..<statBase.count {
statBase[index] = false
}
}
// MARK: methods - clearBallStrike
func clearBallStrike() {
intBalls = 0
intStrikes = 0
}
// MARK: methods - clearBallStrikeOut
func clearBallStrikeOut() {
intBalls = 0
intStrikes = 0
intOuts = 0
resetBaseStatus()
}
// MARK: methods - changeScoringState
func changeScoringState(selectedElement: selItem, action: String, half: inningHalfs?=nil) {
...
}
At the beginning the coding of views ScoreboardViewPortrait and ScoreboardViewLandscape where inside the if brackets. For better code reading I defined own view structs for these two Subviews. Code below is very much simplified too…
struct ScoreboardViewPortrait: View {
// View - device orientation: portrait
var body: some View {
VStack {
Text(String(intInning))
.font(.custom("FaceOffM54", size: 122))
.multilineTextAlignment(.center)
.onTapGesture {
changeScoringState(selectedElement: selItem.inning, action: "inc")
}
.onLongPressGesture(minimumDuration: minDURATION, maximumDistance: 10) {
changeScoringState(selectedElement: selItem.inning, action: "dec")
}
}
}
}
Now the methods defined in the parent view (ScoreboardView), are not recognized in the child views (ScoreboardviewPortrait and ScoreboardViewLandscape) anymore.
Now my general questions is, how do you write good code for such a case? Any help or links to good articles explaining good code design with examples are appreciated very much!