Skip to content

Commit

Permalink
Merge pull request #30 from techstartucalgary/welcome-page
Browse files Browse the repository at this point in the history
Answer state managent
  • Loading branch information
ParsaKargari authored Dec 24, 2023
2 parents a7ec583 + 6fce131 commit ff99d70
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 40 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
2 changes: 2 additions & 0 deletions Rethread/Rethread.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Rethread/Preview Content\"";
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
Expand Down Expand Up @@ -494,6 +495,7 @@
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEVELOPMENT_ASSET_PATHS = "\"Rethread/Preview Content\"";
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
Expand Down
Binary file not shown.
27 changes: 16 additions & 11 deletions Rethread/Rethread/OnboardingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ struct OnboardingView: View {
// # Of pages
var numberOfPages = 4
@State private var currentScreenIndex = 0
@State private var answers: [[String]] = Array(repeating: [], count: 3)

var body: some View {
VStack {
Expand All @@ -15,23 +16,26 @@ struct OnboardingView: View {
options: ["Kahlil Gibran", "Rumi", "Hafez", "Shakespeare"],
isLastPage: false,
action: goToNextScreen,
previousAction: goToPreviousScreen
previousAction: goToPreviousScreen,
selectedAnswers: $answers[currentScreenIndex - 1]
)
} else if currentScreenIndex == 2 {
QuestionView(
question: "Identify the author of this quote: 'The only impossible journey is the one you never begin.'",
options: ["Tony Robbins", "Neil Armstrong", "Mark Twain", "J.R.R. Tolkien"],
isLastPage: false,
action: goToNextScreen,
previousAction: goToPreviousScreen
previousAction: goToPreviousScreen,
selectedAnswers: $answers[currentScreenIndex - 1]
)
} else {
QuestionView(
question: "Determine who said: 'Out beyond ideas of wrongdoing and rightdoing, there is a field. I'll meet you there.'",
options: ["Rumi", "Oscar Wilde", "Emily Dickinson", "Pablo Neruda"],
isLastPage: true,
action: goToNextScreen,
previousAction: goToPreviousScreen
action: goToNextScreen,
previousAction: goToPreviousScreen,
selectedAnswers: $answers[currentScreenIndex - 1]
)
}
}
Expand All @@ -51,18 +55,19 @@ struct OnboardingView: View {
if currentScreenIndex < numberOfPages - 1 {
currentScreenIndex += 1
} else {
// finishOnboarding()
finishOnboarding()
}
}
}

func finishOnboarding() {
// Code to finish onboarding and update @AppStorage value
sendAnswersToBackend(answers)
answers.removeAll()
}
}

struct OnboardingView_Previews: PreviewProvider {
static var previews: some View {
OnboardingView()

func sendAnswersToBackend(_ answers: [[String]]) {
// Implement the network request logic here
print("Sending answers to backend:", answers)
}

}
66 changes: 37 additions & 29 deletions Rethread/Rethread/QuestionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ struct QuestionView: View {
var action: () -> Void
var previousAction: () -> Void

@State private var selectedOptions: Set<String> = []
@Binding var selectedAnswers: [String] // Directly using the binding to manage state

private func currentButtonStyle() -> AnyButtonStyle {
if isLastPage {
return AnyButtonStyle(LetStartButtonStyle(isEnabled: !selectedOptions.isEmpty))
} else {
return AnyButtonStyle(NextButtonStyle(isEnabled: !selectedOptions.isEmpty))
}
if isLastPage {
return AnyButtonStyle(LetStartButtonStyle(isEnabled: !selectedAnswers.isEmpty))
} else {
return AnyButtonStyle(NextButtonStyle(isEnabled: !selectedAnswers.isEmpty))
}
}

var body: some View {
VStack {
Expand All @@ -28,27 +28,13 @@ struct QuestionView: View {
.padding(.horizontal, 20.0)

ForEach(options, id: \.self) { option in
HStack {
// Custom checkbox
Image(systemName: selectedOptions.contains(option) ? "checkmark.square.fill" : "square")
.resizable()
.frame(width: 22, height: 22)
.foregroundColor(Color(red: 102/255, green: 112/255, blue: 128/255))
.onTapGesture {
if selectedOptions.contains(option) {
selectedOptions.remove(option)
} else {
selectedOptions.insert(option)
}
}

Text(option)
.foregroundColor(.black)
.padding(.leading)

Spacer()
OptionRow(option: option, isSelected: selectedAnswers.contains(option)) {
if let index = selectedAnswers.firstIndex(of: option) {
selectedAnswers.remove(at: index) // Deselect the option
} else {
selectedAnswers.append(option) // Select the option
}
}
.padding(.vertical, 15)
}
.padding([.leading, .trailing], 35)

Expand All @@ -75,7 +61,7 @@ struct QuestionView: View {
}
}
.buttonStyle(currentButtonStyle())
.disabled(selectedOptions.isEmpty)
.disabled(selectedAnswers.isEmpty)
}
.padding(.bottom, 70)

Expand All @@ -85,6 +71,30 @@ struct QuestionView: View {
}
}

struct OptionRow: View {
var option: String
var isSelected: Bool
var toggleSelection: () -> Void

var body: some View {
HStack {
Image(systemName: isSelected ? "checkmark.square.fill" : "square")
.resizable()
.frame(width: 22, height: 22)
.foregroundColor(Color(red: 102/255, green: 112/255, blue: 128/255))
.onTapGesture(perform: toggleSelection)

Text(option)
.foregroundColor(.black)
.padding(.leading)
.onTapGesture(perform: toggleSelection)

Spacer()
}
.padding(.vertical, 15)
}
}

struct AnyButtonStyle: ButtonStyle {
private let _makeBody: (Configuration) -> AnyView

Expand All @@ -96,5 +106,3 @@ struct AnyButtonStyle: ButtonStyle {
self._makeBody(configuration)
}
}


0 comments on commit ff99d70

Please sign in to comment.