diff --git a/SwiftUIChallenge/SwiftUIChallenge.xcodeproj/project.pbxproj b/SwiftUIChallenge/SwiftUIChallenge.xcodeproj/project.pbxproj index 6b92e7e..767adc8 100644 --- a/SwiftUIChallenge/SwiftUIChallenge.xcodeproj/project.pbxproj +++ b/SwiftUIChallenge/SwiftUIChallenge.xcodeproj/project.pbxproj @@ -19,6 +19,7 @@ EE7666C624B1C06900224A66 /* AddOverlay.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7666C324B1C06900224A66 /* AddOverlay.swift */; }; EE7666C724B1C06900224A66 /* OutlinedText.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7666C424B1C06900224A66 /* OutlinedText.swift */; }; EE76DB3D24C307F400BEC679 /* ContactDetails.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE76DB3C24C307F400BEC679 /* ContactDetails.swift */; }; + EE76DB3F24C419C200BEC679 /* ScrollViewWithSegmentControl.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE76DB3E24C419C200BEC679 /* ScrollViewWithSegmentControl.swift */; }; EE8D6D9024BC948C00F26B34 /* GradientButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE8D6D8F24BC948C00F26B34 /* GradientButtonStyle.swift */; }; EE8D6D9224BC95D000F26B34 /* ButtonChallenge.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE8D6D9124BC95D000F26B34 /* ButtonChallenge.swift */; }; EE8D6D9624BC97D400F26B34 /* ShadowButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE8D6D9524BC97D400F26B34 /* ShadowButtonStyle.swift */; }; @@ -41,6 +42,7 @@ EE7666C324B1C06900224A66 /* AddOverlay.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AddOverlay.swift; sourceTree = ""; }; EE7666C424B1C06900224A66 /* OutlinedText.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OutlinedText.swift; sourceTree = ""; }; EE76DB3C24C307F400BEC679 /* ContactDetails.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContactDetails.swift; sourceTree = ""; }; + EE76DB3E24C419C200BEC679 /* ScrollViewWithSegmentControl.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ScrollViewWithSegmentControl.swift; sourceTree = ""; }; EE8D6D8F24BC948C00F26B34 /* GradientButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GradientButtonStyle.swift; sourceTree = ""; }; EE8D6D9124BC95D000F26B34 /* ButtonChallenge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonChallenge.swift; sourceTree = ""; }; EE8D6D9524BC97D400F26B34 /* ShadowButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ShadowButtonStyle.swift; sourceTree = ""; }; @@ -123,6 +125,7 @@ children = ( EE5CA8EB24C2D36D008830E6 /* ListWithSegmentedControlChallenge.swift */, EE76DB3C24C307F400BEC679 /* ContactDetails.swift */, + EE76DB3E24C419C200BEC679 /* ScrollViewWithSegmentControl.swift */, ); path = SegmentedControl; sourceTree = ""; @@ -236,6 +239,7 @@ EE48E38A24ADAED300EC6B66 /* ImageChallengeView.swift in Sources */, EE76DB3D24C307F400BEC679 /* ContactDetails.swift in Sources */, EE7666C524B1C06900224A66 /* UIView+Custom.swift in Sources */, + EE76DB3F24C419C200BEC679 /* ScrollViewWithSegmentControl.swift in Sources */, EE7666C724B1C06900224A66 /* OutlinedText.swift in Sources */, EE91A59D24BA489E00B225C2 /* StackChallengeView.swift in Sources */, EE5CA8EC24C2D36D008830E6 /* ListWithSegmentedControlChallenge.swift in Sources */, diff --git a/SwiftUIChallenge/SwiftUIChallenge/SegmentedControl/ScrollViewWithSegmentControl.swift b/SwiftUIChallenge/SwiftUIChallenge/SegmentedControl/ScrollViewWithSegmentControl.swift index 4a8b80e..2d132b9 100644 --- a/SwiftUIChallenge/SwiftUIChallenge/SegmentedControl/ScrollViewWithSegmentControl.swift +++ b/SwiftUIChallenge/SwiftUIChallenge/SegmentedControl/ScrollViewWithSegmentControl.swift @@ -8,9 +8,49 @@ import SwiftUI struct ScrollViewWithSegmentControl: View { + @State private var sortingIndex = 0 + @State var contacts : [ContactInfo] = [.init(name: "John", email: "john@apple.com", number: "123567890"), + .init(name: "Albert", email: "albert@apple.com", number: "123567890"), + .init(name: "Bruce", email: "bruce@apple.com", number: "123567890"), + .init(name: "Sean", email: "sean@apple.com", number: "123567890"), + .init(name: "Robert", email: "robert@apple.com", number: "123567890"), + .init(name: "Albert", email: "albert@apple.com", number: "123567890"), + .init(name: "Francis", email: "francis@apple.com", number: "123567890"), + .init(name: "Joseph", email: "joseph@apple.com", number: "123567890"), + .init(name: "Adam", email: "adam@apple.com", number: "123567890"), + .init(name: "Lucifer", email: "lucifer@apple.com", number: "123567890")] + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + NavigationView { + VStack { + Picker("Sort Contacts by", selection: $sortingIndex) { + Text("Name").tag(0) + Text("Email").tag(1) + }.pickerStyle(SegmentedPickerStyle()) + .onReceive([self.sortingIndex].publisher.first(), perform: { (tag) in + // Here you would want to write your custom logic to perform some operations on segment change + switch tag { + case 0: + contacts.sort { $0.email < $1.email } + case 1: + contacts.sort { $0.name > $1.name } + default: + break + } + }) + .padding() + ScrollView { + ForEach(contacts, id:\.self) { contact in + NavigationLink(destination: ContactDetails(contactInfo: contact)) { + ListContactCell(contactInfo: contact) + } + } + } + Spacer() + }.navigationTitle("Contacts") + } } + } struct ScrollViewWithSegmentControl_Previews: PreviewProvider { @@ -18,3 +58,49 @@ struct ScrollViewWithSegmentControl_Previews: PreviewProvider { ScrollViewWithSegmentControl() } } + +struct ListContactCell: View { + @State private var showingAlert = false + + var contactInfo: ContactInfo + var body: some View { + HStack { + VStack(alignment: .leading, spacing: 5) { + Text(contactInfo.name) + .fontWeight(.bold) + .font(.system(size: 18)) + .foregroundColor(.black) + Text(contactInfo.email) + .fontWeight(.semibold) + .font(.system(size: 16)) + .foregroundColor(.black) + } + Spacer() + Button(action: sendMessage) { + Image(systemName: "message.fill") + .frame(width: 40, height: 40, alignment: .center) + .font(.body) + .foregroundColor(.green) + }.alert(isPresented: $showingAlert) { + Alert(title: Text("Still in Development"), message: Text("Once implementation is complete you will be able to send a text message or make a video call"), dismissButton: .default(Text("Got it!"))) + } + Button(action: makeVideoCall) { + Image(systemName: "video.fill") + .frame(width: 40, height: 40, alignment: .center) + .font(.body) + .foregroundColor(.red) + }.alert(isPresented: $showingAlert) { + Alert(title: Text("Still in Development"), message: Text("Once implementation is complete you will be able to send a text message or make a video call"), dismissButton: .default(Text("Got it!"))) + } + }.padding(.leading, 20) + .padding(.trailing, 20) + } + + func makeVideoCall() { + self.showingAlert = true + } + + func sendMessage() { + self.showingAlert = true + } +} diff --git a/SwiftUIChallenge/SwiftUIChallenge/SwiftUIChallengeApp.swift b/SwiftUIChallenge/SwiftUIChallenge/SwiftUIChallengeApp.swift index 6d8da27..f522ec4 100644 --- a/SwiftUIChallenge/SwiftUIChallenge/SwiftUIChallengeApp.swift +++ b/SwiftUIChallenge/SwiftUIChallenge/SwiftUIChallengeApp.swift @@ -11,7 +11,7 @@ import SwiftUI struct SwiftUIChallengeApp: App { var body: some Scene { WindowGroup { - ListWithSegmentedControlChallenge() + ScrollViewWithSegmentControl() } } }