UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

WeSplitApp: Why does a larger tip range make a new screen(view?)?

Forums > 100 Days of SwiftUI

I banged my head against the wall with this one.

My solution was making a entirely new view and then I added a GeometryReader to scroll down the numbers.

I think my solution was way over complicated for what it is but I learned a lot.

Final thoughts:

Is arriving at the same result but with diffrent methods acceptable? When is it damaging?

Regards

2      

@ian  

Can you post your code?

Is arriving at the same result but with diffrent methods acceptable?

Probably.

When is it damaging?

It seems like the lessons are designed nicely around topics that have been previously covered, and are hopefully completed using those topics. :)

Here is my solution if it helps.

//
//  ContentView.swift
//  WeSplit
//
//  Created by user on 10/17/22.
//

import SwiftUI

struct ContentView: View {

    @State private var checkAmount = 0.0
    @State private var numberOfPeople = 2
    @State private var tipPercentage  = 20

    @FocusState private var amountIsFocused: Bool

    //If tipPercentage picker value is greater than 0 then a TRUE a tip was left else false no tip was left
    var noTipRude: Bool { tipPercentage > 0}

    var localCurrency: FloatingPointFormatStyle<Double>.Currency {
        return .currency(code: Locale.current.currency?.identifier ?? "USD")
    }

    var grandTotal: Double {
        let peopleCount = Double(numberOfPeople + 2)

        let tipSelection = Double(tipPercentage)

        let tipValue = checkAmount / 100 * tipSelection

        let grandTotal = checkAmount + tipValue

        return grandTotal
    }

    var totalPerPerson: Double {
        let peopleCount = Double(numberOfPeople + 2)
        let tipSelection = Double(tipPercentage)

        let tipValue = checkAmount / 100 * tipSelection

        let grandTotal = checkAmount + tipValue

        let amountPerPerson = grandTotal / peopleCount

        return amountPerPerson
    }

    var body: some View {
        NavigationView {
            Form {
                Section {
                    TextField("Amount", value: $checkAmount, format: localCurrency) //.currency(code: Locale.current.currency?.identifier ?? "USD"))
                        .keyboardType(.decimalPad)
                        .focused($amountIsFocused)

                    Picker("Number of People", selection: $numberOfPeople) {
                        ForEach(2..<100){
                            Text("\($0) people")
                        }
                    }
                }

                Section {
                    Picker("Tip percentage", selection: $tipPercentage) {
                        //ForEach(tipPercentages, id: \.self) {
                        ForEach( 0..<101 ) {
                            Text($0, format: .percent)
                        }
                    }
                } header: {
                    Text("How much tip do you want to leave?")
                }

                Section {
                    Text(grandTotal, format: localCurrency) //.currency(code: Locale.current.currency?.identifier ?? "USD"))
                        .foregroundColor(noTipRude ? .black : .red)

                } header: {
                    Text("Grand Total:")
                }

                Section {
                    Text(totalPerPerson, format: localCurrency) //.currency(code: Locale.current.currency?.identifier ?? "USD"))
                } header: {
                    Text("Amount per person:")
                }
            }

            .navigationTitle("WeSplit")
            .toolbar {
                ToolbarItemGroup(placement: .keyboard) {
                    Spacer()
                    Button("Done") {
                        amountIsFocused = false
                    }
                }
            }
        }
    }

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

3      

If you're referring to this challenge

Change the tip percentage picker to show a new screen rather than using a segmented control, and give it a wider range of options – everything from 0% to 100%. Tip: use the range 0..<101 for your range rather than a fixed array.

Paul asked us to write code like this when making the project...

Section {
    Picker("Tip percentage", selection: $tipPercentage) {
        ForEach(tipPercentages, id: \.self) {
            Text($0, format: .percent)
        }
    }
    .pickerStyle(.segmented)
}

The line .pickerStyle(.segmented) causes the picker to use a segmented style. If you remove that line, tapping on the picker will show you a new screen with a list of options to choose from instead.

Is arriving at the same result but with diffrent methods acceptable? When is it damaging?

It can be acceptable if the finished product ends up meeting the requirements that you need it to. However, it can be damaging in many ways.

For one thing, it can make you spend more time than necessary. Like, if you had to create a new view and set up a GeometryReader when you could have just deleted one line of code for example.

Another problem can be preformance issues. If the end result is what you want, but you have overcomplicated the program so much that there is a noticable difference in responsiveness of the program, then you might have a problem.

Yet another problem can be readability of your code. If you are working on a project with others, and you have made things work in some crazy way, then it might not be very easy for other people to read over your code and figure out what you were doing with it.

But in the end, there are no hard rules on these things other than the ones that you restrict yourself to. And while you are learning, there is no reason that you can't experiment a bit until you figure out what works. But, as the previous commenter mentioned, the challenges in this course will not typically ask you to do anything that wasn't previously covered in the course material, unless Paul specifically mentions it in a hint somewhere.

2      

Hacking with Swift is sponsored by RevenueCat.

SPONSORED Take the pain out of configuring and testing your paywalls. RevenueCat's Paywalls allow you to remotely configure your entire paywall view without any code changes or app updates.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.