From 5e3aff478ab6fb063dcb4570d3706abe12e75b51 Mon Sep 17 00:00:00 2001 From: "Shikalgar, Shahrukh" Date: Fri, 24 Jul 2020 17:32:39 +0530 Subject: [PATCH] Component 4 - Segment Controller using List and ScrollView --- SwiftUILearning.xcodeproj/project.pbxproj | 76 +++++++ .../xcdebugger/Breakpoints_v2.xcbkptlist | 14 ++ .../ContentView/Enum/ContentEnum.swift | 1 + .../ViewModel/ContentViewModel.swift | 5 +- .../List - Week 3/View/ListHomeView.swift | 2 +- .../Model/SegmentControllerModel.swift | 30 +++ .../View/SegmentControllerView.swift | 38 ++++ .../View/SubViews/Rows/TeamListRow.swift | 167 +++++++++++++++ .../SubViews/SegmentControllerListView.swift | 80 +++++++ .../SegmentControllerScrollView.swift | 82 ++++++++ .../View/SubViews/TeamDetailsView.swift | 122 +++++++++++ .../SegmentControllerViewModel.swift | 197 ++++++++++++++++++ 12 files changed, 812 insertions(+), 2 deletions(-) create mode 100644 SwiftUILearning/Components/SegmentController - Week 4/Model/SegmentControllerModel.swift create mode 100644 SwiftUILearning/Components/SegmentController - Week 4/View/SegmentControllerView.swift create mode 100644 SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/Rows/TeamListRow.swift create mode 100644 SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerListView.swift create mode 100644 SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerScrollView.swift create mode 100644 SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/TeamDetailsView.swift create mode 100644 SwiftUILearning/Components/SegmentController - Week 4/ViewModel/SegmentControllerViewModel.swift diff --git a/SwiftUILearning.xcodeproj/project.pbxproj b/SwiftUILearning.xcodeproj/project.pbxproj index b60ab3b..d1c68b9 100644 --- a/SwiftUILearning.xcodeproj/project.pbxproj +++ b/SwiftUILearning.xcodeproj/project.pbxproj @@ -12,6 +12,13 @@ 384ECA6F24B30F7E00D2CDC0 /* TextAndImageView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 384ECA6E24B30F7E00D2CDC0 /* TextAndImageView.swift */; }; 384ECA7224B3611D00D2CDC0 /* ImageListType.swift in Sources */ = {isa = PBXBuildFile; fileRef = 384ECA7124B3611D00D2CDC0 /* ImageListType.swift */; }; 384ECA7524B3614E00D2CDC0 /* ImageListViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 384ECA7424B3614E00D2CDC0 /* ImageListViewModel.swift */; }; + 386B8C2024C717AA00162409 /* SegmentControllerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386B8C1F24C717AA00162409 /* SegmentControllerModel.swift */; }; + 386B8C2224C717CA00162409 /* SegmentControllerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386B8C2124C717CA00162409 /* SegmentControllerView.swift */; }; + 386B8C2424C717DD00162409 /* SegmentControllerViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386B8C2324C717DD00162409 /* SegmentControllerViewModel.swift */; }; + 386B8C2724C718C400162409 /* SegmentControllerListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386B8C2624C718C400162409 /* SegmentControllerListView.swift */; }; + 386B8C2924C718D100162409 /* SegmentControllerScrollView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 386B8C2824C718D100162409 /* SegmentControllerScrollView.swift */; }; + 3890EFCB24C9C4FE004EB658 /* TeamDetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3890EFCA24C9C4FE004EB658 /* TeamDetailsView.swift */; }; + 3891E56324C80EAD00D4C661 /* TeamListRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3891E56224C80EAD00D4C661 /* TeamListRow.swift */; }; 38A439C524BC330B00CA4CEB /* ListEnum.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38A439C424BC330B00CA4CEB /* ListEnum.swift */; }; 38A439C724BC336100CA4CEB /* ListHomeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38A439C624BC336100CA4CEB /* ListHomeView.swift */; }; 38A439CA24BC337E00CA4CEB /* ListTypeView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 38A439C924BC337E00CA4CEB /* ListTypeView.swift */; }; @@ -58,6 +65,13 @@ 384ECA6E24B30F7E00D2CDC0 /* TextAndImageView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TextAndImageView.swift; sourceTree = ""; }; 384ECA7124B3611D00D2CDC0 /* ImageListType.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageListType.swift; sourceTree = ""; }; 384ECA7424B3614E00D2CDC0 /* ImageListViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ImageListViewModel.swift; sourceTree = ""; }; + 386B8C1F24C717AA00162409 /* SegmentControllerModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentControllerModel.swift; sourceTree = ""; }; + 386B8C2124C717CA00162409 /* SegmentControllerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentControllerView.swift; sourceTree = ""; }; + 386B8C2324C717DD00162409 /* SegmentControllerViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentControllerViewModel.swift; sourceTree = ""; }; + 386B8C2624C718C400162409 /* SegmentControllerListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentControllerListView.swift; sourceTree = ""; }; + 386B8C2824C718D100162409 /* SegmentControllerScrollView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SegmentControllerScrollView.swift; sourceTree = ""; }; + 3890EFCA24C9C4FE004EB658 /* TeamDetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeamDetailsView.swift; sourceTree = ""; }; + 3891E56224C80EAD00D4C661 /* TeamListRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TeamListRow.swift; sourceTree = ""; }; 38A439C424BC330B00CA4CEB /* ListEnum.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListEnum.swift; sourceTree = ""; }; 38A439C624BC336100CA4CEB /* ListHomeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListHomeView.swift; sourceTree = ""; }; 38A439C924BC337E00CA4CEB /* ListTypeView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ListTypeView.swift; sourceTree = ""; }; @@ -156,6 +170,60 @@ path = ViewModel; sourceTree = ""; }; + 386B8C1B24C7174A00162409 /* SegmentController - Week 4 */ = { + isa = PBXGroup; + children = ( + 386B8C1C24C7177100162409 /* Model */, + 386B8C1D24C7178600162409 /* View */, + 386B8C1E24C7178C00162409 /* ViewModel */, + ); + path = "SegmentController - Week 4"; + sourceTree = ""; + }; + 386B8C1C24C7177100162409 /* Model */ = { + isa = PBXGroup; + children = ( + 386B8C1F24C717AA00162409 /* SegmentControllerModel.swift */, + ); + path = Model; + sourceTree = ""; + }; + 386B8C1D24C7178600162409 /* View */ = { + isa = PBXGroup; + children = ( + 386B8C2124C717CA00162409 /* SegmentControllerView.swift */, + 386B8C2524C718AB00162409 /* SubViews */, + ); + path = View; + sourceTree = ""; + }; + 386B8C1E24C7178C00162409 /* ViewModel */ = { + isa = PBXGroup; + children = ( + 386B8C2324C717DD00162409 /* SegmentControllerViewModel.swift */, + ); + path = ViewModel; + sourceTree = ""; + }; + 386B8C2524C718AB00162409 /* SubViews */ = { + isa = PBXGroup; + children = ( + 3891E56124C80E8900D4C661 /* Rows */, + 386B8C2624C718C400162409 /* SegmentControllerListView.swift */, + 386B8C2824C718D100162409 /* SegmentControllerScrollView.swift */, + 3890EFCA24C9C4FE004EB658 /* TeamDetailsView.swift */, + ); + path = SubViews; + sourceTree = ""; + }; + 3891E56124C80E8900D4C661 /* Rows */ = { + isa = PBXGroup; + children = ( + 3891E56224C80EAD00D4C661 /* TeamListRow.swift */, + ); + path = Rows; + sourceTree = ""; + }; 38A439C024BC32BF00CA4CEB /* List - Week 3 */ = { isa = PBXGroup; children = ( @@ -295,6 +363,7 @@ 38D25C8424A9D29B00AD202F /* Text - Week 1 */, 384ECA6624B3057300D2CDC0 /* ImageView - Week 2 */, 38A439C024BC32BF00CA4CEB /* List - Week 3 */, + 386B8C1B24C7174A00162409 /* SegmentController - Week 4 */, ); path = Components; sourceTree = ""; @@ -485,10 +554,14 @@ 38A439C524BC330B00CA4CEB /* ListEnum.swift in Sources */, 38A439CC24BC339900CA4CEB /* HomeScrollView.swift in Sources */, 38D25C5524A9D26B00AD202F /* SwiftUILearning.xcdatamodeld in Sources */, + 386B8C2424C717DD00162409 /* SegmentControllerViewModel.swift in Sources */, 384ECA7524B3614E00D2CDC0 /* ImageListViewModel.swift in Sources */, 38D25C8624A9D2E600AD202F /* TextSwiftUIView.swift in Sources */, 384ECA6924B3060F00D2CDC0 /* ImageListView.swift in Sources */, + 386B8C2024C717AA00162409 /* SegmentControllerModel.swift in Sources */, + 3890EFCB24C9C4FE004EB658 /* TeamDetailsView.swift in Sources */, 38A439D624BC72B500CA4CEB /* SocialMediaListRow.swift in Sources */, + 3891E56324C80EAD00D4C661 /* TeamListRow.swift in Sources */, 38D25C8B24A9D65400AD202F /* ContentViewModel.swift in Sources */, 38A439CE24BC33AD00CA4CEB /* ListHomeViewModel.swift in Sources */, 38A439CA24BC337E00CA4CEB /* ListTypeView.swift in Sources */, @@ -496,11 +569,14 @@ 38D25C5024A9D26B00AD202F /* AppDelegate.swift in Sources */, 38A439D124BC4C2F00CA4CEB /* ListHomeModel.swift in Sources */, 38D25C9124A9D81700AD202F /* ContentEnum.swift in Sources */, + 386B8C2724C718C400162409 /* SegmentControllerListView.swift in Sources */, + 386B8C2924C718D100162409 /* SegmentControllerScrollView.swift in Sources */, 38D25C5724A9D26B00AD202F /* ContentView.swift in Sources */, 38D25C8E24A9D75E00AD202F /* ContentModel.swift in Sources */, 38A439C724BC336100CA4CEB /* ListHomeView.swift in Sources */, 38D25C5224A9D26B00AD202F /* SceneDelegate.swift in Sources */, 384ECA6F24B30F7E00D2CDC0 /* TextAndImageView.swift in Sources */, + 386B8C2224C717CA00162409 /* SegmentControllerView.swift in Sources */, 384ECA7224B3611D00D2CDC0 /* ImageListType.swift in Sources */, ); runOnlyForDeploymentPostprocessing = 0; diff --git a/SwiftUILearning.xcodeproj/xcuserdata/sshikalgar.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/SwiftUILearning.xcodeproj/xcuserdata/sshikalgar.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index 400e5e5..9f1ed9a 100644 --- a/SwiftUILearning.xcodeproj/xcuserdata/sshikalgar.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/SwiftUILearning.xcodeproj/xcuserdata/sshikalgar.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -3,4 +3,18 @@ uuid = "5F906CB2-805F-4FDA-BA7B-CCFEE249D190" type = "1" version = "2.0"> + + + + + + diff --git a/SwiftUILearning/Components/ContentView/Enum/ContentEnum.swift b/SwiftUILearning/Components/ContentView/Enum/ContentEnum.swift index 841f54d..9d0ade8 100644 --- a/SwiftUILearning/Components/ContentView/Enum/ContentEnum.swift +++ b/SwiftUILearning/Components/ContentView/Enum/ContentEnum.swift @@ -13,4 +13,5 @@ enum ComponentType: String { case text = "Text" case image = "Image" case list = "List" + case segmentControll = "Segment Controll" } diff --git a/SwiftUILearning/Components/ContentView/ViewModel/ContentViewModel.swift b/SwiftUILearning/Components/ContentView/ViewModel/ContentViewModel.swift index 5ff4d36..1b2bce4 100644 --- a/SwiftUILearning/Components/ContentView/ViewModel/ContentViewModel.swift +++ b/SwiftUILearning/Components/ContentView/ViewModel/ContentViewModel.swift @@ -16,7 +16,8 @@ class ContentViewModel { /// Model object array let contentViewItems = [ContentModel(id: 1, contentType: ComponentType.text.rawValue), ContentModel(id: 2, contentType: ComponentType.image.rawValue), - ContentModel(id: 3, contentType: ComponentType.list.rawValue)] + ContentModel(id: 3, contentType: ComponentType.list.rawValue), + ContentModel(id: 4, contentType: ComponentType.segmentControll.rawValue)] // MARK: - User define methods /// Method to get destination view @@ -30,6 +31,8 @@ class ContentViewModel { return AnyView(ImageListView()) case .list: return AnyView(ListHomeView()) + case .segmentControll: + return AnyView(SegmentControllerView()) } } } diff --git a/SwiftUILearning/Components/List - Week 3/View/ListHomeView.swift b/SwiftUILearning/Components/List - Week 3/View/ListHomeView.swift index 19e45d1..befaa56 100644 --- a/SwiftUILearning/Components/List - Week 3/View/ListHomeView.swift +++ b/SwiftUILearning/Components/List - Week 3/View/ListHomeView.swift @@ -24,7 +24,7 @@ struct ListHomeView: View { } } }.listStyle(GroupedListStyle()) - .navigationBarTitle("SwiftUI List") + .navigationBarTitle("SwiftUI List", displayMode: .inline) } } diff --git a/SwiftUILearning/Components/SegmentController - Week 4/Model/SegmentControllerModel.swift b/SwiftUILearning/Components/SegmentController - Week 4/Model/SegmentControllerModel.swift new file mode 100644 index 0000000..24580fd --- /dev/null +++ b/SwiftUILearning/Components/SegmentController - Week 4/Model/SegmentControllerModel.swift @@ -0,0 +1,30 @@ +// +// SegmentControllerModel.swift +// SwiftUILearning +// +// Created by Shikalgar, Shahrukh on 21/07/20. +// Copyright © 2020 Shahrukh Shikalgar. All rights reserved. +// + +import Foundation + +// MARK: - SegmentControll Model +/// Model for segment controller screen +struct SegmentControllModel: Codable, Identifiable, Hashable { + let id: Int? + let teamName: String + let index: Int + let homeIndex: Int + let awayIndex: Int + let all: GameRecord + let home: GameRecord + let away: GameRecord +} + +struct GameRecord: Codable, Identifiable, Hashable { + let id: Int + let played: Int + let won: Int + let draw: Int + let lost: Int +} diff --git a/SwiftUILearning/Components/SegmentController - Week 4/View/SegmentControllerView.swift b/SwiftUILearning/Components/SegmentController - Week 4/View/SegmentControllerView.swift new file mode 100644 index 0000000..74f2326 --- /dev/null +++ b/SwiftUILearning/Components/SegmentController - Week 4/View/SegmentControllerView.swift @@ -0,0 +1,38 @@ +// +// SegmentControllerView.swift +// SwiftUILearning +// +// Created by Shikalgar, Shahrukh on 21/07/20. +// Copyright © 2020 Shahrukh Shikalgar. All rights reserved. +// + +import SwiftUI + +// MARK: - Struct for SegmentControllerView +/// View for SegmentControllerView +struct SegmentControllerView: View { + // MARK: - Variables + private let viewModel = SegmentControllerViewModel() + + // MARK: - View + /// Body for View + var body: some View { + List { + ForEach(self.viewModel.contentViewItems, id: \.self) { item in + NavigationLink(destination: self.viewModel.getDestinationView(type: item.contentType ?? "")) { + Text("\(item.contentType ?? "") - Segment Controller") + } + } + } + .listStyle(GroupedListStyle()) + .navigationBarTitle(Text("Segment Controll"), displayMode: .inline) + } +} + +// MARK: - SegmentControllerView_Previews +/// Preview provider for SegmentControllerView +struct SegmentControllerView_Previews: PreviewProvider { + static var previews: some View { + SegmentControllerView() + } +} diff --git a/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/Rows/TeamListRow.swift b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/Rows/TeamListRow.swift new file mode 100644 index 0000000..55a6de9 --- /dev/null +++ b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/Rows/TeamListRow.swift @@ -0,0 +1,167 @@ +// +// TeamListRow.swift +// SwiftUILearning +// +// Created by Shikalgar, Shahrukh on 22/07/20. +// Copyright © 2020 Shahrukh Shikalgar. All rights reserved. +// + +import SwiftUI + +// MARK: - Constant Constraint +/// Struct for Constant Constraint +struct ConstantConstraint { + static let width: CGFloat = 25 +} + +// MARK: - Struct for TeamListRow +/// View for TeamListRow +struct TeamListRow: View { + // MARK: - Variables + let item: SegmentControllModel + let didTapButton: ((_ type: Int) -> Void) + + @State private var name = "" + @State private var id = 0 + @Binding private var index: String + @Binding private var played: String + @Binding private var win: String + @Binding private var draw: String + @Binding private var lost: String + @Binding private var sortingIndex: Int + + // MARK: - Initializer + /// Intializer for the view + /// - Parameter accountItem: AccountItem object + init(item: SegmentControllModel, sortingIndex: Int, buttonTapAction: @escaping ((_ type: Int) -> Void)) { + self.didTapButton = buttonTapAction + self._sortingIndex = .constant(sortingIndex) + self.item = item + _name = State(initialValue: item.teamName) + _id = State(initialValue: item.id ?? 0) + if sortingIndex == 0 { + _index = .constant("\(item.index)") + _played = .constant("\(item.all.played)") + _win = .constant("\(item.all.won)") + _draw = .constant("\(item.all.draw)") + _lost = .constant("\(item.all.lost)") + } else if sortingIndex == 1 { + _index = .constant("\(item.homeIndex)") + _played = .constant("\(item.home.played)") + _win = .constant("\(item.home.won)") + _draw = .constant("\(item.home.draw)") + _lost = .constant("\(item.home.lost)") + } else { + _index = .constant("\(item.awayIndex)") + _played = .constant("\(item.away.played)") + _win = .constant("\(item.away.won)") + _draw = .constant("\(item.away.draw)") + _lost = .constant("\(item.away.lost)") + } + } + + // MARK: - View + /// Body for View + var body: some View { + VStack(alignment: .leading) { + Button(action: { + self.didTapButton(self.id) + }, label: { + HStack() { + TeamIndexAndNameView(index: index, name: name) + Spacer() + TeamPointsDetailsView(playedGame: played, win: win, draw: draw, lost: lost) + } + }) + } + } +} + +// MARK: - Struct for TeamIndexAndNameView +/// View for TeamIndexAndNameView +struct TeamIndexAndNameView: View { + // MARK: - Variables + let index: String + let name: String + + // MARK: - View + /// Body for View + var body: some View { + HStack() { + Text(index) + .frame(minWidth: ConstantConstraint.width, + idealWidth: ConstantConstraint.width, + maxWidth: ConstantConstraint.width, + alignment: .trailing) + Text(name) + } + .foregroundColor(.black) + } +} + +// MARK: - Struct for TeamPointsDetailsView +/// View for TeamPointsDetailsView +struct TeamPointsDetailsView: View { + // MARK: - Variables + let playedGame: String + let win: String + let draw: String + let lost: String + + // MARK: - View + /// Body for View + var body: some View { + HStack() { + PointBoxView(point: playedGame) + .foregroundColor(.orange) + PointBoxView(point: win) + .foregroundColor(.green) + PointBoxView(point: draw) + .foregroundColor(.blue) + PointBoxView(point: lost) + .foregroundColor(.red) + } + } +} + +// MARK: - Struct for TeamPointsDetailsView +/// View for TeamPointsDetailsView +struct PointBoxView: View { + // MARK: - Variables + let point: String + + // MARK: - View + /// Body for View + var body: some View { + VStack { + Text(point).font(.body) + } + .frame(minWidth: ConstantConstraint.width, + idealWidth: ConstantConstraint.width, + maxWidth: ConstantConstraint.width, + alignment: .leading) + } +} + +// MARK: - Struct for TeamPointsDetailsView +/// View for TeamPointsDetailsView +struct TeamHeader: View { + // MARK: - View + /// Body for View + var body: some View { + HStack(spacing: 20) { + TeamIndexAndNameView(index: "#", name: "Teams") + Spacer() + TeamPointsDetailsView(playedGame: "P", win: "W", draw: "D", lost: "L") + } + .padding([.top, .bottom], 10) + } +} + +// MARK: - TeamListRow_Previews +/// Preview provider for TeamListRow +struct TeamListRow_Previews: PreviewProvider { + static var previews: some View { + TeamListRow(item: SegmentControllModel(id: 0, teamName: "Liverpool", index: 1, homeIndex: 1, awayIndex: 1, all: GameRecord(id: 0, played: 38, won: 30, draw: 3, lost: 3), home: GameRecord(id: 0, played: 19, won: 17, draw: 1, lost: 0), away: GameRecord(id: 0, played: 19, won: 13, draw: 2, lost: 3)), sortingIndex: 0, buttonTapAction: { _ in }) + } +} diff --git a/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerListView.swift b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerListView.swift new file mode 100644 index 0000000..7ec1695 --- /dev/null +++ b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerListView.swift @@ -0,0 +1,80 @@ +// +// SegmentControllerListView.swift +// SwiftUILearning +// +// Created by Shikalgar, Shahrukh on 21/07/20. +// Copyright © 2020 Shahrukh Shikalgar. All rights reserved. +// + +import SwiftUI + +// MARK: - Struct for SegmentControllerScrollView +/// View for SegmentControllerScrollView +struct SegmentControllerListView: View { + // MARK: - Variables + private let viewModel = SegmentControllerViewModel() + + @Environment(\.presentationMode) var presentation + @State private var showingAlert = false + @State var sortingIndex: Int + @State var selectedIndex: Int + @State var items = [SegmentControllModel]() + + // MARK: - View + /// Body for View + var body: some View { + VStack { + /// Picker View + Picker("Sort Contacts by", selection: $sortingIndex) { + Text("All").tag(0) + Text("Home").tag(1) + Text("Away").tag(2) + }.pickerStyle(SegmentedPickerStyle()) + .onReceive([self.sortingIndex].publisher.first(), perform: { tag in + switch tag { + case 0: + self.items.sort { $0.index < $1.index } + case 1: + self.items.sort { $0.homeIndex < $1.homeIndex } + case 2: + self.items.sort { $0.awayIndex < $1.awayIndex } + default: + break + } + }) + /// List View + List { + Section(header: TeamHeader()) { + ForEach(self.items, id: \.self) { item in + ZStack { + NavigationLink(destination: TeamDetailsView(item: item)) { EmptyView()}.hidden() + TeamListRow(item: item, sortingIndex: self.sortingIndex, buttonTapAction: { id in + self.selectedIndex = id + }) + } + } + .padding([.top, .bottom], 10) + } + } + .listStyle(PlainListStyle()) + .navigationBarTitle(Text("PL Teams 2019/20"), displayMode: .inline) + .navigationBarBackButtonHidden(true) + .navigationBarItems(leading: Button(action: { self.presentation.wrappedValue.dismiss() }) { + Image(systemName: "chevron.left") + .foregroundColor(.blue) + .imageScale(.large) + }) + .onAppear { + self.items = self.viewModel.getTeamList() + } + } + } +} + +// MARK: - SegmentControllerListView_Previews +/// Preview provider for SegmentControllerListView +struct SegmentControllerListView_Previews: PreviewProvider { + static var previews: some View { + SegmentControllerListView(sortingIndex: 0, selectedIndex: 0) + } +} diff --git a/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerScrollView.swift b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerScrollView.swift new file mode 100644 index 0000000..0e9439d --- /dev/null +++ b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/SegmentControllerScrollView.swift @@ -0,0 +1,82 @@ +// +// SegmentControllerScrollView.swift +// SwiftUILearning +// +// Created by Shikalgar, Shahrukh on 21/07/20. +// Copyright © 2020 Shahrukh Shikalgar. All rights reserved. +// + +import SwiftUI + +// MARK: - Struct for SegmentControllerScrollView +/// View for SegmentControllerScrollView +struct SegmentControllerScrollView: View { + // MARK: - Variables + @Environment(\.presentationMode) var presentation + @State private var showingAlert = false + @State var sortingIndex: Int + @State var selectedIndex: Int + @State var items = [SegmentControllModel]() + + private let viewModel = SegmentControllerViewModel() + + // MARK: - View + /// Body for View + var body: some View { + VStack { + /// Picker View + Picker("Sort Contacts by", selection: $sortingIndex) { + Text("All").tag(0) + Text("Home").tag(1) + Text("Away").tag(2) + }.pickerStyle(SegmentedPickerStyle()) + .onReceive([self.sortingIndex].publisher.first(), perform: { tag in + switch tag { + case 0: + self.items.sort { $0.index < $1.index } + case 1: + self.items.sort { $0.homeIndex < $1.homeIndex } + case 2: + self.items.sort { $0.awayIndex < $1.awayIndex } + default: + break + } + }) + /// ScrollView + ScrollView { + Section(header: TeamHeader()) { + ForEach(self.items, id: \.self) { item in + TeamListRow(item: item, sortingIndex: self.sortingIndex, buttonTapAction: { id in + self.selectedIndex = id + self.showingAlert = true + }) + } + .padding([.top, .bottom], 10) + } + } + /// Alert + .alert(isPresented: $showingAlert) { + Alert(title: Text("PL Team 2019/20"), message: Text("Selected team - \(self.items[selectedIndex].teamName)"), dismissButton: .default(Text("Got it!"))) + } + .padding([.leading, .trailing]) + .navigationBarTitle(Text("Team Standing"), displayMode: .inline) + .navigationBarBackButtonHidden(true) + .navigationBarItems(leading: Button(action: { self.presentation.wrappedValue.dismiss() }) { + Image(systemName: "chevron.left") + .foregroundColor(.blue) + .imageScale(.large) + }) + .onAppear { + self.items = self.viewModel.getTeamList() + } + } + } +} + +// MARK: - SegmentControllerScrollView_Previews +/// Preview provider for SegmentControllerScrollView +struct SegmentControllerScrollView_Previews: PreviewProvider { + static var previews: some View { + SegmentControllerScrollView(sortingIndex: 0, selectedIndex: 0) + } +} diff --git a/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/TeamDetailsView.swift b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/TeamDetailsView.swift new file mode 100644 index 0000000..9501f73 --- /dev/null +++ b/SwiftUILearning/Components/SegmentController - Week 4/View/SubViews/TeamDetailsView.swift @@ -0,0 +1,122 @@ +// +// TeamDetailsView.swift +// SwiftUILearning +// +// Created by Shikalgar, Shahrukh on 23/07/20. +// Copyright © 2020 Shahrukh Shikalgar. All rights reserved. +// + +import SwiftUI + +// MARK: - Struct for TeamDetailsView +/// View for TeamDetailsView +struct TeamDetailsView: View { + // MARK: - Variables + let item: SegmentControllModel + + // MARK: - View + /// Body for View + var body: some View { + VStack(spacing: 20) { + HStack { + Text("\(item.index).") + .fontWeight(.bold) + .font(.title) + Text(item.teamName) + .fontWeight(.bold) + .font(.title) + Spacer() + } + .foregroundColor(.red) + HStack { + TeamDetailListView(type: "", + played: "P", + won: "W", + draw: "D", + lost: "L") + } + HStack { + TeamDetailListView(type: "All", + played: "\(item.all.played)", + won: "\(item.all.won)", + draw: "\(item.all.draw)", + lost: "\(item.all.lost)") + } + HStack { + TeamDetailListView(type: "Home", + played: "\(item.home.played)", + won: "\(item.home.won)", + draw: "\(item.home.draw)", + lost: "\(item.home.lost)") + } + HStack { + TeamDetailListView(type: "Away", + played: "\(item.away.played)", + won: "\(item.away.won)", + draw: "\(item.away.draw)", + lost: "\(item.away.lost)") + } + Spacer() + } + .padding() + } +} + +// MARK: - Struct for TeamDetailListView +/// View for TeamDetailListView +struct TeamDetailListView: View { + // MARK: - Variables + let type: String + let played: String + let won: String + let draw: String + let lost: String + + // MARK: - View + /// Body for View + var body: some View { + HStack { + Text(type) + .fontWeight(.medium) + Spacer() + Text(played) + .fontWeight(.medium) + .foregroundColor(.orange) + .frame(minWidth: ConstantConstraint.width, + idealWidth: ConstantConstraint.width, + maxWidth: ConstantConstraint.width, + alignment: .trailing) + Text(won) + .fontWeight(.medium) + .foregroundColor(.green) + .frame(minWidth: ConstantConstraint.width, + idealWidth: ConstantConstraint.width, + maxWidth: ConstantConstraint.width, + alignment: .trailing) + Text(draw) + .fontWeight(.medium) + .foregroundColor(.blue) + .frame(minWidth: ConstantConstraint.width, + idealWidth: ConstantConstraint.width, + maxWidth: ConstantConstraint.width, + alignment: .trailing) + Text(lost) + .fontWeight(.medium) + .frame(minWidth: ConstantConstraint.width, + idealWidth: ConstantConstraint.width, + maxWidth: ConstantConstraint.width, + alignment: .trailing) + .foregroundColor(.red) + + } + + } +} + +// MARK: - TeamDetailsView_Previews +/// Preview provider for TeamDetailsView +struct TeamDetailsView_Previews: PreviewProvider { + static var previews: some View { + TeamDetailsView(item: SegmentControllModel(id: 0, teamName: "Liverpool", index: 1, homeIndex: 1, awayIndex: 1, all: GameRecord(id: 0, played: 38, won: 30, draw: 3, lost: 3), home: GameRecord(id: 0, played: 19, won: 17, draw: 1, lost: 0), away: GameRecord(id: 0, played: 19, won: 13, draw: 2, lost: 3))) + } +} diff --git a/SwiftUILearning/Components/SegmentController - Week 4/ViewModel/SegmentControllerViewModel.swift b/SwiftUILearning/Components/SegmentController - Week 4/ViewModel/SegmentControllerViewModel.swift new file mode 100644 index 0000000..b363b3f --- /dev/null +++ b/SwiftUILearning/Components/SegmentController - Week 4/ViewModel/SegmentControllerViewModel.swift @@ -0,0 +1,197 @@ +// +// SegmentControllerViewModel.swift +// SwiftUILearning +// +// Created by Shikalgar, Shahrukh on 21/07/20. +// Copyright © 2020 Shahrukh Shikalgar. All rights reserved. +// + +import Foundation +import SwiftUI + +// MARK: - Class for SegmentControllerViewModel +/// SegmentControllerViewModel class object +class SegmentControllerViewModel { + // MARK: - Variables + /// Model object array + let contentViewItems = [ContentModel(id: 1, contentType: ListType.list.rawValue), + ContentModel(id: 2, contentType: ListType.scrollView.rawValue)] + + /// Method to get destination view + /// - Parameter type: Type of view component + func getDestinationView(type: String) -> AnyView { + guard let rowType = ListType(rawValue: type) else { return AnyView(EmptyView()) } + switch rowType { + case .list: + return AnyView(SegmentControllerListView(sortingIndex: 0, selectedIndex: 0)) + case .scrollView: + return AnyView(SegmentControllerScrollView(sortingIndex: 0, selectedIndex: 0)) + } + } + + /// Func to get team list object + func getTeamList() -> [SegmentControllModel] { + let team1: SegmentControllModel = SegmentControllModel(id: 0, + teamName: "Liverpool", + index: 1, + homeIndex: 1, + awayIndex: 1, + all: GameRecord(id: 0, played: 38, won: 31, draw: 3, lost: 3), + home: GameRecord(id: 0, played: 19, won: 18, draw: 1, lost: 0), + away: GameRecord(id: 0, played: 19, won: 14, draw: 2, lost: 3)) + let team2: SegmentControllModel = SegmentControllModel(id: 1, + teamName: "Manchester City", + index: 2, + homeIndex: 2, + awayIndex: 2, + all: GameRecord(id: 0, played: 38, won: 25, draw: 3, lost: 9), + home: GameRecord(id: 0, played: 19, won: 14, draw: 2, lost: 2), + away: GameRecord(id: 0, played: 19, won: 11, draw: 1, lost: 7)) + let team3: SegmentControllModel = SegmentControllModel(id: 2, + teamName: "Chelsea", + index: 3, + homeIndex: 6, + awayIndex: 4, + all: GameRecord(id: 0, played: 38, won: 19, draw: 6, lost: 11), + home: GameRecord(id: 0, played: 19, won: 10, draw: 3, lost: 5), + away: GameRecord(id: 0, played: 19, won: 9, draw: 3, lost: 6)) + let team4: SegmentControllModel = SegmentControllModel(id: 3, + teamName: "Leicester City", + index: 4, + homeIndex: 4, + awayIndex: 8, + all: GameRecord(id: 0, played: 38, won: 18, draw: 8, lost: 11), + home: GameRecord(id: 0, played: 19, won: 11, draw: 4, lost: 3), + away: GameRecord(id: 0, played: 19, won: 7, draw: 4, lost: 8)) + let team5: SegmentControllModel = SegmentControllModel(id: 4, + teamName: "Manchester United", + index: 5, + homeIndex: 5, + awayIndex: 6, + all: GameRecord(id: 0, played: 38, won: 17, draw: 12, lost: 8), + home: GameRecord(id: 0, played: 19, won: 10, draw: 7, lost: 2), + away: GameRecord(id: 0, played: 19, won: 7, draw: 5, lost: 6)) + let team6: SegmentControllModel = SegmentControllModel(id: 5, + teamName: "Wolves", + index: 6, + homeIndex: 9, + awayIndex: 5, + all: GameRecord(id: 0, played: 38, won: 15, draw: 14, lost: 8), + home: GameRecord(id: 0, played: 19, won: 8, draw: 7, lost: 4), + away: GameRecord(id: 0, played: 19, won: 7, draw: 7, lost: 4)) + let team7: SegmentControllModel = SegmentControllModel(id: 6, + teamName: "Tottenham", + index: 7, + homeIndex: 3, + awayIndex: 12, + all: GameRecord(id: 0, played: 38, won: 16, draw: 10, lost: 11), + home: GameRecord(id: 0, played: 19, won: 12, draw: 3, lost: 4), + away: GameRecord(id: 0, played: 19, won: 4, draw: 7, lost: 7)) + let team8: SegmentControllModel = SegmentControllModel(id: 7, + teamName: "Sheffield Utd", + index: 8, + homeIndex: 8, + awayIndex: 9, + all: GameRecord(id: 0, played: 38, won: 14, draw: 12, lost: 11), + home: GameRecord(id: 0, played: 19, won: 10, draw: 3, lost: 6), + away: GameRecord(id: 0, played: 19, won: 4, draw: 9, lost: 5)) + let team9: SegmentControllModel = SegmentControllModel(id: 8, + teamName: "Burnley", + index: 9, + homeIndex: 11, + awayIndex: 7, + all: GameRecord(id: 0, played: 38, won: 15, draw: 9, lost: 13), + home: GameRecord(id: 0, played: 19, won: 8, draw: 4, lost: 6), + away: GameRecord(id: 0, played: 19, won: 7, draw: 5, lost: 7)) + let team10: SegmentControllModel = SegmentControllModel(id: 9, + teamName: "Arsenal", + index: 10, + homeIndex: 7, + awayIndex: 10, + all: GameRecord(id: 0, played: 38, won: 13, draw: 14, lost: 10), + home: GameRecord(id: 0, played: 19, won: 9, draw: 6, lost: 3), + away: GameRecord(id: 0, played: 19, won: 4, draw: 8, lost: 7)) + let team11: SegmentControllModel = SegmentControllModel(id: 10, + teamName: "Everton", + index: 11, + homeIndex: 10, + awayIndex: 13, + all: GameRecord(id: 0, played: 38, won: 13, draw: 10, lost: 14), + home: GameRecord(id: 0, played: 19, won: 8, draw: 7, lost: 3), + away: GameRecord(id: 0, played: 19, won: 5, draw: 3, lost: 11)) + let team12: SegmentControllModel = SegmentControllModel(id: 11, + teamName: "Southampton", + index: 12, + homeIndex: 19, + awayIndex: 3, + all: GameRecord(id: 0, played: 38, won: 14, draw: 7, lost: 16), + home: GameRecord(id: 0, played: 19, won: 5, draw: 3, lost: 10), + away: GameRecord(id: 0, played: 19, won: 9, draw: 4, lost: 6)) + let team13: SegmentControllModel = SegmentControllModel(id: 12, + teamName: "Newcastle Utd", + index: 13, + homeIndex: 12, + awayIndex: 14, + all: GameRecord(id: 0, played: 38, won: 11, draw: 11, lost: 15), + home: GameRecord(id: 0, played: 19, won: 6, draw: 8, lost: 4), + away: GameRecord(id: 0, played: 19, won: 5, draw: 3, lost: 11)) + let team14: SegmentControllModel = SegmentControllModel(id: 13, + teamName: "Crystal Palace", + index: 14, + homeIndex: 15, + awayIndex: 11, + all: GameRecord(id: 0, played: 38, won: 11, draw: 9, lost: 17), + home: GameRecord(id: 0, played: 19, won: 6, draw: 4, lost: 8), + away: GameRecord(id: 0, played: 19, won: 5, draw: 5, lost: 9)) + let team15: SegmentControllModel = SegmentControllModel(id: 14, + teamName: "Brighton", + index: 15, + homeIndex: 16, + awayIndex: 15, + all: GameRecord(id: 0, played: 38, won: 8, draw: 14, lost: 15), + home: GameRecord(id: 0, played: 19, won: 5, draw: 7, lost: 7), + away: GameRecord(id: 0, played: 19, won: 3, draw: 7, lost: 8)) + let team16: SegmentControllModel = SegmentControllModel(id: 15, + teamName: "West Ham United", + index: 16, + homeIndex: 17, + awayIndex: 16, + all: GameRecord(id: 0, played: 38, won: 10, draw: 7, lost: 19), + home: GameRecord(id: 0, played: 19, won: 6, draw: 3, lost: 9), + away: GameRecord(id: 0, played: 19, won: 4, draw: 4, lost: 10)) + let team17: SegmentControllModel = SegmentControllModel(id: 16, + teamName: "Aston Villa", + index: 17, + homeIndex: 14, + awayIndex: 17, + all: GameRecord(id: 0, played: 38, won: 9, draw: 7, lost: 21), + home: GameRecord(id: 0, played: 19, won: 7, draw: 3, lost: 9), + away: GameRecord(id: 0, played: 19, won: 2, draw: 4, lost: 12)) + let team18: SegmentControllModel = SegmentControllModel(id: 17, + teamName: "Watford", + index: 18, + homeIndex: 13, + awayIndex: 19, + all: GameRecord(id: 0, played: 38, won: 8, draw: 10, lost: 19), + home: GameRecord(id: 0, played: 19, won: 6, draw: 6, lost: 7), + away: GameRecord(id: 0, played: 19, won: 2, draw: 4, lost: 12)) + let team19: SegmentControllModel = SegmentControllModel(id: 18, + teamName: "Bournemouth", + index: 19, + homeIndex: 18, + awayIndex: 18, + all: GameRecord(id: 0, played: 38, won: 8, draw: 7, lost: 22), + home: GameRecord(id: 0, played: 19, won: 5, draw: 6, lost: 8), + away: GameRecord(id: 0, played: 19, won: 3, draw: 1, lost: 14)) + let team20: SegmentControllModel = SegmentControllModel(id: 19, + teamName: "Norwich City", + index: 20, + homeIndex: 20, + awayIndex: 20, + all: GameRecord(id: 0, played: 38, won: 5, draw: 6, lost: 26), + home: GameRecord(id: 0, played: 19, won: 4, draw: 3, lost: 12), + away: GameRecord(id: 0, played: 19, won: 1, draw: 3, lost: 14)) + + return [team1, team2, team3, team4, team5, team6, team7, team8, team9, team10, team11, team12, team13, team14, team15, team16, team17, team18, team19, team20] + } +}