diff --git a/VDS.xcodeproj/project.pbxproj b/VDS.xcodeproj/project.pbxproj index 78fcf40c..77873b97 100644 --- a/VDS.xcodeproj/project.pbxproj +++ b/VDS.xcodeproj/project.pbxproj @@ -45,6 +45,7 @@ EAF7F0962899861000B287F5 /* VDSCheckboxModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0942899861000B287F5 /* VDSCheckboxModel.swift */; }; EAF7F09A2899B17200B287F5 /* CATransaction.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F0992899B17200B287F5 /* CATransaction.swift */; }; EAF7F09E289AAEC000B287F5 /* VDSConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F09D289AAEC000B287F5 /* VDSConstants.swift */; }; + EAF7F0A0289AB7EC00B287F5 /* VDSView.swift in Sources */ = {isa = PBXBuildFile; fileRef = EAF7F09F289AB7EC00B287F5 /* VDSView.swift */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -97,6 +98,7 @@ EAF7F0942899861000B287F5 /* VDSCheckboxModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = VDSCheckboxModel.swift; sourceTree = ""; }; EAF7F0992899B17200B287F5 /* CATransaction.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CATransaction.swift; sourceTree = ""; }; EAF7F09D289AAEC000B287F5 /* VDSConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VDSConstants.swift; sourceTree = ""; }; + EAF7F09F289AB7EC00B287F5 /* VDSView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = VDSView.swift; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -227,6 +229,7 @@ EA3361B5288B2A410071C351 /* VDSControl.swift */, EA3C3B4B2894823E000CA526 /* AnyProxy-PropertyWrapper.swift */, EAF7F09D289AAEC000B287F5 /* VDSConstants.swift */, + EAF7F09F289AB7EC00B287F5 /* VDSView.swift */, ); path = Classes; sourceTree = ""; @@ -401,6 +404,7 @@ files = ( EA3362322891F2ED0071C351 /* VDSFontStyles.swift in Sources */, EA3361C328902D960071C351 /* VDSToggle.swift in Sources */, + EAF7F0A0289AB7EC00B287F5 /* VDSView.swift in Sources */, EA3362402892EF6C0071C351 /* VDSLabel.swift in Sources */, EA33622E2891EA3C0071C351 /* DispatchQueue+Once.swift in Sources */, EA3361C5289030FC0071C351 /* Accessable.swift in Sources */, diff --git a/VDS/Classes/VDSView.swift b/VDS/Classes/VDSView.swift new file mode 100644 index 00000000..56174c0b --- /dev/null +++ b/VDS/Classes/VDSView.swift @@ -0,0 +1,74 @@ +// +// Control.swift +// VDS +// +// Created by Matt Bruce on 7/22/22. +// + +import Foundation +import UIKit +import Combine + + +open class VDSView: UIView, ModelHandlerable, ViewProtocol { + + @Published public var model: ModelType + private var cancellable: AnyCancellable? + + open func set(with model: ModelType) { + self.model = model + } + + open func onStateChange(viewModel: ModelType) { + + } + + //-------------------------------------------------- + // MARK: - Properties + //-------------------------------------------------- + private var initialSetupPerformed = false + + //-------------------------------------------------- + // MARK: - Initializers + //-------------------------------------------------- + + required public init(with model: ModelType) { + self.model = model + super.init(frame: .zero) + initialSetup() + set(with: model) + } + + public required init?(coder: NSCoder) { + self.model = ModelType.init() + super.init(coder: coder) + initialSetup() + } + + //-------------------------------------------------- + // MARK: - Setup + //-------------------------------------------------- + + public func initialSetup() { + if !initialSetupPerformed { + initialSetupPerformed = true + setupView() + } + cancellable = $model.debounce(for: .seconds(VDSConstants.ModelStateDebounce), scheduler: RunLoop.main).sink { [weak self] viewModel in + self?.onStateChange(viewModel: viewModel) + } + } + + open func reset() { + backgroundColor = .clear + } + + // MARK: - ViewProtocol + open func updateView(_ size: CGFloat) { } + + /// Will be called only once. + open func setupView() { + translatesAutoresizingMaskIntoConstraints = false + insetsLayoutMarginsFromSafeArea = false + } +}