Custom flow layout implemented

This commit is contained in:
Damodaram 2020-06-01 15:54:01 +05:30
parent a90758e73a
commit 1dfe49bde6
9 changed files with 150 additions and 97 deletions

View File

@ -222,7 +222,7 @@
BB2FB3BB247E7EBC00DF73CD /* TagCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB2FB3BA247E7EBC00DF73CD /* TagCollectionViewCell.swift */; };
BB2FB3BD247E7EF200DF73CD /* TagsList.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB2FB3BC247E7EF200DF73CD /* TagsList.swift */; };
BB2FB3BF247E7F0900DF73CD /* TagsListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB2FB3BE247E7F0900DF73CD /* TagsListModel.swift */; };
BB2FB3C1247EC1EB00DF73CD /* CollectionViewCenterLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB2FB3C0247EC1EB00DF73CD /* CollectionViewCenterLayout.swift */; };
BB2FB3C1247EC1EB00DF73CD /* CustomCollectionViewFlowLayout.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB2FB3C0247EC1EB00DF73CD /* CustomCollectionViewFlowLayout.swift */; };
BB47A586241615EF002BB23C /* ListOneColumnFullWidthTextDividerSubsectionModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB47A585241615EF002BB23C /* ListOneColumnFullWidthTextDividerSubsectionModel.swift */; };
BB47A588241615FA002BB23C /* ListOneColumnFullWidthTextDividerSubsection.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB47A587241615FA002BB23C /* ListOneColumnFullWidthTextDividerSubsection.swift */; };
BB54C5202434D92F0038326C /* ListRightVariableButtonAllTextAndLinks.swift in Sources */ = {isa = PBXBuildFile; fileRef = BB54C51E2434D92F0038326C /* ListRightVariableButtonAllTextAndLinks.swift */; };
@ -238,6 +238,8 @@
BBAA4F05243D8E3B005AAD5F /* RadioBoxesModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBAA4F01243D8E3B005AAD5F /* RadioBoxesModel.swift */; };
BBBBC87C24374A4900B0F079 /* ListThreeColumnBillChangesDivider.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBBBC87A24374A4900B0F079 /* ListThreeColumnBillChangesDivider.swift */; };
BBBBC87D24374A4900B0F079 /* ListThreeColumnBillChangesDividerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBBBC87B24374A4900B0F079 /* ListThreeColumnBillChangesDividerModel.swift */; };
BBC0C4FD24811DBC0087C44F /* Tag.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC0C4FC24811DBC0087C44F /* Tag.swift */; };
BBC0C4FF24811DCA0087C44F /* TagModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = BBC0C4FE24811DCA0087C44F /* TagModel.swift */; };
C003506123AA94CD00B6AC29 /* Button.swift in Sources */ = {isa = PBXBuildFile; fileRef = C003506023AA94CD00B6AC29 /* Button.swift */; };
C07065C42395677300FBF997 /* Link.swift in Sources */ = {isa = PBXBuildFile; fileRef = C07065C32395677300FBF997 /* Link.swift */; };
C695A67F23C9830600BFB94E /* UnOrderedListModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = C695A67E23C9830600BFB94E /* UnOrderedListModel.swift */; };
@ -659,7 +661,7 @@
BB2FB3BA247E7EBC00DF73CD /* TagCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagCollectionViewCell.swift; sourceTree = "<group>"; };
BB2FB3BC247E7EF200DF73CD /* TagsList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagsList.swift; sourceTree = "<group>"; };
BB2FB3BE247E7F0900DF73CD /* TagsListModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagsListModel.swift; sourceTree = "<group>"; };
BB2FB3C0247EC1EB00DF73CD /* CollectionViewCenterLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewCenterLayout.swift; sourceTree = "<group>"; };
BB2FB3C0247EC1EB00DF73CD /* CustomCollectionViewFlowLayout.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomCollectionViewFlowLayout.swift; sourceTree = "<group>"; };
BB47A585241615EF002BB23C /* ListOneColumnFullWidthTextDividerSubsectionModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListOneColumnFullWidthTextDividerSubsectionModel.swift; sourceTree = "<group>"; };
BB47A587241615FA002BB23C /* ListOneColumnFullWidthTextDividerSubsection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListOneColumnFullWidthTextDividerSubsection.swift; sourceTree = "<group>"; };
BB54C51E2434D92F0038326C /* ListRightVariableButtonAllTextAndLinks.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListRightVariableButtonAllTextAndLinks.swift; sourceTree = "<group>"; };
@ -675,6 +677,8 @@
BBAA4F01243D8E3B005AAD5F /* RadioBoxesModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RadioBoxesModel.swift; sourceTree = "<group>"; };
BBBBC87A24374A4900B0F079 /* ListThreeColumnBillChangesDivider.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListThreeColumnBillChangesDivider.swift; sourceTree = "<group>"; };
BBBBC87B24374A4900B0F079 /* ListThreeColumnBillChangesDividerModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ListThreeColumnBillChangesDividerModel.swift; sourceTree = "<group>"; };
BBC0C4FC24811DBC0087C44F /* Tag.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tag.swift; sourceTree = "<group>"; };
BBC0C4FE24811DCA0087C44F /* TagModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TagModel.swift; sourceTree = "<group>"; };
C003506023AA94CD00B6AC29 /* Button.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Button.swift; sourceTree = "<group>"; };
C07065C32395677300FBF997 /* Link.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Link.swift; sourceTree = "<group>"; };
C695A67E23C9830600BFB94E /* UnOrderedListModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UnOrderedListModel.swift; sourceTree = "<group>"; };
@ -1441,8 +1445,10 @@
D264FAA8243FE17A00D98315 /* Selectors */ = {
isa = PBXGroup;
children = (
BBC0C4FE24811DCA0087C44F /* TagModel.swift */,
BBC0C4FC24811DBC0087C44F /* Tag.swift */,
BB2FB3BA247E7EBC00DF73CD /* TagCollectionViewCell.swift */,
BB2FB3C0247EC1EB00DF73CD /* CollectionViewCenterLayout.swift */,
BB2FB3C0247EC1EB00DF73CD /* CustomCollectionViewFlowLayout.swift */,
BB2FB3BC247E7EF200DF73CD /* TagsList.swift */,
BB2FB3BE247E7F0900DF73CD /* TagsListModel.swift */,
D264FAAB2441009400D98315 /* RadioBoxCollectionViewCell.swift */,
@ -2033,6 +2039,7 @@
94C2D9A923872E5E0006CF46 /* LabelAttributeImageModel.swift in Sources */,
DBC4391922442197001AB423 /* DashLine.swift in Sources */,
D264FAAA2440F97600D98315 /* CollectionView.swift in Sources */,
BBC0C4FF24811DCA0087C44F /* TagModel.swift in Sources */,
0A7BAD74232A8DC700FB8E22 /* HeadlineBodyButton.swift in Sources */,
D2FB151D23A40F1500C20E10 /* MoleculeStackItem.swift in Sources */,
AA11A41F23F15D3100D7962F /* ListRightVariablePayments.swift in Sources */,
@ -2198,7 +2205,7 @@
D260105D23D0BCD400764D80 /* Stack.swift in Sources */,
0A7EF85D23D8A95600B2AAD1 /* TextEntryFieldModel.swift in Sources */,
BB54C5212434D92F0038326C /* ListRightVariableButtonAllTextAndLinksModel.swift in Sources */,
BB2FB3C1247EC1EB00DF73CD /* CollectionViewCenterLayout.swift in Sources */,
BB2FB3C1247EC1EB00DF73CD /* CustomCollectionViewFlowLayout.swift in Sources */,
D2092349244A51D40044AD09 /* RadioSwatchModel.swift in Sources */,
8DD1E370243B3D0500D8F2DF /* ListThreeColumnInternationalData.swift in Sources */,
D23EA802247EBED400D60C34 /* ImageBarButtonItem.swift in Sources */,
@ -2232,6 +2239,7 @@
011B58F223A2AE2C0085F53C /* DropDownListItemModel.swift in Sources */,
D2509ED12472ED9B001BFB9D /* NavigationItemModelProtocol.swift in Sources */,
8D448E5524050A46006211BB /* ListOneColumnFullWidthTextAllTextAndLinksModel.swift in Sources */,
BBC0C4FD24811DBC0087C44F /* Tag.swift in Sources */,
94C2D9842386F3F80006CF46 /* LabelAttributeModel.swift in Sources */,
944589212385D6E900DE9FD4 /* DashLineModel.swift in Sources */,
D2E2A99623D8CF85000B42E6 /* HeadlineBodyLinkToggleModel.swift in Sources */,

View File

@ -1,61 +0,0 @@
//
// CollectionViewCenterLayout.swift
// MVMCoreUI
//
// Created by Dhamodaram Nandi on 27/05/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import UIKit
class CollectionViewRow {
var attributes = [UICollectionViewLayoutAttributes]()
var spacing: CGFloat = 0
init(spacing: CGFloat) {
self.spacing = spacing
}
func add(attribute: UICollectionViewLayoutAttributes) {
attributes.append(attribute)
}
var rowWidth: CGFloat {
return attributes.reduce(0, { result, attribute -> CGFloat in
return result + attribute.frame.width
}) + CGFloat(attributes.count - 1) * spacing
}
func centerLayout(collectionViewWidth: CGFloat) {
let padding = (collectionViewWidth - rowWidth) / 2
var offset = padding
for attribute in attributes {
attribute.frame.origin.x = offset
offset += attribute.frame.width + spacing
}
}
}
class UICollectionViewCenterLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
guard let attributes = super.layoutAttributesForElements(in: rect) else {
return nil
}
var rows = [CollectionViewRow]()
var currentRowY: CGFloat = -1
for attribute in attributes {
if currentRowY != attribute.frame.origin.y {
currentRowY = attribute.frame.origin.y
rows.append(CollectionViewRow(spacing: 10))
}
rows.last?.add(attribute: attribute)
}
rows.forEach { $0.centerLayout(collectionViewWidth: collectionView?.frame.width ?? 0) }
return rows.flatMap { $0.attributes }
}
}

View File

@ -0,0 +1,34 @@
//
// CollectionViewCenterLayout.swift
// MVMCoreUI
//
// Created by Dhamodaram Nandi on 27/05/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import UIKit
class UICollectionViewCenterLayout: UICollectionViewFlowLayout {
override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
let attributesForElementsInRect = super.layoutAttributesForElements(in: rect)
var newAttributesForElementsInRect = [UICollectionViewLayoutAttributes]()
var leftMargin: CGFloat = 0.0;
for attributes in attributesForElementsInRect! {
if (attributes.frame.origin.x == self.sectionInset.left) {
leftMargin = self.sectionInset.left
} else {
var newLeftAlignedFrame = attributes.frame
newLeftAlignedFrame.origin.x = leftMargin
attributes.frame = newLeftAlignedFrame
}
leftMargin += attributes.frame.size.width + 8
newAttributesForElementsInRect.append(attributes)
}
return newAttributesForElementsInRect
}
}

View File

@ -0,0 +1,35 @@
//
// Tag.swift
// MVMCoreUI
//
// Created by Dhamodaram Nandi on 29/05/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import Foundation
@objcMembers open class Tag: View {
public let label = Label.createLabelRegularBodySmall(true)
// MARK: - MVMCoreViewProtocol
open override func setupView() {
super.setupView()
layer.borderColor = UIColor.mvmCoolGray6.cgColor
layer.borderWidth = 1
label.numberOfLines = 1
addSubview(label)
NSLayoutConstraint.constraintPinSubview(label, pinTop: true, topConstant: 13, pinBottom: true, bottomConstant: 13, pinLeft: true, leftConstant: 15, pinRight: true, rightConstant: 15)
}
// MARK: - MoleculeViewProtocol
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
guard let model = model as? TagModel else { return }
label.set(with: model.label, delegateObject, additionalData)
}
@objc override open func updateView(_ size: CGFloat) {
super.updateView(size)
}
}

View File

@ -8,7 +8,7 @@
import Foundation
open class TagCollectionViewCell: CollectionViewCell {
public let tagLabel = Label()
public let tagLabel = Tag()
open override func reset() {
super.reset()
@ -17,16 +17,12 @@ open class TagCollectionViewCell: CollectionViewCell {
open override func setupView() {
super.setupView()
layer.borderColor = UIColor.mvmCoolGray6.cgColor
layer.borderWidth = 1
tagLabel.numberOfLines = 0
addMolecule(tagLabel)
MVMCoreUIUtility.setMarginsFor(contentView, leading: 15, top: 13, trailing: 15, bottom: 13)
MVMCoreUIUtility.setMarginsFor(contentView, leading: 0, top: 0, trailing: 0, bottom: 0)
}
open override func set(with model: MoleculeModelProtocol, _ delegateObject: MVMCoreUIDelegateObject?, _ additionalData: [AnyHashable: Any]?) {
guard let model = model as? LabelModel else { return }
guard let model = model as? TagModel else { return }
tagLabel.set(with: model, delegateObject, additionalData)
}
}

View File

@ -0,0 +1,37 @@
//
// TagModel.swift
// MVMCoreUI
//
// Created by Dhamodaram Nandi on 29/05/20.
// Copyright © 2020 Verizon Wireless. All rights reserved.
//
import Foundation
@objcMembers public class TagModel: MoleculeModelProtocol {
public static var identifier: String = "tag"
public var label: LabelModel
public var action: ActionModelProtocol
public var backgroundColor: Color?
private enum CodingKeys: String, CodingKey {
case moleculeName
case label
case action
case backgroundColor
}
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
label = try typeContainer.decode(LabelModel.self, forKey: .label)
action = try typeContainer.decodeModel(codingKey: .action)
}
public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(moleculeName, forKey: .moleculeName)
try container.encode(label, forKey: .label)
try container.encodeModel(action, forKey: .action)
}
}

View File

@ -15,13 +15,13 @@ open class TagsList: View {
private var tagsListModel: TagsListModel? {
return model as? TagsListModel
}
private var delegateObject: MVMCoreUIDelegateObject?
/// The models for the molecules.
public var tags: [LabelModel]?
public var tags: [TagModel]?
open override func layoutSubviews() {
super.layoutSubviews()
// Accounts for any collection size changes
@ -48,8 +48,11 @@ open class TagsList: View {
guard let tagsListModel = model as? TagsListModel else { return }
tags = tagsListModel.tags
registerCells()
setHeight()
collectionView.reloadData()
if let height = tagsListModel.height {
collectionViewHeight?.constant = CGFloat(height)
collectionViewHeight?.isActive = true
}
}
@objc override open func updateView(_ size: CGFloat) {
@ -61,14 +64,14 @@ open class TagsList: View {
/// Creates the layout for the collection.
open func createCollectionViewLayout() -> UICollectionViewLayout {
let layout = UICollectionViewFlowLayout()
let layout = UICollectionViewCenterLayout()
layout.scrollDirection = .vertical
layout.estimatedItemSize = CGSize(width: 140, height: 40)
layout.minimumLineSpacing = 24.0
layout.minimumInteritemSpacing = itemSpacing
return layout
}
/// Creates the collection view.
open func createCollectionView() -> CollectionView {
let collection = CollectionView(frame: .zero, collectionViewLayout: createCollectionViewLayout())
@ -81,15 +84,15 @@ open class TagsList: View {
open func registerCells() {
collectionView.register(TagCollectionViewCell.self, forCellWithReuseIdentifier: "TagCollectionViewCell")
}
// MARK: - JSON Setters
open func setHeight() {
let height = collectionView.collectionViewLayout.collectionViewContentSize.height
collectionViewHeight.constant = height
}
}
extension TagsList: UICollectionViewDataSource {
open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return tags?.count ?? 0
@ -98,7 +101,7 @@ extension TagsList: UICollectionViewDataSource {
open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let molecule = tags?[indexPath.row],
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "TagCollectionViewCell", for: indexPath) as? TagCollectionViewCell else {
fatalError()
fatalError()
}
cell.reset()
cell.set(with: molecule, delegateObject, nil)
@ -108,13 +111,13 @@ extension TagsList: UICollectionViewDataSource {
}
extension TagsList: UICollectionViewDelegate {
open func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
guard let tagModel = tags?[indexPath.row] else {return}
if let data = try? tagModel.action.encode(using: JSONEncoder()),
let actionMap = try? JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.init()) as? [AnyHashable: Any]{
MVMCoreActionHandler.shared()?.handleAction(with: actionMap, additionalData: nil, delegateObject: delegateObject)
}
}
}

View File

@ -12,24 +12,24 @@ import Foundation
public var backgroundColor: Color?
public static var identifier: String = "tagsList"
public var tags: [LabelModel]
public var tags: [TagModel]
public var borderColor: Color?
public var height: Float?
private enum CodingKeys: String, CodingKey {
case moleculeName
case borderColor
case backgroundColor
case tags
case height
}
required public init(from decoder: Decoder) throws {
let typeContainer = try decoder.container(keyedBy: CodingKeys.self)
borderColor = try typeContainer.decodeIfPresent(Color.self, forKey: .borderColor)
tags = try typeContainer.decode([LabelModel].self, forKey: .tags)
tags = try typeContainer.decode([TagModel].self, forKey: .tags)
backgroundColor = try typeContainer.decodeIfPresent(Color.self, forKey: .backgroundColor)
height = try typeContainer.decodeIfPresent(Float.self, forKey: .height)
}
public func encode(to encoder: Encoder) throws {
@ -38,7 +38,6 @@ import Foundation
try container.encode(tags, forKey: .tags)
try container.encodeIfPresent(borderColor, forKey: .borderColor)
try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
try container.encode(height, forKey: .height)
}
}

View File

@ -74,6 +74,8 @@ import Foundation
MoleculeObjectMapping.shared()?.register(viewClass: Checkbox.self, viewModelClass: CheckboxModel.self)
MoleculeObjectMapping.shared()?.register(viewClass: RadioSwatches.self, viewModelClass: RadioSwatchesModel.self)
MoleculeObjectMapping.shared()?.register(viewClass: TagsList.self, viewModelClass: TagsListModel.self)
MoleculeObjectMapping.shared()?.register(viewClass: Tag.self, viewModelClass: TagModel.self)
// Other Atoms